From f311906d04d74402d5bf98209d45a379878cddd7 Mon Sep 17 00:00:00 2001 From: Mat Sutcliffe Date: Mon, 9 Aug 2021 00:10:30 +0100 Subject: [PATCH] Issue #37 Emit signal when FPS drops below threshold --- src/blackcore/context/contextsimulator.h | 3 +++ src/blackcore/context/contextsimulatorimpl.cpp | 4 ++++ src/blackcore/context/contextsimulatorproxy.cpp | 3 +++ src/blackcore/simulator.h | 3 +++ src/plugins/simulator/xplane/simulatorxplane.cpp | 15 ++++++++++++++- 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/blackcore/context/contextsimulator.h b/src/blackcore/context/contextsimulator.h index 9f5250f12..f7284a410 100644 --- a/src/blackcore/context/contextsimulator.h +++ b/src/blackcore/context/contextsimulator.h @@ -120,6 +120,9 @@ namespace BlackCore //! A formerly vital driver is no longer vital/responding void vitalityLost(); + //! Frame rate has fallen too far below the threshold to maintain consistent sim rate + void insufficientFrameRateDetected(bool fatal); + //! Render restrictions have been changed void renderRestrictionsChanged(bool restricted, bool enabled, int maxAircraft, const BlackMisc::PhysicalQuantities::CLength &maxRenderedDistance); diff --git a/src/blackcore/context/contextsimulatorimpl.cpp b/src/blackcore/context/contextsimulatorimpl.cpp index 4201d4fc6..a83c4ab89 100644 --- a/src/blackcore/context/contextsimulatorimpl.cpp +++ b/src/blackcore/context/contextsimulatorimpl.cpp @@ -534,6 +534,10 @@ namespace BlackCore c = connect(simulator, &ISimulator::autoPublishDataWritten, this, &IContextSimulator::autoPublishDataWritten); Q_ASSERT(c); + // disconnect for X-Plane FPS below 20 + c = connect(simulator, &ISimulator::insufficientFrameRateDetected, this, &IContextSimulator::insufficientFrameRateDetected); + Q_ASSERT(c); + // log from context to simulator c = connect(CLogHandler::instance(), &CLogHandler::localMessageLogged, this, &CContextSimulator::relayStatusMessageToSimulator); Q_ASSERT(c); diff --git a/src/blackcore/context/contextsimulatorproxy.cpp b/src/blackcore/context/contextsimulatorproxy.cpp index 123146732..0d94494ed 100644 --- a/src/blackcore/context/contextsimulatorproxy.cpp +++ b/src/blackcore/context/contextsimulatorproxy.cpp @@ -104,6 +104,9 @@ namespace BlackCore s = connection.connect(serviceName, IContextSimulator::ObjectPath(), IContextSimulator::InterfaceName(), "validatedModelSet", this, SIGNAL(validatedModelSet(BlackMisc::Simulation::CSimulatorInfo, BlackMisc::Simulation::CAircraftModelList, BlackMisc::Simulation::CAircraftModelList, bool, BlackMisc::CStatusMessageList))); Q_ASSERT(s); + s = connection.connect(serviceName, IContextSimulator::ObjectPath(), IContextSimulator::InterfaceName(), + "insufficientFrameRateDetected", this, SIGNAL(insufficientFrameRateDetected(bool))); + Q_ASSERT(s); Q_UNUSED(s) this->relayBaseClassSignals(serviceName, connection, IContextSimulator::ObjectPath(), IContextSimulator::InterfaceName()); } diff --git a/src/blackcore/simulator.h b/src/blackcore/simulator.h index 5f55f8e5c..659d31218 100644 --- a/src/blackcore/simulator.h +++ b/src/blackcore/simulator.h @@ -411,6 +411,9 @@ namespace BlackCore //! Auto publish data written for simulator void autoPublishDataWritten(const BlackMisc::Simulation::CSimulatorInfo &simulator); + //! Frame rate has fallen too far below the threshold to maintain consistent sim rate + void insufficientFrameRateDetected(bool fatal); + protected: //! Constructor with all the providers ISimulator(const BlackMisc::Simulation::CSimulatorPluginInfo &pluginInfo, diff --git a/src/plugins/simulator/xplane/simulatorxplane.cpp b/src/plugins/simulator/xplane/simulatorxplane.cpp index 2b8869a71..81e655040 100644 --- a/src/plugins/simulator/xplane/simulatorxplane.cpp +++ b/src/plugins/simulator/xplane/simulatorxplane.cpp @@ -444,10 +444,23 @@ namespace BlackSimPlugin } // FPS + // reading FPS resets average, so we only monitor over some time if ((m_slowTimerCalls % 5u) == 0u) { - // reading FPS resets average, so we only monitor over some time + constexpr double warningMiles = 1; + constexpr double disconnectMiles = 2; + const double previousMiles = m_trackMilesShort; + m_serviceProxy->getFrameStats(&m_averageFps, &m_simTimeRatio, &m_trackMilesShort, &m_minutesLate); + + if (previousMiles < disconnectMiles && m_trackMilesShort >= disconnectMiles) + { + emit insufficientFrameRateDetected(true); + } + else if (previousMiles < warningMiles && m_trackMilesShort >= warningMiles) + { + emit insufficientFrameRateDetected(false); + } } } }