From 94b249172a4bb2f0d39f7b83371bf10f878b27fb Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 9 Oct 2022 14:52:08 +0200 Subject: [PATCH] - normalize the timer with the app start, not the epoch. This ensures smaller values and less wraparounds with integer values in scripts. --- src/common/utility/i_time.cpp | 18 +++++++++++++++--- src/common/utility/i_time.h | 2 ++ src/d_main.cpp | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/common/utility/i_time.cpp b/src/common/utility/i_time.cpp index 4f2ac96cb0..51ad0359dd 100644 --- a/src/common/utility/i_time.cpp +++ b/src/common/utility/i_time.cpp @@ -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(steady_clock::now().time_since_epoch()).count()); - else return (uint64_t)((duration_cast(steady_clock::now().time_since_epoch()).count()) * (uint64_t)(TimeScale * 1000)); + return (uint64_t)(duration_cast(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) diff --git a/src/common/utility/i_time.h b/src/common/utility/i_time.h index d9501ea96a..e5d3d5c8df 100644 --- a/src/common/utility/i_time.h +++ b/src/common/utility/i_time.h @@ -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(); diff --git a/src/d_main.cpp b/src/d_main.cpp index 0aff8c3562..7d754e65c1 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -3688,6 +3688,7 @@ int GameMain() { int ret = 0; GameTicRate = TICRATE; + I_InitTime(); ConsoleCallbacks cb = { D_UserInfoChanged,