fix: Frame limiting not working correctly on Linux

This commit is contained in:
WerWolv 2024-02-28 18:48:01 +01:00
parent eaa4688182
commit 686d47a59e
1 changed files with 15 additions and 10 deletions

View File

@ -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<Window *>(glfwGetWindowUserPointer(window));
win->m_unlockFrameRate = true;
});
glfwSetWindowFocusCallback(m_window, [](GLFWwindow *, int focused) {
EventWindowFocused::post(focused == GLFW_TRUE);
});