1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-24 15:24:30 +01:00

Add utility function and some minor cleanup/documentation

This commit is contained in:
ζeh Matt
2021-12-13 19:10:47 +02:00
parent c464e38515
commit fc2862323a
3 changed files with 23 additions and 4 deletions

View File

@@ -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();

View File

@@ -12,6 +12,9 @@
namespace OpenRCT2
{
/// <summary>
/// Restartable timer utility that starts at construction.
/// </summary>
class Timer
{
using Clock = std::chrono::high_resolution_clock;
@@ -20,15 +23,32 @@ namespace OpenRCT2
Timepoint _tp = Clock::now();
public:
/// <summary>
/// Restarts the timer.
/// </summary>
/// <returns></returns>
void Restart() noexcept
{
_tp = Clock::now();
}
std::chrono::duration<float> GetElapsedSeconds() const noexcept
/// <summary>
/// Returns the amount of time in seconds since the last start.
/// </summary>
[[nodiscard]] std::chrono::duration<float> GetElapsedTime() const noexcept
{
return Clock::now() - _tp;
}
/// <summary>
/// Returns the amount of time in seconds since the last start and restarts.
/// </summary>
[[nodiscard]] std::chrono::duration<float> GetElapsedTimeAndRestart() noexcept
{
const auto res = Clock::now() - _tp;
Restart();
return res;
}
};
} // namespace OpenRCT2

View File

@@ -75,7 +75,7 @@ void DiscordService::Tick()
{
Discord_RunCallbacks();
if (_updateTimer.GetElapsedSeconds() < REFRESH_INTERVAL)
if (_updateTimer.GetElapsedTime() < REFRESH_INTERVAL)
return;
RefreshPresence();