From 6959c971de6ba2541ad879f2a45f0de351003637 Mon Sep 17 00:00:00 2001 From: Death Killer <884052+deathkiller@users.noreply.github.com> Date: Sun, 28 Jan 2024 23:08:39 +0100 Subject: [PATCH] Improved `Clock` --- Sources/nCine/Base/Clock.cpp | 4 +--- Sources/nCine/Base/Clock.h | 9 +-------- Sources/nCine/Base/FrameTimer.cpp | 6 ++++++ 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/Sources/nCine/Base/Clock.cpp b/Sources/nCine/Base/Clock.cpp index 43b88df7..6cefcece 100644 --- a/Sources/nCine/Base/Clock.cpp +++ b/Sources/nCine/Base/Clock.cpp @@ -24,7 +24,7 @@ namespace nCine } Clock::Clock() - : frequency_(0UL), baseCount_(0ULL) + : frequency_(0UL) { #if defined(DEATH_TARGET_WINDOWS) LARGE_INTEGER li; @@ -51,8 +51,6 @@ namespace nCine frequency_ = 1.0e6L; } #endif - - baseCount_ = counter(); } uint64_t Clock::counter() const diff --git a/Sources/nCine/Base/Clock.h b/Sources/nCine/Base/Clock.h index 69263d3c..f8760ffa 100644 --- a/Sources/nCine/Base/Clock.h +++ b/Sources/nCine/Base/Clock.h @@ -8,9 +8,8 @@ namespace nCine class Clock { public: - /// Returns elapsed time in ticks since base time inline uint64_t now() const { - return counter() - baseCount_; + return counter(); } /// Returns current value of the counter @@ -20,10 +19,6 @@ namespace nCine inline uint32_t frequency() const { return frequency_; } - /// Retruns the counter value at initialization time - inline uint64_t baseCount() const { - return baseCount_; - } private: #if defined(DEATH_TARGET_WINDOWS) @@ -34,8 +29,6 @@ namespace nCine /// Counter frequency in counts per second uint32_t frequency_; - /// Counter value at initialization time - uint64_t baseCount_; /// Private constructor Clock(); diff --git a/Sources/nCine/Base/FrameTimer.cpp b/Sources/nCine/Base/FrameTimer.cpp index ed670d14..a0aef6f5 100644 --- a/Sources/nCine/Base/FrameTimer.cpp +++ b/Sources/nCine/Base/FrameTimer.cpp @@ -29,6 +29,12 @@ namespace nCine timeMults_[1] = timeMultLast; // Update the FPS average calculation every `avgInterval_` seconds + if (frameStart_ < lastAvgUpdate_ || frameStart_ < lastLogUpdate_) { + LOGW("Detected time discontinuity, resetting counters"); + lastAvgUpdate_ = frameStart_; + lastLogUpdate_ = frameStart_; + } + const float secsSinceLastAvgUpdate = (frameStart_ - lastAvgUpdate_).seconds(); if (averageInterval_ > 0.0f && secsSinceLastAvgUpdate > averageInterval_) { avgFps_ = static_cast(avgNumFrames_) / secsSinceLastAvgUpdate;