- Allow ticrate to be specified to timer code, while still defaulting to GameTicRate. Move out I_GetBuildTime() from common code to gamefuncs.h as part of this.

* Allow specification of floating point ticrates as it's possible Blood's QAVs could have a fractional ticrate.
This commit is contained in:
Mitchell Richters 2021-08-01 23:02:13 +10:00
parent 0d9afc1aaf
commit 4a70f6efd0
3 changed files with 21 additions and 32 deletions

View file

@ -67,22 +67,14 @@ static uint64_t NSToMS(uint64_t ns)
return static_cast<uint64_t>(ns / 1'000'000);
}
static int NSToTic(uint64_t ns)
static int NSToTic(uint64_t ns, double const ticrate)
{
return static_cast<int>(ns * GameTicRate / 1'000'000'000);
return static_cast<int>(ns * ticrate / 1'000'000'000);
}
static int NSToBuildTic(uint64_t ns)
static uint64_t TicToNS(double tic, double const ticrate)
{
return static_cast<int>(ns * 120 / 1'000'000'000);
}
static uint64_t TicToNS(int tic)
{
return static_cast<uint64_t>(tic) * 1'000'000'000 / GameTicRate;
}
static uint64_t BuildTicToNS(int tic)
{
return static_cast<uint64_t>(tic) * 1'000'000'000 / 120;
return static_cast<uint64_t>(tic * 1'000'000'000 / ticrate);
}
void I_SetFrameTime()
@ -111,18 +103,18 @@ void I_WaitVBL(int count)
I_SetFrameTime();
}
int I_WaitForTic(int prevtic)
int I_WaitForTic(int prevtic, double const ticrate)
{
// Waits until the current tic is greater than prevtic. Time must not be frozen.
int time;
while ((time = I_GetTime()) <= prevtic)
while ((time = I_GetTime(ticrate)) <= prevtic)
{
// Windows-specific note:
// The minimum amount of time a thread can sleep is controlled by timeBeginPeriod.
// We set this to 1 ms in DoMain.
const uint64_t next = FirstFrameStartTime + TicToNS(prevtic + 1);
const uint64_t next = FirstFrameStartTime + TicToNS(prevtic + 1, ticrate);
const uint64_t now = I_nsTime();
if (next > now)
@ -166,21 +158,16 @@ uint64_t I_GetTimeNS()
return CurrentFrameStartTime - FirstFrameStartTime;
}
int I_GetTime()
int I_GetTime(double const ticrate)
{
return NSToTic(CurrentFrameStartTime - FirstFrameStartTime);
return NSToTic(CurrentFrameStartTime - FirstFrameStartTime, ticrate);
}
int I_GetBuildTime()
double I_GetTimeFrac(double const ticrate)
{
return NSToBuildTic(CurrentFrameStartTime - FirstFrameStartTime);
}
double I_GetTimeFrac()
{
int currentTic = NSToTic(CurrentFrameStartTime - FirstFrameStartTime);
uint64_t ticStartTime = FirstFrameStartTime + TicToNS(currentTic);
uint64_t ticNextTime = FirstFrameStartTime + TicToNS(currentTic + 1);
int currentTic = NSToTic(CurrentFrameStartTime - FirstFrameStartTime, ticrate);
uint64_t ticStartTime = FirstFrameStartTime + TicToNS(currentTic, ticrate);
uint64_t ticNextTime = FirstFrameStartTime + TicToNS(currentTic + 1, ticrate);
return (CurrentFrameStartTime - ticStartTime) / (double)(ticNextTime - ticStartTime);
}

View file

@ -9,17 +9,14 @@ extern double TimeScale;
void I_SetFrameTime();
// Called by D_DoomLoop, returns current time in tics.
int I_GetTime();
int I_GetTime(double const ticrate = GameTicRate);
// same, but using nanoseconds
uint64_t I_GetTimeNS();
// Called by Build games in lieu of totalclock, returns current time in tics at ticrate of 120.
int I_GetBuildTime();
double I_GetTimeFrac();
double I_GetTimeFrac(double const ticrate = GameTicRate);
// like I_GetTime, except it waits for a new tic before returning
int I_WaitForTic(int);
int I_WaitForTic(int prevtic, double const ticrate = GameTicRate);
// Freezes tic counting temporarily. While frozen, calls to I_GetTime()
// will always return the same value.

View file

@ -156,3 +156,8 @@ inline int spriteGetSlope(int spritenum)
auto spr = &sprite[spritenum];
return ((spr->cstat & CSTAT_SPRITE_ALIGNMENT_MASK) != CSTAT_SPRITE_ALIGNMENT_SLOPE) ? 0 : uint8_t(spr->xoffset) + (uint8_t(spr->yoffset) << 8);
}
inline int I_GetBuildTime()
{
return I_GetTime(120);
}