From b2d0d7f9e504b43ac8b8dbd7c1d6d3360b014eb2 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 9 Oct 2022 14:50:45 +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. --- source/common/utility/i_time.cpp | 18 +++++++++++++++--- source/common/utility/i_time.h | 2 ++ source/core/gamecontrol.cpp | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/source/common/utility/i_time.cpp b/source/common/utility/i_time.cpp index 4f2ac96cb..51ad0359d 100644 --- a/source/common/utility/i_time.cpp +++ b/source/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/source/common/utility/i_time.h b/source/common/utility/i_time.h index d9501ea96..e5d3d5c8d 100644 --- a/source/common/utility/i_time.h +++ b/source/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/source/core/gamecontrol.cpp b/source/core/gamecontrol.cpp index bf87d8b3d..a8d130f6d 100644 --- a/source/core/gamecontrol.cpp +++ b/source/core/gamecontrol.cpp @@ -554,7 +554,7 @@ void System_MenuDim(); int GameMain() { int r; - + I_InitTime(); SetConsoleNotifyBuffer(); sysCallbacks = {