From 620f0da65b49720a2dcec168c9e321c5395169a9 Mon Sep 17 00:00:00 2001 From: terminx Date: Mon, 12 Aug 2019 03:15:29 +0000 Subject: [PATCH] Another attempt at improving/fixing the fps limiter git-svn-id: https://svn.eduke32.com/eduke32@7954 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/duke3d/src/game.cpp | 27 ++++++++++----------------- source/duke3d/src/game.h | 2 +- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index 44e1ba8af..c81592af5 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -6326,30 +6326,23 @@ int G_FPSLimit(void) if (!r_maxfps) return 1; - static double nextPageDelay = g_frameDelay; - uint64_t const delay = llrint(nextPageDelay); + static double nextPageDelay; + static uint64_t lastFrameTicks; - uint64_t const frameTicks = timerGetTicksU64(); - uint64_t const frameDelay = (uint64_t)llrint(g_frameDelay); + uint64_t const frameTicks = timerGetTicksU64(); + int64_t const elapsedTime = frameTicks - lastFrameTicks; - static uint64_t lastFrameTicks = frameTicks - frameDelay; - uint64_t elapsedTime = frameTicks-lastFrameTicks; - - int frameWaiting = 0; - - if (elapsedTime >= delay) + if (elapsedTime >= lrint(floor(nextPageDelay))) { - //If we missed a frame, reset any cumulated remainder from rendering frames early - if (elapsedTime > frameDelay) - nextPageDelay = g_frameDelay; - else - nextPageDelay += double(frameDelay-elapsedTime); + if (elapsedTime <= lrint(floor(nextPageDelay + g_frameDelay))) + nextPageDelay = nearbyint(nextPageDelay + g_frameDelay - (double)elapsedTime); lastFrameTicks = frameTicks; - ++frameWaiting; + + return 1; } - return frameWaiting; + return 0; } // TODO: reorder (net)actor_t to eliminate slop and update assertion diff --git a/source/duke3d/src/game.h b/source/duke3d/src/game.h index 76f33f853..e28be0979 100644 --- a/source/duke3d/src/game.h +++ b/source/duke3d/src/game.h @@ -349,7 +349,7 @@ extern palette_t CrosshairColors; extern palette_t DefaultCrosshairColors; extern double g_frameDelay; -static inline double calcFrameDelay(int maxFPS) { return maxFPS > 0 ? ((double)timerGetFreqU64() / (double)(maxFPS)) : 0.0; } +static inline double calcFrameDelay(int const maxFPS) { return maxFPS > 0 ? nearbyint((double)timerGetFreqU64()/maxFPS) : 0.0; } int32_t A_CheckInventorySprite(spritetype *s); int32_t A_InsertSprite(int16_t whatsect, int32_t s_x, int32_t s_y, int32_t s_z, int16_t s_pn, int8_t s_s, uint8_t s_xr,