From fc2862323a36af6c58f0bb1a358c655f98bf725d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 13 Dec 2021 19:10:47 +0200 Subject: [PATCH] Add utility function and some minor cleanup/documentation --- src/openrct2/Context.cpp | 3 +-- src/openrct2/core/Timer.hpp | 22 +++++++++++++++++++++- src/openrct2/network/DiscordService.cpp | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index 60c0b62c0f..c60c87d79a 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -996,8 +996,7 @@ namespace OpenRCT2 void RunFrame() { - const auto deltaTime = _timer.GetElapsedSeconds().count(); - _timer.Restart(); + const auto deltaTime = _timer.GetElapsedTimeAndRestart().count(); // Make sure we catch the state change and reset it. bool useVariableFrame = ShouldRunVariableFrame(); diff --git a/src/openrct2/core/Timer.hpp b/src/openrct2/core/Timer.hpp index 192392f8f1..cde73d0640 100644 --- a/src/openrct2/core/Timer.hpp +++ b/src/openrct2/core/Timer.hpp @@ -12,6 +12,9 @@ namespace OpenRCT2 { + /// + /// Restartable timer utility that starts at construction. + /// class Timer { using Clock = std::chrono::high_resolution_clock; @@ -20,15 +23,32 @@ namespace OpenRCT2 Timepoint _tp = Clock::now(); public: + /// + /// Restarts the timer. + /// + /// void Restart() noexcept { _tp = Clock::now(); } - std::chrono::duration GetElapsedSeconds() const noexcept + /// + /// Returns the amount of time in seconds since the last start. + /// + [[nodiscard]] std::chrono::duration GetElapsedTime() const noexcept { return Clock::now() - _tp; } + + /// + /// Returns the amount of time in seconds since the last start and restarts. + /// + [[nodiscard]] std::chrono::duration GetElapsedTimeAndRestart() noexcept + { + const auto res = Clock::now() - _tp; + Restart(); + return res; + } }; } // namespace OpenRCT2 diff --git a/src/openrct2/network/DiscordService.cpp b/src/openrct2/network/DiscordService.cpp index c4375b147a..9c9c9b4639 100644 --- a/src/openrct2/network/DiscordService.cpp +++ b/src/openrct2/network/DiscordService.cpp @@ -75,7 +75,7 @@ void DiscordService::Tick() { Discord_RunCallbacks(); - if (_updateTimer.GetElapsedSeconds() < REFRESH_INTERVAL) + if (_updateTimer.GetElapsedTime() < REFRESH_INTERVAL) return; RefreshPresence();