From d3ed3f4daa335bb881dbcf55aa425c602ddbd2f4 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 9 Nov 2024 12:58:26 +0100 Subject: changed variables --- lib/sdl2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/sdl2') diff --git a/lib/sdl2 b/lib/sdl2 index 9519b99..c98c4fb 160000 --- a/lib/sdl2 +++ b/lib/sdl2 @@ -1 +1 @@ -Subproject commit 9519b9916cd29a14587af0507292f2bd31dd5752 +Subproject commit c98c4fbff6d8f3016a3ce6685bf8f43433c3efcc -- cgit v1.2.3 From d23b23610f75797ff37cfd73881c6fd7d5a21c88 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 9 Nov 2024 13:35:25 +0100 Subject: cleanup code --- lib/sdl2 | 2 +- lib/sdl_image | 2 +- lib/sdl_ttf | 2 +- lib/soloud | 2 +- src/crepe/api/CMakeLists.txt | 4 ---- src/crepe/api/LoopManager.cpp | 1 - src/crepe/api/LoopTimer.cpp | 1 - src/example/CMakeLists.txt | 1 - src/example/gameLoop.cpp | 8 -------- src/makefile | 6 +++--- 10 files changed, 7 insertions(+), 22 deletions(-) delete mode 100644 src/example/gameLoop.cpp (limited to 'lib/sdl2') diff --git a/lib/sdl2 b/lib/sdl2 index c98c4fb..9519b99 160000 --- a/lib/sdl2 +++ b/lib/sdl2 @@ -1 +1 @@ -Subproject commit c98c4fbff6d8f3016a3ce6685bf8f43433c3efcc +Subproject commit 9519b9916cd29a14587af0507292f2bd31dd5752 diff --git a/lib/sdl_image b/lib/sdl_image index 5656019..abcf63a 160000 --- a/lib/sdl_image +++ b/lib/sdl_image @@ -1 +1 @@ -Subproject commit 56560190a6c5b5740e5afdce747e2a2bfbf16db1 +Subproject commit abcf63aa71b4e3ac32120fa9870a6500ddcdcc89 diff --git a/lib/sdl_ttf b/lib/sdl_ttf index a3d0895..4a318f8 160000 --- a/lib/sdl_ttf +++ b/lib/sdl_ttf @@ -1 +1 @@ -Subproject commit a3d0895c1b60c41ff9e85d9203ddd7485c014dae +Subproject commit 4a318f8dfaa1bb6f10e0c5e54052e25d3c7f3440 diff --git a/lib/soloud b/lib/soloud index e82fd32..c8e339f 160000 --- a/lib/soloud +++ b/lib/soloud @@ -1 +1 @@ -Subproject commit e82fd32c1f62183922f08c14c814a02b58db1873 +Subproject commit c8e339fdce5c7107bdb3e64bbf707c8fd3449beb diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index e7894d7..3b20142 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -16,8 +16,6 @@ target_sources(crepe PUBLIC Scene.cpp SceneManager.cpp Vector2.cpp - LoopManager.cpp - LoopTimer.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -40,6 +38,4 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Metadata.h SceneManager.h SceneManager.hpp - LoopManager.h - LoopTimer.h ) diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 0e22522..99763e4 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,5 +1,4 @@ - #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index 912493a..c6ffe9d 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -3,7 +3,6 @@ #include "LoopTimer.h" - using namespace crepe; LoopTimer::LoopTimer() {} diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 751b556..36f9d4d 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -28,5 +28,4 @@ add_example(proxy) add_example(db) add_example(ecs) add_example(scene_manager) -add_example(gameLoop) diff --git a/src/example/gameLoop.cpp b/src/example/gameLoop.cpp deleted file mode 100644 index f3edbdb..0000000 --- a/src/example/gameLoop.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "crepe/api/LoopManager.h" -using namespace crepe; - -int main(){ - LoopManager loop; - loop.start(); - return 1; -} diff --git a/src/makefile b/src/makefile index 5e525e8..ce5a29f 100644 --- a/src/makefile +++ b/src/makefile @@ -94,11 +94,11 @@ LOEK += example/script.cpp LOEK += test/audio.cpp LOEK += test/dummy.cpp JARO += test/PhysicsTest.cpp -WOUTER += crepe/api/LoopManager.cpp WOUTER += crepe/api/LoopManager.h -WOUTER += crepe/api/LoopTimer.cpp +WOUTER += crepe/api/LoopManager.cpp WOUTER += crepe/api/LoopTimer.h -FMT := $(JARO) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 +WOUTER += crepe/api/LoopTimer.cpp +FMT := $(WOUTER) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 format: FORCE clang-tidy -p build/compile_commands.json --fix-errors $(FMT) -- cgit v1.2.3 From 31a6448f8c97934dd9974758d9b5049650040bc7 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 11 Nov 2024 18:16:28 +0100 Subject: changed seconds into ms --- lib/sdl2 | 2 +- lib/sdl_image | 2 +- lib/sdl_ttf | 2 +- src/crepe/api/LoopManager.cpp | 3 +- src/crepe/api/LoopTimer.cpp | 27 ++--- src/crepe/api/LoopTimer.h | 265 +++++++++++++++++++++--------------------- 6 files changed, 146 insertions(+), 155 deletions(-) (limited to 'lib/sdl2') diff --git a/lib/sdl2 b/lib/sdl2 index 9519b99..c98c4fb 160000 --- a/lib/sdl2 +++ b/lib/sdl2 @@ -1 +1 @@ -Subproject commit 9519b9916cd29a14587af0507292f2bd31dd5752 +Subproject commit c98c4fbff6d8f3016a3ce6685bf8f43433c3efcc diff --git a/lib/sdl_image b/lib/sdl_image index abcf63a..5656019 160000 --- a/lib/sdl_image +++ b/lib/sdl_image @@ -1 +1 @@ -Subproject commit abcf63aa71b4e3ac32120fa9870a6500ddcdcc89 +Subproject commit 56560190a6c5b5740e5afdce747e2a2bfbf16db1 diff --git a/lib/sdl_ttf b/lib/sdl_ttf index 4a318f8..a3d0895 160000 --- a/lib/sdl_ttf +++ b/lib/sdl_ttf @@ -1 +1 @@ -Subproject commit 4a318f8dfaa1bb6f10e0c5e54052e25d3c7f3440 +Subproject commit a3d0895c1b60c41ff9e85d9203ddd7485c014dae diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index abf1f7e..d4345d0 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -6,7 +6,7 @@ #include "LoopManager.h" #include "LoopTimer.h" -namespace crepe { +using namespace crepe; LoopManager::LoopManager(const RenderSystem& renderSystem, const SDLContext& sdlContext, const LoopTimer& loopTimer, const ScriptSystem& scriptSystem, const SoundSystem& soundSystem, const ParticleSystem& particleSystem, @@ -64,4 +64,3 @@ void LoopManager::update() { LoopTimer & timer = LoopTimer::get_instance(); } -} // namespace crepe diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index f4be510..dd4d7e5 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -1,18 +1,14 @@ - #include "../facade/SDLContext.h" #include "../util/log.h" - #include "LoopTimer.h" -#define MS_PER_SECOND 1000.0 - using namespace crepe; LoopTimer::LoopTimer() { - dbg_trace(); + dbg_trace(); } -LoopTimer & LoopTimer::get_instance() { +LoopTimer& LoopTimer::get_instance() { static LoopTimer instance; return instance; } @@ -26,41 +22,36 @@ void LoopTimer::start() { void LoopTimer::update() { uint64_t current_frame_time = SDLContext::get_instance().get_ticks(); - this->delta_time = (current_frame_time - last_frame_time) / MS_PER_SECOND; + this->delta_time = (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->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; } - -int LoopTimer::get_current_time() const { return this->elapsed_time; } - +double LoopTimer::get_current_time() const { return this->elapsed_time; } void LoopTimer::advance_fixed_update() { this->elapsed_fixed_time += this->fixed_delta_time; } - double LoopTimer::get_fixed_delta_time() const { return this->fixed_delta_time; } void LoopTimer::set_fps(int fps) { this->fps = fps; - this->frame_target_time = 1.0 / this->fps; + this->frame_target_time = 1000.0 / 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; } void LoopTimer::enforce_frame_rate() { uint64_t current_frame_time = SDLContext::get_instance().get_ticks(); - double frame_duration = (current_frame_time - this->last_frame_time) / MS_PER_SECOND; + double frame_duration = (current_frame_time - this->last_frame_time); if (frame_duration < this->frame_target_time) { - uint32_t delay_time = static_cast((this->frame_target_time - frame_duration) * MS_PER_SECOND); + uint32_t delay_time = static_cast(this->frame_target_time - frame_duration); SDLContext::get_instance().delay(delay_time); } diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index 4ce0053..5f9b6a7 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -2,142 +2,143 @@ #include -namespace crepe{ +namespace crepe { class LoopTimer { public: - /** - * \brief Get the singleton instance of LoopTimer. - * - * \return A reference to the LoopTimer 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; - - /** - * \brief Get the current game time. - * - * \note The current game time may vary from the real-world elapsed time. - * It is the cumulative sum of each frame's delta time. - * - * \return Elapsed game time in milliseconds. - */ - int get_current_time() const; - - /** - * \brief Set the target frames per second (FPS). - * - * \param fps The number of frames that should be rendered per second. - */ - void set_fps(int fps); - - /** - * \brief Get the current frames per second (FPS). - * - * \return Current FPS. - */ - 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; - - /** - * \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); - - private: - friend class LoopManager; - - /** - * \brief Start the loop timer. - * - * Initializes the timer to begin tracking frame times. - */ - 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(); - - /** - * \brief Get the fixed delta time for consistent updates. - * - * Fixed delta time is used for operations that require uniform time steps, - * such as physics calculations. - * - * \return Fixed delta time in seconds. - */ - 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 - * actual frame time, useful for managing fixed update intervals. - * - * \return Accumulated lag in seconds. - */ - double get_lag() const; - - /** - * \brief Construct a new LoopTimer object. - * - * Private constructor for singleton pattern to restrict instantiation - * outside the class. - */ - 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(); - - /** - * \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(); + /** + * \brief Get the singleton instance of LoopTimer. + * + * \return A reference to the LoopTimer instance. + */ + static LoopTimer& get_instance(); + + /** + * \brief Get the current delta time for the current frame. + * + * \return Delta time in milliseconds since the last frame. + */ + double get_delta_time() const; + + /** + * \brief Get the current game time. + * + * \note The current game time may vary from real-world elapsed time. + * It is the cumulative sum of each frame's delta time. + * + * \return Elapsed game time in milliseconds. + */ + 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); + + /** + * \brief Get the current frames per second (FPS). + * + * \return Current FPS. + */ + 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; + + /** + * \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); + +private: + friend class LoopManager; + + /** + * \brief Start the loop timer. + * + * Initializes the timer to begin tracking frame times. + */ + 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(); + + /** + * \brief Get the fixed delta time for consistent updates. + * + * Fixed delta time is used for operations that require uniform time steps, + * such as physics calculations. + * + * \return Fixed delta time in milliseconds. + */ + 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 + * actual frame time, useful for managing fixed update intervals. + * + * \return Accumulated lag in milliseconds. + */ + double get_lag() const; + + /** + * \brief Construct a new LoopTimer object. + * + * Private constructor for singleton pattern to restrict instantiation + * outside the class. + */ + 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(); + + /** + * \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(); + private: - //! Current frames per second - int fps = 50; - //! Current game scale - double game_scale = 1; - //! Maximum delta time to avoid large jumps - double maximum_delta_time = 0.25; - //! Delta time for the current frame - double delta_time = 0; - //! Target time per frame in seconds - double frame_target_time = 1.0 / fps; - //! Fixed delta time for fixed updates - double fixed_delta_time = 0.02; - //! Total elapsed game time - double elapsed_time = 0; - //! Total elapsed time for fixed updates - double elapsed_fixed_time = 0; - //! Time of the last frame - uint64_t last_frame_time = 0; + //! Current frames per second + int fps = 50; + //! Current game scale + double game_scale = 1; + //! Maximum delta time in milliseconds to avoid large jumps + double maximum_delta_time = 250; + //! Delta time for the current frame in milliseconds + double delta_time = 0; + //! Target time per frame in milliseconds + double frame_target_time = 1000.0 / fps; + //! Fixed delta time for fixed updates in milliseconds + double fixed_delta_time = 20; + //! Total elapsed game time in milliseconds + double elapsed_time = 0; + //! Total elapsed time for fixed updates in milliseconds + double elapsed_fixed_time = 0; + //! Time of the last frame in milliseconds + uint64_t last_frame_time = 0; }; } // namespace crepe -- cgit v1.2.3