From 77a6a0a51068ee6c8156ebd02194026b8a197f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20W=C3=BCbbers?= Date: Sat, 29 Mar 2025 05:37:07 +0100 Subject: [PATCH] fps display --- src/game/Setup.cpp | 3 ++- src/game/Tools.cpp | 35 ++++++++++++++++++++++++++++++++++- src/game/Tools.h | 1 + 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/game/Setup.cpp b/src/game/Setup.cpp index 1e8d30e..7299960 100644 --- a/src/game/Setup.cpp +++ b/src/game/Setup.cpp @@ -1,8 +1,8 @@ #include "Global.h" -#include "Input.h" #include "Instance.h" #include "Log.h" #include "Setup.h" +#include "Tools.h" #include "rendering/Rendering.h" #include "bx/bx.h" @@ -87,6 +87,7 @@ namespace Game bx::memCopy(win.LastHeldScanCodes, win.HeldScanCodes, sizeof(win.HeldScanCodes)); bx::memCopy(win.LastHeldMouseButtons, win.HeldMouseButtons, sizeof(win.HeldMouseButtons)); } + Tools::MeasureFrameEnd(); FrameMark; } diff --git a/src/game/Tools.cpp b/src/game/Tools.cpp index 919a2a8..091687b 100644 --- a/src/game/Tools.cpp +++ b/src/game/Tools.cpp @@ -1,10 +1,18 @@ #include "Instance.h" #include "Mesh.h" #include "Tools.h" +#include "bx/timer.h" #include #include +namespace +{ + constexpr int32_t FrameTimeBufSize = 512; + int64_t FrameTimes[FrameTimeBufSize]{0}; + int32_t FrameTimeIdx = 0; +} // namespace + namespace Tools { const char* GetAssetPath(Generated::AssetHandle assetHandle) @@ -337,10 +345,35 @@ namespace Tools { auto& drawList = *ImGui::GetWindowDrawList(); ImVec2 pos = ImGui::GetWindowPos(); - drawList.AddRectFilled(pos, {pos.x + FpsPlotSize.x, pos.y + FpsPlotSize.y}, 0xFFFFFF33); + drawList.AddRectFilled(pos, {pos.x + FpsPlotSize.x, pos.y + FpsPlotSize.y}, 0x22222233); + + float scale = 1000.0f; + + for (int32_t i = 0; i < FrameTimeBufSize; ++i) + { + int32_t idx = FrameTimeIdx - i - 1; + int32_t prevIdx = idx - 1; + if (idx < 0) idx += FrameTimeBufSize; + if (prevIdx < 0) prevIdx += FrameTimeBufSize; + + if (FrameTimes[idx] == 0 || FrameTimes[prevIdx] == 0) continue; + + int64_t frameTime = FrameTimes[idx] - FrameTimes[prevIdx]; + double frameTimeSec = (double)frameTime / bx::getHPFrequency(); + drawList.AddLine( + {pos.x + (FpsPlotSize.x - i - 1), pos.y + FpsPlotSize.y}, + {pos.x + (FpsPlotSize.x - i - 1), pos.y + FpsPlotSize.y - (float)frameTimeSec * scale}, + 0xFFFFFFFF); + } } ImGui::EndChild(); } ImGui::End(); } + void MeasureFrameEnd() + { + FrameTimes[FrameTimeIdx] = bx::getHPCounter(); + ++FrameTimeIdx; + if (FrameTimeIdx >= FrameTimeBufSize) FrameTimeIdx = 0; + } } // namespace Tools diff --git a/src/game/Tools.h b/src/game/Tools.h index 87f1e44..827f90e 100644 --- a/src/game/Tools.h +++ b/src/game/Tools.h @@ -7,4 +7,5 @@ namespace Tools void ModelDropdown(Generated::ModelHandle& modelHandle); void TextureDropdown(Generated::TextureHandle& texHandle); void RenderDebugUI(Game::GameRendering& rendering); + void MeasureFrameEnd(); } // namespace Tools