Qt: Improve FPS timer stability

This commit is contained in:
Vicki Pfau 2018-05-31 09:09:32 -07:00
parent 9ea438e1f0
commit 27ea14ea94
2 changed files with 25 additions and 7 deletions

View File

@ -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);

View File

@ -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<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;