From 3f914412f3164c8cc5b4a3c877ae40ba0eb635c3 Mon Sep 17 00:00:00 2001 From: fleroviux Date: Thu, 26 Oct 2023 22:31:59 +0200 Subject: [PATCH] sdl2: add a basic frame counter --- src/platform/sdl/src/application.cpp | 21 +++++++++++++++++++-- src/platform/sdl/src/application.hpp | 4 ++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/platform/sdl/src/application.cpp b/src/platform/sdl/src/application.cpp index 3862d74..447b36f 100644 --- a/src/platform/sdl/src/application.cpp +++ b/src/platform/sdl/src/application.cpp @@ -26,7 +26,7 @@ int Application::Run(int argc, char **argv) { LoadBootROM("boot9.bin", true); LoadBootROM("boot7.bin", false); if(argc < 2) { - LoadROM("Simple_Tri.nds"); + LoadROM("pokeplatin.nds"); } else { LoadROM(argv[1]); } @@ -44,7 +44,7 @@ void Application::CreateWindow() { SDL_WINDOW_ALLOW_HIGHDPI ); - m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED); for(auto& texture : m_textures) { texture = SDL_CreateTexture(m_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 256, 192); @@ -138,6 +138,8 @@ void Application::MainLoop() { SDL_RenderPresent(m_renderer); m_emu_thread.ReleaseFrame(); + + UpdateFPS(); } m_emu_thread.SetFastForward(SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_SPACE]); @@ -155,3 +157,18 @@ void Application::MainLoop() { } } } + +void Application::UpdateFPS() { + const auto now = std::chrono::system_clock::now(); + const auto elapsed_time = std::chrono::duration_cast( + now - m_last_fps_update).count(); + + m_fps_counter++; + + if(elapsed_time >= 1000) { + const f32 fps = (f32)m_fps_counter / (f32)elapsed_time * 1000.0f; + SDL_SetWindowTitle(m_window, fmt::format("irisdual [{:.2f} fps]", fps).c_str()); + m_fps_counter = 0; + m_last_fps_update = std::chrono::system_clock::now(); + } +} diff --git a/src/platform/sdl/src/application.hpp b/src/platform/sdl/src/application.hpp index a85b558..edaa9fb 100644 --- a/src/platform/sdl/src/application.hpp +++ b/src/platform/sdl/src/application.hpp @@ -1,6 +1,7 @@ #pragma once +#include #include #include @@ -20,6 +21,7 @@ class Application { void LoadROM(const char* path); void LoadBootROM(const char* path, bool arm9); void MainLoop(); + void UpdateFPS(); SDL_Window* m_window; SDL_Renderer* m_renderer; @@ -27,4 +29,6 @@ class Application { std::unique_ptr m_nds{}; EmulatorThread m_emu_thread{}; + int m_fps_counter{}; + std::chrono::time_point m_last_fps_update{}; }; \ No newline at end of file