diff options
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/Collider.h | 3 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.h | 2 | ||||
-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 | 14 | ||||
-rw-r--r-- | src/crepe/api/Vector2.h | 39 | ||||
-rw-r--r-- | src/crepe/api/Vector2.hpp | 9 | ||||
-rw-r--r-- | src/crepe/facade/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/facade/SignalCatch.cpp | 20 | ||||
-rw-r--r-- | src/crepe/facade/SignalCatch.h | 23 | ||||
-rw-r--r-- | src/crepe/manager/SystemManager.cpp | 30 | ||||
-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 | 25 |
15 files changed, 167 insertions, 38 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.h b/src/crepe/api/BehaviorScript.h index 3909b96..52cf259 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -48,6 +48,8 @@ public: BehaviorScript & set_script(Args &&... args); protected: + //! Script type name + 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..bfc1c4a 100644 --- a/src/crepe/api/Engine.cpp +++ b/src/crepe/api/Engine.cpp @@ -1,3 +1,6 @@ +#include <segvcatch.h> + +#include "../facade/SignalCatch.h" #include "../util/Log.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) { @@ -45,23 +50,24 @@ void Engine::loop() { while (timer.get_lag() >= timer.get_fixed_delta_time()) { try { systems.fixed_update(); + timer.advance_fixed_elapsed_time(); } 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(); + timer.enforce_frame_rate(); } 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(); } } + diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 52e1bb6..0f46964 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -11,55 +11,55 @@ struct Vector2 { T y = 0; //! Subtracts another vector from this vector and returns the result. - Vector2 operator-(const Vector2<T> & other) const; + Vector2<T> operator-(const Vector2<T> & other) const; //! Subtracts a scalar value from both components of this vector and returns the result. - Vector2 operator-(T scalar) const; + Vector2<T> operator-(T scalar) const; //! Adds another vector to this vector and returns the result. - Vector2 operator+(const Vector2<T> & other) const; + Vector2<T> operator+(const Vector2<T> & other) const; //! Adds a scalar value to both components of this vector and returns the result. - Vector2 operator+(T scalar) const; + Vector2<T> operator+(T scalar) const; //! Multiplies this vector by another vector element-wise and returns the result. - Vector2 operator*(const Vector2<T> & other) const; + Vector2<T> operator*(const Vector2<T> & other) const; //! Multiplies this vector by a scalar and returns the result. - Vector2 operator*(T scalar) const; + Vector2<T> operator*(T scalar) const; //! Divides this vector by another vector element-wise and returns the result. - Vector2 operator/(const Vector2<T> & other) const; + Vector2<T> operator/(const Vector2<T> & other) const; //! Divides this vector by a scalar and returns the result. - Vector2 operator/(T scalar) const; + Vector2<T> operator/(T scalar) const; //! Adds another vector to this vector and updates this vector. - Vector2 & operator+=(const Vector2<T> & other); + Vector2<T> & operator+=(const Vector2<T> & other); //! Adds a scalar value to both components of this vector and updates this vector. - Vector2 & operator+=(T other); + Vector2<T> & operator+=(T other); //! Subtracts another vector from this vector and updates this vector. - Vector2 & operator-=(const Vector2<T> & other); + Vector2<T> & operator-=(const Vector2<T> & other); //! Subtracts a scalar value from both components of this vector and updates this vector. - Vector2 & operator-=(T other); + Vector2<T> & operator-=(T other); //! Multiplies this vector by another vector element-wise and updates this vector. - Vector2 & operator*=(const Vector2<T> & other); + Vector2<T> & operator*=(const Vector2<T> & other); //! Multiplies this vector by a scalar and updates this vector. - Vector2 & operator*=(T other); + Vector2<T> & operator*=(T other); //! Divides this vector by another vector element-wise and updates this vector. - Vector2 & operator/=(const Vector2<T> & other); + Vector2<T> & operator/=(const Vector2<T> & other); //! Divides this vector by a scalar and updates this vector. - Vector2 & operator/=(T other); + Vector2<T> & operator/=(T other); //! Returns the negation of this vector. - Vector2 operator-() const; + Vector2<T> operator-() const; //! Checks if this vector is equal to another vector. bool operator==(const Vector2<T> & other) const; @@ -89,10 +89,13 @@ struct Vector2 { T distance_squared(const Vector2<T> & other) const; //! Returns the perpendicular vector to this vector. - Vector2 perpendicular() const; + Vector2<T> perpendicular() const; //! Checks if both components of the vector are NaN. bool is_nan() const; + + //! Rotate this vector clockwise by \c deg degrees + Vector2<T> rotate(float deg) const; }; } // namespace crepe diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp index e195760..5709f46 100644 --- a/src/crepe/api/Vector2.hpp +++ b/src/crepe/api/Vector2.hpp @@ -168,4 +168,13 @@ bool Vector2<T>::is_nan() const { return std::isnan(x) && std::isnan(y); } +template <class T> +Vector2<T> Vector2<T>::rotate(float deg) const { + float rad = -deg / 180 * M_PI; + return { + x * std::cos(rad) - y * std::sin(rad), + x * std::sin(rad) + y * std::cos(rad), + }; +} + } // namespace crepe 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..4988047 --- /dev/null +++ b/src/crepe/facade/SignalCatch.cpp @@ -0,0 +1,20 @@ +#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..cf86b56 --- /dev/null +++ b/src/crepe/facade/SignalCatch.h @@ -0,0 +1,23 @@ +#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; +}; + +} // namespace crepe diff --git a/src/crepe/manager/SystemManager.cpp b/src/crepe/manager/SystemManager.cpp index eabc022..fea59aa 100644 --- a/src/crepe/manager/SystemManager.cpp +++ b/src/crepe/manager/SystemManager.cpp @@ -31,17 +31,31 @@ 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..a19a253 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..f1e31f9 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -32,10 +32,29 @@ 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, + "Disabled script \"{}\" due to exception in init function: {}", + behavior_script.name, e.what() + ); + behavior_script.active = false; + } } - (*script.*update_function)(delta_time); + try { + (*script.*update_function)(delta_time); + } catch (const exception & e) { + // TODO: discern between fixed/frame update + Log::logf( + Log::Level::WARNING, + "Disabled script \"{}\" due to exception in update function: {}", + behavior_script.name, e.what() + ); + behavior_script.active = false; + } } } |