diff options
| author | Loek Le Blansch <loek@pipeframe.xyz> | 2025-01-11 21:32:30 +0100 | 
|---|---|---|
| committer | Loek Le Blansch <loek@pipeframe.xyz> | 2025-01-11 21:32:30 +0100 | 
| commit | a6803980f1e74ecf1abb007b7c77f00d2cd92c43 (patch) | |
| tree | 425ca961b27117d6e5d5fa0ae5cfca93351e0b33 /src/crepe/api/Engine.cpp | |
| parent | 6bc0025e4c24ed6659d993f3469c10615fb0e273 (diff) | |
| parent | 525636bb2158ecea68ebb9d6b8d2dc722524c5e5 (diff) | |
merge master into loek/doxygen
Diffstat (limited to 'src/crepe/api/Engine.cpp')
| -rw-r--r-- | src/crepe/api/Engine.cpp | 68 | 
1 files changed, 68 insertions, 0 deletions
diff --git a/src/crepe/api/Engine.cpp b/src/crepe/api/Engine.cpp new file mode 100644 index 0000000..0bbe51f --- /dev/null +++ b/src/crepe/api/Engine.cpp @@ -0,0 +1,68 @@ +#include "../util/Log.h" + +#include "Engine.h" + +using namespace crepe; +using namespace std; + +int Engine::main() noexcept { +	try { +		this->setup(); +	} catch (const exception & e) { +		Log::logf(Log::Level::ERROR, "Uncaught exception in setup: {}\n", e.what()); +		return EXIT_FAILURE; +	} + +	try { +		this->loop(); +	} catch (const exception & e) { +		Log::logf(Log::Level::ERROR, "Uncaught exception in main loop: {}\n", e.what()); +		this->event_manager.trigger_event<ShutDownEvent>(); +	} + +	return EXIT_SUCCESS; +} + +void Engine::setup() { +	this->loop_timer.start(); +	this->scene_manager.load_next_scene(); + +	this->event_manager.subscribe<ShutDownEvent>([this](const ShutDownEvent & event) { +		this->game_running = false; + +		// propagate to possible user ShutDownEvent listeners +		return false; +	}); +} + +void Engine::loop() { +	LoopTimerManager & timer = this->loop_timer; +	SystemManager & systems = this->system_manager; + +	while (this->game_running) { +		timer.update(); + +		while (timer.get_lag() >= timer.get_fixed_delta_time()) { +			try { +				systems.fixed_update(); +			} catch (const exception & e) { +				Log::logf( +					Log::Level::WARNING, "Uncaught exception in fixed update function: {}\n", +					e.what() +				); +			} +			timer.advance_fixed_elapsed_time(); +		} + +		try { +			systems.frame_update(); +			this->scene_manager.load_next_scene(); +		} catch (const exception & e) { +			Log::logf( +				Log::Level::WARNING, "Uncaught exception in frame update function: {}\n", +				e.what() +			); +		} +		timer.enforce_frame_rate(); +	} +}  |