From 30c1eeeb9f1ca5a643a30edb4e7010f8a60bdd30 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 11 Nov 2024 18:09:39 +0100 Subject: code cleanup changes --- src/crepe/api/LoopManager.cpp | 19 +++++++++++----- src/crepe/api/LoopManager.h | 15 +++++++++++- src/crepe/api/LoopTimer.cpp | 53 ++++++++++++++++++++++++------------------- src/crepe/api/LoopTimer.h | 2 +- 4 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 5c79920..abf1f7e 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -7,7 +7,14 @@ #include "LoopTimer.h" namespace crepe { - +LoopManager::LoopManager(const RenderSystem& renderSystem, const SDLContext& sdlContext, + const LoopTimer& loopTimer, const ScriptSystem& scriptSystem, + const SoundSystem& soundSystem, const ParticleSystem& particleSystem, + const PhysicsSystem& physicsSystem, const AnimatorSystem& animatorSystem, + const CollisionSystem& collisionSystem) { + // Initialize systems if needed + // Example: this->renderSystem = renderSystem; +} void LoopManager::process_input() { SDLContext::get_instance().handle_events(this->game_running); } @@ -28,13 +35,13 @@ void LoopManager::loop() { timer.update(); while (timer.get_lag() >= timer.get_fixed_delta_time()) { - process_input(); - fixed_update(); + this->process_input(); + this->fixed_update(); timer.advance_fixed_update(); } - update(); - render(); + this->update(); + this->render(); timer.enforce_frame_rate(); } @@ -48,7 +55,7 @@ void LoopManager::setup() { } void LoopManager::render() { - if (game_running) { + if (this->game_running) { RenderSystem::get_instance().update(); } } diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index af60d44..79abae6 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -1,11 +1,24 @@ #pragma once + namespace crepe { +class RenderSystem; +class SDLContext; +class LoopTimer; +class ScriptSystem; +class SoundSystem; +class ParticleSystem; +class PhysicsSystem; +class AnimatorSystem; +class CollisionSystem; + class LoopManager { public: void start(); - + LoopManager(const RenderSystem&, const SDLContext&, const LoopTimer&, const ScriptSystem&, + const SoundSystem&, const ParticleSystem&, const PhysicsSystem&, const AnimatorSystem&, + const CollisionSystem&); private: /** * \brief Setup function for one-time initialization. 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((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; } diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index 0089b24..4ce0053 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -122,7 +122,7 @@ public: private: //! Current frames per second int fps = 50; - ///< Current game scale + //! Current game scale double game_scale = 1; //! Maximum delta time to avoid large jumps double maximum_delta_time = 0.25; -- cgit v1.2.3