From f732704b83572c6993ec3ba9bcbefcf75a4ca59b Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Sun, 29 Oct 2023 23:46:34 +0200 Subject: [PATCH] implemented fps counter --- src/emulator.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/src/emulator.cpp b/src/emulator.cpp index 44e69683..4bc747be 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -1,17 +1,29 @@ #include "emulator.h" -#include + #include #include -#include "Emulator/Util/singleton.h" +#include +#include #include #include "Core/PS4/HLE/Graphics/video_out.h" #include "Emulator/HLE/Libraries/LibPad/pad.h" +#include "Emulator/Util/singleton.h" #include "version.h" namespace Emu { bool m_emu_needs_exit = false; +double m_current_time_seconds = {0.0}; +double m_previous_time_seconds = {0.0}; +int m_update_num = {0}; +int m_frame_num = {0}; +double m_update_time_seconds = {0.0}; +double m_current_fps = {0.0}; +int m_max_updates_per_frame = {4}; +double m_update_fixed_time = 1.0 / 60.0; +int m_fps_frames_num = {0}; +double m_fps_start_time = {0}; void emuInit(u32 width, u32 height) { auto* window_ctx = singleton::instance(); @@ -49,7 +61,39 @@ static void CreateSdlWindow(WindowCtx* ctx) { SDL_SetWindowResizable(ctx->m_window, SDL_FALSE); // we don't support resizable atm } +static void update() { + static double lag = 0.0; + + lag += m_current_time_seconds - m_previous_time_seconds; + + int num = 0; + + while (lag >= m_update_fixed_time) { + if (num < m_max_updates_per_frame) { + m_update_num++; + num++; + m_update_time_seconds = m_update_num * m_update_fixed_time; + } + + lag -= m_update_fixed_time; + } +} +static void calculateFps(double game_time_s) { + m_previous_time_seconds = m_current_time_seconds; + m_current_time_seconds = game_time_s; + + m_frame_num++; + + m_fps_frames_num++; + if (m_current_time_seconds - m_fps_start_time > 0.25f) { + m_current_fps = static_cast(m_fps_frames_num) / (m_current_time_seconds - m_fps_start_time); + m_fps_frames_num = 0; + m_fps_start_time = m_current_time_seconds; + } +} void emuRun() { + Lib::Timer timer; + timer.Start(); auto* window_ctx = singleton::instance(); { // init window and wait until init finishes @@ -58,6 +102,7 @@ void emuRun() { Graphics::Vulkan::vulkanCreate(window_ctx); window_ctx->m_is_graphic_initialized = true; window_ctx->m_graphic_initialized_cond.notify_one(); + calculateFps(timer.GetTimeSec()); } bool exit_loop = false; @@ -88,9 +133,13 @@ void emuRun() { continue; } exit_loop = m_emu_needs_exit; - if (!exit_loop) { - HLE::Libs::Graphics::VideoOut::videoOutFlip(100000); // flip every 0.1 sec + update(); + } + if (!exit_loop) { + if (HLE::Libs::Graphics::VideoOut::videoOutFlip(100000)) { // flip every 0.1 sec + calculateFps(timer.GetTimeSec()); + } } } std::exit(0); @@ -102,6 +151,12 @@ HLE::Libs::Graphics::GraphicCtx* getGraphicCtx() { return &window_ctx->m_graphic_ctx; } +void updateSDLTitle() { + char title[256]; + sprintf(title, "shadps4 v %s FPS: %f", Emulator::VERSION, m_current_fps); + auto* window_ctx = singleton::instance(); + SDL_SetWindowTitle(window_ctx->m_window, title); +} void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) { auto* window_ctx = singleton::instance(); if (window_ctx->is_window_hidden) { @@ -192,6 +247,7 @@ void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) { fmt::print("vkQueuePresentKHR failed\n"); std::exit(0); } + updateSDLTitle(); } void keyboardEvent(SDL_Event* event) {