From 863702b94de593c53d7aaaf871de6d0926ef15ae Mon Sep 17 00:00:00 2001 From: Fabian Greffrath Date: Thu, 10 Feb 2022 10:53:58 +0100 Subject: [PATCH] clean up timer functions, once again (#467) This time based on the implementation in Woof! --- prboom2/src/SDL/i_main.c | 46 +++++++++----------------------------- prboom2/src/SDL/i_system.c | 5 +---- 2 files changed, 11 insertions(+), 40 deletions(-) diff --git a/prboom2/src/SDL/i_main.c b/prboom2/src/SDL/i_main.c index df306bf70..c0fc02888 100644 --- a/prboom2/src/SDL/i_main.c +++ b/prboom2/src/SDL/i_main.c @@ -88,28 +88,20 @@ static int basetime = 0; static int I_GetTime_MS(void) { - int ticks = SDL_GetTicks(); + int ticks = SDL_GetTicks(); - if (basetime == 0) - basetime = ticks; + if (basetime == 0) + basetime = ticks; - return ticks - basetime; + return ticks - basetime; } int ms_to_next_tick; int I_GetTime_RealTime (void) { - int i; - int t = SDL_GetTicks(); - - //e6y: removing startup delay - if (basetime == 0) - basetime = t; - - t -= basetime; - - i = t * TICRATE / 1000; + int t = I_GetTime_MS(); + int i = t * TICRATE / 1000; ms_to_next_tick = (i + 1) * 1000 / TICRATE - t; ms_to_next_tick = BETWEEN(0, 1000 / TICRATE, ms_to_next_tick); @@ -121,15 +113,8 @@ int realtic_clock_rate = 100; static int I_GetTime_Scaled(void) { - int i; - int t = SDL_GetTicks(); - - if (basetime == 0) - basetime = t; - - t -= basetime; - - i = t * TICRATE * realtic_clock_rate / 100000; + int t = I_GetTime_MS(); + int i = t * TICRATE * realtic_clock_rate / 100000; ms_to_next_tick = (i + 1) * 100000 / realtic_clock_rate / TICRATE - t; ms_to_next_tick = BETWEEN(0, 100000 / realtic_clock_rate / TICRATE, ms_to_next_tick); @@ -137,8 +122,6 @@ static int I_GetTime_Scaled(void) return i; } - - static int I_GetTime_FastDemo(void) { static int fasttic; @@ -148,16 +131,12 @@ static int I_GetTime_FastDemo(void) return fasttic++; } - - static int I_GetTime_Error(void) { I_Error("I_GetTime_Error: GetTime() used before initialization"); return 0; } - - int (*I_GetTime)(void) = I_GetTime_Error; // During a fast demo, no time elapses in between ticks @@ -168,17 +147,12 @@ static int I_TickElapsedTime_FastDemo(void) static int I_TickElapsedTime_RealTime(void) { - int tic = I_GetTime(); - - return I_GetTime_MS() - tic * 1000 / TICRATE; + return I_GetTime_MS() * TICRATE % 1000 * FRACUNIT / 1000; } static int I_TickElapsedTime_Scaled(void) { - int tic = I_GetTime(); - int scaled_time = I_GetTime_MS() * realtic_clock_rate / 100; - - return scaled_time - tic * 1000 / TICRATE; + return I_GetTime_MS() * realtic_clock_rate * TICRATE / 100 % 1000 * FRACUNIT / 1000; } int (*I_TickElapsedTime)(void) = I_TickElapsedTime_RealTime; diff --git a/prboom2/src/SDL/i_system.c b/prboom2/src/SDL/i_system.c index a969b338c..71949ec76 100644 --- a/prboom2/src/SDL/i_system.c +++ b/prboom2/src/SDL/i_system.c @@ -140,10 +140,7 @@ fixed_t I_GetTimeFrac (void) } else { - int tic_time = I_TickElapsedTime(); - - frac = tic_time * FRACUNIT * TICRATE / 1000; - frac = BETWEEN(0, FRACUNIT, frac); + frac = I_TickElapsedTime(); } return frac;