Skip to content

Commit

Permalink
Improved Clock
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Jan 28, 2024
1 parent 366d284 commit 6959c97
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
4 changes: 1 addition & 3 deletions Sources/nCine/Base/Clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace nCine
}

Clock::Clock()
: frequency_(0UL), baseCount_(0ULL)
: frequency_(0UL)
{
#if defined(DEATH_TARGET_WINDOWS)
LARGE_INTEGER li;
Expand All @@ -51,8 +51,6 @@ namespace nCine
frequency_ = 1.0e6L;
}
#endif

baseCount_ = counter();
}

uint64_t Clock::counter() const
Expand Down
9 changes: 1 addition & 8 deletions Sources/nCine/Base/Clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions Sources/nCine/Base/FrameTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(avgNumFrames_) / secsSinceLastAvgUpdate;
Expand Down

0 comments on commit 6959c97

Please sign in to comment.