Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
clean up timer functions, once again (#467)
Browse files Browse the repository at this point in the history
This time based on the implementation in Woof!
  • Loading branch information
fabiangreffrath authored Feb 10, 2022
1 parent 78102ed commit 863702b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 40 deletions.
46 changes: 10 additions & 36 deletions prboom2/src/SDL/i_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -121,24 +113,15 @@ 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);

return i;
}



static int I_GetTime_FastDemo(void)
{
static int fasttic;
Expand All @@ -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
Expand All @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions prboom2/src/SDL/i_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 863702b

Please sign in to comment.