From 686d47a59e244e4236b6b8bc661c4941fc6da26a Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 28 Feb 2024 18:48:01 +0100 Subject: [PATCH] fix: Frame limiting not working correctly on Linux --- main/gui/source/window/window.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/main/gui/source/window/window.cpp b/main/gui/source/window/window.cpp index ac3cbf41f..d703ce41b 100644 --- a/main/gui/source/window/window.cpp +++ b/main/gui/source/window/window.cpp @@ -183,6 +183,7 @@ namespace hex { glfwWaitEvents(); } else { // If the application is visible, render a frame + glfwPollEvents(); // If the application is in long sleep mode, only render a frame every 200ms // Long sleep mode is enabled automatically after a few frames if the window content hasn't changed @@ -192,9 +193,10 @@ namespace hex { constexpr static auto LongSleepFPS = 5.0; const double timeout = std::max(0.0, (1.0 / LongSleepFPS) - (glfwGetTime() - m_lastStartFrameTime)); - glfwWaitEventsTimeout(timeout); - } else { - glfwPollEvents(); + auto endTime = std::chrono::steady_clock::now() + std::chrono::milliseconds(i64(timeout * 1000)); + while (!m_unlockFrameRate && std::chrono::steady_clock::now() < endTime) { + std::this_thread::sleep_for(std::chrono::microseconds(100)); + } } } @@ -220,13 +222,11 @@ namespace hex { } else if (targetFPS > 200) { glfwSwapInterval(0); } else { - if (!shouldLongSleep) { - glfwSwapInterval(0); - const auto frameTime = glfwGetTime() - m_lastStartFrameTime; - const auto targetFrameTime = 1.0 / targetFPS; - if (frameTime < targetFrameTime) { - glfwWaitEventsTimeout(targetFrameTime - frameTime); - } + glfwSwapInterval(0); + const auto frameTime = glfwGetTime() - m_lastStartFrameTime; + const auto targetFrameTime = 1.0 / targetFPS; + if (frameTime < targetFrameTime) { + std::this_thread::sleep_for(std::chrono::milliseconds(i64((targetFrameTime - frameTime) * 1000))); } } @@ -773,6 +773,11 @@ namespace hex { win->m_unlockFrameRate = true; }); + glfwSetMouseButtonCallback(m_window, [](GLFWwindow *window, int, int, int) { + auto win = static_cast(glfwGetWindowUserPointer(window)); + win->m_unlockFrameRate = true; + }); + glfwSetWindowFocusCallback(m_window, [](GLFWwindow *, int focused) { EventWindowFocused::post(focused == GLFW_TRUE); });