From 4f5597885ce6af93ca4655c193982aa701afb8cf Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 11 Nov 2024 20:28:29 +0100 Subject: git format --- src/crepe/api/LoopManager.cpp | 3 +- src/crepe/api/LoopManager.h | 18 ++++---- src/crepe/api/LoopTimer.cpp | 95 +++++++++++++++++++-------------------- src/crepe/api/LoopTimer.h | 100 +++++++++++++++++++++--------------------- 4 files changed, 106 insertions(+), 110 deletions(-) diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index ea63bf6..2e9823f 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -8,8 +8,7 @@ using namespace crepe; -LoopManager::LoopManager() { -} +LoopManager::LoopManager() {} void LoopManager::process_input() { SDLContext::get_instance().handle_events(this->game_running); } diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index d4668b8..755e2d2 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -13,8 +13,6 @@ class AnimatorSystem; class CollisionSystem; namespace crepe { - - class LoopManager { public: void start(); @@ -76,14 +74,14 @@ private: bool game_running = false; // For later if singletons are removed cant make them now because destructors are private. - // std::unique_ptr render_system; - // std::unique_ptr sdl_context; - // std::unique_ptr loop_timer; - // std::unique_ptr script_system; - // std::unique_ptr particle_system; - // std::unique_ptr physics_system; - // std::unique_ptr animator_system; - // std::unique_ptr collision_system; + // std::unique_ptr render_system; + // std::unique_ptr sdl_context; + // std::unique_ptr loop_timer; + // std::unique_ptr script_system; + // std::unique_ptr particle_system; + // std::unique_ptr physics_system; + // std::unique_ptr animator_system; + // std::unique_ptr collision_system; }; } // namespace crepe diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index 039e35e..8f09e41 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -7,83 +7,80 @@ using namespace crepe; -LoopTimer::LoopTimer() { - dbg_trace(); -} +LoopTimer::LoopTimer() { dbg_trace(); } LoopTimer & LoopTimer::get_instance() { - static LoopTimer instance; - return instance; + static LoopTimer instance; + return instance; } void LoopTimer::start() { - this->last_frame_time = std::chrono::steady_clock::now(); - this->elapsed_time = std::chrono::milliseconds(0); - this->elapsed_fixed_time = std::chrono::milliseconds(0); - this->delta_time = std::chrono::milliseconds(0); + this->last_frame_time = std::chrono::steady_clock::now(); + this->elapsed_time = std::chrono::milliseconds(0); + this->elapsed_fixed_time = std::chrono::milliseconds(0); + this->delta_time = std::chrono::milliseconds(0); } void LoopTimer::update() { - auto current_frame_time = std::chrono::steady_clock::now(); - // Convert to duration in seconds for delta time - this->delta_time = std::chrono::duration_cast>(current_frame_time - last_frame_time); - - if (this->delta_time > this->maximum_delta_time) { - this->delta_time = this->maximum_delta_time; - } - - this->delta_time *= this->game_scale; - this->elapsed_time += this->delta_time; - this->last_frame_time = current_frame_time; + auto current_frame_time = std::chrono::steady_clock::now(); + // Convert to duration in seconds for delta time + this->delta_time + = std::chrono::duration_cast>( + current_frame_time - last_frame_time); + + if (this->delta_time > this->maximum_delta_time) { + this->delta_time = this->maximum_delta_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 this->delta_time.count(); -} +double LoopTimer::get_delta_time() const { return this->delta_time.count(); } double LoopTimer::get_current_time() const { - return this->elapsed_time.count(); + return this->elapsed_time.count(); } void LoopTimer::advance_fixed_update() { - this->elapsed_fixed_time += this->fixed_delta_time; + this->elapsed_fixed_time += this->fixed_delta_time; } double LoopTimer::get_fixed_delta_time() const { - return this->fixed_delta_time.count(); + return this->fixed_delta_time.count(); } void LoopTimer::set_fps(int fps) { - this->fps = fps; + this->fps = fps; // target time per frame in seconds - this->frame_target_time = std::chrono::seconds(1) / fps; + this->frame_target_time = std::chrono::seconds(1) / fps; } -int LoopTimer::get_fps() const { - return this->fps; -} +int LoopTimer::get_fps() const { return this->fps; } -void LoopTimer::set_game_scale(double value) { - this->game_scale = value; -} +void LoopTimer::set_game_scale(double value) { this->game_scale = value; } -double LoopTimer::get_game_scale() const { - return this->game_scale; -} +double LoopTimer::get_game_scale() const { return this->game_scale; } void LoopTimer::enforce_frame_rate() { - std::chrono::steady_clock::time_point current_frame_time = std::chrono::steady_clock::now(); - std::chrono::milliseconds frame_duration = std::chrono::duration_cast(current_frame_time - this->last_frame_time); - - if (frame_duration < this->frame_target_time) { - std::chrono::milliseconds delay_time = std::chrono::duration_cast(this->frame_target_time - frame_duration); - if (delay_time.count() > 0) { - SDLContext::get_instance().delay(delay_time.count()); - } - } - - this->last_frame_time = current_frame_time; + std::chrono::steady_clock::time_point current_frame_time + = std::chrono::steady_clock::now(); + std::chrono::milliseconds frame_duration + = std::chrono::duration_cast( + current_frame_time - this->last_frame_time); + + if (frame_duration < this->frame_target_time) { + std::chrono::milliseconds delay_time + = std::chrono::duration_cast( + this->frame_target_time - frame_duration); + if (delay_time.count() > 0) { + SDLContext::get_instance().delay(delay_time.count()); + } + } + + this->last_frame_time = current_frame_time; } double LoopTimer::get_lag() const { - return (this->elapsed_time - this->elapsed_fixed_time).count(); + return (this->elapsed_time - this->elapsed_fixed_time).count(); } diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index ec48e4a..85687be 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -1,27 +1,27 @@ - #pragma once +#pragma once -#include #include +#include namespace crepe { class LoopTimer { public: - /** + /** * \brief Get the singleton instance of LoopTimer. * * \return A reference to the LoopTimer instance. */ - static LoopTimer& get_instance(); + static LoopTimer & get_instance(); - /** + /** * \brief Get the current delta time for the current frame. * * \return Delta time in seconds since the last frame. */ - double get_delta_time() const; + double get_delta_time() const; - /** + /** * \brief Get the current game time. * * \note The current game time may vary from real-world elapsed time. @@ -29,55 +29,55 @@ public: * * \return Elapsed game time in seconds. */ - double get_current_time() const; + double get_current_time() const; - /** + /** * \brief Set the target frames per second (FPS). * * \param fps The desired frames rendered per second. */ - void set_fps(int fps); + void set_fps(int fps); - /** + /** * \brief Get the current frames per second (FPS). * * \return Current FPS. */ - int get_fps() const; + int get_fps() const; - /** + /** * \brief Get the current game scale. * * \return The current game scale, where 0 = paused, 1 = normal speed, and values > 1 speed up the game. */ - double get_game_scale() const; + double get_game_scale() const; - /** + /** * \brief Set the game scale. * * \param game_scale The desired game scale (0 = pause, 1 = normal speed, > 1 = speed up). */ - void set_game_scale(double game_scale); + void set_game_scale(double game_scale); private: - friend class LoopManager; + friend class LoopManager; - /** + /** * \brief Start the loop timer. * * Initializes the timer to begin tracking frame times. */ - void start(); + void start(); - /** + /** * \brief Enforce the frame rate limit. * * Ensures that the game loop does not exceed the target FPS by delaying * frame updates as necessary. */ - void enforce_frame_rate(); + void enforce_frame_rate(); - /** + /** * \brief Get the fixed delta time for consistent updates. * * Fixed delta time is used for operations that require uniform time steps, @@ -85,9 +85,9 @@ private: * * \return Fixed delta time in seconds. */ - double get_fixed_delta_time() const; + double get_fixed_delta_time() const; - /** + /** * \brief Get the accumulated lag in the game loop. * * Lag represents the difference between the target frame time and the @@ -95,51 +95,53 @@ private: * * \return Accumulated lag in seconds. */ - double get_lag() const; + double get_lag() const; - /** + /** * \brief Construct a new LoopTimer object. * * Private constructor for singleton pattern to restrict instantiation * outside the class. */ - LoopTimer(); + LoopTimer(); - /** + /** * \brief Update the timer to the current frame. * * Calculates and updates the delta time for the current frame and adds it to * the cumulative game time. */ - void update(); + void update(); - /** + /** * \brief Advance the game loop by a fixed update interval. * * This method progresses the game state by a consistent, fixed time step, * allowing for stable updates independent of frame rate fluctuations. */ - void advance_fixed_update(); + void advance_fixed_update(); private: - //! Current frames per second - int fps = 50; - //! Current game scale - double game_scale = 1; - //! Maximum delta time in seconds to avoid large jumps - std::chrono::duration maximum_delta_time{0.25}; - //! Delta time for the current frame in seconds - std::chrono::duration delta_time{0.0}; - //! Target time per frame in seconds - std::chrono::duration frame_target_time = std::chrono::seconds(1) / fps; - //! Fixed delta time for fixed updates in seconds - std::chrono::duration fixed_delta_time = std::chrono::seconds(1) / 50; - //! Total elapsed game time in seconds - std::chrono::duration elapsed_time{0.0}; - //! Total elapsed time for fixed updates in seconds - std::chrono::duration elapsed_fixed_time{0.0}; - //! Time of the last frame - std::chrono::steady_clock::time_point last_frame_time; + //! Current frames per second + int fps = 50; + //! Current game scale + double game_scale = 1; + //! Maximum delta time in seconds to avoid large jumps + std::chrono::duration maximum_delta_time{0.25}; + //! Delta time for the current frame in seconds + std::chrono::duration delta_time{0.0}; + //! Target time per frame in seconds + std::chrono::duration frame_target_time + = std::chrono::seconds(1) / fps; + //! Fixed delta time for fixed updates in seconds + std::chrono::duration fixed_delta_time + = std::chrono::seconds(1) / 50; + //! Total elapsed game time in seconds + std::chrono::duration elapsed_time{0.0}; + //! Total elapsed time for fixed updates in seconds + std::chrono::duration elapsed_fixed_time{0.0}; + //! Time of the last frame + std::chrono::steady_clock::time_point last_frame_time; }; } // namespace crepe -- cgit v1.2.3