diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-22 16:12:31 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-22 16:12:31 +0100 |
commit | 7e12ebdf945d40d6f11872cf5852c9bb54d1864f (patch) | |
tree | d81f4661e2fd47f487cf0c9627baa1e21dc45b37 | |
parent | 61148c757a1f742ff09e40e5347e74e638c7371c (diff) |
big WIP
35 files changed, 421 insertions, 146 deletions
diff --git a/.gitmodules b/.gitmodules index 8155600..6e2ae88 100644 --- a/.gitmodules +++ b/.gitmodules @@ -30,3 +30,6 @@ path = lib/fontconfig url = https://gitlab.freedesktop.org/fontconfig/fontconfig.git shallow = true +[submodule "lib/segvcatch/lib"] + path = lib/segvcatch/lib + url = https://github.com/Plaristote/segvcatch diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 937b5e6..a149487 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -8,20 +8,19 @@ set(CMAKE_BUILD_TYPE Debug) project(game C CXX) add_subdirectory(../src crepe) -add_executable(main - background/AquariumSubScene.cpp - background/BackgroundSubScene.cpp - background/ForestParallaxScript.cpp - background/ForestSubScene.cpp + +add_executable(main) + +target_sources(main PUBLIC GameScene.cpp - background/HallwaySubScene.cpp MoveCameraManualyScript.cpp - PlayerScript.cpp - PlayerSubScene.cpp StartGameScript.cpp - background/StartSubScene.cpp main.cpp ) +add_subdirectory(background) +add_subdirectory(prefab) + target_link_libraries(main PUBLIC crepe) +target_include_directories(main PRIVATE .) diff --git a/game/Config.h b/game/Config.h index ec753df..350cd02 100644 --- a/game/Config.h +++ b/game/Config.h @@ -1,5 +1,16 @@ #pragma once +#include <crepe/api/Config.h> + +static const crepe::Config ENGINE_CONFIG { + .log { + .level = crepe::Log::Level::DEBUG, + }, + .window_settings { + .window_title = "Jetpack joyride clone", + }, +}; + static constexpr int SORT_IN_LAY_BACK_BACKGROUND = 3; // For all scenes static constexpr int SORT_IN_LAY_BACKGROUND = 4; // For all scenes static constexpr int SORT_IN_LAY_FORE_BACKGROUND = 5; // For all scenes diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 2511567..57c6531 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -1,11 +1,3 @@ -#include "GameScene.h" -#include "Config.h" -#include "MoveCameraManualyScript.h" -#include "PlayerSubScene.h" -#include "StartGameScript.h" - -#include "background/BackgroundSubScene.h" - #include <cmath> #include <crepe/api/Animator.h> #include <crepe/api/Asset.h> @@ -22,10 +14,20 @@ #include <crepe/api/Transform.h> #include <crepe/types.h> +#include "GameScene.h" +#include "Config.h" +#include "MoveCameraManualyScript.h" +#include "StartGameScript.h" + +#include "background/BackgroundSubScene.h" +#include "prefab/PlayerObject.h" + using namespace crepe; using namespace std; void GameScene::load_scene() { + logf(Log::DEBUG, "Loading (main) GameScene..."); + BackgroundSubScene background(*this); GameObject camera = new_object("camera", "camera", vec2(650, 0)); @@ -38,7 +40,7 @@ void GameScene::load_scene() { camera.add_component<BehaviorScript>().set_script<MoveCameraManualyScript>(); camera.add_component<Rigidbody>(Rigidbody::Data {}); - PlayerSubScene player(*this); + PlayerObject player {new_object("player", "player", vec2(-100, 200))}; GameObject floor = new_object("floor", "game_world", vec2(0, 325)); floor.add_component<Rigidbody>(Rigidbody::Data { diff --git a/game/PlayerScript.cpp b/game/PlayerScript.cpp deleted file mode 100644 index 1c388f5..0000000 --- a/game/PlayerScript.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "PlayerScript.h" - -#include <crepe/api/Rigidbody.h> - -using namespace crepe; -using namespace std; - -void PlayerScript::fixed_update(crepe::duration_t dt) { - Rigidbody & rb = this->get_components_by_name<Rigidbody>("player").front(); - if (this->get_key_state(Keycode::SPACE)) rb.add_force_linear(vec2(0, -10)); -} diff --git a/game/PlayerScript.h b/game/PlayerScript.h deleted file mode 100644 index 84c4f7f..0000000 --- a/game/PlayerScript.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include <crepe/api/Script.h> - -class PlayerScript : public crepe::Script { -public: - void fixed_update(crepe::duration_t dt); -}; diff --git a/game/PlayerSubScene.cpp b/game/PlayerSubScene.cpp deleted file mode 100644 index 00b7810..0000000 --- a/game/PlayerSubScene.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include "PlayerSubScene.h" -#include "Config.h" -#include "PlayerScript.h" - -#include <crepe/api/Animator.h> -#include <crepe/api/GameObject.h> -#include <crepe/api/Scene.h> -#include <crepe/api/Script.h> -#include <crepe/api/Sprite.h> - -using namespace crepe; -using namespace std; - -PlayerSubScene::PlayerSubScene(Scene & scn) { - GameObject player = scn.new_object("player", "player", vec2(-100, 200)); - Asset player_body_asset {"asset/barry/defaultBody.png"}; - Sprite & player_body_sprite = player.add_component<Sprite>( - player_body_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_PLAYER, - .order_in_layer = 0, - .size = vec2(0, 50), - } - ); - player.add_component<Animator>( - player_body_sprite, ivec2(32, 32), uvec2(4, 8), - Animator::Data { - .fps = 5, - .looping = true, - } - ); - Asset player_head_asset {"asset/barry/defaultHead.png"}; - Sprite & player_head_sprite = player.add_component<Sprite>( - player_head_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_PLAYER, - .order_in_layer = 1, - .size = vec2(0, 50), - .position_offset = vec2(0, -20), - } - ); - player.add_component<Animator>( - player_head_sprite, ivec2(32, 32), uvec2(4, 8), - Animator::Data { - .fps = 5, - .looping = true, - } - ); - Asset player_jetpack_asset {"asset/barry/jetpackDefault.png"}; - Sprite & player_jetpack_sprite = player.add_component<Sprite>( - player_jetpack_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_PLAYER, - .order_in_layer = 2, - .size = vec2(0, 60), - .position_offset = vec2(-20, 0), - } - ); - player_jetpack_sprite.active = false; - player.add_component<Animator>( - player_jetpack_sprite, ivec2(32, 44), uvec2(4, 4), - Animator::Data { - .fps = 5, - .looping = true, - } - ); - player.add_component<Rigidbody>(Rigidbody::Data { - .gravity_scale = 20, - .body_type = Rigidbody::BodyType::DYNAMIC, - .linear_velocity = vec2(100, 0), - .collision_layers = {COLL_LAY_BOT_TOP}, - .collision_layer = COLL_LAY_PLAYER, - }); - player.add_component<BoxCollider>(vec2(50, 50)); - player.add_component<BehaviorScript>().set_script<PlayerScript>().active = false; -} diff --git a/game/PlayerSubScene.h b/game/PlayerSubScene.h deleted file mode 100644 index bf94c32..0000000 --- a/game/PlayerSubScene.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -namespace crepe { -class Scene; -} - -class PlayerSubScene { -public: - PlayerSubScene(crepe::Scene & scn); -}; diff --git a/game/background/CMakeLists.txt b/game/background/CMakeLists.txt new file mode 100644 index 0000000..1d705f5 --- /dev/null +++ b/game/background/CMakeLists.txt @@ -0,0 +1,9 @@ +target_sources(main PUBLIC + AquariumSubScene.cpp + BackgroundSubScene.cpp + ForestParallaxScript.cpp + ForestSubScene.cpp + HallwaySubScene.cpp + StartSubScene.cpp +) + diff --git a/game/main.cpp b/game/main.cpp index 325b66d..2198f73 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -1,11 +1,14 @@ #include <crepe/api/Engine.h> #include <crepe/api/Script.h> +#include "Config.h" #include "GameScene.h" using namespace crepe; int main() { + Config::get_instance() = ENGINE_CONFIG; + Engine gameloop; gameloop.add_scene<GameScene>(); diff --git a/game/prefab/CMakeLists.txt b/game/prefab/CMakeLists.txt new file mode 100644 index 0000000..a588090 --- /dev/null +++ b/game/prefab/CMakeLists.txt @@ -0,0 +1,5 @@ +target_sources(main PUBLIC + PlayerObject.cpp + PlayerScript.cpp +) + diff --git a/game/prefab/PlayerObject.cpp b/game/prefab/PlayerObject.cpp new file mode 100644 index 0000000..736704a --- /dev/null +++ b/game/prefab/PlayerObject.cpp @@ -0,0 +1,76 @@ +#include <crepe/util/Log.h> + +#include "Config.h" +#include "PlayerObject.h" +#include "PlayerScript.h" + +using namespace crepe; + +PlayerObject::PlayerObject(crepe::GameObject && base) + : GameObject(std::move(base)), + sprite { + .body = add_component<Sprite>( + Asset {"asset/barry/defaultBody.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_PLAYER, + .order_in_layer = 0, + .size = vec2(0, 50), + } + ), + .head = add_component<Sprite>( + Asset {"asset/barry/defaultHead.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_PLAYER, + .order_in_layer = 1, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ), + .jetpack = add_component<Sprite>( + Asset {"asset/barry/jetpackDefault.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_PLAYER, + .order_in_layer = 2, + .size = vec2(0, 60), + .position_offset = vec2(-20, 0), + } + ) + }, + animator { + .body = add_component<Animator>( + sprite.body, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = 5, + .looping = true, + } + ), + .head = add_component<Animator>( + sprite.head, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = 5, + .looping = true, + } + ), + .jetpack = add_component<Animator>( + sprite.jetpack, ivec2(32, 44), uvec2(4, 4), + Animator::Data { + .fps = 5, + .looping = true, + } + ), + }, + body(add_component<Rigidbody>(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(100, 0), + .collision_layers = {COLL_LAY_BOT_TOP}, + .collision_layer = COLL_LAY_PLAYER, + })), + collider(add_component<BoxCollider>(vec2(50, 50))), + controller(add_component<BehaviorScript>().set_script<PlayerScript>(this)) { + sprite.jetpack.active = false; + // controller.active = false; + + Log::logf(Log::DEBUG, "PlayerObject: ref {}", (void*) &(this->body.game_object_id)); +} + diff --git a/game/prefab/PlayerObject.h b/game/prefab/PlayerObject.h new file mode 100644 index 0000000..a44d367 --- /dev/null +++ b/game/prefab/PlayerObject.h @@ -0,0 +1,30 @@ +#pragma once + +#include <crepe/api/Animator.h> +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/BoxCollider.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Sprite.h> + +class PlayerObject : public crepe::GameObject { +public: + PlayerObject(crepe::GameObject &&); + +public: + struct { + crepe::Sprite & body; + crepe::Sprite & head; + crepe::Sprite & jetpack; + } sprite; + + struct { + crepe::Animator & body; + crepe::Animator & head; + crepe::Animator & jetpack; + } animator; + + crepe::Rigidbody & body; + crepe::BoxCollider & collider; + crepe::BehaviorScript & controller; +}; diff --git a/game/prefab/PlayerScript.cpp b/game/prefab/PlayerScript.cpp new file mode 100644 index 0000000..e4a4951 --- /dev/null +++ b/game/prefab/PlayerScript.cpp @@ -0,0 +1,26 @@ +#include <crepe/api/Rigidbody.h> +#include <cassert> + +#include "PlayerScript.h" + +using namespace crepe; +using namespace std; + +PlayerScript::PlayerScript(PlayerObject * player) : player(player) { + logf(Log::DEBUG, "PlayerScript: [C] player {}", (void*) &(*player)); + logf(Log::DEBUG, "PlayerScript: [C] player.body {}", (void*) &(player->body)); + logf(Log::DEBUG, "PlayerScript: [C] player.body.id {}", (void*) &(player->body.game_object_id)); +} + +void PlayerScript::init() { + logf(Log::DEBUG, "PlayerScript: [C] player {}", (void*) &(*player)); + logf(Log::DEBUG, "PlayerScript: [C] player.body {}", (void*) &(player->body)); + logf(Log::DEBUG, "PlayerScript: [C] player.body.id {}", (void*) &(player->body.game_object_id)); + player->controller.active = false; +} + +void PlayerScript::fixed_update(crepe::duration_t dt) { + if (this->get_key_state(Keycode::SPACE)) + player->body.add_force_linear({ 0, -10 }); +} + diff --git a/game/prefab/PlayerScript.h b/game/prefab/PlayerScript.h new file mode 100644 index 0000000..bd4a00a --- /dev/null +++ b/game/prefab/PlayerScript.h @@ -0,0 +1,23 @@ +#pragma once + +#include <crepe/api/Script.h> + +#include "PlayerObject.h" + +class PlayerScript : public crepe::Script { +public: + PlayerScript(PlayerObject * player); + + PlayerScript(const PlayerScript &) = delete; + PlayerScript(PlayerScript &&) = delete; + PlayerScript & operator=(const PlayerScript &) = delete; + PlayerScript & operator=(PlayerScript &&) = delete; + +protected: + void fixed_update(crepe::duration_t dt); + void init(); + +protected: + PlayerObject * player = nullptr; +}; + diff --git a/game/prefab/ZapperObject.h b/game/prefab/ZapperObject.h new file mode 100644 index 0000000..1f32cd7 --- /dev/null +++ b/game/prefab/ZapperObject.h @@ -0,0 +1,7 @@ +#pragma once + +#include <crepe/api/GameObject.h> + +class ZapperObject : public crepe::GameObject { + // afsd +}; diff --git a/lib/segvcatch/CMakeLists.txt b/lib/segvcatch/CMakeLists.txt new file mode 100644 index 0000000..4449e77 --- /dev/null +++ b/lib/segvcatch/CMakeLists.txt @@ -0,0 +1,35 @@ +cmake_minimum_required(VERSION 3.28) +set(CMAKE_CXX_STANDARD 20) +project(segvcatch CXX) + +include(CMakePackageConfigHelpers) + +add_library(segvcatch SHARED lib/lib/segvcatch.cpp) + +install( + TARGETS segvcatch + EXPORT segvcatchTargets + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + RUNTIME DESTINATION lib + INCLUDES DESTINATION include +) +install( + FILES lib/lib/segvcatch.h + DESTINATION include +) +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/segvcatch-config-version.cmake" + VERSION 0.0.0 + COMPATIBILITY AnyNewerVersion +) +install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/segvcatch-config-version.cmake" + DESTINATION lib/cmake/segvcatch +) +install( + EXPORT segvcatchTargets + FILE segvcatch-config.cmake + DESTINATION lib/cmake/segvcatch +) diff --git a/lib/segvcatch/lib b/lib/segvcatch/lib new file mode 160000 +Subproject afe79c49f7d996e2b3143199e6cef69f406247b diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 696856c..b7d63d7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,8 @@ find_package(GTest REQUIRED) find_package(whereami REQUIRED) find_library(BERKELEY_DB db) find_library(FONTCONFIG_LIB fontconfig) +find_package(segvcatch REQUIRED) +find_package(segvcatch REQUIRED) add_library(crepe SHARED) add_executable(test_main EXCLUDE_FROM_ALL) @@ -31,6 +33,7 @@ target_link_libraries(crepe PUBLIC ${BERKELEY_DB} PUBLIC whereami PUBLIC ${FONTCONFIG_LIB} + PUBLIC segvcatch ) add_subdirectory(crepe) diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h index 42ccfd4..4344f15 100644 --- a/src/crepe/Collider.h +++ b/src/crepe/Collider.h @@ -5,6 +5,9 @@ namespace crepe { +/** + * \brief Base collider class + */ class Collider : public Component { public: Collider(game_object_id_t id, const vec2 & offset); diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp index af7572c..e1c06b0 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -13,3 +13,21 @@ BehaviorScript & GameObject::add_component<BehaviorScript>() { ComponentManager & mgr = this->mediator.component_manager; return mgr.add_component<BehaviorScript>(this->id, this->mediator); } + +BehaviorScript::BehaviorScript(const BehaviorScript & other) : mediator(other.mediator), Component(other.game_object_id) { + Log::logf("COPY CONSTRUCTOR!!!"); +} + +BehaviorScript::BehaviorScript(BehaviorScript && other) : mediator(other.mediator), Component(other.game_object_id) { + Log::logf("MOVE CONSTRUCTOR!!!"); +} + +BehaviorScript & BehaviorScript::operator = (const BehaviorScript & other) { + Log::logf("COPY OPERATOR!!!"); + return *this; +} + +BehaviorScript & BehaviorScript::operator = (BehaviorScript && other) { + Log::logf("MOVE OPERATOR!!!"); + return *this; +} diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 3909b96..52a7cbf 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -33,6 +33,11 @@ protected: //! Only ComponentManager is allowed to instantiate BehaviorScript friend class ComponentManager; + BehaviorScript(const BehaviorScript &); + BehaviorScript(BehaviorScript &&); + BehaviorScript & operator = (const BehaviorScript &); + BehaviorScript & operator = (BehaviorScript &&); + public: /** * \brief Set the concrete script of this component @@ -48,6 +53,7 @@ public: BehaviorScript & set_script(Args &&... args); protected: + std::string name = "unknown script"; //! Script instance std::unique_ptr<Script> script = nullptr; //! ScriptSystem needs direct access to the script instance diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index 353d5e2..218f27c 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -11,6 +11,7 @@ template <class T, typename... Args> BehaviorScript & BehaviorScript::set_script(Args &&... args) { static_assert(std::is_base_of<Script, T>::value); this->script = std::unique_ptr<Script>(new T(std::forward<Args>(args)...)); + this->name = typeid(T).name(); this->script->game_object_id = this->game_object_id; this->script->active = this->active; diff --git a/src/crepe/api/Components.h b/src/crepe/api/Components.h new file mode 100644 index 0000000..fa0663d --- /dev/null +++ b/src/crepe/api/Components.h @@ -0,0 +1,16 @@ +#pragma once + +#include "AI.h" +#include "Animator.h" +#include "AudioSource.h" +#include "BehaviorScript.h" +#include "BoxCollider.h" +#include "Button.h" +#include "Camera.h" +#include "CircleCollider.h" +#include "Metadata.h" +#include "ParticleEmitter.h" +#include "Rigidbody.h" +#include "Sprite.h" +#include "Text.h" +#include "Transform.h" diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 6b9e3ca..32f1a2e 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -60,7 +60,8 @@ struct Config final { struct { //! default screen size in pixels ivec2 default_size = {1280, 720}; - std::string window_title = "Jetpack joyride clone"; + //! default window title + std::string window_title = "crepe window"; } window_settings; //! Asset loading options diff --git a/src/crepe/api/Engine.cpp b/src/crepe/api/Engine.cpp index cd9786b..bf3f50c 100644 --- a/src/crepe/api/Engine.cpp +++ b/src/crepe/api/Engine.cpp @@ -1,4 +1,7 @@ +#include <segvcatch.h> + #include "../util/Log.h" +#include "../facade/SignalCatch.h" #include "Engine.h" @@ -6,6 +9,8 @@ using namespace crepe; using namespace std; int Engine::main() noexcept { + SignalCatch signal_catch; + try { this->setup(); } catch (const exception & e) { @@ -37,31 +42,39 @@ void Engine::setup() { 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(); + this->fixed_update(); } catch (const exception & e) { Log::logf( - Log::Level::WARNING, "Uncaught exception in fixed update function: {}\n", + Log::Level::WARNING, "Uncaught exception in fixed update function: {}", e.what() ); } - timer.advance_fixed_elapsed_time(); } try { - systems.frame_update(); + this->frame_update(); } catch (const exception & e) { Log::logf( - Log::Level::WARNING, "Uncaught exception in frame update function: {}\n", + Log::Level::WARNING, "Uncaught exception in frame update function: {}", e.what() ); } - timer.enforce_frame_rate(); } } + +void Engine::fixed_update() { + this->system_manager.fixed_update(); + this->loop_timer.advance_fixed_elapsed_time(); +} + +void Engine::frame_update() { + this->system_manager.frame_update(); + this->loop_timer.enforce_frame_rate(); +} + diff --git a/src/crepe/api/Engine.h b/src/crepe/api/Engine.h index 452a856..23acfb4 100644 --- a/src/crepe/api/Engine.h +++ b/src/crepe/api/Engine.h @@ -46,6 +46,11 @@ private: */ void loop(); + //! Fixed update function + void fixed_update(); + //! Frame update function + void frame_update(); + //! Game loop condition bool game_running = true; diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 043913a..c66da3d 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -37,6 +37,13 @@ private: //! ComponentManager instances GameObject friend class ComponentManager; +protected: + GameObject(GameObject &&) = default; + + GameObject(const GameObject &) = delete; + GameObject & operator=(const GameObject &) = delete; + GameObject & operator=(GameObject &&) = delete; + public: //! The id of the GameObject const game_object_id_t id; diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index 243ae46..4873e8d 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -6,6 +6,7 @@ target_sources(crepe PUBLIC DB.cpp FontFacade.cpp Font.cpp + SignalCatch.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -16,5 +17,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES DB.h FontFacade.h Font.h + SignalCatch.h ) diff --git a/src/crepe/facade/SignalCatch.cpp b/src/crepe/facade/SignalCatch.cpp new file mode 100644 index 0000000..ad92d28 --- /dev/null +++ b/src/crepe/facade/SignalCatch.cpp @@ -0,0 +1,25 @@ +#include <stdexcept> + +#include "SignalCatch.h" + +using namespace crepe; +using namespace std; + +SignalCatch::SignalCatch() { + segvcatch::init_segv(&SignalCatch::segv); + segvcatch::init_fpe(&SignalCatch::fpe); +} + +SignalCatch::~SignalCatch() { + segvcatch::init_segv(); + segvcatch::init_fpe(); +} + +void SignalCatch::segv() { + throw runtime_error("segmentation fault"); +} + +void SignalCatch::fpe() { + throw domain_error("floating point exception"); +} + diff --git a/src/crepe/facade/SignalCatch.h b/src/crepe/facade/SignalCatch.h new file mode 100644 index 0000000..4562215 --- /dev/null +++ b/src/crepe/facade/SignalCatch.h @@ -0,0 +1,24 @@ +#pragma once + +#include <segvcatch.h> + +namespace crepe { + +class SignalCatch { +public: + SignalCatch(); + ~SignalCatch(); + +private: + static void segv(); + static void fpe(); + +public: + SignalCatch(const SignalCatch &) = delete; + SignalCatch(SignalCatch &&) = delete; + SignalCatch & operator=(const SignalCatch &) = delete; + SignalCatch & operator=(SignalCatch &&) = delete; +}; + +} + diff --git a/src/crepe/manager/SystemManager.cpp b/src/crepe/manager/SystemManager.cpp index eabc022..c8f4f3d 100644 --- a/src/crepe/manager/SystemManager.cpp +++ b/src/crepe/manager/SystemManager.cpp @@ -31,17 +31,25 @@ SystemManager::SystemManager(Mediator & mediator) : Manager(mediator) { this->mediator.system_manager = *this; } -void SystemManager::fixed_update() { - for (System & system : this->system_order) { - if (!system.active) continue; - system.fixed_update(); +void SystemManager::fixed_update() noexcept { + for (SystemEntry & entry : this->system_order) { + if (!entry.system.active) continue; + try { + entry.system.fixed_update(); + } catch (const exception & e) { + Log::logf(Log::Level::WARNING, "Uncaught exception in {} fixed update: {}", entry.name, e.what()); + } } } -void SystemManager::frame_update() { - for (System & system : this->system_order) { - if (!system.active) continue; - system.frame_update(); +void SystemManager::frame_update() noexcept { + for (SystemEntry & entry : this->system_order) { + if (!entry.system.active) continue; + try { + entry.system.frame_update(); + } catch (const exception & e) { + Log::logf(Log::Level::WARNING, "Uncaught exception in {} frame update: {}", entry.name, e.what()); + } } } diff --git a/src/crepe/manager/SystemManager.h b/src/crepe/manager/SystemManager.h index 614d90c..7b862a3 100644 --- a/src/crepe/manager/SystemManager.h +++ b/src/crepe/manager/SystemManager.h @@ -26,14 +26,14 @@ public: * * Updates the game state based on the elapsed time since the last frame. */ - void frame_update(); + void frame_update() noexcept; /** * \brief Fixed update executed at a fixed rate. * * This function updates physics and game logic based on LoopTimer's fixed_delta_time. */ - void fixed_update(); + void fixed_update() noexcept; private: /** @@ -43,13 +43,20 @@ private: * constructor of \c SystemManager using SystemManager::load_system. */ std::unordered_map<std::type_index, std::unique_ptr<System>> systems; + //! Internal ordered system list entry + struct SystemEntry { + //! System instance reference + System & system; + //! System name + std::string name; + }; /** * \brief Collection of System instances * * This map holds System instances indexed by the system's class typeid. It is filled in the * constructor of \c SystemManager using SystemManager::load_system. */ - std::vector<std::reference_wrapper<System>> system_order; + std::vector<SystemEntry> system_order; /** * \brief Initialize a system * \tparam T System type (must be derivative of \c System) diff --git a/src/crepe/manager/SystemManager.hpp b/src/crepe/manager/SystemManager.hpp index addd274..a4c11e3 100644 --- a/src/crepe/manager/SystemManager.hpp +++ b/src/crepe/manager/SystemManager.hpp @@ -38,7 +38,10 @@ void SystemManager::load_system() { throw runtime_error(format("SystemManager: {} is already initialized", type.name())); System * system = new T(this->mediator); this->systems[type] = unique_ptr<System>(system); - this->system_order.push_back(*this->systems[type]); + this->system_order.push_back(SystemEntry{ + .system = *this->systems[type], + .name = type.name(), + }); } } // namespace crepe diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index ed0c7cc..4cce42b 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -32,10 +32,19 @@ void ScriptSystem::update( if (script == nullptr) continue; if (!script->initialized) { - script->init(); - script->initialized = true; + try { + script->init(); + script->initialized = true; + } catch (const exception & e) { + Log::logf(Log::Level::WARNING, "Uncaught exception in {} init: {}", behavior_script.name, e.what()); + } } - (*script.*update_function)(delta_time); + try { + (*script.*update_function)(delta_time); + } catch (const exception & e) { + // TODO: print if it is fixed/frame update + Log::logf(Log::Level::WARNING, "Uncaught exception in {}: {}", behavior_script.name, e.what()); + } } } |