From 27ea14ea94f9b83d9cfd5dead6ca60c538a4d748 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Thu, 31 May 2018 09:09:32 -0700 Subject: [PATCH] Qt: Improve FPS timer stability --- src/platform/qt/Window.cpp | 24 +++++++++++++++++++----- src/platform/qt/Window.h | 8 ++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/platform/qt/Window.cpp b/src/platform/qt/Window.cpp index 405a0f677..ad4035edd 100644 --- a/src/platform/qt/Window.cpp +++ b/src/platform/qt/Window.cpp @@ -138,11 +138,13 @@ Window::Window(CoreManager* manager, ConfigController* config, int playerId, QWi 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 @@ void Window::gameStarted() { 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 @@ void Window::gameStopped() { m_audioChannels->clear(); m_fpsTimer.stop(); + m_frameTimer.stop(); m_focusCheck.stop(); emit paused(false); @@ -887,19 +891,27 @@ void Window::mustRestart() { } 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 @@ void Window::setupMenu(QMenuBar* menubar) { 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); diff --git a/src/platform/qt/Window.h b/src/platform/qt/Window.h index 2d9d139cc..5418e0dc1 100644 --- a/src/platform/qt/Window.h +++ b/src/platform/qt/Window.h @@ -132,6 +132,7 @@ private slots: void mustRestart(); void recordFrame(); + void delimitFrames(); void showFPS(); void focusCheck(); @@ -139,7 +140,8 @@ private slots: 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 @@ private: QPixmap m_logo{":/res/mgba-1024.png"}; ConfigController* m_config; InputController m_inputController; - QList m_frameList; + QList m_frameList; + int m_frameCounter = 0; QTimer m_fpsTimer; + QTimer m_frameTimer; QList m_mruFiles; QMenu* m_mruMenu = nullptr; QMenu* m_videoLayers;