diff options
Diffstat (limited to 'src/crepe/api/LoopTimer.cpp')
-rw-r--r-- | src/crepe/api/LoopTimer.cpp | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index aaba8b3..f4be510 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -4,6 +4,8 @@ #include "LoopTimer.h" +#define MS_PER_SECOND 1000.0 + using namespace crepe; LoopTimer::LoopTimer() { @@ -16,48 +18,53 @@ LoopTimer & LoopTimer::get_instance() { } void LoopTimer::start() { - last_frame_time = SDLContext::get_instance().get_ticks(); - elapsed_time = 0; - elapsed_fixed_time = 0; - delta_time = 0; + this->last_frame_time = SDLContext::get_instance().get_ticks(); + this->elapsed_time = 0; + this->elapsed_fixed_time = 0; + this->delta_time = 0; } void LoopTimer::update() { uint64_t current_frame_time = SDLContext::get_instance().get_ticks(); - delta_time = (current_frame_time - last_frame_time) / 1000.0; + this->delta_time = (current_frame_time - last_frame_time) / MS_PER_SECOND; - if (delta_time > maximum_delta_time) { - delta_time = maximum_delta_time; + if (this->delta_time > this->maximum_delta_time) { + this->delta_time = this->maximum_delta_time; } - delta_time = game_scale; - elapsed_time += delta_time; - last_frame_time = current_frame_time; + this->delta_time = this->game_scale; + this->elapsed_time += this->delta_time; + this->last_frame_time = current_frame_time; } -double LoopTimer::get_delta_time() const { return delta_time; } -int LoopTimer::get_current_time() const { return elapsed_time; } +double LoopTimer::get_delta_time() const { return this->delta_time; } + +int LoopTimer::get_current_time() const { return this->elapsed_time; } -void LoopTimer::advance_fixed_update() { elapsed_fixed_time += fixed_delta_time; } +void LoopTimer::advance_fixed_update() { this->elapsed_fixed_time += this->fixed_delta_time; } -double LoopTimer::get_fixed_delta_time() const { return fixed_delta_time; } +double LoopTimer::get_fixed_delta_time() const { return this->fixed_delta_time; } void LoopTimer::set_fps(int fps) { this->fps = fps; - frame_target_time = 1.0 / fps; + this->frame_target_time = 1.0 / this->fps; } -int LoopTimer::get_fps() const { return fps; } -void LoopTimer::set_game_scale(double value) { game_scale = value; }; -double LoopTimer::get_game_scale() const { return game_scale; } +int LoopTimer::get_fps() const { return this->fps; } + +void LoopTimer::set_game_scale(double value) { this->game_scale = value; }; + +double LoopTimer::get_game_scale() const { return this->game_scale; } + void LoopTimer::enforce_frame_rate() { uint64_t current_frame_time = SDLContext::get_instance().get_ticks(); - double frame_duration = (current_frame_time - last_frame_time) / 1000.0; + double frame_duration = (current_frame_time - this->last_frame_time) / MS_PER_SECOND; - if (frame_duration < frame_target_time) { - uint32_t delay_time - = (uint32_t) ((frame_target_time - frame_duration) / 1000.0); + if (frame_duration < this->frame_target_time) { + uint32_t delay_time = static_cast<uint32_t>((this->frame_target_time - frame_duration) * MS_PER_SECOND); SDLContext::get_instance().delay(delay_time); } + + this->last_frame_time = current_frame_time; } -double LoopTimer::get_lag() const { return elapsed_time - elapsed_fixed_time; } +double LoopTimer::get_lag() const { return this->elapsed_time - this->elapsed_fixed_time; } |