From 385dfc44f766f39d006e3224aa373700b91e488b Mon Sep 17 00:00:00 2001 From: Roman Fomin Date: Tue, 30 Nov 2021 14:52:46 +0700 Subject: [PATCH] fix scaled time interpolation (#363) * fix scaled time interpolation * remove float --- Source/doomtype.h | 3 +++ Source/i_system.c | 32 +++++++++++++++++++++++++++++++- Source/i_system.h | 2 ++ Source/i_video.c | 5 ++++- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/Source/doomtype.h b/Source/doomtype.h index 2e99ad45..bdb94f38 100644 --- a/Source/doomtype.h +++ b/Source/doomtype.h @@ -76,6 +76,9 @@ typedef int64_t Long64; #ifndef MAX #define MAX(a,b) (((a)>(b))?(a):(b)) #endif +#ifndef BETWEEN + #define BETWEEN(l,u,x) ((l)>(x)?(l):(x)>(u)?(u):(x)) +#endif #if defined(_MSC_VER) && !defined(__cplusplus) #define inline __inline diff --git a/Source/i_system.c b/Source/i_system.c index e490e5ce..18676c81 100644 --- a/Source/i_system.c +++ b/Source/i_system.c @@ -98,6 +98,7 @@ int I_GetTimeMS(void) // killough 4/13/98: Make clock rate adjustable by scale factor int realtic_clock_rate = 100; +static int clock_rate; static Long64 I_GetTime_Scale = 1<<24; int I_GetTime_Scaled(void) { @@ -119,6 +120,26 @@ static int I_GetTime_Error() int (*I_GetTime)() = I_GetTime_Error; // killough +// During a fast demo, no time elapses in between ticks +static int I_TickElapsedTimeFastDemo(void) +{ + return 0; +} + +static int I_TickElapsedRealTime(void) +{ + return I_GetTimeMS() - I_GetTime() * 1000 / TICRATE; +} + +static int I_TickElapsedScaledTime(void) +{ + int scaled_time = I_GetTimeMS() * clock_rate / 100; + + return scaled_time - I_GetTime() * 1000 / TICRATE; +} + +int (*I_TickElapsedTime)(void) = I_TickElapsedRealTime; + int controllerpresent; // phares 4/3/98 int leds_always_off; // Tells it not to update LEDs @@ -227,7 +248,9 @@ extern boolean nomusicparm, nosfxparm; void I_Init(void) { - int clock_rate = realtic_clock_rate, p; + int p; + + clock_rate = realtic_clock_rate; if((p = M_CheckParm("-speed")) && p < myargc-1 && (p = atoi(myargv[p+1])) >= 10 && p <= 1000) @@ -238,15 +261,22 @@ void I_Init(void) // killough 4/14/98: Adjustable speedup based on realtic_clock_rate if(fastdemo) + { I_GetTime = I_GetTime_FastDemo; + I_TickElapsedTime = I_TickElapsedTimeFastDemo; + } else if(clock_rate != 100) { I_GetTime_Scale = ((Long64) clock_rate << 24) / 100; I_GetTime = I_GetTime_Scaled; + I_TickElapsedTime = I_TickElapsedScaledTime; } else + { I_GetTime = I_GetTime_RealTime; + I_TickElapsedTime = I_TickElapsedRealTime; + } I_InitJoystick(); diff --git a/Source/i_system.h b/Source/i_system.h index 3ab3db22..8f94fd56 100644 --- a/Source/i_system.h +++ b/Source/i_system.h @@ -44,6 +44,8 @@ int I_GetTime_RealTime(); // killough int I_GetTime_Adaptive(void); // killough 4/10/98 extern int GetTime_Scale; +extern int (*I_TickElapsedTime)(void); + // [FG] Same as I_GetTime, but returns time in milliseconds int I_GetTimeMS(); // [FG] toggle demo warp mode diff --git a/Source/i_video.c b/Source/i_video.c index 307d29e9..a9ccb858 100644 --- a/Source/i_video.c +++ b/Source/i_video.c @@ -939,7 +939,10 @@ void I_FinishUpdate(void) // [AM] Figure out how far into the current tic we're in as a fixed_t. if (uncapped) { - fractionaltic = I_GetTimeMS() * TICRATE % 1000 * FRACUNIT / 1000; + int tic_time = I_TickElapsedTime(); + + fractionaltic = tic_time * FRACUNIT * TICRATE / 1000; + fractionaltic = BETWEEN(0, FRACUNIT, fractionaltic); } I_RestoreDiskBackground();