From 8fdf94fd97e2482dcf12011e86dbeed44f5eae55 Mon Sep 17 00:00:00 2001 From: SimoneN64 Date: Mon, 21 Oct 2024 20:46:43 +0200 Subject: [PATCH] Introduce FPS counter on the bottom right --- src/frontend/EmuThread.cpp | 27 +++++++++++++++++++++++++-- src/frontend/EmuThread.hpp | 3 ++- src/frontend/KaizenQt.cpp | 4 ++-- src/frontend/MainWindow.cpp | 3 +++ src/frontend/MainWindow.hpp | 1 + 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/frontend/EmuThread.cpp b/src/frontend/EmuThread.cpp index 1d11ffb8..5d7a3527 100644 --- a/src/frontend/EmuThread.cpp +++ b/src/frontend/EmuThread.cpp @@ -1,14 +1,22 @@ #include #include -EmuThread::EmuThread(const std::shared_ptr &core, RenderWidget &renderWidget, - SettingsWindow &settings) noexcept : renderWidget(renderWidget), core(core), settings(settings) {} +EmuThread::EmuThread(const std::shared_ptr &core, QLabel &fps, RenderWidget &renderWidget, + SettingsWindow &settings) noexcept : + renderWidget(renderWidget), core(core), settings(settings), fps(fps) {} void EmuThread::run() noexcept { core->parallel.Init(renderWidget.qtVkInstanceFactory, renderWidget.wsiPlatform, renderWidget.windowInfo, core->cpu->GetMem().GetRDRAMPtr()); + auto lastSample = std::chrono::high_resolution_clock::now(); + auto avgFps = 16.667; + auto sampledFps = 0; + + fps.setText(fmt::format("{:.2f} FPS", 1000.0 / avgFps).c_str()); + while (!isInterruptionRequested()) { + const auto startFrameTime = std::chrono::high_resolution_clock::now(); if (!core->pause) { core->Run(settings.getVolumeL(), settings.getVolumeR()); } @@ -16,6 +24,21 @@ void EmuThread::run() noexcept { if (core->render) { core->parallel.UpdateScreen(core->cpu->GetMem().mmio.vi); } + + const auto endFrameTime = std::chrono::high_resolution_clock::now(); + using namespace std::chrono_literals; + const auto frameTimeMs = std::chrono::duration(endFrameTime - startFrameTime) / 1ms; + avgFps += frameTimeMs; + + sampledFps++; + + if (const auto elapsedSinceLastSample = std::chrono::duration(endFrameTime - lastSample) / 1s; + elapsedSinceLastSample >= 1.0) { + lastSample = endFrameTime; + avgFps /= sampledFps; + sampledFps = 0; + fps.setText(fmt::format("{:.2f} FPS", 1000.0 / avgFps).c_str()); + } } SetRender(false); diff --git a/src/frontend/EmuThread.hpp b/src/frontend/EmuThread.hpp index 75193987..af61655c 100644 --- a/src/frontend/EmuThread.hpp +++ b/src/frontend/EmuThread.hpp @@ -14,7 +14,7 @@ class EmuThread final : public QThread { RenderWidget &renderWidget; public: - explicit EmuThread(const std::shared_ptr &, RenderWidget &, SettingsWindow &) noexcept; + explicit EmuThread(const std::shared_ptr &, QLabel &, RenderWidget &, SettingsWindow &) noexcept; void run() noexcept override; void TogglePause() const noexcept; @@ -24,4 +24,5 @@ public: std::shared_ptr core; SettingsWindow &settings; + QLabel &fps; }; diff --git a/src/frontend/KaizenQt.cpp b/src/frontend/KaizenQt.cpp index 68e097ea..b28a4a5a 100644 --- a/src/frontend/KaizenQt.cpp +++ b/src/frontend/KaizenQt.cpp @@ -11,7 +11,7 @@ KaizenQt::KaizenQt() noexcept : QWidget(nullptr) { core = std::make_shared(); mainWindow = std::make_unique(core); settingsWindow = std::make_unique(); - emuThread = std::make_unique(core, *mainWindow->vulkanWidget, *settingsWindow); + emuThread = std::make_unique(core, *mainWindow->fpsCounter, *mainWindow->vulkanWidget, *settingsWindow); debugger = std::make_unique(); ConnectMainWindowSignalsToSlots(); @@ -40,7 +40,7 @@ void KaizenQt::ConnectMainWindowSignalsToSlots() noexcept { connect(mainWindow.get(), &MainWindow::OpenROM, this, &KaizenQt::LoadROM); connect(mainWindow.get(), &MainWindow::Exit, this, &KaizenQt::Quit); connect(mainWindow.get(), &MainWindow::Reset, emuThread.get(), &EmuThread::Reset); - connect(mainWindow.get(), &MainWindow::Stop, emuThread.get(), &EmuThread::Stop); + connect(mainWindow.get(), &MainWindow::Stop, this, [this] { emuThread->requestInterruption(); }); connect(mainWindow.get(), &MainWindow::Pause, emuThread.get(), &EmuThread::TogglePause); } diff --git a/src/frontend/MainWindow.cpp b/src/frontend/MainWindow.cpp index a86f3173..84dbd232 100644 --- a/src/frontend/MainWindow.cpp +++ b/src/frontend/MainWindow.cpp @@ -1,4 +1,5 @@ #include +#include MainWindow::MainWindow(const std::shared_ptr &core) noexcept { if (objectName().isEmpty()) @@ -48,6 +49,8 @@ MainWindow::MainWindow(const std::shared_ptr &core) noexcept { setMenuBar(menubar.get()); statusbar = std::make_unique(this); statusbar->setObjectName("statusbar"); + fpsCounter = std::make_unique("Not playing"); + statusbar->addPermanentWidget(fpsCounter.get()); setStatusBar(statusbar.get()); menubar->addAction(menuFile->menuAction()); diff --git a/src/frontend/MainWindow.hpp b/src/frontend/MainWindow.hpp index c99ad048..8d85573f 100644 --- a/src/frontend/MainWindow.hpp +++ b/src/frontend/MainWindow.hpp @@ -35,6 +35,7 @@ public: std::unique_ptr menuTools{}; std::unique_ptr menuAbout{}; std::unique_ptr statusbar{}; + std::unique_ptr fpsCounter{}; private: void Retranslate();