Skip to content

Fix framerate accuracy in low-framerate or lazy-update scenarios by discarding outdated data. #8521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5201,6 +5201,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);
Expand Down