From 5ed5ff7ff09caf48286e0898ed0e3d4c40a16c10 Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Sun, 22 Dec 2024 01:58:09 +0300 Subject: [PATCH] Redo frame limiter logic to fix drifting. --- UnleashedRecomp/app.cpp | 9 ++++++--- UnleashedRecomp/app.h | 3 +-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/UnleashedRecomp/app.cpp b/UnleashedRecomp/app.cpp index 6ab35db0..157b8424 100644 --- a/UnleashedRecomp/app.cpp +++ b/UnleashedRecomp/app.cpp @@ -10,18 +10,21 @@ void FrameLimiter::execute(int64_t fps) { using namespace std::chrono_literals; - auto next = start + frame * 1000000000ns / fps; auto now = std::chrono::steady_clock::now(); - if (next > now && (next - now) < (2000000000ns / fps)) + if (now < next) { std::this_thread::sleep_for(std::chrono::floor(next - now - 1ms)); while ((now = std::chrono::steady_clock::now()) < next) std::this_thread::yield(); } + else + { + next = now; + } - frame = std::chrono::nanoseconds(std::chrono::steady_clock::now() - start).count() * fps / 1000000000ll + 1; + next += 1000000000ns / fps; } static FrameLimiter g_frameLimiter; diff --git a/UnleashedRecomp/app.h b/UnleashedRecomp/app.h index 5abf4452..d8441383 100644 --- a/UnleashedRecomp/app.h +++ b/UnleashedRecomp/app.h @@ -4,8 +4,7 @@ struct FrameLimiter { - std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); - int64_t frame = 0; + std::chrono::steady_clock::time_point next; void execute(int64_t fps); };