test: replace the timing system

i watched a random youtube video at 3am moment
this mostly gets rid of the global timestate in favor of tracking the time from gamestart to calling I_GetTime/Frac
this is more in line with most doom source ports handle it
This commit is contained in:
Alug committed 2025-11-07 16:24:34 +01:00
1 parent 8ffff231e1
commit bd3cedf233
11 files changed
+102 -164

No files matched your search

+4 -3
View File
@@ -2720,7 +2720,7 @@ static boolean CL_ServerConnectionTicker(const char *tmpsave, tic_t *oldtic, tic
Net_AckTicker();
// Call it only once by tic
if (*oldtic != I_GetTime())
if (*oldtic != I_GetGlobalTime())
{
INT32 key;
@@ -2764,7 +2764,8 @@ static boolean CL_ServerConnectionTicker(const char *tmpsave, tic_t *oldtic, tic
return false;
}
*oldtic = I_GetTime();
*oldtic = I_GetGlobalTime();
if (client && cl_mode != CL_CONNECTED && cl_mode != CL_ABORTED)
{
@@ -2786,7 +2787,7 @@ static boolean CL_ServerConnectionTicker(const char *tmpsave, tic_t *oldtic, tic
else
{
I_Sleep(cv_sleep.value);
I_UpdateTime(cv_timescale.value);
I_UpdateTime();
}
return true;
+19 -19
View File
@@ -676,6 +676,7 @@ void D_SRB2Loop(void)
tic_t entertic = 0, oldentertics = 0, realtics = 0, rendertimeout = INFTICS;
double deltatics = 0.0;
double deltasecs = 0.0;
UINT64 precision;
boolean interp = false;
boolean doDisplay = false;
@@ -685,8 +686,6 @@ void D_SRB2Loop(void)
server = true;
// Pushing of + parameters is now done back in D_SRB2Main, not here.
I_UpdateTime(cv_timescale.value);
oldentertics = I_GetTime();
// end of loading screen: CONS_Printf() will no more call FinishUpdate()
@@ -711,23 +710,26 @@ void D_SRB2Loop(void)
V_DrawFixedPatch(0, 0, FRACUNIT/2, 0, W_CachePatchNum(W_GetNumForName("KARTKREW"), PU_PATCH_LOWPRIORITY), NULL);
I_FinishUpdate(); // page flip or blit buffer
precision = I_GetPrecisePrecision();
for (;;)
{
// capbudget is the minimum precise_t duration of a single loop iteration
precise_t capbudget;
precise_t enterprecise = I_GetPreciseTime();
precise_t elapsed;
precise_t enterprecise, finishprecise;
enterprecise = I_GetPreciseTime();
memset(&g_dc, 0, sizeof(g_dc));
Z_Frame_Reset();
// Casting the return value of a function is bad practice (apparently)
double budget = ((R_GetFramerateCap() == 0) ? 0.0 : round((1.0 / R_GetFramerateCap()) * I_GetPrecisePrecision()));
capbudget = (precise_t)budget;
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)
{
oldentertics = lastwipetic;
@@ -800,16 +802,14 @@ void D_SRB2Loop(void)
if (interp)
{
renderdeltatics = FloatToFixed(deltatics);
renderdeltatics = DoubleToFixed(deltatics);
// I looked at the possibility of putting in a float drawer for
// perfstats and it's very complicated, so we'll just do this instead...
ps_interp_frac.value.p = (precise_t)((FIXED_TO_FLOAT(I_GetTimeFrac())) * 1000.0f);
ps_interp_lag.value.p = (precise_t)((deltasecs) * 1000.0);
const boolean lagging = ((deltatics >= 1.0) || hu_stopped);
R_SetTimeFrac(lagging ? FRACUNIT : I_GetTimeFrac());
R_SetTimeFrac(hu_stopped ? FRACUNIT : I_GetTimeFrac());
}
else
{
@@ -849,12 +849,13 @@ void D_SRB2Loop(void)
}
#endif
// Fully completed frame made.
precise_t finishprecise = I_GetPreciseTime();
finishprecise = I_GetPreciseTime();
// Use the time before sleep for frameskip calculations:
// post-sleep time is literally being intentionally wasted
deltasecs = (double)((INT64)(finishprecise - enterprecise)) / I_GetPrecisePrecision();
deltatics = deltasecs * NEWTICRATE;
elapsed = finishprecise - enterprecise;
deltasecs = (double)elapsed / (double)precision;
deltatics = deltasecs * (double)NEWTICRATE;
// If time spent this game loop exceeds a single tic,
// it's probably because of rendering.
@@ -882,12 +883,10 @@ void D_SRB2Loop(void)
if (!singletics)
{
INT64 elapsed = (INT64)(finishprecise - enterprecise);
// 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) && ((INT64)capbudget > elapsed) && !vsync_with_match_refresh)
if ((elapsed > 0) && (capbudget > elapsed) && !vsync_with_match_refresh)
{
I_SleepDuration(capbudget - elapsed);
}
@@ -895,8 +894,9 @@ void D_SRB2Loop(void)
// Capture the time once more to get the real delta time.
finishprecise = I_GetPreciseTime();
deltasecs = (double)((INT64)(finishprecise - enterprecise)) / I_GetPrecisePrecision();
deltatics = deltasecs * NEWTICRATE;
elapsed = finishprecise - enterprecise;
deltasecs = (double)elapsed / (double)precision;
deltatics = deltasecs * (double)NEWTICRATE;
}
}
+3 -3
View File
@@ -595,12 +595,12 @@ void Net_WaitAllAckReceived(UINT32 timeout)
HGetPacket();
while (timeout > I_GetTime() && !Net_AllAcksReceived())
while (timeout > I_GetGlobalTime() && !Net_AllAcksReceived())
{
while (tictac == I_GetTime())
while (tictac == I_GetGlobalTime())
{
I_Sleep(cv_sleep.value);
I_UpdateTime(cv_timescale.value);
I_UpdateTime();
}
tictac = I_GetTime();
+4 -3
View File
@@ -313,11 +313,12 @@ void F_IntroTicker(void)
// Stay on black for a bit. =)
{
tic_t quittime;
quittime = I_GetTime() + NEWTICRATE*2; // Shortened the quit time, used to be 2 seconds
while (quittime > I_GetTime())
quittime = I_GetGlobalTime() + NEWTICRATE*2; // Shortened the quit time, used to be 2 seconds
while (quittime > I_GetGlobalTime())
{
I_Sleep(cv_sleep.value);
I_UpdateTime(cv_timescale.value);
I_UpdateTime();
}
}
+4 -3
View File
@@ -368,10 +368,10 @@ void F_RunWipe(UINT8 wipetype, boolean drawMenu)
break;
// wait loop
while (!((nowtime = I_GetTime()) - lastwipetic))
while (!((nowtime = I_GetGlobalTime()) - lastwipetic))
{
I_Sleep(cv_sleep.value);
I_UpdateTime(cv_timescale.value);
I_UpdateTime();
}
lastwipetic = nowtime;
@@ -380,7 +380,7 @@ void F_RunWipe(UINT8 wipetype, boolean drawMenu)
HWR_DoWipe(wipetype, wipeframe-1); // send in the wipe type and wipeframe because we need to cache the graphic
else
#endif
if (rendermode != render_none) //this allows F_RunWipe to be called in dedicated servers
if (rendermode != render_none) // this allows F_RunWipe to be called in dedicated servers
F_DoWipe(fmask);
I_OsPolling();
@@ -404,6 +404,7 @@ void F_RunWipe(UINT8 wipetype, boolean drawMenu)
NetKeepAlive(); // Update the network so we don't cause timeouts
}
WipeInAction = false;
#endif
}
-4
View File
@@ -61,10 +61,6 @@ precise_t I_GetPreciseTime(void);
*/
UINT64 I_GetPrecisePrecision(void);
/** \brief Get the current time in rendering tics, including fractions.
*/
double I_GetFrameTime(void);
/** \brief Sleeps for the given duration in milliseconds. Depending on the
operating system's scheduler, the calling thread may give up its
time slice for a longer duration. The implementation should give a
+53 -64
View File
@@ -17,60 +17,59 @@
#include "command.h"
#include "doomtype.h"
#include "d_netcmd.h"
#include "m_fixed.h"
#include "i_system.h"
#include "f_finale.h"
timestate_t g_time;
static CV_PossibleValue_t timescale_cons_t[] = {{FRACUNIT/20, "MIN"}, {20*FRACUNIT, "MAX"}, {0, NULL}};
consvar_t cv_timescale = {"timescale", "1.0", CV_NETVAR|CV_CHEAT|CV_FLOAT, timescale_cons_t, NULL, FRACUNIT, NULL, NULL, 0, 0, NULL};
static precise_t enterprecise, oldenterprecise;
static fixed_t entertic, oldentertics;
static double tictimer;
static double ticratescaled;
static precise_t baseprecise;
static precise_t oldenterprecise;
// experiment to prevent timing issues
// this returns the global time state accounted with how much time has passed
// since it was last updated
static void I_GetTimeAndFrac(tic_t *outtics, fixed_t *outfrac)
static tic_t g_time;
static fixed_t I_GetTimeScale(void)
{
double elapsedseconds;
elapsedseconds = (double)(I_GetPreciseTime() - oldenterprecise) / I_GetPrecisePrecision();
double fractional, integral;
fractional = modf((tictimer + elapsedseconds) * ticratescaled, &integral);
if (outtics)
*outtics = g_time.time + (tic_t)integral;
if (outfrac)
*outfrac = DoubleToFixed(fractional);
return cv_timescale.value;
}
// get the current time that has passed since gamestart
tic_t I_GetTime(void)
{
// Beware wipes run on their own loop which affects global time state!
if (WipeInAction)
return g_time.time;
const double ticratescaled = (double)TICRATE * FixedToDouble(I_GetTimeScale());
tic_t tic;
I_GetTimeAndFrac(&tic, NULL);
return tic;
// stoopid gcc
const precise_t elapsed = I_GetPreciseTime() - baseprecise;
const UINT64 precision = I_GetPrecisePrecision();
// explictily do this in double precision
const double totaltime = (double)elapsed / (double)precision;
const double timeinticks = totaltime * ticratescaled;
return (tic_t)(timeinticks);
}
tic_t I_GetGlobalTime(void)
{
return g_time;
}
fixed_t I_GetTimeFrac(void)
{
// Beware wipes run on their own loop which affects global time state!
if (WipeInAction)
return g_time.timefrac;
const double ticratescaled = (double)TICRATE * FixedToDouble(I_GetTimeScale());
fixed_t frac;
I_GetTimeAndFrac(NULL, &frac);
return frac;
// stoopid gcc
const precise_t elapsed = I_GetPreciseTime() - baseprecise;
const UINT64 precision = I_GetPrecisePrecision();
// explictily do this in double precision
const double totaltime = (double)elapsed / (double)precision;
const double timeinticks = totaltime * ticratescaled;
double integral;
const double fractional = modf(timeinticks, &integral);
return DoubleToFixed(fractional);
}
void I_InitializeTime(void)
@@ -81,42 +80,32 @@ void I_InitializeTime(void)
// timing information for I_GetPreciseTime and sleeping
I_StartupTimer();
g_time.time = 0;
g_time.timefrac = 0;
g_time = 0;
enterprecise = I_GetPreciseTime();
oldenterprecise = enterprecise;
entertic = 0;
oldentertics = 0;
tictimer = 0.0;
ticratescaled = 1.0;
baseprecise = I_GetPreciseTime();
oldenterprecise = baseprecise;
}
void I_UpdateTime(fixed_t timescale)
// Used to track the time between two calls
// uhhh pretty much just for wipes and things like that
// probably would be better to not use a time global
// but i dont think it really matters too much in those cases
void I_UpdateTime(void)
{
double elapsedseconds;
tic_t realtics;
static tic_t oldentertics = 0;
// get real tics
ticratescaled = (double)((float)TICRATE * FixedToFloat(timescale));
const double ticratescaled = (double)TICRATE * FixedToDouble(I_GetTimeScale());
enterprecise = I_GetPreciseTime();
elapsedseconds = (double)(enterprecise - oldenterprecise) / I_GetPrecisePrecision();
tictimer += elapsedseconds;
while (tictimer > 1.0/ticratescaled)
{
entertic += 1;
tictimer -= 1.0/ticratescaled;
}
realtics = entertic - oldentertics;
const precise_t elapsed = I_GetPreciseTime() - baseprecise;
const UINT64 precision = I_GetPrecisePrecision();
const double totaltime = (double)elapsed / (double)precision;
const tic_t entertic = (tic_t)(totaltime * ticratescaled);
const tic_t realtics = entertic - oldentertics;
oldentertics = entertic;
oldenterprecise = enterprecise;
// Update global time state
g_time.time += realtics;
{
double fractional, integral;
fractional = modf(tictimer * ticratescaled, &integral);
g_time.timefrac = DoubleToFixed(fractional);
}
g_time += realtics;
}
+3 -7
View File
@@ -22,25 +22,21 @@
extern "C" {
#endif
typedef struct timestate_s {
tic_t time;
fixed_t timefrac;
} timestate_t;
extern timestate_t g_time;
extern consvar_t cv_timescale;
/** \brief Called by D_SRB2Loop, returns current time in game tics.
*/
tic_t I_GetTime(void);
tic_t I_GetGlobalTime(void);
fixed_t I_GetTimeFrac(void);
/** \brief Initializes timing system.
*/
void I_InitializeTime(void);
void I_UpdateTime(fixed_t timescale);
void I_UpdateTime(void);
/** \brief Block for at minimum the duration specified. This function makes a
best effort not to oversleep, and will spinloop if sleeping would
+9 -4
View File
@@ -10255,22 +10255,27 @@ void M_QuitResponse(INT32 ch)
if (ch != 'y' && ch != KEY_ENTER)
return;
if (!(netgame || cv_debug))
{
mrand = M_RandomKey(sizeof(quitsounds)/sizeof(INT32));
if (quitsounds[mrand]) S_StartSound(NULL, quitsounds[mrand]);
if (quitsounds[mrand])
S_StartSound(NULL, quitsounds[mrand]);
//added : 12-02-98: do that instead of I_WaitVbl which does not work
ptime = I_GetTime() + NEWTICRATE*2; // Shortened the quit time, used to be 2 seconds Tails 03-26-2001
while (ptime > I_GetTime())
ptime = I_GetGlobalTime() + NEWTICRATE*2; // Shortened the quit time, used to be 2 seconds Tails 03-26-2001
while (ptime > I_GetGlobalTime())
{
V_DrawFill(0, 0, BASEVIDWIDTH, BASEVIDHEIGHT, 31);
V_DrawSmallScaledPatch(0, 0, 0, (patch_t *)W_CachePatchName("GAMEQUIT", PU_PATCH)); // Demo 3 Quit Screen Tails 06-16-2001
I_FinishUpdate(); // Update the screen with the image Tails 06-19-2001
I_Sleep(cv_sleep.value);
I_UpdateTime(cv_timescale.value);
I_UpdateTime();
}
}
I_Quit();
}
+2 -2
View File
@@ -2534,10 +2534,10 @@ static boolean P_RunSpecialWipe(boolean reloadinggamestate)
while (nowtime < endtime)
{
// wait loop
while (!((nowtime = I_GetTime()) - lastwipetic))
while (!((nowtime = I_GetGlobalTime()) - lastwipetic))
{
I_Sleep(cv_sleep.value);
I_UpdateTime(cv_timescale.value);
I_UpdateTime();
}
lastwipetic = nowtime;
+1 -52
View File
@@ -1308,55 +1308,7 @@ precise_t I_GetPreciseTime(void)
UINT64 I_GetPrecisePrecision(void)
{
return SDL_GetPerformanceFrequency();
}
static UINT32 frame_rate;
static double frame_frequency;
static UINT64 frame_epoch;
static double elapsed_frames;
static void I_InitFrameTime(const UINT64 now, const UINT32 cap)
{
frame_rate = cap;
frame_epoch = now;
//elapsed_frames = 0.0;
if (frame_rate == 0)
{
// Shouldn't be used, but just in case...?
frame_frequency = 1.0;
return;
}
frame_frequency = timer_frequency / (double)frame_rate;
}
double I_GetFrameTime(void)
{
const UINT64 now = SDL_GetPerformanceCounter();
const UINT32 cap = R_GetFramerateCap();
if (cap != frame_rate)
{
// Maybe do this in a OnChange function for cv_fpscap?
I_InitFrameTime(now, cap);
}
if (frame_rate == 0)
{
// Always advance a frame.
elapsed_frames += 1.0;
}
else
{
elapsed_frames += (now - frame_epoch) / frame_frequency;
}
frame_epoch = now; // moving epoch
return elapsed_frames;
return timer_frequency;
}
//
@@ -1365,9 +1317,6 @@ double I_GetFrameTime(void)
void I_StartupTimer(void)
{
timer_frequency = SDL_GetPerformanceFrequency();
I_InitFrameTime(0, R_GetFramerateCap());
elapsed_frames = 0.0;
}
void I_Sleep(UINT32 ms)