Compare commits

...
3 Commits
Author SHA1 Message Date
Alug 669b42c4bb remove the loop condition for framelimiter
this was for an older iteration where it ran the loop until it hit the target per condition, keeping like this may cause issue if the loop exits per condition instead of the return block
now we return from within the loop
2026-02-04 18:05:13 +01:00
Alug 2a658a8476 rewrite the framerate limiter
this completely rewrites the old simple sleep based limiter with
whatever tf i came up with at 2am
this almost completely gets rid of any jitter, stutter, uneven framepacing and missing the cap
it might be slightly less efficient due to busy waiting more, but so far it seems to be about the same
2026-02-04 18:05:00 +01:00
Alug 8cab07a4b2 add missing "ifdef LOGMESSAGES" 2026-02-03 15:59:53 +01:00
3 changed files with 74 additions and 30 deletions

No files matched your search

+70 -24
View File
@@ -653,6 +653,68 @@ static boolean D_Display(void)
return ranwipe;
}
static UINT64 precision = 0;
static precise_t sleeptime = 0;
static void D_LimitFps(void)
{
static precise_t curtime = 0, lasttime = 0;
static precise_t start;
precise_t elapsed = 0;
sleeptime = 0;
// timedemo runs uncapped!
if (singletics)
return;
// in the case of "match refresh rate" + vsync, don't sleep at all
if (cv_vidwait.value && cv_fpscap.value == 0)
return;
const UINT32 framecap = R_GetFramerateCap();
// uncapped obviously does not need to be... capped...
if (framecap == 0)
return;
start = I_GetPreciseTime();
// frametarget in precise_t ticks (ex. 60fps = ~16,666,666)
const precise_t frametarget = precision / framecap;
while (1)
{
// check the current time against the "time of the old frame""
curtime = I_GetPreciseTime();
elapsed = curtime - lasttime;
// done waiting!
// continue as normal
if (elapsed >= frametarget)
{
lasttime = curtime;
// time we spent :chaosleep:ing or doing absolutely nothing
// for frameskip calculations
sleeptime = curtime - start;
return;
}
// if we have more than 1ms to wait
// sleep for a bit to not trash the cpu lul
precise_t remaining = frametarget - elapsed;
if (remaining > (precise_t)(precision / 1000))
{
// sleep for 90% of the time we gotta wait for the next frame
// since we dont wanna oversleep :chaosleep:
I_SleepDuration(9 * remaining / 10);
}
}
// the rest we just be waitin until we meet our frametime target
}
// =========================================================================
// D_SRB2Loop
// =========================================================================
@@ -664,7 +726,6 @@ void D_SRB2Loop(void)
tic_t entertic = 0, oldentertics = 0, realtics = 0, rendertimeout = INFTICS;
double deltatics = 0.0;
double deltasecs = 0.0;
UINT64 precision = 0;
boolean interp = false;
boolean doDisplay = false;
@@ -704,22 +765,16 @@ void D_SRB2Loop(void)
for (;;)
{
// capbudget is the minimum precise_t duration of a single loop iteration
precise_t capbudget;
precise_t elapsed;
precise_t enterprecise, finishprecise;
enterprecise = I_GetPreciseTime();
boolean ranwipe = false;
memset(&g_dc, 0, sizeof(g_dc));
Z_Frame_Reset();
// Casting the return value of a function is bad practice (apparently)
const UINT32 framecap = R_GetFramerateCap();
capbudget = (framecap == 0) ? 0 : (precise_t)((double)precision / (double)framecap + 0.5); // + 0.5 instead of round
boolean ranwipe = false;
I_UpdateTime(cv_timescale.value);
if (lastwipetic)
@@ -809,6 +864,10 @@ void D_SRB2Loop(void)
R_SetTimeFrac(FRACUNIT);
}
// be sure to limit our fps *before* drawing
// so we can properly "pace" our frames
D_LimitFps();
if (interp || doDisplay)
{
if (!frameskip)
@@ -843,9 +902,9 @@ void D_SRB2Loop(void)
// Fully completed frame made.
finishprecise = I_GetPreciseTime();
// Use the time before sleep for frameskip calculations:
// Use the time without sleep for frameskip calculations:
// post-sleep time is literally being intentionally wasted
elapsed = finishprecise - enterprecise;
elapsed = (finishprecise - enterprecise) - sleeptime;
deltasecs = (double)elapsed / (double)precision;
deltatics = deltasecs * (double)NEWTICRATE;
@@ -873,19 +932,6 @@ void D_SRB2Loop(void)
frameskip = 0;
}
if (!singletics)
{
// in the case of "match refresh rate" + vsync, don't sleep at all
const boolean vsync_with_match_refresh = cv_vidwait.value && cv_fpscap.value == 0;
if ((elapsed > 0) && (capbudget > elapsed) && !vsync_with_match_refresh)
{
I_SleepDuration(capbudget - elapsed);
}
}
// Capture the time once more to get the real delta time.
finishprecise = I_GetPreciseTime();
elapsed = finishprecise - enterprecise;
deltasecs = (double)elapsed / (double)precision;
deltatics = deltasecs * (double)NEWTICRATE;
+2 -1
View File
@@ -225,9 +225,10 @@ HMS_connect (const char *format, ...)
{
cc = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
if (cc != CURLE_OK) I_OutputMsg("libcurl: %s\n", buffer->errbuf);
#ifdef LOGMESSAGES
cc = curl_easy_setopt(curl, CURLOPT_STDERR, logstream);
if (cc != CURLE_OK) I_OutputMsg("libcurl: %s\n", buffer->errbuf);
#endif
}
if (M_CheckParm("-bindaddr") && M_IsNextParm())
+2 -5
View File
@@ -1841,18 +1841,15 @@ static void I_Fork(void)
I_RegisterChildSignals();
break;
default:
#ifdef LOGMESSAGES
if (logstream)
fclose(logstream);/* the child has this */
#endif
c = wait(&status);
#ifdef LOGMESSAGES
/* By the way, exit closes files. */
logstream = fopen(logfilename, "at");
#else
logstream = 0;
#endif
if (c == -1)
{
kill(child, SIGKILL);