- normalize the timer with the app start, not the epoch.

This ensures smaller values and less wraparounds with integer values in scripts.
This commit is contained in:
Christoph Oelckers 2022-10-09 14:52:08 +02:00
parent b225a910a0
commit 94b249172a
3 changed files with 18 additions and 3 deletions

View file

@ -44,6 +44,7 @@
//
//==========================================================================
static uint64_t StartupTimeNS;
static uint64_t FirstFrameStartTime;
static uint64_t CurrentFrameStartTime;
static uint64_t FreezeTime;
@ -52,11 +53,22 @@ int GameTicRate = 35; // make sure it is not 0, even if the client doesn't set i
double TimeScale = 1.0;
static uint64_t GetClockTimeNS()
static uint64_t GetTimePoint()
{
using namespace std::chrono;
if (TimeScale == 1.0) return (uint64_t)(duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count());
else return (uint64_t)((duration_cast<microseconds>(steady_clock::now().time_since_epoch()).count()) * (uint64_t)(TimeScale * 1000));
return (uint64_t)(duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count());
}
void I_InitTime()
{
StartupTimeNS = GetTimePoint();
}
static uint64_t GetClockTimeNS()
{
auto tp = GetTimePoint() - StartupTimeNS;
if (TimeScale == 1.0) return tp;
else return tp / 1000 * TimeScale * 1000;
}
static uint64_t MSToNS(unsigned int ms)

View file

@ -5,6 +5,8 @@
extern int GameTicRate;
extern double TimeScale;
void I_InitTime();
// Called by D_DoomLoop, sets the time for the current frame
void I_SetFrameTime();

View file

@ -3688,6 +3688,7 @@ int GameMain()
{
int ret = 0;
GameTicRate = TICRATE;
I_InitTime();
ConsoleCallbacks cb = {
D_UserInfoChanged,