aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/LoopManager.cpp45
-rw-r--r--src/crepe/api/LoopManager.h18
2 files changed, 26 insertions, 37 deletions
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp
index 46a7635..a1da8be 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -7,7 +7,8 @@
#include "../system/PhysicsSystem.h"
#include "../system/RenderSystem.h"
#include "../system/ScriptSystem.h"
-#include "manager/EventManager.h"
+#include "../manager/EventManager.h"
+#include "../util/Log.h"
#include "LoopManager.h"
@@ -25,56 +26,56 @@ LoopManager::LoopManager() {
this->event_manager.subscribe<ShutDownEvent>(
[this](const ShutDownEvent & event) { return this->on_shutdown(event); });
}
-
-void LoopManager::process_input() {
- this->get_system<InputSystem>().update();
- this->event_manager.dispatch_events();
-}
-
void LoopManager::start() {
this->setup();
this->loop();
}
-void LoopManager::fixed_update() {
- this->get_system<ScriptSystem>().update();
- this->get_system<PhysicsSystem>().update();
- this->get_system<CollisionSystem>().update();
+void LoopManager::setup() {
+ this->game_running = true;
+ this->loop_timer.start();
+ this->scene_manager.load_next_scene();
}
void LoopManager::loop() {
+ try {
while (game_running) {
this->loop_timer.update();
while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) {
- this->process_input();
this->fixed_update();
this->loop_timer.advance_fixed_elapsed_time();
}
this->frame_update();
- this->render();
this->loop_timer.enforce_frame_rate();
}
+ }catch(const exception & e){
+ Log::logf(Log::Level::ERROR, "Exception caught in main loop: %s", e.what());
+ this->event_manager.trigger_event<ShutDownEvent>(ShutDownEvent{});
+ }
}
-void LoopManager::setup() {
- this->game_running = true;
- this->loop_timer.start();
- this->scene_manager.load_next_scene();
+// will be called at a fixed interval
+void LoopManager::fixed_update() {
+ this->get_system<InputSystem>().update();
+ this->event_manager.dispatch_events();
+ this->get_system<ScriptSystem>().update();
+ this->get_system<PhysicsSystem>().update();
+ this->get_system<CollisionSystem>().update();
}
-void LoopManager::render() {
- if (!this->game_running) return;
-
+// will be called every frame
+void LoopManager::frame_update() {
+ this->scene_manager.load_next_scene();
this->get_system<AnimatorSystem>().update();
+ //render
this->get_system<RenderSystem>().update();
}
bool LoopManager::on_shutdown(const ShutDownEvent & e) {
this->game_running = false;
+ // propagate to possible user ShutDownEvent listeners
return false;
}
-
-void LoopManager::frame_update() { this->scene_manager.load_next_scene(); }
diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h
index f94cea1..2319d65 100644
--- a/src/crepe/api/LoopManager.h
+++ b/src/crepe/api/LoopManager.h
@@ -25,7 +25,7 @@ public:
* \brief Start the gameloop
*
* This is the start of the engine where the setup is called and then the loop keeps running until the game stops running.
- * Developers need to call this function to run the game.
+ * The Game programmer needs to call this function to run the game. This should be done after creating and adding all scenes.
*/
void start();
@@ -52,13 +52,6 @@ private:
void loop();
/**
- * \brief Function for handling input-related system calls.
- *
- * Processes user inputs from keyboard and mouse.
- */
- void process_input();
-
- /**
* \brief Per-frame update.
*
* Updates the game state based on the elapsed time since the last frame.
@@ -71,15 +64,9 @@ private:
* This function updates physics and game logic based on LoopTimer's fixed_delta_time.
*/
virtual void fixed_update();
- /**
- * \brief Function for executing render-related systems.
- *
- * Renders the current state of the game to the screen.
- */
- virtual void render();
+ //! Indicates whether the game is running.
bool game_running = false;
-
private:
//! Global context
Mediator mediator;
@@ -97,6 +84,7 @@ private:
SDLContext & sdl_context = SDLContext::get_instance();
private:
+
/**
* \brief Callback function for ShutDownEvent
*