Redo frame limiter logic to fix drifting.

This commit is contained in:
Skyth 2024-12-22 01:58:09 +03:00
parent 42f808c926
commit 5ed5ff7ff0
2 changed files with 7 additions and 5 deletions

View file

@ -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<std::chrono::milliseconds>(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;

View file

@ -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);
};