From e1a8391170b9ab861e3171945cd943caf157f8fd Mon Sep 17 00:00:00 2001 From: RadWolfie Date: Wed, 30 Jun 2021 14:32:30 -0500 Subject: [PATCH] imgui: move fps updater functions into imgui ui class --- src/core/common/imgui/ui.cpp | 38 +++++++++++++++++++++-- src/core/common/imgui/ui.hpp | 4 +++ src/core/hle/D3D8/Direct3D9/Direct3D9.cpp | 4 +-- src/core/kernel/init/CxbxKrnl.cpp | 31 ------------------ src/devices/video/nv2a.cpp | 3 +- 5 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/core/common/imgui/ui.cpp b/src/core/common/imgui/ui.cpp index f4bf2eba4..d4a002140 100644 --- a/src/core/common/imgui/ui.cpp +++ b/src/core/common/imgui/ui.cpp @@ -45,6 +45,11 @@ bool ImGuiUI::Initialize() g_EmuShared->GetOverlaySettings(&m_settings); g_EmuShared->GetFlagsLLE(&m_lle_flags); + + // Internal initialize (when necessary, move into its own function.) + fps_counter = 30.0f; + + // Miscs m_audio.Initialize(); m_video.Initialize(); @@ -76,6 +81,36 @@ void ImGuiUI::ToggleImGui() g_EmuShared->SetImGuiFocusFlag(m_is_focus); } +static clock_t g_DeltaTime = 0; // Used for benchmarking/fps count +static unsigned int g_Frames = 0; + +// ****************************************************************** +// * update the current milliseconds per frame +// ****************************************************************** +void ImGuiUI::UpdateCurrentMSpFAndFPS() { + if (g_EmuShared) { + + fps_counter = (float)(g_Frames * 0.5 + fps_counter * 0.5); + g_EmuShared->SetCurrentFPS(&fps_counter); + } +} + +void ImGuiUI::UpdateFPSCounter() +{ + static clock_t lastDrawFunctionCallTime = 0; + clock_t currentDrawFunctionCallTime = clock(); + + g_DeltaTime += currentDrawFunctionCallTime - lastDrawFunctionCallTime; + lastDrawFunctionCallTime = currentDrawFunctionCallTime; + g_Frames++; + + if (g_DeltaTime >= CLOCKS_PER_SEC) { + UpdateCurrentMSpFAndFPS(); + g_Frames = 0; + g_DeltaTime -= CLOCKS_PER_SEC; + } +} + void ImGuiUI::DrawMenu() { if (!m_is_focus) { @@ -116,9 +151,6 @@ void ImGuiUI::DrawWidgets() ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoFocusOnAppearing)) { if (m_settings.fps) { - - float fps_counter; - g_EmuShared->GetCurrentFPS(&fps_counter); ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "FPS: %.2f MS / F : %.2f", fps_counter, (float)(1000.0 / fps_counter)); } diff --git a/src/core/common/imgui/ui.hpp b/src/core/common/imgui/ui.hpp index 931547437..19abf1498 100644 --- a/src/core/common/imgui/ui.hpp +++ b/src/core/common/imgui/ui.hpp @@ -28,6 +28,7 @@ public: void ToggleImGui(); bool IsImGuiFocus(); + void UpdateFPSCounter(); void DrawMenu(); void DrawWidgets(); @@ -48,6 +49,8 @@ protected: callback(this, arg); } + void UpdateCurrentMSpFAndFPS(); + std::mutex m_imgui_mutex; ImGuiContext* m_imgui_context; char m_file_path[FILENAME_MAX+1]; @@ -57,6 +60,7 @@ protected: ImGuiVideo m_video; overlay_settings m_settings; unsigned int m_lle_flags; + float fps_counter; // Make them as settings storage. /*bool m_show_fps; bool m_show_LLE_stats; diff --git a/src/core/hle/D3D8/Direct3D9/Direct3D9.cpp b/src/core/hle/D3D8/Direct3D9/Direct3D9.cpp index 059daf0d3..f1bf42c1b 100644 --- a/src/core/hle/D3D8/Direct3D9/Direct3D9.cpp +++ b/src/core/hle/D3D8/Direct3D9/Direct3D9.cpp @@ -223,8 +223,6 @@ static inline void EmuVerifyResourceIsRegistered(xbox::X_D3DRes static void UpdateCurrentMSpFAndFPS(); // Used for benchmarking/fps count static void CxbxImpl_SetRenderTarget(xbox::X_D3DSurface *pRenderTarget, xbox::X_D3DSurface *pNewZStencil); -extern void UpdateFPSCounter(); - #define CXBX_D3DCOMMON_IDENTIFYING_MASK (X_D3DCOMMON_TYPE_MASK | X_D3DCOMMON_D3DCREATED) @@ -5483,7 +5481,7 @@ xbox::dword_xt WINAPI xbox::EMUPATCH(D3DDevice_Swap) frameStartTime = std::chrono::steady_clock::now(); - UpdateFPSCounter(); + g_renderbase->UpdateFPSCounter(); if (Flags == CXBX_SWAP_PRESENT_FORWARD) // Only do this when forwarded from Present { diff --git a/src/core/kernel/init/CxbxKrnl.cpp b/src/core/kernel/init/CxbxKrnl.cpp index 822a0e1e6..2b3290dd7 100644 --- a/src/core/kernel/init/CxbxKrnl.cpp +++ b/src/core/kernel/init/CxbxKrnl.cpp @@ -2055,34 +2055,3 @@ void CxbxKrnlPanic() { CxbxKrnlCleanup("Kernel Panic!"); } - -static clock_t g_DeltaTime = 0; // Used for benchmarking/fps count -static unsigned int g_Frames = 0; - -// ****************************************************************** -// * update the current milliseconds per frame -// ****************************************************************** -static void UpdateCurrentMSpFAndFPS() { - if (g_EmuShared) { - static float currentFPSVal = 30; - - currentFPSVal = (float)(g_Frames*0.5 + currentFPSVal * 0.5); - g_EmuShared->SetCurrentFPS(¤tFPSVal); - } -} - -void UpdateFPSCounter() -{ - static clock_t lastDrawFunctionCallTime = 0; - clock_t currentDrawFunctionCallTime = clock(); - - g_DeltaTime += currentDrawFunctionCallTime - lastDrawFunctionCallTime; - lastDrawFunctionCallTime = currentDrawFunctionCallTime; - g_Frames++; - - if (g_DeltaTime >= CLOCKS_PER_SEC) { - UpdateCurrentMSpFAndFPS(); - g_Frames = 0; - g_DeltaTime -= CLOCKS_PER_SEC; - } -} diff --git a/src/devices/video/nv2a.cpp b/src/devices/video/nv2a.cpp index cc7baeeaf..6f33868e0 100644 --- a/src/devices/video/nv2a.cpp +++ b/src/devices/video/nv2a.cpp @@ -1040,7 +1040,6 @@ void cxbx_gl_render_overlays(NV2AState *d) } } -extern void UpdateFPSCounter(); void NV2ADevice::UpdateHostDisplay(NV2AState *d) { PGRAPHState *pg = &d->pgraph; @@ -1096,7 +1095,7 @@ void NV2ADevice::UpdateHostDisplay(NV2AState *d) // glo_set_current(NULL); - UpdateFPSCounter(); + g_renderbase->UpdateFPSCounter(); } // TODO: Fix this properly