From 007c6e122df8bec13d5fc9bdc4f7a929e2f2a03b Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 31 Aug 2020 00:16:43 +0200 Subject: [PATCH] - gameclock abstraction This may need more work to have a reliable timer --- source/core/gamecontrol.cpp | 3 ++- source/core/gamecontrol.h | 14 +++++++++++++- source/core/mainloop.cpp | 4 ++-- source/games/duke/src/gameloop.cpp | 3 +-- source/sw/src/game.cpp | 4 ++-- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/source/core/gamecontrol.cpp b/source/core/gamecontrol.cpp index a2e83fe45..92df3d908 100644 --- a/source/core/gamecontrol.cpp +++ b/source/core/gamecontrol.cpp @@ -96,7 +96,8 @@ GameInterface* gi; int myconnectindex, numplayers; int connecthead, connectpoint2[MAXMULTIPLAYERS]; auto vsnprintfptr = vsnprintf; // This is an inline in Visual Studio but we need an address for it to satisfy the MinGW compiled libraries. -int gameclock, gameclockstart; +int gameclock; +uint64_t gameclockstart; int lastTic; int automapMode; diff --git a/source/core/gamecontrol.h b/source/core/gamecontrol.h index 2fbf6d124..e814f98d8 100644 --- a/source/core/gamecontrol.h +++ b/source/core/gamecontrol.h @@ -9,6 +9,7 @@ #include "name.h" #include "memarena.h" #include "stats.h" +#include "i_time.h" extern FString currentGame; extern FString LumpFilter; @@ -223,5 +224,16 @@ extern int automapMode; extern bool automapFollow; extern bool sendPause; extern int gameclock; -extern int gameclockstart; +extern uint64_t gameclockstart; extern int lastTic; + +inline void setGameClockStart() +{ + gameclockstart = I_GetTimeNS(); + gameclock = 0; +} + +inline void updateGameClock() +{ + gameclock = static_cast((I_GetTimeNS() - gameclockstart) * 120 / 1'000'000'000); +} diff --git a/source/core/mainloop.cpp b/source/core/mainloop.cpp index 5c799fe48..bfca7608c 100644 --- a/source/core/mainloop.cpp +++ b/source/core/mainloop.cpp @@ -194,7 +194,7 @@ static void GameTicker() case GS_LEVEL: gameupdatetime.Reset(); gameupdatetime.Clock(); - gameclock = I_GetBuildTime() - gameclockstart; + updateGameClock(); gi->Ticker(); gameupdatetime.Unclock(); break; @@ -248,7 +248,7 @@ void Display() twod->Clear(); twod->SetSize(screen->GetWidth(), screen->GetHeight()); twodpsp.SetSize(screen->GetWidth(), screen->GetHeight()); - gameclock = I_GetBuildTime() - gameclockstart; + updateGameClock(); gi->Render(); DrawFullscreenBlends(); } diff --git a/source/games/duke/src/gameloop.cpp b/source/games/duke/src/gameloop.cpp index 58f8ac5f0..2fccffa6b 100644 --- a/source/games/duke/src/gameloop.cpp +++ b/source/games/duke/src/gameloop.cpp @@ -119,8 +119,7 @@ void GameInterface::Ticker() void resetGameClock() { I_SetFrameTime(); - gameclockstart = I_GetBuildTime(); - gameclock = 0; + setGameClockStart(); cloudclock = 0; } diff --git a/source/sw/src/game.cpp b/source/sw/src/game.cpp index 8ddb78ba1..d0443170f 100644 --- a/source/sw/src/game.cpp +++ b/source/sw/src/game.cpp @@ -728,7 +728,7 @@ void GameTicker(void) ready2send = 1; int const currentTic = I_GetTime(); - gameclock = I_GetBuildTime() - gameclockstart; + updateGameClock(); if (paused) { @@ -775,7 +775,7 @@ void GameTicker(void) void resetGameClock() { I_SetFrameTime(); - gameclockstart = I_GetBuildTime(); + setGameClockStart(); ogameclock = gameclock = 0; }