game.cpp: improve G_FPSLimit() to be more stable with regard to floating point precision.

Additionally, prefer rendering early and compensating with a late frame due to clock precision rather than the other way around so that we are more consistently within a target vblank period.

git-svn-id: https://svn.eduke32.com/eduke32@7731 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
pogokeen 2019-06-25 18:34:59 +00:00 committed by Christoph Oelckers
parent 19c2c690d3
commit b014ae4d54

View file

@ -6137,17 +6137,26 @@ void G_MaybeAllocPlayer(int32_t pnum)
int G_FPSLimit(void)
{
static auto nextPageTicks = (double)timerGetTicksU64();
static double nextPageDelay = g_frameDelay;
static uint64_t lastFrameTicks = timerGetTicksU64() - (uint64_t) g_frameDelay;
int frameWaiting = 0;
auto const frameTicks = (double)timerGetTicksU64();
uint64_t const frameTicks = timerGetTicksU64();
uint64_t elapsedTime = frameTicks-lastFrameTicks;
if (!r_maxfps || frameTicks >= nextPageTicks)
if (!r_maxfps || elapsedTime >= (uint64_t) nextPageDelay)
{
if (frameTicks >= nextPageTicks + g_frameDelay)
nextPageTicks = frameTicks;
if (elapsedTime >= (uint64_t) (nextPageDelay + g_frameDelay))
{
//If we missed a frame, reset any cumulated remainder from rendering frames early
nextPageDelay = g_frameDelay;
}
else
{
nextPageDelay += g_frameDelay - elapsedTime;
}
nextPageTicks += g_frameDelay;
lastFrameTicks = frameTicks;
++frameWaiting;
}