aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitmodules3
-rw-r--r--game/CMakeLists.txt14
-rw-r--r--game/Config.h11
-rw-r--r--game/GameScene.cpp23
-rw-r--r--game/background/CMakeLists.txt9
-rw-r--r--game/main.cpp3
-rw-r--r--game/prefab/CMakeLists.txt4
-rw-r--r--game/prefab/ZapperObject.cpp99
-rw-r--r--game/prefab/ZapperObject.h37
-rwxr-xr-xgame/util/scrollgen36
-rw-r--r--lib/segvcatch/CMakeLists.txt35
m---------lib/segvcatch/lib0
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/crepe/Collider.h3
-rw-r--r--src/crepe/api/BehaviorScript.h2
-rw-r--r--src/crepe/api/BehaviorScript.hpp1
-rw-r--r--src/crepe/api/Components.h16
-rw-r--r--src/crepe/api/Config.h3
-rw-r--r--src/crepe/api/Engine.cpp14
-rw-r--r--src/crepe/api/Vector2.h39
-rw-r--r--src/crepe/api/Vector2.hpp9
-rw-r--r--src/crepe/facade/CMakeLists.txt2
-rw-r--r--src/crepe/facade/SignalCatch.cpp20
-rw-r--r--src/crepe/facade/SignalCatch.h23
-rw-r--r--src/crepe/manager/SystemManager.cpp30
-rw-r--r--src/crepe/manager/SystemManager.h13
-rw-r--r--src/crepe/manager/SystemManager.hpp5
-rw-r--r--src/crepe/system/ScriptSystem.cpp25
-rw-r--r--src/test/Vector2Test.cpp10
29 files changed, 439 insertions, 52 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 8e3692b..cc0cc84 100644
--- a/game/CMakeLists.txt
+++ b/game/CMakeLists.txt
@@ -8,13 +8,11 @@ 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
player/PlayerScript.cpp
player/PlayerSubScene.cpp
@@ -24,5 +22,9 @@ add_executable(main
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 d6f8349..940bf1d 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 a8fcb47..4193581 100644
--- a/game/GameScene.cpp
+++ b/game/GameScene.cpp
@@ -1,11 +1,3 @@
-#include "GameScene.h"
-#include "Config.h"
-#include "MoveCameraManualyScript.h"
-#include "StartGameScript.h"
-
-#include "background/BackgroundSubScene.h"
-#include "player/PlayerSubScene.h"
-
#include <cmath>
#include <crepe/api/Animator.h>
#include <crepe/api/Asset.h>
@@ -22,10 +14,23 @@
#include <crepe/api/Transform.h>
#include <crepe/types.h>
+#include "Config.h"
+#include "MoveCameraManualyScript.h"
+#include "StartGameScript.h"
+
+#include "GameScene.h"
+#include "MoveCameraManualyScript.h"
+
+#include "player/PlayerSubScene.h"
+#include "background/BackgroundSubScene.h"
+#include "prefab/ZapperObject.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));
@@ -64,6 +69,8 @@ void GameScene::load_scene() {
});
ceiling.add_component<BoxCollider>(vec2(INFINITY, 200));
+ ZapperObject {new_object("zapper", "zapper", vec2(800, 0))};
+
GameObject start_game_script = new_object("start_game_script", "script", vec2(0, 0));
start_game_script.add_component<BehaviorScript>().set_script<StartGameScript>();
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..5f8ea6c
--- /dev/null
+++ b/game/prefab/CMakeLists.txt
@@ -0,0 +1,4 @@
+target_sources(main PUBLIC
+ ZapperObject.cpp
+)
+
diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp
new file mode 100644
index 0000000..8df2075
--- /dev/null
+++ b/game/prefab/ZapperObject.cpp
@@ -0,0 +1,99 @@
+#include <crepe/api/Transform.h>
+
+#include "Config.h"
+#include "ZapperObject.h"
+
+using namespace crepe;
+
+ZapperObject::ZapperObject(crepe::GameObject && base)
+ : GameObject(std::move(base)),
+ sprite {
+ .orb_start = add_component<Sprite>(
+ Asset {"asset/obstacles/zapper/orbAnim.png"},
+ Sprite::Data {
+ .sorting_in_layer = SORT_IN_LAY_OBSTACLES,
+ .order_in_layer = 1,
+ .size = vec2(0, 42) * SCALE,
+ }
+ ),
+ .orb_end = add_component<Sprite>(
+ sprite.orb_start.source,
+ Sprite::Data {
+ .flip = {true, true},
+ .sorting_in_layer = SORT_IN_LAY_OBSTACLES,
+ .order_in_layer = 1,
+ .size = vec2(0, 42) * SCALE,
+ }
+ ),
+ .glow_start = add_component<Sprite>(
+ Asset {"asset/obstacles/zapper/regular_zappers/glow.png"},
+ Sprite::Data {
+ .sorting_in_layer = SORT_IN_LAY_OBSTACLES,
+ .order_in_layer = -1,
+ .size = vec2(128, 128) * SCALE,
+ }
+ ),
+ .glow_end = add_component<Sprite>(
+ sprite.glow_start.source,
+ Sprite::Data {
+ .flip = {true, true},
+ .sorting_in_layer = SORT_IN_LAY_OBSTACLES,
+ .order_in_layer = -1,
+ .size = vec2(128, 128) * SCALE,
+ }
+ ),
+ .beam = add_component<Sprite>(
+ Asset {"asset/obstacles/zapper/regular_zappers/zapEffect.png"},
+ Sprite::Data {
+ .sorting_in_layer = SORT_IN_LAY_OBSTACLES,
+ .order_in_layer = 0,
+ .size = vec2(0, 40 * SCALE),
+ .angle_offset = 90,
+ }
+ ),
+ },
+ animator {
+ .orb_start = add_component<Animator>(
+ sprite.orb_start, ivec2(62, 42), uvec2(4, 1),
+ Animator::Data {
+ .fps = 10,
+ .looping = true,
+ }
+ ),
+ .orb_end = add_component<Animator>(
+ sprite.orb_end, ivec2(62, 42), uvec2(4, 1), animator.orb_start.data
+ ),
+ .glow_start = add_component<Animator>(
+ sprite.glow_start, ivec2(128, 128), uvec2(16, 1),
+ Animator::Data {
+ .fps = 30,
+ .looping = true,
+ }
+ ),
+ .glow_end = add_component<Animator>(
+ sprite.glow_end, ivec2(128, 128), uvec2(16, 1), animator.glow_start.data
+ ),
+ },
+ body {add_component<Rigidbody>(Rigidbody::Data {
+ .body_type = Rigidbody::BodyType::KINEMATIC,
+ .kinematic_collision = false,
+ })},
+ collider {add_component<BoxCollider>(vec2(0, 0))} {
+ this->place(this->transform.position, 0, 300);
+}
+
+void ZapperObject::place(const crepe::vec2 & position, float rotation, float length) {
+ this->transform.position = position;
+ this->transform.rotation = rotation;
+
+ vec2 offset = vec2(0, 1) * length / 2;
+
+ this->sprite.orb_start.data.position_offset = offset;
+ this->sprite.glow_start.data.position_offset = offset;
+ this->sprite.orb_end.data.position_offset = -offset;
+ this->sprite.glow_end.data.position_offset = -offset;
+
+ this->sprite.beam.data.size.x = length;
+
+ this->collider.dimensions = offset.rotate(rotation) * 2 + vec2(30, 30) * SCALE;
+}
diff --git a/game/prefab/ZapperObject.h b/game/prefab/ZapperObject.h
new file mode 100644
index 0000000..c43af67
--- /dev/null
+++ b/game/prefab/ZapperObject.h
@@ -0,0 +1,37 @@
+#pragma once
+
+#include <crepe/api/Animator.h>
+#include <crepe/api/BoxCollider.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Rigidbody.h>
+#include <crepe/api/Sprite.h>
+
+class ZapperObject : public crepe::GameObject {
+public:
+ ZapperObject(crepe::GameObject &&);
+
+public:
+ struct {
+ crepe::Sprite & orb_start;
+ crepe::Sprite & orb_end;
+ crepe::Sprite & glow_start;
+ crepe::Sprite & glow_end;
+ crepe::Sprite & beam;
+ } sprite;
+
+ struct {
+ crepe::Animator & orb_start;
+ crepe::Animator & orb_end;
+ crepe::Animator & glow_start;
+ crepe::Animator & glow_end;
+ } animator;
+
+ crepe::Rigidbody & body;
+ crepe::BoxCollider & collider;
+
+private:
+ static constexpr float SCALE = 0.8;
+
+public:
+ void place(const crepe::vec2 & position, float rotation, float length);
+};
diff --git a/game/util/scrollgen b/game/util/scrollgen
new file mode 100755
index 0000000..0389107
--- /dev/null
+++ b/game/util/scrollgen
@@ -0,0 +1,36 @@
+#!/bin/sh
+INPUT="$1"
+FRAMES="$2"
+
+die() {
+ echo "$@"
+ exit 1
+}
+check_command() {
+ cmd="$1"
+ command -v "$cmd" > /dev/null || die "command '$cmd' not found"
+}
+
+check_command magick
+check_command identify
+[ "$#" -eq 2 ] || die "usage: $0 <input image> <frame count>"
+[ -e "$INPUT" ] || die "file not found: $INPUT"
+[ "$FRAMES" -gt 0 ] || die "invalid frame count: $FRAMES"
+
+tile_width=$(identify -format "%w" "$INPUT")
+tile_height=$(identify -format "%h" "$INPUT")
+
+OUTPUT="$INPUT.scroll.png"
+magick -size "${tile_width}x$(( $tile_height * $FRAMES ))" 'xc:#ff00ff00' "$OUTPUT"
+
+for i in $(seq 0 $(( $FRAMES - 1 ))); do
+ offset_x=$(( $tile_width * $i / $FRAMES ))
+ offset_y=$(( i * $tile_height ))
+
+ magick "$OUTPUT" "$INPUT" -geometry "+${offset_x}+${offset_y}" -composite "$OUTPUT"
+ magick "$OUTPUT" "$INPUT" -geometry "+$(( $offset_x - $tile_width ))+${offset_y}" -composite "$OUTPUT"
+ echo "+${offset_x}+${offset_y}"
+done
+
+# magick -size ${total_width}x${sprite_height} xc:none canvas.png
+
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..90312b3 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -15,6 +15,7 @@ find_package(GTest REQUIRED)
find_package(whereami REQUIRED)
find_library(BERKELEY_DB db)
find_library(FONTCONFIG_LIB fontconfig)
+find_package(segvcatch REQUIRED)
add_library(crepe SHARED)
add_executable(test_main EXCLUDE_FROM_ALL)
@@ -31,6 +32,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.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;
+ }
}
}
diff --git a/src/test/Vector2Test.cpp b/src/test/Vector2Test.cpp
index 1e21af9..b17f95a 100644
--- a/src/test/Vector2Test.cpp
+++ b/src/test/Vector2Test.cpp
@@ -1,6 +1,7 @@
#include <gtest/gtest.h>
#include <crepe/api/Vector2.h>
+#include <crepe/types.h>
using namespace crepe;
@@ -530,3 +531,12 @@ TEST_F(Vector2Test, Perpendicular) {
EXPECT_FLOAT_EQ(result4.x, -4.0f);
EXPECT_FLOAT_EQ(result4.y, 3.0f);
}
+
+TEST_F(Vector2Test, Rotate) {
+ vec2 foo {0, 1};
+
+ foo = foo.rotate(90);
+ const float GOOD_ENOUGH = 0.001;
+ EXPECT_NEAR(foo.x, 1, GOOD_ENOUGH);
+ EXPECT_NEAR(foo.y, 0, GOOD_ENOUGH);
+}