Skip to content

Commit

Permalink
impr: Try to improve framerate limiting once more
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Mar 14, 2024
1 parent 642722b commit 95da957
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions main/gui/source/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,11 @@ namespace hex {
// Determine if the application should be in long sleep mode
bool shouldLongSleep = !m_unlockFrameRate;

// Wait 5 frames before actually enabling the long sleep mode to make animations not stutter
static i32 lockTimeout = 0;
static double lockTimeout = 0;
if (!shouldLongSleep) {
lockTimeout = m_lastFrameTime * 10'000;
lockTimeout = 0.05;
} else if (lockTimeout > 0) {
lockTimeout -= 1;
lockTimeout -= m_lastFrameTime;
}

if (shouldLongSleep && lockTimeout > 0)
Expand All @@ -209,6 +208,7 @@ namespace hex {
// Long sleep mode is enabled automatically after a few frames if the window content hasn't changed
// and no events have been received
if (shouldLongSleep) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
// Calculate the time until the next frame
constexpr static auto LongSleepFPS = 5.0;
const double timeout = std::max(0.0, (1.0 / LongSleepFPS) - (glfwGetTime() - m_lastStartFrameTime));
Expand Down Expand Up @@ -244,6 +244,7 @@ namespace hex {
} else {
if (!shouldLongSleep) {
glfwSwapInterval(0);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
const auto frameTime = glfwGetTime() - m_lastStartFrameTime;
const auto targetFrameTime = 1.0 / targetFPS;
if (frameTime < targetFrameTime) {
Expand Down

0 comments on commit 95da957

Please sign in to comment.