forked from jaildesigner-jailgames/jaildoctors_dilemma
migrat Logo a time based
This commit is contained in:
39
source/utils/delta_timer.cpp
Normal file
39
source/utils/delta_timer.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "utils/delta_timer.hpp"
|
||||
|
||||
DeltaTimer::DeltaTimer() noexcept
|
||||
: last_counter_(SDL_GetPerformanceCounter()),
|
||||
perf_freq_(static_cast<double>(SDL_GetPerformanceFrequency())),
|
||||
time_scale_(1.0f) {
|
||||
}
|
||||
|
||||
float DeltaTimer::tick() noexcept {
|
||||
const Uint64 now = SDL_GetPerformanceCounter();
|
||||
const Uint64 diff = (now > last_counter_) ? (now - last_counter_) : 0;
|
||||
last_counter_ = now;
|
||||
const double seconds = static_cast<double>(diff) / perf_freq_;
|
||||
return static_cast<float>(seconds * static_cast<double>(time_scale_));
|
||||
}
|
||||
|
||||
float DeltaTimer::peek() const noexcept {
|
||||
const Uint64 now = SDL_GetPerformanceCounter();
|
||||
const Uint64 diff = (now > last_counter_) ? (now - last_counter_) : 0;
|
||||
const double seconds = static_cast<double>(diff) / perf_freq_;
|
||||
return static_cast<float>(seconds * static_cast<double>(time_scale_));
|
||||
}
|
||||
|
||||
void DeltaTimer::reset(Uint64 counter) noexcept {
|
||||
if (counter == 0) {
|
||||
last_counter_ = SDL_GetPerformanceCounter();
|
||||
} else {
|
||||
last_counter_ = counter;
|
||||
}
|
||||
}
|
||||
|
||||
void DeltaTimer::setTimeScale(float scale) noexcept {
|
||||
time_scale_ = std::max(scale, 0.0f);
|
||||
}
|
||||
|
||||
float DeltaTimer::getTimeScale() const noexcept {
|
||||
return time_scale_;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user