From 6fbbf899b0516908c9ba70869640bbcd87104367 Mon Sep 17 00:00:00 2001 From: daver32 <38791383+daver32@users.noreply.github.com> Date: Wed, 1 May 2024 20:48:41 +0200 Subject: [PATCH] fix: Frame rate getting unlocked when inputs are being processed (#1632) ### Problem description The framerate limiter doesn't work when inputs are being sent (eg mouse cursor moving over the window), because `glfwWaitEventsTimeout` returns early when it encounters an event. ### Implementation description I made it sleep for the remaining time when that happens. --- main/gui/source/window/window.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/main/gui/source/window/window.cpp b/main/gui/source/window/window.cpp index 23a65e403..0af149fbc 100644 --- a/main/gui/source/window/window.cpp +++ b/main/gui/source/window/window.cpp @@ -262,6 +262,13 @@ namespace hex { const auto targetFrameTime = 1.0 / targetFPS; if (frameTime < targetFrameTime) { glfwWaitEventsTimeout(targetFrameTime - frameTime); + + // glfwWaitEventsTimeout might return early if there's an event + const auto frameTime = glfwGetTime() - m_lastStartFrameTime; + if (frameTime < targetFrameTime) { + const auto timeToSleepMs = (int)((targetFrameTime - frameTime) * 1000); + std::this_thread::sleep_for(std::chrono::milliseconds(timeToSleepMs)); + } } } }