diff options
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/Collider.h | 3 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.cpp | 18 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.h | 6 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.hpp | 1 | ||||
-rw-r--r-- | src/crepe/api/Components.h | 16 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 3 | ||||
-rw-r--r-- | src/crepe/api/Engine.cpp | 27 | ||||
-rw-r--r-- | src/crepe/api/Engine.h | 5 | ||||
-rw-r--r-- | src/crepe/api/GameObject.h | 7 | ||||
-rw-r--r-- | src/crepe/facade/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/facade/SignalCatch.cpp | 25 | ||||
-rw-r--r-- | src/crepe/facade/SignalCatch.h | 24 | ||||
-rw-r--r-- | src/crepe/manager/SystemManager.cpp | 24 | ||||
-rw-r--r-- | src/crepe/manager/SystemManager.h | 13 | ||||
-rw-r--r-- | src/crepe/manager/SystemManager.hpp | 5 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.cpp | 15 |
16 files changed, 171 insertions, 23 deletions
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()); + } } } |