From 8e603dc982351f599bbec0dee1cedd943d8a3140 Mon Sep 17 00:00:00 2001 From: Wen-Chao Huang Date: Wed, 26 Mar 2025 03:15:17 +0800 Subject: [PATCH] Fix framerate accuracy in low-framerate or lazy-update scenarios by discarding outdated data. --- imgui.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/imgui.cpp b/imgui.cpp index ba3271c54..dfd854d26 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -5198,6 +5198,15 @@ void ImGui::NewFrame() g.FramerateSecPerFrameCount = ImMin(g.FramerateSecPerFrameCount + 1, IM_ARRAYSIZE(g.FramerateSecPerFrame)); g.IO.Framerate = (g.FramerateSecPerFrameAccum > 0.0f) ? (1.0f / (g.FramerateSecPerFrameAccum / (float)g.FramerateSecPerFrameCount)) : FLT_MAX; + // While the accumulated frame time exceeds 1 second and there is more than one frame in the buffer, remove the oldest frame to keep the window within approximately 1 second. + while ((g.FramerateSecPerFrameAccum > 1.0f) && (g.FramerateSecPerFrameCount > 1)) + { + int pop_index = (g.FramerateSecPerFrameIdx + IM_ARRAYSIZE(g.FramerateSecPerFrame) - g.FramerateSecPerFrameCount) % IM_ARRAYSIZE(g.FramerateSecPerFrame); + g.FramerateSecPerFrameAccum -= g.FramerateSecPerFrame[pop_index]; + g.FramerateSecPerFrame[pop_index] = 0.0f; + g.FramerateSecPerFrameCount--; + } + // Process input queue (trickle as many events as possible), turn events into writes to IO structure g.InputEventsTrail.resize(0); UpdateInputEvents(g.IO.ConfigInputTrickleEventQueue);