Qt: Improve FPS timer stability
Vicki Pfau vi@endrift.com
Thu, 31 May 2018 09:09:32 -0700
2 files changed,
25 insertions(+),
7 deletions(-)
M
src/platform/qt/Window.cpp
→
src/platform/qt/Window.cpp
@@ -138,11 +138,13 @@ setCentralWidget(m_screenWidget);
connect(this, &Window::shutdown, m_logView, &QWidget::hide); connect(&m_fpsTimer, &QTimer::timeout, this, &Window::showFPS); + connect(&m_frameTimer, &QTimer::timeout, this, &Window::delimitFrames); connect(&m_focusCheck, &QTimer::timeout, this, &Window::focusCheck); connect(&m_inputController, &InputController::profileLoaded, m_shortcutController, &ShortcutController::loadProfile); m_log.setLevels(mLOG_WARN | mLOG_ERROR | mLOG_FATAL); m_fpsTimer.setInterval(FPS_TIMER_INTERVAL); + m_frameTimer.setInterval(FRAME_LIST_INTERVAL); m_focusCheck.setInterval(200); m_shortcutController->setConfigController(m_config);@@ -690,6 +692,7 @@
m_hitUnimplementedBiosCall = false; if (m_config->getOption("showFps", "1").toInt()) { m_fpsTimer.start(); + m_frameTimer.start(); } m_focusCheck.start(); if (m_display->underMouse()) {@@ -759,6 +762,7 @@ m_videoLayers->clear();
m_audioChannels->clear(); m_fpsTimer.stop(); + m_frameTimer.stop(); m_focusCheck.stop(); emit paused(false);@@ -887,19 +891,27 @@ dialog->show();
} void Window::recordFrame() { - m_frameList.append(QDateTime::currentDateTime()); - while (m_frameList.count() > FRAME_LIST_SIZE) { - m_frameList.removeFirst(); + if (m_frameList.isEmpty()) { + m_frameList.append(1); + } else { + ++m_frameList.back(); } } +void Window::delimitFrames() { + if (m_frameList.size() >= FRAME_LIST_SIZE) { + m_frameCounter -= m_frameList.takeAt(0); + } + m_frameCounter += m_frameList.back(); + m_frameList.append(0); +} + void Window::showFPS() { if (m_frameList.isEmpty()) { updateTitle(); return; } - qint64 interval = m_frameList.first().msecsTo(m_frameList.last()); - float fps = (m_frameList.count() - 1) * 10000.f / interval; + float fps = m_frameCounter * 10000.f / (FRAME_LIST_INTERVAL * (m_frameList.size() - 1)); fps = round(fps) / 10.f; updateTitle(fps); }@@ -1618,9 +1630,11 @@ ConfigOption* showFps = m_config->addOption("showFps");
showFps->connect([this](const QVariant& value) { if (!value.toInt()) { m_fpsTimer.stop(); + m_frameTimer.stop(); updateTitle(); } else if (m_controller) { m_fpsTimer.start(); + m_frameTimer.start(); } }, this);
M
src/platform/qt/Window.h
→
src/platform/qt/Window.h
@@ -132,6 +132,7 @@ void tryMakePortable();
void mustRestart(); void recordFrame(); + void delimitFrames(); void showFPS(); void focusCheck();@@ -139,7 +140,8 @@ void updateFrame();
private: static const int FPS_TIMER_INTERVAL = 2000; - static const int FRAME_LIST_SIZE = 120; + static const int FRAME_LIST_INTERVAL = 100; + static const int FRAME_LIST_SIZE = 40; void setupMenu(QMenuBar*); void openStateWindow(LoadSave);@@ -187,8 +189,10 @@ WindowBackground* m_screenWidget;
QPixmap m_logo{":/res/mgba-1024.png"}; ConfigController* m_config; InputController m_inputController; - QList<QDateTime> m_frameList; + QList<int> m_frameList; + int m_frameCounter = 0; QTimer m_fpsTimer; + QTimer m_frameTimer; QList<QString> m_mruFiles; QMenu* m_mruMenu = nullptr; QMenu* m_videoLayers;