From c1d16124e57837ba30abf2147a117fe666a32e0e Mon Sep 17 00:00:00 2001 From: NepDisk Date: Wed, 15 Oct 2025 16:32:16 -0400 Subject: [PATCH] Port Saturn timestuff branch Should be better then I_UpdateTime spam https://github.com/Indev450/SRB2Kart-Saturn/commits/timestuff/ --- src/d_main.cpp | 4 +--- src/i_time.c | 32 ++++++++++++++++++++++++++++++-- src/i_time.h | 2 ++ src/m_fixed.h | 14 ++++++++++++++ 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/d_main.cpp b/src/d_main.cpp index a2d3a9569..187c3dbef 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -1081,9 +1081,7 @@ void D_SRB2Loop(void) const boolean lagging = ((deltatics >= 1.0) || hu_stopped); - I_UpdateTime(cv_timescale.value); - - R_SetTimeFrac(lagging ? FRACUNIT : g_time.timefrac); + R_SetTimeFrac(lagging ? FRACUNIT : I_GetTimeFrac()); } else { diff --git a/src/i_time.c b/src/i_time.c index 3875afd9b..74380e6f7 100644 --- a/src/i_time.c +++ b/src/i_time.c @@ -29,6 +29,25 @@ consvar_t cv_timescale = CVAR_INIT ("timescale", "1.0", CV_NETVAR|CV_CHEAT|CV_FL static precise_t enterprecise, oldenterprecise; static fixed_t entertic, oldentertics; static double tictimer; +static double ticratescaled; + +// 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) +{ + 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); +} // A little more than the minimum sleep duration on Windows. // May be incorrect for other platforms, but we don't currently have a way to @@ -42,7 +61,16 @@ static double tictimer; tic_t I_GetTime(void) { - return g_time.time; + tic_t tic; + I_GetTimeAndFrac(&tic, NULL); + return tic; +} + +fixed_t I_GetTimeFrac(void) +{ + fixed_t frac; + I_GetTimeAndFrac(NULL, &frac); + return frac; } void I_InitializeTime(void) @@ -53,6 +81,7 @@ void I_InitializeTime(void) enterprecise = 0; oldenterprecise = 0; tictimer = 0.0; + ticratescaled = 1.0; CV_RegisterVar(&cv_timescale); @@ -63,7 +92,6 @@ void I_InitializeTime(void) void I_UpdateTime(fixed_t timescale) { - double ticratescaled; double elapsedseconds; tic_t realtics; diff --git a/src/i_time.h b/src/i_time.h index 141fbcb2d..3bf59b589 100644 --- a/src/i_time.h +++ b/src/i_time.h @@ -34,6 +34,8 @@ extern consvar_t cv_timescale; */ tic_t I_GetTime(void); +fixed_t I_GetTimeFrac(void); + /** \brief Initializes timing system. */ void I_InitializeTime(void); diff --git a/src/m_fixed.h b/src/m_fixed.h index 2080c396f..5f2aa864d 100644 --- a/src/m_fixed.h +++ b/src/m_fixed.h @@ -70,6 +70,20 @@ FUNCMATH FUNCINLINE static ATTRINLINE INT64 FloatToFixed64(float f) return (INT64)(f * FRACUNIT); } +/*! + * \brief convert fixed_t into double-precision floating number + */ + +FUNCMATH FUNCINLINE static ATTRINLINE double FixedToDouble(fixed_t x) +{ + return x / (double)FRACUNIT; +} + +FUNCMATH FUNCINLINE static ATTRINLINE fixed_t DoubleToFixed(double f) +{ + return (fixed_t)(f * FRACUNIT); +} + // for backwards compat #define FIXED_TO_FLOAT(x) FixedToFloat(x) // (((float)(x)) / ((float)FRACUNIT)) #define FLOAT_TO_FIXED(f) FloatToFixed(f) // (fixed_t)((f) * ((float)FRACUNIT))