aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-04 10:27:42 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-04 10:27:42 +0100
commit7d1f3b90a5d640e4ae23d6ff8f0bf1bf35b6e4cb (patch)
tree0697c269ce96e55c7841344ec426e0e3daa2c864 /src/crepe/api
parent5981ab2262a98b80a1339f86a6b71093576a9b35 (diff)
parentc7c4cc0e3b1a3152256bc8ebf6494c19519538db (diff)
merge 2 gameloop branches
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/LoopManager.cpp32
-rw-r--r--src/crepe/api/LoopManager.h4
-rw-r--r--src/crepe/api/LoopTimer.cpp11
-rw-r--r--src/crepe/api/LoopTimer.h15
-rw-r--r--src/crepe/api/Sprite.cpp4
-rw-r--r--src/crepe/api/Texture.cpp11
-rw-r--r--src/crepe/api/Texture.h13
7 files changed, 39 insertions, 51 deletions
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp
index df09f7e..14e68c2 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -6,20 +6,25 @@
#include "../system/PhysicsSystem.h"
#include "../system/RenderSystem.h"
#include "../system/ScriptSystem.h"
-
+#include "../api/EventManager.h"
#include "LoopManager.h"
#include "LoopTimer.h"
-
+#include <iostream>
using namespace crepe;
using namespace std;
LoopManager::LoopManager() {
+ this->loop_timer = make_unique<LoopTimer>();
this->load_system<AnimatorSystem>();
this->load_system<CollisionSystem>();
this->load_system<ParticleSystem>();
this->load_system<PhysicsSystem>();
this->load_system<RenderSystem>();
this->load_system<ScriptSystem>();
+ EventManager::get_instance().subscribe<ShutDownEvent>([this](const ShutDownEvent& event) {
+ return this->on_shutdown(event);
+ });
+
}
void LoopManager::process_input() {
@@ -35,29 +40,32 @@ void LoopManager::set_running(bool running) { this->game_running = running; }
void LoopManager::fixed_update() {}
void LoopManager::loop() {
- LoopTimer & timer = LoopTimer::get_instance();
- timer.start();
+ this->loop_timer->start();
while (game_running) {
- timer.update();
+ this->loop_timer->update();
- while (timer.get_lag() >= timer.get_fixed_delta_time()) {
+ while (this->loop_timer->get_lag() >= this->loop_timer->get_fixed_delta_time()) {
this->process_input();
this->fixed_update();
- timer.advance_fixed_update();
+ this->loop_timer->advance_fixed_update();
}
this->update();
this->render();
-
- timer.enforce_frame_rate();
+ this->loop_timer->enforce_frame_rate();
}
}
void LoopManager::setup() {
this->game_running = true;
+<<<<<<< HEAD
LoopTimer::get_instance().start();
LoopTimer::get_instance().set_target_fps(200);
+=======
+ this->loop_timer->start();
+ this->loop_timer->set_fps(60);
+>>>>>>> wouter/gameloop-improvements
}
void LoopManager::render() {
@@ -65,5 +73,9 @@ void LoopManager::render() {
this->get_system<RenderSystem>().update();
}
}
+bool LoopManager::on_shutdown(const ShutDownEvent & e){
+ this->game_running = false;
+ return false;
+}
-void LoopManager::update() { LoopTimer & timer = LoopTimer::get_instance(); }
+void LoopManager::update() {}
diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h
index 13e6dac..ff1ff55 100644
--- a/src/crepe/api/LoopManager.h
+++ b/src/crepe/api/LoopManager.h
@@ -7,7 +7,7 @@
#include "api/SceneManager.h"
namespace crepe {
-
+class LoopTimer;
/**
* \brief Main game loop manager
*
@@ -91,6 +91,8 @@ private:
SceneManager scene_manager{component_manager};
private:
+ std::unique_ptr<LoopTimer> loop_timer;
+ bool on_shutdown(const ShutDownEvent & e);
/**
* \brief Collection of System instances
*
diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp
index f94e24b..fe5544d 100644
--- a/src/crepe/api/LoopTimer.cpp
+++ b/src/crepe/api/LoopTimer.cpp
@@ -9,10 +9,6 @@ using namespace crepe;
LoopTimer::LoopTimer() { dbg_trace(); }
-LoopTimer & LoopTimer::get_instance() {
- static LoopTimer instance;
- return instance;
-}
void LoopTimer::start() {
this->last_frame_time = std::chrono::steady_clock::now();
@@ -57,11 +53,8 @@ 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() {
- 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);
+ auto current_frame_time = std::chrono::steady_clock::now();
+ auto frame_duration = current_frame_time - this->last_frame_time;
if (frame_duration < this->frame_target_time) {
std::chrono::milliseconds delay_time
diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h
index 0a48e20..e348628 100644
--- a/src/crepe/api/LoopTimer.h
+++ b/src/crepe/api/LoopTimer.h
@@ -6,13 +6,7 @@ namespace crepe {
class LoopTimer {
public:
- /**
- * \brief Get the singleton instance of LoopTimer.
- *
- * \return A reference to the LoopTimer instance.
- */
- static LoopTimer & get_instance();
-
+ LoopTimer();
/**
* \brief Get the current delta time for the current frame.
*
@@ -96,12 +90,7 @@ private:
*/
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.
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
index 8647794..0a2ad4c 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -22,8 +22,8 @@ Sprite::Sprite(game_object_id_t id, Texture & image, const Color & color,
dbg_trace();
- this->mask.w = sprite_image.get_width();
- this->mask.h = sprite_image.get_height();
+ this->mask.w = sprite_image.get_size().x;
+ this->mask.h = sprite_image.get_size().y;
this->aspect_ratio = static_cast<double>(this->mask.w) / this->mask.h;
}
diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp
index e43bdaa..2b56271 100644
--- a/src/crepe/api/Texture.cpp
+++ b/src/crepe/api/Texture.cpp
@@ -3,6 +3,7 @@
#include "Asset.h"
#include "Texture.h"
+#include "types.h"
using namespace crepe;
using namespace std;
@@ -31,11 +32,7 @@ void Texture::load(const Asset & res) {
this->texture = ctx.texture_from_path(res.get_path());
}
-int Texture::get_width() const {
- if (this->texture == nullptr) return 0;
- return SDLContext::get_instance().get_width(*this);
-}
-int Texture::get_height() const {
- if (this->texture == nullptr) return 0;
- return SDLContext::get_instance().get_height(*this);
+ivec2 Texture::get_size() const {
+ if (this->texture == nullptr) return {};
+ return SDLContext::get_instance().get_size(*this);
}
diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h
index 7206a66..1817910 100644
--- a/src/crepe/api/Texture.h
+++ b/src/crepe/api/Texture.h
@@ -8,6 +8,7 @@
#include <memory>
#include "Asset.h"
+#include "types.h"
namespace crepe {
@@ -42,16 +43,10 @@ public:
Texture & operator=(const Texture &) = delete;
/**
- * \brief Gets the width of the texture.
- * \return Width of the texture in pixels.
+ * \brief Gets the width and height of the texture.
+ * \return Width and height of the texture in pixels.
*/
- int get_width() const;
-
- /**
- * \brief Gets the height of the texture.
- * \return Height of the texture in pixels.
- */
- int get_height() const;
+ ivec2 get_size() const;
private:
/**