aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-11 20:21:31 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-11 20:21:31 +0100
commit149b3b5dbc8b18a142bf35869e54ab05e8f26d54 (patch)
tree11169ff5d0a5ca52ed1e6976b2c43c8e19f8f741
parentec7a4af3e1cc0ebfe2c6df21f15575a1b1abbd6c (diff)
added chrono::duration
-rw-r--r--src/crepe/api/CMakeLists.txt4
-rw-r--r--src/crepe/api/LoopManager.cpp9
-rw-r--r--src/crepe/api/LoopManager.h19
-rw-r--r--src/crepe/api/LoopTimer.cpp90
-rw-r--r--src/crepe/api/LoopTimer.h105
-rw-r--r--src/crepe/facade/SDLContext.h1
6 files changed, 128 insertions, 100 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 87cbb09..85696c4 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -18,6 +18,8 @@ target_sources(crepe PUBLIC
Vector2.cpp
Camera.cpp
Animator.cpp
+ LoopManager.cpp
+ LoopTimer.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -42,4 +44,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
SceneManager.hpp
Camera.h
Animator.h
+ LoopManager.h
+ LoopTimer.h
)
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp
index f477efb..ea63bf6 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -8,14 +8,7 @@
using 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;
+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 5d8c91b..d4668b8 100644
--- a/src/crepe/api/LoopManager.h
+++ b/src/crepe/api/LoopManager.h
@@ -1,6 +1,6 @@
#pragma once
-namespace crepe {
+#include <memory>
class RenderSystem;
class SDLContext;
@@ -11,14 +11,14 @@ class ParticleSystem;
class PhysicsSystem;
class AnimatorSystem;
class CollisionSystem;
+namespace crepe {
+
+
class LoopManager {
public:
void start();
- LoopManager(const RenderSystem &, const SDLContext &, const LoopTimer &,
- const ScriptSystem &, const SoundSystem &,
- const ParticleSystem &, const PhysicsSystem &,
- const AnimatorSystem &, const CollisionSystem &);
+ LoopManager();
private:
/**
@@ -75,6 +75,15 @@ private:
void render();
bool game_running = false;
+ // For later if singletons are removed cant make them now because destructors are private.
+ // std::unique_ptr<RenderSystem> render_system;
+ // std::unique_ptr<SDLContext> sdl_context;
+ // std::unique_ptr<LoopTimer> loop_timer;
+ // std::unique_ptr<ScriptSystem> script_system;
+ // std::unique_ptr<ParticleSystem> particle_system;
+ // std::unique_ptr<PhysicsSystem> physics_system;
+ // std::unique_ptr<AnimatorSystem> animator_system;
+ // std::unique_ptr<CollisionSystem> collision_system;
};
} // namespace crepe
diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp
index 16d813f..039e35e 100644
--- a/src/crepe/api/LoopTimer.cpp
+++ b/src/crepe/api/LoopTimer.cpp
@@ -1,3 +1,4 @@
+#include <chrono>
#include "../facade/SDLContext.h"
#include "../util/log.h"
@@ -6,64 +7,83 @@
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 = SDLContext::get_instance().get_ticks();
- this->elapsed_time = 0;
- this->elapsed_fixed_time = 0;
- this->delta_time = 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() {
- uint64_t current_frame_time = SDLContext::get_instance().get_ticks();
- 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->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<std::chrono::duration<double>>(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_current_time() const {
+ return this->elapsed_time.count();
}
-double LoopTimer::get_delta_time() const { return this->delta_time; }
-double LoopTimer::get_current_time() const { return this->elapsed_time; }
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;
+ return this->fixed_delta_time.count();
}
void LoopTimer::set_fps(int fps) {
- this->fps = fps;
- //! use fps to calculate frame_target_time in ms
- this->frame_target_time = 1000.0 / this->fps;
+ this->fps = fps;
+ // target time per frame in seconds
+ 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; }
-double LoopTimer::get_game_scale() const { return this->game_scale; }
+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);
+ std::chrono::steady_clock::time_point current_frame_time = std::chrono::steady_clock::now();
+ std::chrono::milliseconds frame_duration = std::chrono::duration_cast<std::chrono::milliseconds>(current_frame_time - this->last_frame_time);
- if (frame_duration < this->frame_target_time) {
- uint32_t delay_time
- = static_cast<uint32_t>(this->frame_target_time - frame_duration);
- SDLContext::get_instance().delay(delay_time);
- }
+ if (frame_duration < this->frame_target_time) {
+ std::chrono::milliseconds delay_time = std::chrono::duration_cast<std::chrono::milliseconds>(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;
+ this->last_frame_time = current_frame_time;
}
double LoopTimer::get_lag() const {
- return this->elapsed_time - this->elapsed_fixed_time;
+ return (this->elapsed_time - this->elapsed_fixed_time).count();
}
diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h
index 5309d2f..ec48e4a 100644
--- a/src/crepe/api/LoopTimer.h
+++ b/src/crepe/api/LoopTimer.h
@@ -1,144 +1,145 @@
-#pragma once
+ #pragma once
#include <cstdint>
+#include <chrono>
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 milliseconds since the last 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.
* It is the cumulative sum of each frame's delta time.
*
- * \return Elapsed game time in milliseconds.
+ * \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,
* such as physics calculations.
*
- * \return Fixed delta time in milliseconds.
+ * \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
* actual frame time, useful for managing fixed update intervals.
*
- * \return Accumulated lag in milliseconds.
+ * \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 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;
+ //! 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<double> maximum_delta_time{0.25};
+ //! Delta time for the current frame in seconds
+ std::chrono::duration<double> delta_time{0.0};
+ //! Target time per frame in seconds
+ std::chrono::duration<double> frame_target_time = std::chrono::seconds(1) / fps;
+ //! Fixed delta time for fixed updates in seconds
+ std::chrono::duration<double> fixed_delta_time = std::chrono::seconds(1) / 50;
+ //! Total elapsed game time in seconds
+ std::chrono::duration<double> elapsed_time{0.0};
+ //! Total elapsed time for fixed updates in seconds
+ std::chrono::duration<double> elapsed_fixed_time{0.0};
+ //! Time of the last frame
+ std::chrono::steady_clock::time_point last_frame_time;
};
} // namespace crepe
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 90147e0..b51fdd5 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -58,6 +58,7 @@ private:
private:
//! Will only use get_ticks
friend class AnimatorSystem;
+ //! Will use the funtions: get_ticks, delay
friend class LoopTimer;
/**
* \brief Gets the current SDL ticks since the program started.