mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 23:05:36 +08:00
[xswiftbus] Added FPS monitoring
This commit is contained in:
@@ -319,6 +319,15 @@ namespace BlackSimPlugin
|
||||
m_dbusInterface->callDBusAsync(QLatin1String("isUsingRealTime"), setterCallback(o_isRealTime));
|
||||
}
|
||||
|
||||
double CXSwiftBusServiceProxy::getAverageFPS() const
|
||||
{
|
||||
return m_dbusInterface->callDBusRet<double>(QLatin1String("getAverageFPS"));
|
||||
}
|
||||
void CXSwiftBusServiceProxy::getAverageFPSAsync(double *o_frameRate)
|
||||
{
|
||||
m_dbusInterface->callDBusAsync(QLatin1String("getAverageFPS"), setterCallback(o_frameRate));
|
||||
}
|
||||
|
||||
double CXSwiftBusServiceProxy::getLatitudeDeg() const
|
||||
{
|
||||
return m_dbusInterface->callDBusRet<double>(QLatin1String("getLatitudeDeg"));
|
||||
|
||||
@@ -208,6 +208,12 @@ namespace BlackSimPlugin
|
||||
void isUsingRealTimeAsync(bool *o_isRealTime);
|
||||
//! @}
|
||||
|
||||
//! \copydoc XSwiftBus::CService::getAverageFPS
|
||||
//! @{
|
||||
double getAverageFPS() const;
|
||||
void getAverageFPSAsync(double *o_frameRate);
|
||||
//! @}
|
||||
|
||||
//! \copydoc XSwiftBus::CService::getLatitudeDeg
|
||||
//! @{
|
||||
double getLatitudeDeg() const;
|
||||
|
||||
@@ -36,6 +36,8 @@ namespace XSwiftBus
|
||||
}
|
||||
}
|
||||
|
||||
bool isValid() const { return m_ref; }
|
||||
|
||||
template <typename T>
|
||||
void implSet(T);
|
||||
|
||||
@@ -60,6 +62,8 @@ namespace XSwiftBus
|
||||
}
|
||||
}
|
||||
|
||||
bool isValid() const { return m_ref; }
|
||||
|
||||
template <typename T>
|
||||
void implSetAll(std::vector<T> const &);
|
||||
|
||||
@@ -108,6 +112,8 @@ namespace XSwiftBus
|
||||
|
||||
//! Get the value of the dataref
|
||||
DataRefType get() const { return DataRefImpl::implGet<DataRefType>(); }
|
||||
|
||||
using DataRefImpl::isValid;
|
||||
};
|
||||
|
||||
/*!
|
||||
@@ -141,6 +147,8 @@ namespace XSwiftBus
|
||||
|
||||
//! Get the value of a single element
|
||||
DataRefType getAt(int index) const { return ArrayDataRefImpl::implGetAt<DataRefType>(index); }
|
||||
|
||||
using ArrayDataRefImpl::isValid;
|
||||
};
|
||||
|
||||
/*!
|
||||
@@ -165,6 +173,9 @@ namespace XSwiftBus
|
||||
}
|
||||
}
|
||||
|
||||
//! True if the dataref exists
|
||||
bool isValid() const { return m_ref; }
|
||||
|
||||
//! Set the value of the whole string (if it is writable)
|
||||
void set(std::string const &s) { setSubstr(0, s); }
|
||||
|
||||
|
||||
@@ -23,12 +23,54 @@ using namespace BlackMisc::Simulation::XPlane::QtFreeUtils;
|
||||
|
||||
namespace XSwiftBus
|
||||
{
|
||||
CService::CService(CSettingsProvider *settingsProvider) : CDBusObject(settingsProvider)
|
||||
//! \private
|
||||
struct CService::FramePeriodSampler : public CDrawable
|
||||
{
|
||||
DataRef<xplane::data::sim::operation::misc::frame_rate_period> m_thisFramePeriod;
|
||||
DataRef<xplane::data::sim::time::framerate_period> m_thisFramePeriodXP11;
|
||||
std::vector<float> m_samples;
|
||||
float m_total = 0;
|
||||
size_t m_lastSampleIndex = 0;
|
||||
static constexpr size_t c_maxSampleCount = 500;
|
||||
|
||||
FramePeriodSampler() : CDrawable(xplm_Phase_LastCockpit, false) {}
|
||||
|
||||
float getAverageFPS()
|
||||
{
|
||||
if (m_total == 0) { return 0; }
|
||||
const float fps = m_samples.size() / m_total;
|
||||
m_total = 0;
|
||||
m_samples.clear();
|
||||
m_lastSampleIndex = 0;
|
||||
return fps;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void draw() override // called once per frame
|
||||
{
|
||||
float current = m_thisFramePeriodXP11.isValid() ? m_thisFramePeriodXP11.get() : m_thisFramePeriod.get();
|
||||
++m_lastSampleIndex %= c_maxSampleCount;
|
||||
if (m_samples.size() == c_maxSampleCount)
|
||||
{
|
||||
m_total -= m_samples[m_lastSampleIndex];
|
||||
m_samples[m_lastSampleIndex] = current;
|
||||
}
|
||||
else { m_samples.push_back(current); }
|
||||
m_total += m_samples[m_lastSampleIndex];
|
||||
}
|
||||
};
|
||||
|
||||
CService::CService(CSettingsProvider *settingsProvider) :
|
||||
CDBusObject(settingsProvider),
|
||||
m_framePeriodSampler(std::make_unique<FramePeriodSampler>())
|
||||
{
|
||||
this->updateAirportsInRange();
|
||||
this->updateMessageBoxFromSettings();
|
||||
m_framePeriodSampler->show();
|
||||
}
|
||||
|
||||
CService::~CService() = default;
|
||||
|
||||
void CService::onAircraftModelChanged()
|
||||
{
|
||||
char filename[256];
|
||||
@@ -63,6 +105,12 @@ namespace XSwiftBus
|
||||
return XSWIFTBUS_COMMIT;
|
||||
}
|
||||
|
||||
double CService::getAverageFPS()
|
||||
{
|
||||
if (!m_framePeriodSampler) { return 0; }
|
||||
return static_cast<double>(m_framePeriodSampler->getAverageFPS());
|
||||
}
|
||||
|
||||
void CService::addTextMessage(const std::string &text, double red, double green, double blue)
|
||||
{
|
||||
if (text.empty()) { return; }
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace XSwiftBus
|
||||
CService(CSettingsProvider *settingsProvider);
|
||||
|
||||
//! Destructor
|
||||
virtual ~CService() override = default;
|
||||
virtual ~CService() override;
|
||||
|
||||
//! DBus interface name
|
||||
static const std::string &InterfaceName()
|
||||
@@ -110,6 +110,11 @@ namespace XSwiftBus
|
||||
//! True if sim time is tracking operating system time
|
||||
bool isUsingRealTime() const { return m_useSystemTime.get(); }
|
||||
|
||||
//! Frames-per-second, averaged over the last 500 frames,
|
||||
//! or since this function was last called, whichever is later.
|
||||
//! \return Zero if no samples were collected since this function was last called.
|
||||
double getAverageFPS();
|
||||
|
||||
//! Get aircraft latitude in degrees
|
||||
double getLatitudeDeg() const { return m_latitude.get(); }
|
||||
|
||||
@@ -309,6 +314,9 @@ namespace XSwiftBus
|
||||
//! Redraw message box after reading from the settings
|
||||
void updateMessageBoxFromSettings();
|
||||
|
||||
struct FramePeriodSampler;
|
||||
std::unique_ptr<FramePeriodSampler> m_framePeriodSampler;
|
||||
|
||||
StringDataRef<xplane::data::sim::aircraft::view::acf_livery_path> m_liveryPath;
|
||||
StringDataRef<xplane::data::sim::aircraft::view::acf_ICAO> m_icao;
|
||||
StringDataRef<xplane::data::sim::aircraft::view::acf_descrip> m_descrip;
|
||||
|
||||
Reference in New Issue
Block a user