aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Doxyfile4
-rwxr-xr-xasset/texture/circle.pngbin0 -> 509 bytes
-rwxr-xr-xasset/texture/square.pngbin0 -> 162 bytes
-rw-r--r--contributing.md63
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/crepe/CMakeLists.txt6
-rw-r--r--src/crepe/Collider.cpp2
-rw-r--r--src/crepe/Collider.h13
-rw-r--r--src/crepe/Component.h11
-rw-r--r--src/crepe/Resource.cpp6
-rw-r--r--src/crepe/Resource.h30
-rw-r--r--src/crepe/api/AI.cpp89
-rw-r--r--src/crepe/api/AI.h128
-rw-r--r--src/crepe/api/Animator.cpp50
-rw-r--r--src/crepe/api/Animator.h95
-rw-r--r--src/crepe/api/AssetManager.cpp17
-rw-r--r--src/crepe/api/AssetManager.h62
-rw-r--r--src/crepe/api/AssetManager.hpp22
-rw-r--r--src/crepe/api/AudioSource.cpp15
-rw-r--r--src/crepe/api/AudioSource.h74
-rw-r--r--src/crepe/api/BehaviorScript.cpp6
-rw-r--r--src/crepe/api/BehaviorScript.h9
-rw-r--r--src/crepe/api/BehaviorScript.hpp10
-rw-r--r--src/crepe/api/BoxCollider.cpp10
-rw-r--r--src/crepe/api/BoxCollider.h22
-rw-r--r--src/crepe/api/Button.cpp11
-rw-r--r--src/crepe/api/Button.h67
-rw-r--r--src/crepe/api/CMakeLists.txt27
-rw-r--r--src/crepe/api/Camera.cpp11
-rw-r--r--src/crepe/api/Camera.h38
-rw-r--r--src/crepe/api/CircleCollider.cpp8
-rw-r--r--src/crepe/api/CircleCollider.h16
-rw-r--r--src/crepe/api/Config.h27
-rw-r--r--src/crepe/api/Event.h22
-rw-r--r--src/crepe/api/EventHandler.h76
-rw-r--r--src/crepe/api/GameObject.h12
-rw-r--r--src/crepe/api/GameObject.hpp2
-rw-r--r--src/crepe/api/IKeyListener.h25
-rw-r--r--src/crepe/api/IMouseListener.h45
-rw-r--r--src/crepe/api/KeyCodes.h21
-rw-r--r--src/crepe/api/LoopManager.cpp42
-rw-r--r--src/crepe/api/LoopManager.h25
-rw-r--r--src/crepe/api/LoopManager.hpp7
-rw-r--r--src/crepe/api/LoopTimer.cpp14
-rw-r--r--src/crepe/api/LoopTimer.h12
-rw-r--r--src/crepe/api/Metadata.h2
-rw-r--r--src/crepe/api/Rigidbody.cpp4
-rw-r--r--src/crepe/api/Rigidbody.h144
-rw-r--r--src/crepe/api/Scene.h12
-rw-r--r--src/crepe/api/Script.cpp16
-rw-r--r--src/crepe/api/Script.h41
-rw-r--r--src/crepe/api/Script.hpp30
-rw-r--r--src/crepe/api/Sprite.cpp19
-rw-r--r--src/crepe/api/Sprite.h99
-rw-r--r--src/crepe/api/Texture.cpp38
-rw-r--r--src/crepe/api/Texture.h69
-rw-r--r--src/crepe/api/Transform.h8
-rw-r--r--src/crepe/api/UIObject.cpp8
-rw-r--r--src/crepe/api/UIObject.h25
-rw-r--r--src/crepe/api/Vector2.h24
-rw-r--r--src/crepe/api/Vector2.hpp48
-rw-r--r--src/crepe/facade/CMakeLists.txt2
-rw-r--r--src/crepe/facade/SDLContext.cpp387
-rw-r--r--src/crepe/facade/SDLContext.h196
-rw-r--r--src/crepe/facade/Sound.cpp54
-rw-r--r--src/crepe/facade/Sound.h77
-rw-r--r--src/crepe/facade/SoundContext.cpp30
-rw-r--r--src/crepe/facade/SoundContext.h62
-rw-r--r--src/crepe/facade/SoundHandle.h12
-rw-r--r--src/crepe/facade/Texture.cpp28
-rw-r--r--src/crepe/facade/Texture.h69
-rw-r--r--src/crepe/manager/CMakeLists.txt23
-rw-r--r--src/crepe/manager/ComponentManager.cpp (renamed from src/crepe/ComponentManager.cpp)22
-rw-r--r--src/crepe/manager/ComponentManager.h (renamed from src/crepe/ComponentManager.h)113
-rw-r--r--src/crepe/manager/ComponentManager.hpp (renamed from src/crepe/ComponentManager.hpp)81
-rw-r--r--src/crepe/manager/EventManager.cpp (renamed from src/crepe/api/EventManager.cpp)0
-rw-r--r--src/crepe/manager/EventManager.h (renamed from src/crepe/api/EventManager.h)32
-rw-r--r--src/crepe/manager/EventManager.hpp (renamed from src/crepe/api/EventManager.hpp)0
-rw-r--r--src/crepe/manager/Manager.cpp5
-rw-r--r--src/crepe/manager/Manager.h24
-rw-r--r--src/crepe/manager/Mediator.h39
-rw-r--r--src/crepe/manager/ResourceManager.cpp30
-rw-r--r--src/crepe/manager/ResourceManager.h78
-rw-r--r--src/crepe/manager/ResourceManager.hpp27
-rw-r--r--src/crepe/manager/SaveManager.cpp (renamed from src/crepe/api/SaveManager.cpp)37
-rw-r--r--src/crepe/manager/SaveManager.h (renamed from src/crepe/api/SaveManager.h)32
-rw-r--r--src/crepe/manager/SceneManager.cpp (renamed from src/crepe/api/SceneManager.cpp)9
-rw-r--r--src/crepe/manager/SceneManager.h (renamed from src/crepe/api/SceneManager.h)11
-rw-r--r--src/crepe/manager/SceneManager.hpp (renamed from src/crepe/api/SceneManager.hpp)2
-rw-r--r--src/crepe/system/AISystem.cpp186
-rw-r--r--src/crepe/system/AISystem.h81
-rw-r--r--src/crepe/system/AnimatorSystem.cpp37
-rw-r--r--src/crepe/system/AnimatorSystem.h3
-rw-r--r--src/crepe/system/AudioSystem.cpp62
-rw-r--r--src/crepe/system/AudioSystem.h51
-rw-r--r--src/crepe/system/CMakeLists.txt6
-rw-r--r--src/crepe/system/CollisionSystem.cpp548
-rw-r--r--src/crepe/system/CollisionSystem.h298
-rw-r--r--src/crepe/system/InputSystem.cpp190
-rw-r--r--src/crepe/system/InputSystem.h85
-rw-r--r--src/crepe/system/ParticleSystem.cpp8
-rw-r--r--src/crepe/system/ParticleSystem.h10
-rw-r--r--src/crepe/system/PhysicsSystem.cpp17
-rw-r--r--src/crepe/system/PhysicsSystem.h4
-rw-r--r--src/crepe/system/RenderSystem.cpp57
-rw-r--r--src/crepe/system/RenderSystem.h23
-rw-r--r--src/crepe/system/ScriptSystem.cpp4
-rw-r--r--src/crepe/system/ScriptSystem.h2
-rw-r--r--src/crepe/system/System.cpp4
-rw-r--r--src/crepe/system/System.h11
-rw-r--r--src/crepe/util/OptionalRef.h10
-rw-r--r--src/crepe/util/OptionalRef.hpp7
-rw-r--r--src/doc/feature/animator_creation.dox0
-rw-r--r--src/doc/feature/scene.dox41
-rw-r--r--src/doc/features.dox18
-rw-r--r--src/doc/index.dox7
-rw-r--r--src/doc/layout.xml10
-rw-r--r--src/doc/style.css6
-rw-r--r--src/example/AITest.cpp86
-rw-r--r--src/example/CMakeLists.txt7
-rw-r--r--src/example/asset_manager.cpp36
-rw-r--r--src/example/button.cpp54
-rw-r--r--src/example/game.cpp255
-rw-r--r--src/example/gameloop.cpp7
-rw-r--r--src/example/rendering_particle.cpp88
-rw-r--r--src/example/savemgr.cpp44
-rw-r--r--src/test/AudioTest.cpp161
-rw-r--r--src/test/CMakeLists.txt10
-rw-r--r--src/test/CollisionTest.cpp390
-rw-r--r--src/test/DBTest.cpp3
-rw-r--r--src/test/ECSTest.cpp85
-rw-r--r--src/test/EventTest.cpp52
-rw-r--r--src/test/InputTest.cpp294
-rw-r--r--src/test/ParticleTest.cpp18
-rw-r--r--src/test/PhysicsTest.cpp18
-rw-r--r--src/test/Profiling.cpp247
-rw-r--r--src/test/RenderSystemTest.cpp150
-rw-r--r--src/test/ResourceManagerTest.cpp84
-rw-r--r--src/test/SaveManagerTest.cpp40
-rw-r--r--src/test/SceneManagerTest.cpp24
-rw-r--r--src/test/ScriptECSTest.cpp41
-rw-r--r--src/test/ScriptEventTest.cpp50
-rw-r--r--src/test/ScriptSaveManagerTest.cpp35
-rw-r--r--src/test/ScriptSceneTest.cpp30
-rw-r--r--src/test/ScriptTest.cpp150
-rw-r--r--src/test/ScriptTest.h33
-rw-r--r--src/test/Vector2Test.cpp148
-rw-r--r--src/test/main.cpp14
148 files changed, 6331 insertions, 1431 deletions
diff --git a/Doxyfile b/Doxyfile
index f2714cd..07c86d1 100644
--- a/Doxyfile
+++ b/Doxyfile
@@ -17,11 +17,10 @@ GENERATE_LATEX = NO
LAYOUT_FILE = src/doc/layout.xml
TAB_SIZE = 2
-HTML_INDEX_NUM_ENTRIES = 2
+HTML_INDEX_NUM_ENTRIES = 999
HTML_EXTRA_STYLESHEET = src/doc/style.css
SHOW_HEADERFILE = NO
-USE_MDFILE_AS_MAINPAGE = ./readme.md
REPEAT_BRIEF = NO
EXTRACT_STATIC = YES
@@ -29,6 +28,7 @@ HIDE_UNDOC_NAMESPACES = YES
HIDE_UNDOC_CLASSES = YES
QUIET = YES
+WARNINGS = NO
# set these to NO for user-only docs
INTERNAL_DOCS = YES
diff --git a/asset/texture/circle.png b/asset/texture/circle.png
new file mode 100755
index 0000000..0a92ac7
--- /dev/null
+++ b/asset/texture/circle.png
Binary files differ
diff --git a/asset/texture/square.png b/asset/texture/square.png
new file mode 100755
index 0000000..d07ec98
--- /dev/null
+++ b/asset/texture/square.png
Binary files differ
diff --git a/contributing.md b/contributing.md
index b0f623b..0faed2b 100644
--- a/contributing.md
+++ b/contributing.md
@@ -635,15 +635,21 @@ that you can click on to open them.
</summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
```cpp
+ void Foo::bar() { }
+
void Foo::set_value(int value) {
this->value = value;
+ this->bar();
}
```
</td><td>
```cpp
+ void Foo::bar() { }
+
void Foo::set_value(int new_value) {
value = new_value;
+ bar();
}
```
</td></tr></table></details>
@@ -849,6 +855,11 @@ that you can click on to open them.
parameter of `TEST()` / `TEST_F()` macro)
- Test source files match their suite name (or test fixture name in the case of
tests that use a fixture)
+- Tests that measure time or use delays must be [disabled][gtest-disable] (by
+ prepending `DISABLED_` to the suite or case name).
+
+ These tests will still be compiled, but will only run when the `test_main`
+ binary is run with the `--gtest_also_run_disabled_tests` flag.
# Structure
@@ -868,6 +879,8 @@ that you can click on to open them.
# Documentation
+[Doxygen commands](https://www.doxygen.nl/manual/commands.html)
+
- All documentation is written in U.S. English
- <details><summary>
Doxygen commands are used with a backslash instead of an at-sign.
@@ -953,6 +966,52 @@ that you can click on to open them.
Foo & operator=(Foo &&) = delete;
```
</td></tr></table></details>
+- Do not use markdown headings in Doxygen
+
+## Documenting features
+
+Engine features are small 'building blocks' that the user (game developer) may
+reference when building a game with the engine. Features do not necessarily map
+1-1 to engine components or systems. If a component or system has a single,
+distinct feature it should be named after that feature, not the component or
+system itself.
+
+The sources for these pages are located under `src/doc/feature/`, and have the
+following format:
+
+- A feature description which explains—
+ - the purpose and function of the feature (focus on what it enables or
+ achieves for the user)
+ - additional information about when to implement the feature, such as specific
+ use cases or scenarios
+- A list of 'see also' references to relevant classes and/or types
+- A **minimal** example to demonstrate how the feature is used. The example
+ should be written such that the following is clear to the reader:
+ - Which headers need to be included to utilize the feature
+ - *Why* the example works, not what is happening in the example
+ - Where is this code supposed to be called (e.g. inside scene/script
+ functions)
+ - Which restrictions should be kept in mind (e.g. copy/move semantics, max
+ component instances, speed considerations)
+
+Features should be documented as clear and concise as possible, so the following
+points should be kept in mind:
+
+- <details><summary>
+ If a page expands on an example from another page, directly reference the
+ other page using a cross-reference (`\ref`) in a `\note` block at the top of
+ the page.
+ </summary>
+
+ ```
+ \note This page builds on top of the example shown in \ref feature_script
+ ```
+ </details>
+- When explaining the usage of specific functions, qualify them such that
+ Doxygen is able to add a cross-reference or manually add a reference using the
+ `\ref` command.
+- Users will likely copy-paste examples as-is, so do this yourself to check if
+ the example code actually works!
# Libraries
@@ -960,4 +1019,8 @@ that you can click on to open them.
subdirectory
- When adding new submodules, please set the `shallow` option to `true` in the
[.gitmodules](./.gitmodules) file
+- When adding new libraries, please update the library version table in
+ [readme\.md](./readme.md)
+
+[gtest-disable]: https://google.github.io/googletest/advanced.html#temporarily-disabling-tests
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index c3f29da..97b21f0 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -40,5 +40,6 @@ install(
target_link_libraries(test_main
PRIVATE gtest
+ PRIVATE gmock
PUBLIC crepe
)
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt
index 7e176e7..6cbb9fe 100644
--- a/src/crepe/CMakeLists.txt
+++ b/src/crepe/CMakeLists.txt
@@ -1,21 +1,21 @@
target_sources(crepe PUBLIC
Particle.cpp
- ComponentManager.cpp
Component.cpp
Collider.cpp
+ Resource.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
- ComponentManager.h
- ComponentManager.hpp
Component.h
Collider.h
ValueBroker.h
ValueBroker.hpp
+ Resource.h
)
add_subdirectory(api)
add_subdirectory(facade)
+add_subdirectory(manager)
add_subdirectory(system)
add_subdirectory(util)
diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp
index bbec488..77e11c8 100644
--- a/src/crepe/Collider.cpp
+++ b/src/crepe/Collider.cpp
@@ -2,4 +2,4 @@
using namespace crepe;
-Collider::Collider(game_object_id_t id) : Component(id) {}
+Collider::Collider(game_object_id_t id, const vec2 & offset) : Component(id), offset(offset) {}
diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h
index 827f83d..42ccfd4 100644
--- a/src/crepe/Collider.h
+++ b/src/crepe/Collider.h
@@ -1,14 +1,23 @@
#pragma once
#include "Component.h"
+#include "types.h"
namespace crepe {
class Collider : public Component {
public:
- Collider(game_object_id_t id);
+ Collider(game_object_id_t id, const vec2 & offset);
- int size;
+public:
+ /**
+ * \brief Offset of the collider relative to the rigidbody position.
+ *
+ * The `offset` defines the positional shift applied to the collider relative to the position of the rigidbody it is attached to.
+ * This allows the collider to be placed at a different position than the rigidbody.
+ *
+ */
+ vec2 offset;
};
} // namespace crepe
diff --git a/src/crepe/Component.h b/src/crepe/Component.h
index dc17721..eff5a58 100644
--- a/src/crepe/Component.h
+++ b/src/crepe/Component.h
@@ -8,7 +8,7 @@ class ComponentManager;
/**
* \brief Base class for all components
- *
+ *
* This class is the base class for all components. It provides a common interface for all
* components.
*/
@@ -16,7 +16,12 @@ class Component {
public:
//! Whether the component is active
bool active = true;
- //! The id of the GameObject this component belongs to
+ /**
+ * \brief The id of the GameObject this component belongs to
+ *
+ * \note Only systems are supposed to use this member, but since friend
+ * relations aren't inherited this needs to be public.
+ */
const game_object_id_t game_object_id;
protected:
@@ -24,7 +29,7 @@ protected:
* \param id The id of the GameObject this component belongs to
*/
Component(game_object_id_t id);
- //! Only the ComponentManager can create components
+ //! Only ComponentManager can create components
friend class ComponentManager;
Component(const Component &) = delete;
diff --git a/src/crepe/Resource.cpp b/src/crepe/Resource.cpp
new file mode 100644
index 0000000..85913ed
--- /dev/null
+++ b/src/crepe/Resource.cpp
@@ -0,0 +1,6 @@
+#include "Resource.h"
+#include "manager/Mediator.h"
+
+using namespace crepe;
+
+Resource::Resource(const Asset & asset, Mediator & mediator) {}
diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h
new file mode 100644
index 0000000..d65206b
--- /dev/null
+++ b/src/crepe/Resource.h
@@ -0,0 +1,30 @@
+#pragma once
+
+namespace crepe {
+
+class ResourceManager;
+class Asset;
+class Mediator;
+
+/**
+ * \brief Resource interface
+ *
+ * Resource is an interface class used to represent a (deserialized) game resource (e.g.
+ * textures, sounds). Resources are always created from \ref Asset "assets" by ResourceManager.
+ *
+ * The game programmer has the ability to use the ResourceManager to keep instances of concrete
+ * resources between scenes, preventing them from being reinstantiated during a scene
+ * transition.
+ */
+class Resource {
+public:
+ Resource(const Asset & src, Mediator & mediator);
+ virtual ~Resource() = default;
+
+ Resource(const Resource &) = delete;
+ Resource(Resource &&) = delete;
+ Resource & operator=(const Resource &) = delete;
+ Resource & operator=(Resource &&) = delete;
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/AI.cpp b/src/crepe/api/AI.cpp
new file mode 100644
index 0000000..2195249
--- /dev/null
+++ b/src/crepe/api/AI.cpp
@@ -0,0 +1,89 @@
+#include <stdexcept>
+#include <type_traits>
+
+#include "AI.h"
+#include "types.h"
+
+namespace crepe {
+
+AI::AI(game_object_id_t id, float max_force) : Component(id), max_force(max_force) {}
+
+void AI::make_circle_path(float radius, const vec2 & center, float start_angle,
+ bool clockwise) {
+ if (radius <= 0) {
+ throw std::runtime_error("Radius must be greater than 0");
+ }
+
+ // The step size is determined by the radius (step size is in radians)
+ float step = RADIUS_TO_STEP / radius;
+ // Force at least MIN_STEP steps (in case of a small radius)
+ if (step > 2 * M_PI / MIN_STEP) {
+ step = 2 * M_PI / MIN_STEP;
+ }
+ // The path node distance is determined by the step size and the radius
+ this->path_node_distance = radius * step * PATH_NODE_DISTANCE_FACTOR;
+
+ if (clockwise) {
+ for (float i = start_angle; i < 2 * M_PI + start_angle; i += step) {
+ path.push_back(vec2{static_cast<float>(center.x + radius * cos(i)),
+ static_cast<float>(center.y + radius * sin(i))});
+ }
+ } else {
+ for (float i = start_angle; i > start_angle - 2 * M_PI; i -= step) {
+ path.push_back(vec2{static_cast<float>(center.x + radius * cos(i)),
+ static_cast<float>(center.y + radius * sin(i))});
+ }
+ }
+}
+
+void AI::make_oval_path(float radius_x, float radius_y, const vec2 & center, float start_angle,
+ bool clockwise, float rotation) {
+ if (radius_x <= 0 && radius_y <= 0) {
+ throw std::runtime_error("Radius must be greater than 0");
+ }
+
+ float max_radius = std::max(radius_x, radius_y);
+ // The step size is determined by the radius (step size is in radians)
+ float step = RADIUS_TO_STEP / max_radius;
+ // Force at least MIN_STEP steps (in case of a small radius)
+ if (step > 2 * M_PI / MIN_STEP) {
+ step = 2 * M_PI / MIN_STEP;
+ }
+ // The path node distance is determined by the step size and the radius
+ this->path_node_distance = max_radius * step * PATH_NODE_DISTANCE_FACTOR;
+
+ std::function<vec2(vec2, vec2)> rotate_point = [rotation](vec2 point, vec2 center) {
+ float s = sin(rotation);
+ float c = cos(rotation);
+
+ // Translate point back to origin
+ point.x -= center.x;
+ point.y -= center.y;
+
+ // Rotate point
+ float xnew = point.x * c - point.y * s;
+ float ynew = point.x * s + point.y * c;
+
+ // Translate point back
+ point.x = xnew + center.x;
+ point.y = ynew + center.y;
+
+ return point;
+ };
+
+ if (clockwise) {
+ for (float i = start_angle; i < 2 * M_PI + start_angle; i += step) {
+ vec2 point = {static_cast<float>(center.x + radius_x * cos(i)),
+ static_cast<float>(center.y + radius_y * sin(i))};
+ path.push_back(rotate_point(point, center));
+ }
+ } else {
+ for (float i = start_angle; i > start_angle - 2 * M_PI; i -= step) {
+ vec2 point = {static_cast<float>(center.x + radius_x * cos(i)),
+ static_cast<float>(center.y + radius_y * sin(i))};
+ path.push_back(rotate_point(point, center));
+ }
+ }
+}
+
+} // namespace crepe
diff --git a/src/crepe/api/AI.h b/src/crepe/api/AI.h
new file mode 100644
index 0000000..c780a91
--- /dev/null
+++ b/src/crepe/api/AI.h
@@ -0,0 +1,128 @@
+#pragma once
+
+#include "Component.h"
+#include "types.h"
+
+namespace crepe {
+
+/**
+ * \brief The AI component is used to control the movement of an entity using AI.
+ *
+ * The AI component can be used to control the movement of an entity. The AI component can be used
+ * to implement different behaviors such as seeking, fleeing, arriving, and path following.
+ */
+class AI : public Component {
+public:
+ //! The different types of behaviors that can be used
+ enum BehaviorTypeMask {
+ SEEK = 0x00002,
+ FLEE = 0x00004,
+ ARRIVE = 0x00008,
+ PATH_FOLLOW = 0x00010,
+ };
+
+public:
+ /**
+ * \param id The id of the game object
+ * \param max_force The maximum force that can be applied to the entity
+ */
+ AI(game_object_id_t id, float max_force);
+
+ /**
+ * \brief Check if a behavior is on (aka activated)
+ *
+ * \param behavior The behavior to check
+ * \return true if the behavior is on, false otherwise
+ */
+ bool on(BehaviorTypeMask behavior) const { return (flags & behavior); }
+ //! Turn on the seek behavior
+ void seek_on() { flags |= SEEK; }
+ //! Turn off the seek behavior
+ void seek_off() { flags &= ~SEEK; }
+ //! Turn on the flee behavior
+ void flee_on() { flags |= FLEE; }
+ //! Turn off the flee behavior
+ void flee_off() { flags &= ~FLEE; }
+ //! Turn on the arrive behavior
+ void arrive_on() { flags |= ARRIVE; }
+ //! Turn off the arrive behavior
+ void arrive_off() { flags &= ~ARRIVE; }
+ //! Turn on the path follow behavior
+ void path_follow_on() { flags |= PATH_FOLLOW; }
+ //! Turn off the path follow behavior
+ void path_follow_off() { flags &= ~PATH_FOLLOW; }
+
+ /**
+ * \brief Add a path node (for the path following behavior)
+ *
+ * \note The path is not relative to the entity's position (it is an absolute path)
+ *
+ * \param node The path node to add
+ */
+ void add_path_node(const vec2 & node) { path.push_back(node); }
+ /**
+ * \brief Make a circle path (for the path following behavior)
+ *
+ * \note The path is not relative to the entity's position (it is an absolute path)
+ *
+ * \param radius The radius of the circle (in game units)
+ * \param center The center of the circle (in game units)
+ * \param start_angle The start angle of the circle (in radians)
+ * \param clockwise The direction of the circle
+ */
+ void make_circle_path(float radius, const vec2 & center = {0, 0}, float start_angle = 0,
+ bool clockwise = true);
+ /**
+ * \brief Make an oval path (for the path following behavior)
+ *
+ * \note The path is not relative to the entity's position (it is an absolute path)
+ *
+ * \param radius_x The x radius of the oval (in game units)
+ * \param radius_y The y radius of the oval (in game units)
+ * \param center The center of the oval (in game units)
+ * \param start_angle The start angle of the oval (in radians)
+ * \param clockwise The direction of the oval
+ * \param rotation The rotation of the oval (in radians)
+ */
+ void make_oval_path(float radius_x, float radius_y, const vec2 & center = {0, 0},
+ float start_angle = 0, bool clockwise = true, float rotation = 0);
+
+public:
+ //! The maximum force that can be applied to the entity (higher values will make the entity adjust faster)
+ float max_force;
+
+ //! The target to seek at
+ vec2 seek_target;
+ //! The target to arrive at
+ vec2 arrive_target;
+ //! The target to flee from
+ vec2 flee_target;
+ //! The distance at which the entity will start to flee from the target
+ float square_flee_panic_distance = 200.0f * 200.0f;
+ //! The deceleration rate for the arrive behavior (higher values will make the entity decelerate faster (less overshoot))
+ float arrive_deceleration = 40.0f;
+ //! The path to follow (for the path following behavior)
+ std::vector<vec2> path;
+ //! The distance from the path node at which the entity will move to the next node (automatically set by make_circle_path())
+ float path_node_distance = 400.0f;
+ //! Looping behavior for the path
+ bool path_loop = true;
+
+private:
+ //! The flags for the behaviors
+ int flags = 0;
+ //! The current path index
+ size_t path_index = 0;
+
+ //! The AISystem is the only class that should access the flags and path_index variables
+ friend class AISystem;
+
+ //! The minimum amount of steps for the path following behavior
+ static constexpr int MIN_STEP = 16;
+ //! The radius to step size ratio for the path following behavior
+ static constexpr float RADIUS_TO_STEP = 400.0f;
+ //! The path node distance factor for the path following behavior
+ static constexpr float PATH_NODE_DISTANCE_FACTOR = 0.75f;
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp
index 45f67f6..4ce4bf0 100644
--- a/src/crepe/api/Animator.cpp
+++ b/src/crepe/api/Animator.cpp
@@ -7,21 +7,51 @@
using namespace crepe;
-Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_animator)
+Animator::Animator(game_object_id_t id, Sprite & spritesheet, const ivec2 & single_frame_size,
+ const uvec2 & grid_size, const Animator::Data & data)
: Component(id),
- spritesheet(ss),
- row(row),
- col(col) {
+ spritesheet(spritesheet),
+ grid_size(grid_size),
+ data(data) {
dbg_trace();
- this->spritesheet.mask.h /= col;
- this->spritesheet.mask.w /= row;
+ this->spritesheet.mask.w = single_frame_size.x;
+ this->spritesheet.mask.h = single_frame_size.y;
this->spritesheet.mask.x = 0;
- this->spritesheet.mask.y = col_animator * this->spritesheet.mask.h;
- this->active = false;
+ this->spritesheet.mask.y = 0;
- // need to do this for to get the aspect ratio for a single clipping in the spritesheet
this->spritesheet.aspect_ratio
- = static_cast<double>(this->spritesheet.mask.w) / this->spritesheet.mask.h;
+ = static_cast<float>(single_frame_size.x) / single_frame_size.y;
}
+
Animator::~Animator() { dbg_trace(); }
+
+void Animator::loop() { this->data.looping = true; }
+
+void Animator::play() { this->active = true; }
+
+void Animator::pause() { this->active = false; }
+
+void Animator::stop() {
+ this->active = false;
+ this->data.col = 0;
+ this->data.row = 0;
+}
+void Animator::set_fps(int fps) { this->data.fps = fps; }
+
+void Animator::set_cycle_range(int start, int end) {
+ this->data.cycle_start = start, this->data.cycle_end = end;
+}
+
+void Animator::set_anim(int col) {
+ Animator::Data & ctx = this->data;
+ this->spritesheet.mask.x = ctx.row = 0;
+ ctx.col = col;
+ this->spritesheet.mask.y = ctx.col * this->spritesheet.mask.h;
+}
+
+void Animator::next_anim() {
+ Animator::Data & ctx = this->data;
+ ctx.row = ctx.row++ % this->grid_size.x;
+ this->spritesheet.mask.x = ctx.row * this->spritesheet.mask.w;
+}
diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h
index 6c506aa..5918800 100644
--- a/src/crepe/api/Animator.h
+++ b/src/crepe/api/Animator.h
@@ -1,5 +1,7 @@
#pragma once
+#include "../types.h"
+
#include "Component.h"
#include "Sprite.h"
@@ -16,56 +18,87 @@ class SDLContext;
* sheet. It can be used to play animations, loop them, or stop them.
*/
class Animator : public Component {
+public:
+ struct Data {
+ //! frames per second for animation
+ unsigned int fps = 1;
+ //! The current col being animated.
+ unsigned int col = 0;
+ //! The current row being animated.
+ unsigned int row = 0;
+ //! should the animation loop
+ bool looping = false;
+ //! starting frame for cycling
+ unsigned int cycle_start = 0;
+ //! end frame for cycling (-1 = use last frame)
+ int cycle_end = -1;
+ };
public:
- //TODO: need to implement this
+ //! Animator will repeat the animation
void loop();
+ //! starts the animation
+ void play();
+ //! pauses the animation
+ void pause();
+ /**
+ * \brief stops the animation
+ *
+ * sets the active on false and resets all the current rows and columns
+ */
void stop();
+ /**
+ * \brief set frames per second
+ *
+ * \param fps frames per second
+ */
+ void set_fps(int fps);
+ /**
+ * \brief set the range in the row
+ *
+ * \param start of row animation
+ * \param end of row animation
+ */
+ void set_cycle_range(int start, int end);
+ /**
+ * \brief select which column to animate from
+ *
+ * \param col animation column
+ */
+ void set_anim(int col);
+ //! will go to the next animaiton of current row
+ void next_anim();
public:
/**
* \brief Constructs an Animator object that will control animations for a sprite sheet.
*
* \param id The unique identifier for the component, typically assigned automatically.
- * \param spritesheet A reference to the Sprite object which holds the sprite sheet for
- * animation.
- * \param row The maximum number of rows in the sprite sheet.
- * \param col The maximum number of columns in the sprite sheet.
- * \param col_animate The specific col index of the sprite sheet to animate. This allows
- * selecting which col to animate from multiple col in the sheet.
+ * \param spritesheet the reference to the spritesheet
+ * \param single_frame_size the width and height in pixels of a single frame inside the
+ * spritesheet
+ * \param grid_size the max rows and columns inside the given spritesheet
+ * \param data extra animation data for more control
*
* This constructor sets up the Animator with the given parameters, and initializes the
* animation system.
*/
- Animator(uint32_t id, Sprite & spritesheet, int row, int col, int col_animate);
-
+ Animator(game_object_id_t id, Sprite & spritesheet, const ivec2 & single_frame_size,
+ const uvec2 & grid_size, const Animator::Data & data);
~Animator(); // dbg_trace
+public:
+ Animator::Data data;
+
private:
- //! A reference to the Sprite sheet containing the animation frames.
+ //! A reference to the Sprite sheet containing.
Sprite & spritesheet;
- //! The maximum number of columns in the sprite sheet.
- const int col;
-
- //! The maximum number of rows in the sprite sheet.
- const int row;
+ //! The maximum number of rows and columns inside the spritesheet
+ const uvec2 grid_size;
- //! The current col being animated.
- int curr_col = 0;
-
- //! The current row being animated.
- int curr_row = 0;
-
- //TODO: Is this necessary?
- //int fps;
-
-private:
- //! AnimatorSystem adjust the private member parameters of Animator;
- friend class AnimatorSystem;
-
- //! SDLContext reads the Animator member var's
- friend class SDLContext;
+ //! Uses the spritesheet
+ friend AnimatorSystem;
};
+
} // namespace crepe
-//
diff --git a/src/crepe/api/AssetManager.cpp b/src/crepe/api/AssetManager.cpp
deleted file mode 100644
index 3925758..0000000
--- a/src/crepe/api/AssetManager.cpp
+++ /dev/null
@@ -1,17 +0,0 @@
-#include "util/Log.h"
-
-#include "AssetManager.h"
-
-using namespace crepe;
-
-AssetManager & AssetManager::get_instance() {
- static AssetManager instance;
- return instance;
-}
-
-AssetManager::~AssetManager() {
- dbg_trace();
- this->asset_cache.clear();
-}
-
-AssetManager::AssetManager() { dbg_trace(); }
diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h
deleted file mode 100644
index fee6780..0000000
--- a/src/crepe/api/AssetManager.h
+++ /dev/null
@@ -1,62 +0,0 @@
-#pragma once
-
-#include <any>
-#include <memory>
-#include <string>
-#include <unordered_map>
-
-namespace crepe {
-
-/**
- * \brief The AssetManager is responsible for storing and managing assets over multiple scenes.
- *
- * The AssetManager ensures that assets are loaded once and can be accessed across different
- * scenes. It caches assets to avoid reloading them every time a scene is loaded. Assets are
- * retained in memory until the AssetManager is destroyed, at which point the cached assets are
- * cleared.
- */
-class AssetManager {
-
-private:
- //! A cache that holds all the assets, accessible by their file path, over multiple scenes.
- std::unordered_map<std::string, std::any> asset_cache;
-
-private:
- AssetManager();
- virtual ~AssetManager();
-
-public:
- AssetManager(const AssetManager &) = delete;
- AssetManager(AssetManager &&) = delete;
- AssetManager & operator=(const AssetManager &) = delete;
- AssetManager & operator=(AssetManager &&) = delete;
-
- /**
- * \brief Retrieves the singleton instance of the AssetManager.
- *
- * \return A reference to the single instance of the AssetManager.
- */
- static AssetManager & get_instance();
-
-public:
- /**
- * \brief Caches an asset by loading it from the given file path.
- *
- * \param file_path The path to the asset file to load.
- * \param reload If true, the asset will be reloaded from the file, even if it is already
- * cached.
- * \tparam T The type of asset to cache (e.g., texture, sound, etc.).
- *
- * \return A shared pointer to the cached asset.
- *
- * This template function caches the asset at the given file path. If the asset is already
- * cached and `reload` is false, the existing cached version will be returned. Otherwise, the
- * asset will be reloaded and added to the cache.
- */
- template <typename T>
- std::shared_ptr<T> cache(const std::string & file_path, bool reload = false);
-};
-
-} // namespace crepe
-
-#include "AssetManager.hpp"
diff --git a/src/crepe/api/AssetManager.hpp b/src/crepe/api/AssetManager.hpp
deleted file mode 100644
index 1c0e978..0000000
--- a/src/crepe/api/AssetManager.hpp
+++ /dev/null
@@ -1,22 +0,0 @@
-#pragma once
-
-#include "AssetManager.h"
-
-namespace crepe {
-
-template <typename asset>
-std::shared_ptr<asset> AssetManager::cache(const std::string & file_path, bool reload) {
- auto it = asset_cache.find(file_path);
-
- if (!reload && it != asset_cache.end()) {
- return std::any_cast<std::shared_ptr<asset>>(it->second);
- }
-
- std::shared_ptr<asset> new_asset = std::make_shared<asset>(file_path.c_str());
-
- asset_cache[file_path] = new_asset;
-
- return new_asset;
-}
-
-} // namespace crepe
diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp
new file mode 100644
index 0000000..7b05cb1
--- /dev/null
+++ b/src/crepe/api/AudioSource.cpp
@@ -0,0 +1,15 @@
+#include "AudioSource.h"
+
+using namespace crepe;
+using namespace std;
+
+AudioSource::AudioSource(game_object_id_t id, const Asset & src)
+ : Component(id),
+ source(src) {}
+
+void AudioSource::play(bool looping) {
+ this->loop = looping;
+ this->oneshot_play = true;
+}
+
+void AudioSource::stop() { this->oneshot_stop = true; }
diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h
new file mode 100644
index 0000000..b20e490
--- /dev/null
+++ b/src/crepe/api/AudioSource.h
@@ -0,0 +1,74 @@
+#pragma once
+
+#include "../Component.h"
+#include "../facade/SoundHandle.h"
+#include "../types.h"
+
+#include "Asset.h"
+#include "GameObject.h"
+
+namespace crepe {
+
+class AudioSystem;
+
+//! Audio source component
+class AudioSource : public Component {
+ //! AudioSource components are handled by AudioSystem
+ friend class AudioSystem;
+
+protected:
+ /**
+ * \param source Sound sample to load
+ */
+ AudioSource(game_object_id_t id, const Asset & source);
+ //! Only ComponentManager creates components
+ friend class ComponentManager;
+
+public:
+ // std::unique_ptr needs to be able to destoy this component
+ virtual ~AudioSource() = default;
+
+public:
+ //! Start this audio source
+ void play(bool looping = false);
+ //! Stop this audio source
+ void stop();
+
+public:
+ //! Play when this component becomes active
+ bool play_on_awake = false;
+ //! Repeat the current audio clip during playback
+ bool loop = false;
+ //! Normalized volume (0.0 - 1.0)
+ float volume = 1.0;
+
+private:
+ //! This audio source's clip
+ const Asset source;
+
+ /**
+ * \name One-shot state variables
+ *
+ * These variables trigger function calls when set to true, and are unconditionally reset on
+ * every system update.
+ *
+ * \{
+ */
+ //! Play this sample
+ bool oneshot_play = false;
+ //! Stop this sample
+ bool oneshot_stop = false;
+ //! \}
+ /**
+ * \name State diffing variables
+ * \{
+ */
+ typeof(active) last_active = false;
+ typeof(volume) last_volume = volume;
+ typeof(loop) last_loop = loop;
+ //! \}
+ //! This source's voice handle
+ SoundHandle voice{};
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp
index 7bbace0..d22afdf 100644
--- a/src/crepe/api/BehaviorScript.cpp
+++ b/src/crepe/api/BehaviorScript.cpp
@@ -4,12 +4,12 @@
using namespace crepe;
-BehaviorScript::BehaviorScript(game_object_id_t id, ComponentManager & mgr)
+BehaviorScript::BehaviorScript(game_object_id_t id, Mediator & mediator)
: Component(id),
- component_manager(mgr) {}
+ mediator(mediator) {}
template <>
BehaviorScript & GameObject::add_component<BehaviorScript>() {
ComponentManager & mgr = this->component_manager;
- return mgr.add_component<BehaviorScript>(this->id, mgr);
+ return mgr.add_component<BehaviorScript>(this->id, mgr.mediator);
}
diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h
index d556fe5..3909b96 100644
--- a/src/crepe/api/BehaviorScript.h
+++ b/src/crepe/api/BehaviorScript.h
@@ -23,14 +23,13 @@ class BehaviorScript : public Component {
protected:
/**
* \param id Parent \c GameObject id
- * \param component_manager Reference to component manager (passed through to \c Script
- * instance)
+ * \param mediator Mediator reference
*
* \note Calls to this constructor (should) always pass through \c GameObject::add_component,
* which has an exception for this specific component type. This was done so the user does
* not have to pass references used within \c Script to each \c BehaviorScript instance.
*/
- BehaviorScript(game_object_id_t id, ComponentManager & component_manager);
+ BehaviorScript(game_object_id_t id, Mediator & mediator);
//! Only ComponentManager is allowed to instantiate BehaviorScript
friend class ComponentManager;
@@ -55,8 +54,8 @@ protected:
friend class ScriptSystem;
protected:
- //! Reference to component manager (passed to Script)
- ComponentManager & component_manager;
+ //! Reference mediator
+ Mediator & mediator;
};
/**
diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp
index bd59337..b9bb1e2 100644
--- a/src/crepe/api/BehaviorScript.hpp
+++ b/src/crepe/api/BehaviorScript.hpp
@@ -13,14 +13,12 @@ template <class T, typename... Args>
BehaviorScript & BehaviorScript::set_script(Args &&... args) {
dbg_trace();
static_assert(std::is_base_of<Script, T>::value);
- Script * s = new T(std::forward<Args>(args)...);
+ this->script = std::unique_ptr<Script>(new T(std::forward<Args>(args)...));
- s->game_object_id = this->game_object_id;
- s->active = this->active;
- s->component_manager = this->component_manager;
- s->event_manager = EventManager::get_instance();
+ this->script->game_object_id = this->game_object_id;
+ this->script->active = this->active;
+ this->script->mediator = this->mediator;
- this->script = std::unique_ptr<Script>(s);
return *this;
}
diff --git a/src/crepe/api/BoxCollider.cpp b/src/crepe/api/BoxCollider.cpp
new file mode 100644
index 0000000..c097a24
--- /dev/null
+++ b/src/crepe/api/BoxCollider.cpp
@@ -0,0 +1,10 @@
+#include "BoxCollider.h"
+
+#include "../Collider.h"
+
+using namespace crepe;
+
+BoxCollider::BoxCollider(game_object_id_t game_object_id, const vec2 & offset,
+ const vec2 & dimensions)
+ : Collider(game_object_id, offset),
+ dimensions(dimensions) {}
diff --git a/src/crepe/api/BoxCollider.h b/src/crepe/api/BoxCollider.h
new file mode 100644
index 0000000..1ac4d46
--- /dev/null
+++ b/src/crepe/api/BoxCollider.h
@@ -0,0 +1,22 @@
+#pragma once
+
+#include "../Collider.h"
+#include "Vector2.h"
+#include "types.h"
+
+namespace crepe {
+
+/**
+ * \brief A class representing a box-shaped collider.
+ *
+ * This class is used for collision detection with other colliders (e.g., CircleCollider).
+ */
+class BoxCollider : public Collider {
+public:
+ BoxCollider(game_object_id_t game_object_id, const vec2 & offset, const vec2 & dimensions);
+
+ //! Width and height of the box collider
+ vec2 dimensions;
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp
new file mode 100644
index 0000000..76f74f0
--- /dev/null
+++ b/src/crepe/api/Button.cpp
@@ -0,0 +1,11 @@
+#include "Button.h"
+
+namespace crepe {
+
+Button::Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
+ const std::function<void()> & on_click, bool is_toggle)
+ : UIObject(id, dimensions, offset),
+ is_toggle(is_toggle),
+ on_click(on_click) {}
+
+} // namespace crepe
diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h
new file mode 100644
index 0000000..61b18d7
--- /dev/null
+++ b/src/crepe/api/Button.h
@@ -0,0 +1,67 @@
+#pragma once
+
+#include <functional>
+
+#include "UIObject.h"
+
+namespace crepe {
+
+//! Represents a clickable UI button, derived from the UiObject class.
+class Button : public UIObject {
+public:
+ /**
+ * \brief Constructs a Button with the specified game object ID and dimensions.
+ *
+ * \param id The unique ID of the game object associated with this button.
+ * \param dimensions The width and height of the UIObject
+ * \param offset The offset relative this GameObjects Transform
+ * \param is_toggle Optional flag to indicate if the button is a toggle button. Defaults to false.
+ * \param on_click callback function that will be invoked when the button is clicked.
+ */
+ Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
+ const std::function<void()> & on_click, bool is_toggle = false);
+
+ /**
+ * \brief Indicates if the button is a toggle button (can be pressed and released).
+ *
+ * A toggle button allows for a pressed/released state, whereas a regular button
+ * typically only has an on-click state.
+ */
+ bool is_toggle = false;
+ // TODO: create separate toggle button class
+ /**
+ * \brief The callback function to be executed when the button is clicked.
+ *
+ * This function is invoked whenever the button is clicked. It can be set to any
+ * function that matches the signature `void()`.
+ */
+ std::function<void()> on_click = nullptr;
+
+ /**
+ * \brief Callback function to be executed when the mouse enters the button's boundaries.
+ *
+ * This function is triggered when the mouse cursor moves over the button, allowing
+ * custom actions like visual effects, highlighting, or sound effects.
+ */
+ std::function<void()> on_mouse_enter = nullptr;
+
+ /**
+ * \brief Callback function to be executed when the mouse exits the button's boundaries.
+ *
+ * This function is triggered when the mouse cursor moves out of the button's area,
+ * allowing custom actions like resetting visual effects or playing exit-related effects.
+ */
+ std::function<void()> on_mouse_exit = nullptr;
+
+private:
+ //! friend relation for is_pressed and hover variables
+ friend class InputSystem;
+ //! Indicates whether the toggle button is pressed
+ bool is_pressed = false;
+ //! Indicates whether the mouse is currently hovering over the button
+ bool hover = false;
+
+public:
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 50c51ed..46deb67 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -1,21 +1,18 @@
target_sources(crepe PUBLIC
- # AudioSource.cpp
+ AudioSource.cpp
BehaviorScript.cpp
GameObject.cpp
Rigidbody.cpp
ParticleEmitter.cpp
Transform.cpp
Color.cpp
- Texture.cpp
- AssetManager.cpp
Sprite.cpp
- SaveManager.cpp
Config.cpp
Metadata.cpp
- SceneManager.cpp
Camera.cpp
Animator.cpp
- EventManager.cpp
+ BoxCollider.cpp
+ CircleCollider.cpp
IKeyListener.cpp
IMouseListener.cpp
LoopManager.cpp
@@ -23,10 +20,13 @@ target_sources(crepe PUBLIC
Asset.cpp
EventHandler.cpp
Script.cpp
+ Button.cpp
+ UIObject.cpp
+ AI.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
- # AudioSource.h
+ AudioSource.h
BehaviorScript.h
Config.h
Script.h
@@ -38,18 +38,12 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Vector2.h
Vector2.hpp
Color.h
- Texture.h
- AssetManager.h
- AssetManager.hpp
- SaveManager.h
Scene.h
Metadata.h
- SceneManager.h
- SceneManager.hpp
Camera.h
Animator.h
- EventManager.h
- EventManager.hpp
+ BoxCollider.h
+ CircleCollider.h
EventHandler.h
EventHandler.hpp
Event.h
@@ -58,4 +52,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
LoopManager.h
LoopTimer.h
Asset.h
+ Button.h
+ UIObject.h
+ AI.h
)
diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp
index 39d8ab0..179dc18 100644
--- a/src/crepe/api/Camera.cpp
+++ b/src/crepe/api/Camera.cpp
@@ -1,20 +1,17 @@
-#include "types.h"
#include "util/Log.h"
#include "Camera.h"
-#include "Color.h"
#include "Component.h"
+#include "types.h"
using namespace crepe;
-Camera::Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen,
- const vec2 & viewport_size, const double & zoom, const vec2 & offset)
+Camera::Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size,
+ const Data & data)
: Component(id),
- bg_color(bg_color),
- offset(offset),
screen(screen),
viewport_size(viewport_size),
- zoom(zoom) {
+ data(data) {
dbg_trace();
}
diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h
index 2d8fa48..54d9a73 100644
--- a/src/crepe/api/Camera.h
+++ b/src/crepe/api/Camera.h
@@ -14,23 +14,42 @@ namespace crepe {
* position, and zoom level. It controls what part of the game world is visible on the screen.
*/
class Camera : public Component {
+public:
+ struct Data {
+ /**
+ * \bg_color background color of the game
+ *
+ * This will make the background the same color as the given value.
+ */
+ const Color bg_color = Color::BLACK;
+
+ /**
+ * \zoom Zooming level of the game
+ *
+ * zoom = 1 --> no zoom.
+ * zoom < 1 --> zoom out
+ * zoom > 1 --> zoom in
+ */
+ double zoom = 1;
+
+ //! offset postion from the game object transform component
+ vec2 postion_offset;
+ };
public:
/**
* \brief Constructs a Camera with the specified ID and background color.
* \param id Unique identifier for the camera component.
- * \param bg_color Background color for the camera view.
+ * \param screen is the actual screen size in pixels
+ * \param viewport_size is the view of the world in game units
+ * \param data the camera component data
*/
- Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen,
- const vec2 & viewport_size, const double & zoom, const vec2 & offset = {0, 0});
+ Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size,
+ const Camera::Data & data);
~Camera(); // dbg_trace only
public:
- //! Background color of the camera view.
- const Color bg_color;
-
- //! offset postion from the game object transform component
- vec2 offset;
+ Camera::Data data;
//! screen the display size in pixels ( output resolution )
const ivec2 screen;
@@ -38,9 +57,6 @@ public:
//! viewport is the area of the world visible through the camera (in world units)
const vec2 viewport_size;
- //! Zoom level of the camera view.
- const double zoom;
-
public:
/**
* \brief Gets the maximum number of camera instances allowed.
diff --git a/src/crepe/api/CircleCollider.cpp b/src/crepe/api/CircleCollider.cpp
new file mode 100644
index 0000000..a4271e9
--- /dev/null
+++ b/src/crepe/api/CircleCollider.cpp
@@ -0,0 +1,8 @@
+#include "CircleCollider.h"
+
+using namespace crepe;
+
+CircleCollider::CircleCollider(game_object_id_t game_object_id, const vec2 & offset,
+ float radius)
+ : Collider(game_object_id, offset),
+ radius(radius) {}
diff --git a/src/crepe/api/CircleCollider.h b/src/crepe/api/CircleCollider.h
index e77a592..c7bf66e 100644
--- a/src/crepe/api/CircleCollider.h
+++ b/src/crepe/api/CircleCollider.h
@@ -1,14 +1,22 @@
#pragma once
+
+#include "Vector2.h"
+
#include "../Collider.h"
namespace crepe {
+/**
+ * \brief A class representing a circle-shaped collider.
+ *
+ * This class is used for collision detection with other colliders (e.g., BoxCollider).
+ */
class CircleCollider : public Collider {
public:
- CircleCollider(game_object_id_t game_object_id, int radius)
- : Collider(game_object_id),
- radius(radius) {}
- int radius;
+ CircleCollider(game_object_id_t game_object_id, const vec2 & offset, float radius);
+
+ //! Radius of the circle collider.
+ float radius;
};
} // namespace crepe
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index 225e9b9..6472270 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -1,8 +1,10 @@
#pragma once
+#include <string>
+
#include "../util/Log.h"
+
#include "types.h"
-#include <string>
namespace crepe {
@@ -13,20 +15,10 @@ namespace crepe {
* modified *before* execution is handed over from the game programmer to the engine (i.e. the
* main loop is started).
*/
-class Config final {
-public:
+struct Config final {
//! Retrieve handle to global Config instance
static Config & get_instance();
-private:
- Config() = default;
- ~Config() = default;
- Config(const Config &) = default;
- Config(Config &&) = default;
- Config & operator=(const Config &) = default;
- Config & operator=(Config &&) = default;
-
-public:
//! Logging-related settings
struct {
/**
@@ -66,10 +58,9 @@ public:
//! default window settings
struct {
- //TODO make this constexpr because this will never change
- ivec2 default_size = {1080, 720};
+ //! default screen size in pixels
+ ivec2 default_size = {1280, 720};
std::string window_title = "Jetpack joyride clone";
-
} window_settings;
//! Asset loading options
@@ -85,6 +76,12 @@ public:
*/
std::string root_pattern = ".crepe-root";
} asset;
+
+ //! Audio system settings
+ struct {
+ //! Max amount of simultanious voices
+ unsigned int voices = 32;
+ } audio;
};
} // namespace crepe
diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h
index b267e3e..f2f3daf 100644
--- a/src/crepe/api/Event.h
+++ b/src/crepe/api/Event.h
@@ -88,12 +88,30 @@ public:
//! Y-coordinate of the mouse position at the time of the event.
int mouse_y = 0;
+
+ // Movement since last event in x
+ int delta_x = 0;
+
+ // Movement since last event in y
+ int delta_y = 0;
};
/**
- * \brief Event triggered during a collision between objects.
+ * \brief Event triggered when the mouse is moved.
*/
-class CollisionEvent : public Event {};
+class MouseScrollEvent : public Event {
+public:
+ //! X-coordinate of the mouse position at the time of the event.
+ int mouse_x = 0;
+
+ //! Y-coordinate of the mouse position at the time of the event.
+ int mouse_y = 0;
+
+ //! scroll direction (-1 = down, 1 = up)
+ int scroll_direction = 0;
+ //! scroll amount in y axis (from and away from the person).
+ float scroll_delta = 0;
+};
/**
* \brief Event triggered when text is submitted, e.g., from a text input.
diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h
index ef659fd..7bb501b 100644
--- a/src/crepe/api/EventHandler.h
+++ b/src/crepe/api/EventHandler.h
@@ -8,12 +8,12 @@
namespace crepe {
/**
* \brief A type alias for an event handler function.
- *
- * The EventHandler is a std::function that takes an EventType reference and returns a boolean value
+ *
+ * The EventHandler is a std::function that takes an EventType reference and returns a boolean value
* indicating whether the event is handled.
- *
+ *
* \tparam EventType The type of event this handler will handle.
- *
+ *
* Returning \c false from an event handler results in the event being propogated to other listeners for the same event type, while returning \c true stops propogation altogether.
*/
template <typename EventType>
@@ -22,70 +22,70 @@ using EventHandler = std::function<bool(const EventType & e)>;
/**
* \class IEventHandlerWrapper
* \brief An abstract base class for event handler wrappers.
- *
+ *
* This class provides the interface for handling events. Derived classes must implement the
* `call()` method to process events
*/
class IEventHandlerWrapper {
public:
/**
- * \brief Virtual destructor for IEventHandlerWrapper.
- */
+ * \brief Virtual destructor for IEventHandlerWrapper.
+ */
virtual ~IEventHandlerWrapper() = default;
/**
- * \brief Executes the handler with the given event.
- *
- * This method calls the `call()` method of the derived class, passing the event to the handler.
- *
- * \param e The event to be processed.
- * \return A boolean value indicating whether the event is handled.
- */
+ * \brief Executes the handler with the given event.
+ *
+ * This method calls the `call()` method of the derived class, passing the event to the handler.
+ *
+ * \param e The event to be processed.
+ * \return A boolean value indicating whether the event is handled.
+ */
bool exec(const Event & e);
private:
/**
- * \brief The method responsible for handling the event.
- *
- * This method is implemented by derived classes to process the event.
- *
- * \param e The event to be processed.
- * \return A boolean value indicating whether the event is handled.
- */
+ * \brief The method responsible for handling the event.
+ *
+ * This method is implemented by derived classes to process the event.
+ *
+ * \param e The event to be processed.
+ * \return A boolean value indicating whether the event is handled.
+ */
virtual bool call(const Event & e) = 0;
};
/**
* \class EventHandlerWrapper
* \brief A wrapper for event handler functions.
- *
- * This class wraps an event handler function of a specific event type. It implements the
- * `call()` and `get_type()` methods to allow the handler to be executed and its type to be
+ *
+ * This class wraps an event handler function of a specific event type. It implements the
+ * `call()` and `get_type()` methods to allow the handler to be executed and its type to be
* queried.
- *
+ *
* \tparam EventType The type of event this handler will handle.
*/
template <typename EventType>
class EventHandlerWrapper : public IEventHandlerWrapper {
public:
/**
- * \brief Constructs an EventHandlerWrapper with a given handler.
- *
- * The constructor takes an event handler function and stores it in the wrapper.
- *
- * \param handler The event handler function.
- */
+ * \brief Constructs an EventHandlerWrapper with a given handler.
+ *
+ * The constructor takes an event handler function and stores it in the wrapper.
+ *
+ * \param handler The event handler function.
+ */
explicit EventHandlerWrapper(const EventHandler<EventType> & handler);
private:
/**
- * \brief Calls the stored event handler with the event.
- *
- * This method casts the event to the appropriate type and calls the handler.
- *
- * \param e The event to be handled.
- * \return A boolean value indicating whether the event is handled.
- */
+ * \brief Calls the stored event handler with the event.
+ *
+ * This method casts the event to the appropriate type and calls the handler.
+ *
+ * \param e The event to be handled.
+ * \return A boolean value indicating whether the event is handled.
+ */
bool call(const Event & e) override;
//! The event handler function.
EventHandler<EventType> handler;
diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h
index 4cd2bc0..ff80f49 100644
--- a/src/crepe/api/GameObject.h
+++ b/src/crepe/api/GameObject.h
@@ -10,7 +10,7 @@ class ComponentManager;
/**
* \brief Represents a GameObject
- *
+ *
* This class represents a GameObject. The GameObject class is only used as an interface for
* the game programmer. The actual implementation is done in the ComponentManager.
*/
@@ -19,7 +19,7 @@ private:
/**
* This constructor creates a new GameObject. It creates a new Transform and Metadata
* component and adds them to the ComponentManager.
- *
+ *
* \param component_manager Reference to component_manager
* \param id The id of the GameObject
* \param name The name of the GameObject
@@ -37,20 +37,20 @@ private:
public:
/**
* \brief Set the parent of this GameObject
- *
+ *
* This method sets the parent of this GameObject. It sets the parent in the Metadata
* component of this GameObject and adds this GameObject to the children list of the parent
* GameObject.
- *
+ *
* \param parent The parent GameObject
*/
void set_parent(const GameObject & parent);
/**
* \brief Add a component to the GameObject
- *
+ *
* This method adds a component to the GameObject. It forwards the arguments to the
* ComponentManager.
- *
+ *
* \tparam T The type of the component
* \tparam Args The types of the arguments
* \param args The arguments to create the component
diff --git a/src/crepe/api/GameObject.hpp b/src/crepe/api/GameObject.hpp
index 17b17d7..a6b45b0 100644
--- a/src/crepe/api/GameObject.hpp
+++ b/src/crepe/api/GameObject.hpp
@@ -1,6 +1,6 @@
#pragma once
-#include "../ComponentManager.h"
+#include "../manager/ComponentManager.h"
#include "GameObject.h"
diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h
index 328a4c2..180a0a6 100644
--- a/src/crepe/api/IKeyListener.h
+++ b/src/crepe/api/IKeyListener.h
@@ -1,8 +1,9 @@
#pragma once
+#include "../manager/EventManager.h"
+
#include "Event.h"
#include "EventHandler.h"
-#include "EventManager.h"
namespace crepe {
@@ -13,9 +14,9 @@ namespace crepe {
class IKeyListener {
public:
/**
- * \brief Constructs an IKeyListener with a specified channel.
- * \param channel The channel ID for event handling.
- */
+ * \brief Constructs an IKeyListener with a specified channel.
+ * \param channel The channel ID for event handling.
+ */
IKeyListener(event_channel_t channel = EventManager::CHANNEL_ALL);
virtual ~IKeyListener();
IKeyListener(const IKeyListener &) = delete;
@@ -24,17 +25,17 @@ public:
IKeyListener(IKeyListener &&) = delete;
/**
- * \brief Pure virtual function to handle key press events.
- * \param event The key press event to handle.
- * \return True if the event was handled, false otherwise.
- */
+ * \brief Pure virtual function to handle key press events.
+ * \param event The key press event to handle.
+ * \return True if the event was handled, false otherwise.
+ */
virtual bool on_key_pressed(const KeyPressEvent & event) = 0;
/**
- * \brief Pure virtual function to handle key release events.
- * \param event The key release event to handle.
- * \return True if the event was handled, false otherwise.
- */
+ * \brief Pure virtual function to handle key release events.
+ * \param event The key release event to handle.
+ * \return True if the event was handled, false otherwise.
+ */
virtual bool on_key_released(const KeyReleaseEvent & event) = 0;
private:
diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h
index 15e1619..e19897d 100644
--- a/src/crepe/api/IMouseListener.h
+++ b/src/crepe/api/IMouseListener.h
@@ -1,8 +1,9 @@
#pragma once
+#include "../manager/EventManager.h"
+
#include "Event.h"
#include "EventHandler.h"
-#include "EventManager.h"
namespace crepe {
@@ -13,9 +14,9 @@ namespace crepe {
class IMouseListener {
public:
/**
- * \brief Constructs an IMouseListener with a specified channel.
- * \param channel The channel ID for event handling.
- */
+ * \brief Constructs an IMouseListener with a specified channel.
+ * \param channel The channel ID for event handling.
+ */
IMouseListener(event_channel_t channel = EventManager::CHANNEL_ALL);
virtual ~IMouseListener();
IMouseListener & operator=(const IMouseListener &) = delete;
@@ -24,36 +25,36 @@ public:
IMouseListener(IMouseListener &&) = delete;
/**
- * \brief Move assignment operator (deleted).
- */
+ * \brief Move assignment operator (deleted).
+ */
IMouseListener & operator=(IMouseListener &&) = delete;
/**
- * \brief Handles a mouse click event.
- * \param event The mouse click event to handle.
- * \return True if the event was handled, false otherwise.
- */
+ * \brief Handles a mouse click event.
+ * \param event The mouse click event to handle.
+ * \return True if the event was handled, false otherwise.
+ */
virtual bool on_mouse_clicked(const MouseClickEvent & event) = 0;
/**
- * \brief Handles a mouse press event.
- * \param event The mouse press event to handle.
- * \return True if the event was handled, false otherwise.
- */
+ * \brief Handles a mouse press event.
+ * \param event The mouse press event to handle.
+ * \return True if the event was handled, false otherwise.
+ */
virtual bool on_mouse_pressed(const MousePressEvent & event) = 0;
/**
- * \brief Handles a mouse release event.
- * \param event The mouse release event to handle.
- * \return True if the event was handled, false otherwise.
- */
+ * \brief Handles a mouse release event.
+ * \param event The mouse release event to handle.
+ * \return True if the event was handled, false otherwise.
+ */
virtual bool on_mouse_released(const MouseReleaseEvent & event) = 0;
/**
- * \brief Handles a mouse move event.
- * \param event The mouse move event to handle.
- * \return True if the event was handled, false otherwise.
- */
+ * \brief Handles a mouse move event.
+ * \param event The mouse move event to handle.
+ * \return True if the event was handled, false otherwise.
+ */
virtual bool on_mouse_moved(const MouseMoveEvent & event) = 0;
private:
diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h
index 9e173e0..fcfc080 100644
--- a/src/crepe/api/KeyCodes.h
+++ b/src/crepe/api/KeyCodes.h
@@ -1,5 +1,5 @@
#pragma once
-
+namespace crepe {
//! Enumeration for mouse button inputs, including standard and extended buttons.
enum class MouseButton {
NONE = 0, //!< No mouse button input.
@@ -85,9 +85,9 @@ enum class Keycode {
PRINT_SCREEN = 283, //!< Print Screen key.
PAUSE = 284, //!< Pause key.
/**
- * \name Function keys (F1-F25).
- * \{
- */
+ * \name Function keys (F1-F25).
+ * \{
+ */
F1 = 290,
F2 = 291,
F3 = 292,
@@ -115,9 +115,9 @@ enum class Keycode {
F25 = 314,
/// \}
/**
- * \name Keypad digits and operators.
- * \{
- */
+ * \name Keypad digits and operators.
+ * \{
+ */
KP0 = 320,
KP1 = 321,
KP2 = 322,
@@ -137,9 +137,9 @@ enum class Keycode {
KP_EQUAL = 336,
/// \}
/**
- * \name Modifier keys.
- * \{
- */
+ * \name Modifier keys.
+ * \{
+ */
LEFT_SHIFT = 340,
LEFT_CONTROL = 341,
LEFT_ALT = 342,
@@ -151,3 +151,4 @@ enum class Keycode {
/// \}
MENU = 348, //!< Menu key.
};
+} // namespace crepe
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp
index 7edf4d1..1f0ba72 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -1,14 +1,15 @@
-#include "../facade/SDLContext.h"
-
+#include "../system/AISystem.h"
#include "../system/AnimatorSystem.h"
+#include "../system/AudioSystem.h"
#include "../system/CollisionSystem.h"
+#include "../system/InputSystem.h"
#include "../system/ParticleSystem.h"
#include "../system/PhysicsSystem.h"
#include "../system/RenderSystem.h"
#include "../system/ScriptSystem.h"
+#include "manager/EventManager.h"
#include "LoopManager.h"
-#include "LoopTimer.h"
using namespace crepe;
using namespace std;
@@ -20,11 +21,12 @@ LoopManager::LoopManager() {
this->load_system<PhysicsSystem>();
this->load_system<RenderSystem>();
this->load_system<ScriptSystem>();
+ this->load_system<InputSystem>();
+ this->load_system<AudioSystem>();
+ this->load_system<AISystem>();
}
-void LoopManager::process_input() {
- SDLContext::get_instance().handle_events(this->game_running);
-}
+void LoopManager::process_input() { this->get_system<InputSystem>().update(); }
void LoopManager::start() {
this->setup();
@@ -32,10 +34,19 @@ void LoopManager::start() {
}
void LoopManager::set_running(bool running) { this->game_running = running; }
-void LoopManager::fixed_update() {}
+void LoopManager::fixed_update() {
+ // TODO: retrieve EventManager from direct member after singleton refactor
+ EventManager & ev = this->mediator.event_manager;
+ ev.dispatch_events();
+ this->get_system<ScriptSystem>().update();
+ this->get_system<AISystem>().update();
+ this->get_system<PhysicsSystem>().update();
+ this->get_system<CollisionSystem>().update();
+ this->get_system<AudioSystem>().update();
+}
void LoopManager::loop() {
- LoopTimer & timer = LoopTimer::get_instance();
+ LoopTimer & timer = this->loop_timer;
timer.start();
while (game_running) {
@@ -55,15 +66,18 @@ void LoopManager::loop() {
}
void LoopManager::setup() {
+ LoopTimer & timer = this->loop_timer;
this->game_running = true;
- LoopTimer::get_instance().start();
- LoopTimer::get_instance().set_fps(200);
+ this->scene_manager.load_next_scene();
+ timer.start();
+ timer.set_fps(200);
}
void LoopManager::render() {
- if (this->game_running) {
- this->get_system<RenderSystem>().update();
- }
+ if (!this->game_running) return;
+
+ this->get_system<AnimatorSystem>().update();
+ this->get_system<RenderSystem>().update();
}
-void LoopManager::update() { LoopTimer & timer = LoopTimer::get_instance(); }
+void LoopManager::update() {}
diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h
index 13e6dac..1bafa56 100644
--- a/src/crepe/api/LoopManager.h
+++ b/src/crepe/api/LoopManager.h
@@ -2,9 +2,15 @@
#include <memory>
-#include "../ComponentManager.h"
+#include "../facade/SDLContext.h"
+#include "../manager/ComponentManager.h"
+#include "../manager/ResourceManager.h"
+#include "../manager/SaveManager.h"
+#include "../manager/SceneManager.h"
#include "../system/System.h"
-#include "api/SceneManager.h"
+
+#include "LoopTimer.h"
+#include "manager/ResourceManager.h"
namespace crepe {
@@ -85,10 +91,21 @@ private:
bool game_running = false;
private:
+ //! Global context
+ Mediator mediator;
+
//! Component manager instance
- ComponentManager component_manager{};
+ ComponentManager component_manager{mediator};
//! Scene manager instance
- SceneManager scene_manager{component_manager};
+ SceneManager scene_manager{mediator};
+ //! Resource manager instance
+ ResourceManager resource_manager{mediator};
+ //! Save manager instance
+ SaveManager save_manager{mediator};
+ //! SDLContext instance
+ SDLContext sdl_context{mediator};
+ //! LoopTimer instance
+ LoopTimer loop_timer{mediator};
private:
/**
diff --git a/src/crepe/api/LoopManager.hpp b/src/crepe/api/LoopManager.hpp
index 9cf470b..266758a 100644
--- a/src/crepe/api/LoopManager.hpp
+++ b/src/crepe/api/LoopManager.hpp
@@ -38,8 +38,11 @@ void LoopManager::load_system() {
static_assert(is_base_of<System, T>::value,
"load_system must recieve a derivative class of System");
- System * system = new T(this->component_manager);
- this->systems[typeid(T)] = unique_ptr<System>(system);
+ const type_info & type = typeid(T);
+ if (this->systems.contains(type))
+ throw runtime_error(format("LoopManager: {} is already initialized", type.name()));
+ System * system = new T(this->mediator);
+ this->systems[type] = unique_ptr<System>(system);
}
} // namespace crepe
diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp
index 15a0e3a..56e48d3 100644
--- a/src/crepe/api/LoopTimer.cpp
+++ b/src/crepe/api/LoopTimer.cpp
@@ -1,17 +1,16 @@
#include <chrono>
-#include "../facade/SDLContext.h"
#include "../util/Log.h"
+#include "facade/SDLContext.h"
+#include "manager/Manager.h"
#include "LoopTimer.h"
using namespace crepe;
-LoopTimer::LoopTimer() { dbg_trace(); }
-
-LoopTimer & LoopTimer::get_instance() {
- static LoopTimer instance;
- return instance;
+LoopTimer::LoopTimer(Mediator & mediator) : Manager(mediator) {
+ dbg_trace();
+ mediator.timer = *this;
}
void LoopTimer::start() {
@@ -67,7 +66,8 @@ void LoopTimer::enforce_frame_rate() {
= std::chrono::duration_cast<std::chrono::milliseconds>(this->frame_target_time
- frame_duration);
if (delay_time.count() > 0) {
- SDLContext::get_instance().delay(delay_time.count());
+ SDLContext & ctx = this->mediator.sdl_context;
+ ctx.delay(delay_time.count());
}
}
diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h
index 9393439..0a73a4c 100644
--- a/src/crepe/api/LoopTimer.h
+++ b/src/crepe/api/LoopTimer.h
@@ -1,19 +1,13 @@
#pragma once
+#include "manager/Manager.h"
#include <chrono>
namespace crepe {
-class LoopTimer {
+class LoopTimer : public Manager {
public:
/**
- * \brief Get the singleton instance of LoopTimer.
- *
- * \return A reference to the LoopTimer instance.
- */
- static LoopTimer & get_instance();
-
- /**
* \brief Get the current delta time for the current frame.
*
* \return Delta time in seconds since the last frame.
@@ -102,7 +96,7 @@ private:
*
* Private constructor for singleton pattern to restrict instantiation outside the class.
*/
- LoopTimer();
+ LoopTimer(Mediator & mediator);
/**
* \brief Update the timer to the current frame.
diff --git a/src/crepe/api/Metadata.h b/src/crepe/api/Metadata.h
index 235d42f..f404703 100644
--- a/src/crepe/api/Metadata.h
+++ b/src/crepe/api/Metadata.h
@@ -9,7 +9,7 @@ namespace crepe {
/**
* \brief Metadata component
- *
+ *
* This class represents the Metadata component. It stores the name, tag, parent and children
* of a GameObject.
*/
diff --git a/src/crepe/api/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp
index 576ca45..8213afb 100644
--- a/src/crepe/api/Rigidbody.cpp
+++ b/src/crepe/api/Rigidbody.cpp
@@ -10,6 +10,4 @@ void crepe::Rigidbody::add_force_linear(const vec2 & force) {
this->data.linear_velocity += force;
}
-void crepe::Rigidbody::add_force_angular(double force) {
- this->data.angular_velocity += force;
-}
+void crepe::Rigidbody::add_force_angular(float force) { this->data.angular_velocity += force; }
diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h
index 3b0588f..40c6bf1 100644
--- a/src/crepe/api/Rigidbody.h
+++ b/src/crepe/api/Rigidbody.h
@@ -1,5 +1,8 @@
#pragma once
+#include <cmath>
+#include <set>
+
#include "../Component.h"
#include "types.h"
@@ -8,7 +11,7 @@ namespace crepe {
/**
* \brief Rigidbody class
- *
+ *
* This class is used by the physics sytem and collision system. It configures how to system
* interact with the gameobject for movement and collisions.
*/
@@ -16,7 +19,7 @@ class Rigidbody : public Component {
public:
/**
* \brief BodyType enum
- *
+ *
* This enum provides three bodytypes the physics sytem and collision system use.
*/
enum class BodyType {
@@ -29,54 +32,118 @@ public:
};
/**
* \brief PhysicsConstraints to constrain movement
- *
+ *
* This struct configures the movement constraint for this object. If a constraint is enabled
* the systems will not move the object.
*/
struct PhysicsConstraints {
- //! X constraint
+ //! Prevent movement along X axis
bool x = false;
- //! Y constraint
+ //! Prevent movement along Y axis
bool y = false;
- //! rotation constraint
+ //! Prevent rotation
bool rotation = false;
};
public:
- /**
+ /**
* \brief struct for Rigidbody data
- *
+ *
* This struct holds the data for the Rigidbody.
*/
struct Data {
//! objects mass
- double mass = 0.0;
- //! gravtiy scale
- double gravity_scale = 0.0;
- //! Changes if physics apply
+ float mass = 0.0;
+ /**
+ * \brief Gravity scale factor.
+ *
+ * The `gravity_scale` controls how much gravity affects the object. It is a multiplier applied to the default
+ * gravity force, allowing for fine-grained control over how the object responds to gravity.
+ *
+ */
+ float gravity_scale = 0;
+
+ //! Defines the type of the physics body, which determines how the physics system interacts with the object.
BodyType body_type = BodyType::DYNAMIC;
- //! linear velocity of object
+
+ /**
+ * \name Linear (positional) motion
+ *
+ * These variables define the linear motion (movement along the position) of an object.
+ * The linear velocity is applied to the object's position in each update of the PhysicsSystem.
+ * The motion is affected by the object's linear velocity, its maximum velocity, and a coefficient
+ * that can scale the velocity over time.
+ *
+ * \{
+ */
+ //! Linear velocity of the object (speed and direction).
vec2 linear_velocity;
- //! maximum linear velocity of object
- vec2 max_linear_velocity;
- //! linear damping of object
- vec2 linear_damping;
- //! angular velocity of object
- double angular_velocity = 0.0;
- //! max angular velocity of object
- double max_angular_velocity = 0.0;
- //! angular damping of object
- double angular_damping = 0.0;
- //! movements constraints of object
+ //! Maximum linear velocity of the object. This limits the object's speed.
+ vec2 max_linear_velocity = {INFINITY, INFINITY};
+ //! Linear velocity coefficient. This scales the object's velocity for adjustment or damping.
+ vec2 linear_velocity_coefficient = {1, 1};
+ //! \}
+
+ /**
+ * \name Angular (rotational) motion
+ *
+ * These variables define the angular motion (rotation) of an object.
+ * The angular velocity determines how quickly the object rotates, while the maximum angular velocity
+ * sets a limit on the rotation speed. The angular velocity coefficient applies damping or scaling
+ * to the angular velocity, which can be used to simulate friction or other effects that slow down rotation.
+ *
+ * \{
+ */
+ //! Angular velocity of the object, representing the rate of rotation (in degrees).
+ float angular_velocity = 0;
+ //! Maximum angular velocity of the object. This limits the maximum rate of rotation.
+ float max_angular_velocity = INFINITY;
+ //! Angular velocity coefficient. This scales the object's angular velocity, typically used for damping.
+ float angular_velocity_coefficient = 1;
+ //! \}
+
+ /**
+ * \brief Movement constraints for an object.
+ *
+ * The `PhysicsConstraints` struct defines the constraints that restrict an object's movement
+ * in certain directions or prevent rotation. These constraints effect only the physics system
+ * to prevent the object from moving or rotating in specified ways.
+ *
+ */
PhysicsConstraints constraints;
- //! if gravity applies
- bool use_gravity = true;
- //! if object bounces
- bool bounce = false;
+
+ /**
+ * \brief Elasticity factor of the material (bounce factor).
+ *
+ * The `elasticity_coefficient` controls how much of the object's velocity is retained after a collision.
+ * It represents the material's ability to bounce or recover velocity upon impact. The coefficient is a value
+ * above 0.0.
+ *
+ */
+ float elastisity_coefficient = 0.0;
+
+ /**
+ * \brief Offset of all colliders relative to the object's transform position.
+ *
+ * The `offset` defines a positional shift applied to all colliders associated with the object, relative to the object's
+ * transform position. This allows for the colliders to be placed at a different position than the object's actual
+ * position, without modifying the object's transform itself.
+ *
+ */
+ vec2 offset;
+
+ /**
+ * \brief Defines the collision layers of a GameObject.
+ *
+ * The `collision_layers` specifies the layers that the GameObject will collide with.
+ * Each element represents a layer ID, and the GameObject will only detect
+ * collisions with other GameObjects that belong to these layers.
+ */
+ std::set<int> collision_layers;
};
public:
- /**
+ /**
* \param game_object_id id of the gameobject the rigibody is added to.
* \param data struct to configure the rigidbody.
*/
@@ -85,18 +152,27 @@ public:
Data data;
public:
- /**
+ /**
* \brief add a linear force to the Rigidbody.
- *
+ *
* \param force Vector2 that is added to the linear force.
*/
void add_force_linear(const vec2 & force);
- /**
+ /**
* \brief add a angular force to the Rigidbody.
- *
+ *
* \param force Vector2 that is added to the angular force.
*/
- void add_force_angular(double force);
+ void add_force_angular(float force);
+
+protected:
+ /**
+ * Ensures there is at most one Rigidbody component per entity.
+ * \return Always returns 1, indicating this constraint.
+ */
+ virtual int get_instances_max() const { return 1; }
+ //! ComponentManager instantiates all components
+ friend class ComponentManager;
};
} // namespace crepe
diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h
index f6fdb2a..ba9bb76 100644
--- a/src/crepe/api/Scene.h
+++ b/src/crepe/api/Scene.h
@@ -2,6 +2,7 @@
#include <string>
+#include "../manager/Mediator.h"
#include "../util/OptionalRef.h"
namespace crepe {
@@ -11,7 +12,7 @@ class ComponentManager;
/**
* \brief Represents a Scene
- *
+ *
* This class represents a Scene. The Scene class is only used as an interface for the game
* programmer.
*/
@@ -34,10 +35,13 @@ public:
*/
virtual std::string get_name() const = 0;
+ // TODO: Late references should ALWAYS be private! This is currently kept as-is so unit tests
+ // keep passing, but this reference should not be directly accessible by the user!!!
+
protected:
/**
* \name Late references
- *
+ *
* These references are set by SceneManager immediately after calling the constructor of Scene.
*
* \note Scene must have a constructor without arguments so the game programmer doesn't need to
@@ -46,8 +50,8 @@ protected:
*
* \{
*/
- //! Reference to the ComponentManager
- OptionalRef<ComponentManager> component_manager;
+ //! Mediator reference
+ OptionalRef<Mediator> mediator;
//! \}
};
diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp
index fcbe4c7..753a9e3 100644
--- a/src/crepe/api/Script.cpp
+++ b/src/crepe/api/Script.cpp
@@ -1,11 +1,16 @@
+#include <string>
+
+#include "../manager/SceneManager.h"
+
#include "Script.h"
using namespace crepe;
+using namespace std;
Script::~Script() {
- EventManager & evmgr = this->event_manager;
+ EventManager & mgr = this->mediator->event_manager;
for (auto id : this->listeners) {
- evmgr.unsubscribe(id);
+ mgr.unsubscribe(id);
}
}
@@ -13,3 +18,10 @@ template <>
void Script::subscribe(const EventHandler<CollisionEvent> & callback) {
this->subscribe_internal(callback, this->game_object_id);
}
+
+void Script::set_next_scene(const string & name) {
+ SceneManager & mgr = this->mediator->scene_manager;
+ mgr.set_next_scene(name);
+}
+
+SaveManager & Script::get_save_manager() const { return this->mediator->save_manager; }
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h
index a0870cb..668e5d1 100644
--- a/src/crepe/api/Script.h
+++ b/src/crepe/api/Script.h
@@ -2,11 +2,12 @@
#include <vector>
+#include "../manager/EventManager.h"
+#include "../manager/Mediator.h"
+#include "../system/CollisionSystem.h"
#include "../types.h"
#include "../util/OptionalRef.h"
-#include "EventManager.h"
-
namespace crepe {
class ScriptSystem;
@@ -19,7 +20,7 @@ class ComponentManager;
* This class is used as a base class for user-defined scripts that can be added to game
* objects using the \c BehaviorScript component.
*
- * \info Additional *events* (like Unity's OnDisable and OnEnable) should be implemented as
+ * \note Additional *events* (like Unity's OnDisable and OnEnable) should be implemented as
* member or lambda methods in derivative user script classes and registered in \c init().
*
* \warning Concrete scripts are allowed do create a custom constructor, but the utility
@@ -85,6 +86,25 @@ protected:
RefVector<T> get_components() const;
/**
+ * \copydoc ComponentManager::get_components_by_id
+ * \see ComponentManager::get_components_by_id
+ */
+ template <typename T>
+ RefVector<T> get_components_by_id(game_object_id_t id) const;
+ /**
+ * \copydoc ComponentManager::get_components_by_name
+ * \see ComponentManager::get_components_by_name
+ */
+ template <typename T>
+ RefVector<T> get_components_by_name(const std::string & name) const;
+ /**
+ * \copydoc ComponentManager::get_components_by_tag
+ * \see ComponentManager::get_components_by_tag
+ */
+ template <typename T>
+ RefVector<T> get_components_by_tag(const std::string & tag) const;
+
+ /**
* \brief Log a message using Log::logf
*
* \tparam Args Log::logf parameters
@@ -106,6 +126,15 @@ protected:
template <typename EventType>
void subscribe(const EventHandler<EventType> & callback);
+ /**
+ * \brief Set the next scene using SceneManager
+ * \see SceneManager::set_next_scene
+ */
+ void set_next_scene(const std::string & name);
+
+ //! Retrieve SaveManager reference
+ SaveManager & get_save_manager() const;
+
//! \}
private:
@@ -160,10 +189,8 @@ private:
game_object_id_t game_object_id;
//! Reference to parent component
OptionalRef<bool> active;
- //! Reference to component manager instance
- OptionalRef<ComponentManager> component_manager;
- //! Reference to event manager instance
- OptionalRef<EventManager> event_manager;
+ //! Mediator reference
+ OptionalRef<Mediator> mediator;
//! \}
private:
diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp
index a2463bf..225a51c 100644
--- a/src/crepe/api/Script.hpp
+++ b/src/crepe/api/Script.hpp
@@ -1,6 +1,6 @@
#pragma once
-#include "../ComponentManager.h"
+#include "../manager/ComponentManager.h"
#include "BehaviorScript.h"
#include "Script.h"
@@ -20,9 +20,7 @@ T & Script::get_component() const {
template <typename T>
RefVector<T> Script::get_components() const {
- ComponentManager & mgr = this->component_manager;
-
- return mgr.get_components_by_id<T>(this->game_object_id);
+ return this->get_components_by_id<T>(this->game_object_id);
}
template <typename... Args>
@@ -33,7 +31,7 @@ void Script::logf(Args &&... args) {
template <typename EventType>
void Script::subscribe_internal(const EventHandler<EventType> & callback,
event_channel_t channel) {
- EventManager & mgr = this->event_manager;
+ EventManager & mgr = this->mediator->event_manager;
subscription_t listener = mgr.subscribe<EventType>(
[this, callback](const EventType & data) -> bool {
bool & active = this->active;
@@ -54,4 +52,26 @@ void Script::subscribe(const EventHandler<EventType> & callback) {
this->subscribe_internal(callback, EventManager::CHANNEL_ALL);
}
+template <typename T>
+RefVector<T> Script::get_components_by_id(game_object_id_t id) const {
+ Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
+
+ return mgr.get_components_by_id<T>(id);
+}
+template <typename T>
+RefVector<T> Script::get_components_by_name(const std::string & name) const {
+ Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
+
+ return mgr.get_components_by_name<T>(name);
+}
+template <typename T>
+RefVector<T> Script::get_components_by_tag(const std::string & tag) const {
+ Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
+
+ return mgr.get_components_by_tag<T>(tag);
+}
+
} // namespace crepe
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
index 0a2ad4c..ba684ba 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -1,30 +1,21 @@
#include <cmath>
-#include <utility>
#include "../util/Log.h"
+#include "api/Asset.h"
#include "Component.h"
#include "Sprite.h"
-#include "Texture.h"
+#include "types.h"
using namespace std;
using namespace crepe;
-Sprite::Sprite(game_object_id_t id, Texture & image, const Color & color,
- const FlipSettings & flip, int sort_layer, int order_layer, int height)
+Sprite::Sprite(game_object_id_t id, const Asset & texture, const Sprite::Data & data)
: Component(id),
- color(color),
- flip(flip),
- sprite_image(std::move(image)),
- sorting_in_layer(sort_layer),
- order_in_layer(order_layer),
- height(height) {
+ source(texture),
+ data(data) {
dbg_trace();
-
- this->mask.w = sprite_image.get_size().x;
- this->mask.h = sprite_image.get_size().y;
- this->aspect_ratio = static_cast<double>(this->mask.w) / this->mask.h;
}
Sprite::~Sprite() { dbg_trace(); }
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
index a0e90a0..a2409c2 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -1,11 +1,10 @@
#pragma once
-#include <cstdint>
-
#include "../Component.h"
+#include "api/Asset.h"
#include "Color.h"
-#include "Texture.h"
+#include "types.h"
namespace crepe {
@@ -20,58 +19,68 @@ class AnimatorSystem;
* flip settings, and is managed in layers with defined sorting orders.
*/
class Sprite : public Component {
-
public:
+ //! settings to flip the image
struct FlipSettings {
+ //! horizantal flip
bool flip_x = false;
+ //! vertical flip
bool flip_y = false;
};
+ //! Sprite data that does not have to be set in the constructor
+ struct Data {
+ /**
+ * \brief Sprite tint (multiplied)
+ *
+ * The sprite texture's pixels are multiplied by this color before being displayed
+ * (including alpha channel for transparency).
+ */
+ Color color = Color::WHITE;
+
+ //! Flip settings for the sprite
+ FlipSettings flip;
+
+ //! Layer sorting level of the sprite
+ const int sorting_in_layer = 0;
+
+ //! Order within the sorting layer
+ const int order_in_layer = 0;
+
+ /**
+ * \brief width and height of the sprite in game units
+ *
+ * - if exclusively width is specified, the height is calculated using the texture's aspect
+ * ratio
+ * - if exclusively height is specified, the width is calculated using the texture's aspect
+ * ratio
+ * - if both are specified the texture is streched to fit the specified size
+ */
+ vec2 size = {0, 0};
+
+ //! independent sprite angle. rotating clockwise direction in degrees
+ float angle_offset = 0;
+
+ //! independent sprite scale multiplier
+ float scale_offset = 1;
+
+ //! independent sprite offset position
+ vec2 position_offset;
+ };
+
public:
- // TODO: Loek comment in github #27 will be looked another time
- // about shared_ptr Texture
/**
- * \brief Constructs a Sprite with specified parameters.
* \param game_id Unique identifier for the game object this sprite belongs to.
- * \param image Shared pointer to the texture for this sprite.
- * \param color Color tint applied to the sprite.
- * \param flip Flip settings for horizontal and vertical orientation.
- * \param order_layer decides the sorting in layer of the sprite.
- * \param sort_layer decides the order in layer of the sprite.
- * \param height the height of the image in game units
- */
- Sprite(game_object_id_t id, Texture & image, const Color & color,
- const FlipSettings & flip, int sort_layer, int order_layer, int height);
-
- /**
- * \brief Destroys the Sprite instance.
+ * \param texture asset of the image
+ * \param ctx all the sprite data
*/
+ Sprite(game_object_id_t id, const Asset & texture, const Data & data);
~Sprite();
//! Texture used for the sprite
- const Texture sprite_image;
-
- //! Color tint of the sprite
- Color color;
-
- //! Flip settings for the sprite
- FlipSettings flip;
+ const Asset source;
- //! Layer sorting level of the sprite
- const int sorting_in_layer;
- //! Order within the sorting layer
- const int order_in_layer;
-
- //! height in world units
- const int height;
-
- /**
- * \aspect_ratio ratio of the img so that scaling will not become weird
- *
- * cannot be const because if Animator component is addded then ratio becomes scuffed and
- * does it need to be calculated again in the Animator
- */
- double aspect_ratio;
+ Data data;
private:
//! Reads the mask of sprite
@@ -83,6 +92,14 @@ private:
//! Reads the all the variables plus the mask
friend class AnimatorSystem;
+ /**
+ * \aspect_ratio the ratio of the sprite image
+ *
+ * - this value will only be set by the \c Animator component for the ratio of the Animation
+ * - if \c Animator component is not added it will not use this ratio (because 0) and will use aspect_ratio of the Asset.
+ */
+ float aspect_ratio = 0;
+
struct Rect {
int w = 0;
int h = 0;
diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp
deleted file mode 100644
index c23b91e..0000000
--- a/src/crepe/api/Texture.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
-#include "../facade/SDLContext.h"
-#include "../util/Log.h"
-
-#include "Asset.h"
-#include "types.h"
-#include "Texture.h"
-
-using namespace crepe;
-using namespace std;
-
-Texture::Texture(const Asset & src) {
- dbg_trace();
- this->load(src);
-}
-
-Texture::~Texture() {
- dbg_trace();
- this->texture.reset();
-}
-
-Texture::Texture(Texture && other) noexcept : texture(std::move(other.texture)) {}
-
-Texture & Texture::operator=(Texture && other) noexcept {
- if (this != &other) {
- texture = std::move(other.texture);
- }
- return *this;
-}
-
-void Texture::load(const Asset & res) {
- SDLContext & ctx = SDLContext::get_instance();
- this->texture = ctx.texture_from_path(res.get_path());
-}
-
-ivec2 Texture::get_size() const {
- if (this->texture == nullptr) return {};
- return SDLContext::get_instance().get_size(*this);
-}
diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h
deleted file mode 100644
index 1817910..0000000
--- a/src/crepe/api/Texture.h
+++ /dev/null
@@ -1,69 +0,0 @@
-#pragma once
-
-// FIXME: this header can't be included because this is an API header, and SDL2 development
-// headers won't be bundled with crepe. Why is this facade in the API namespace?
-
-#include <SDL2/SDL_render.h>
-#include <functional>
-#include <memory>
-
-#include "Asset.h"
-#include "types.h"
-
-namespace crepe {
-
-class SDLContext;
-class Animator;
-
-/**
- * \class Texture
- * \brief Manages texture loading and properties.
- *
- * The Texture class is responsible for loading an image from a source and providing access to
- * its dimensions. Textures can be used for rendering.
- */
-class Texture {
-
-public:
- /**
- * \brief Constructs a Texture from an Asset resource.
- * \param src Asset with texture data to load.
- */
- Texture(const Asset & src);
-
- /**
- * \brief Destroys the Texture instance, freeing associated resources.
- */
- ~Texture();
- // FIXME: this constructor shouldn't be necessary because this class doesn't manage memory
-
- Texture(Texture && other) noexcept;
- Texture & operator=(Texture && other) noexcept;
- Texture(const Texture &) = delete;
- Texture & operator=(const Texture &) = delete;
-
- /**
- * \brief Gets the width and height of the texture.
- * \return Width and height of the texture in pixels.
- */
- ivec2 get_size() const;
-
-private:
- /**
- * \brief Loads the texture from an Asset resource.
- * \param res Unique pointer to an Asset resource to load the texture from.
- */
- void load(const Asset & res);
-
-private:
- //! The texture of the class from the library
- std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> texture;
-
- //! Grants SDLContext access to private members.
- friend class SDLContext;
-
- //! Grants Animator access to private members.
- friend class Animator;
-};
-
-} // namespace crepe
diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h
index 6243a93..7ee6d65 100644
--- a/src/crepe/api/Transform.h
+++ b/src/crepe/api/Transform.h
@@ -7,7 +7,7 @@ namespace crepe {
/**
* \brief Transform component
- *
+ *
* This class represents the Transform component. It stores the position, rotation and scale of
* a GameObject.
*/
@@ -15,10 +15,10 @@ class Transform : public Component {
public:
//! Translation (shift)
vec2 position = {0, 0};
- //! Rotation, in degrees
- double rotation = 0;
+ //! Rotation, in degrees clockwise
+ float rotation = 0;
//! Multiplication factor
- double scale = 0;
+ float scale = 0;
protected:
/**
diff --git a/src/crepe/api/UIObject.cpp b/src/crepe/api/UIObject.cpp
new file mode 100644
index 0000000..d239b89
--- /dev/null
+++ b/src/crepe/api/UIObject.cpp
@@ -0,0 +1,8 @@
+#include "UIObject.h"
+
+using namespace crepe;
+
+UIObject::UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset)
+ : Component(id),
+ dimensions(dimensions),
+ offset(offset) {}
diff --git a/src/crepe/api/UIObject.h b/src/crepe/api/UIObject.h
new file mode 100644
index 0000000..f7f4fba
--- /dev/null
+++ b/src/crepe/api/UIObject.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include "../Component.h"
+
+namespace crepe {
+
+/**
+ * \brief Represents a UI object in the game, derived from the Component class.
+ */
+class UIObject : public Component {
+public:
+ /**
+ * \brief Constructs a UiObject with the specified game object ID.
+ * \param id The unique ID of the game object associated with this UI object.
+ * \param dimensions width and height of the UIObject
+ * \param offset Offset relative to the GameObject Transform
+ */
+ UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset);
+ //! Width and height of the UIObject
+ vec2 dimensions;
+ //! Position offset relative to this GameObjects Transform
+ vec2 offset;
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h
index c278c87..bf9d124 100644
--- a/src/crepe/api/Vector2.h
+++ b/src/crepe/api/Vector2.h
@@ -66,6 +66,30 @@ struct Vector2 {
//! Checks if this vector is not equal to another vector.
bool operator!=(const Vector2<T> & other) const;
+
+ //! Truncates the vector to a maximum length.
+ void truncate(T max);
+
+ //! Normalizes the vector (resulting in vector with a length of 1).
+ void normalize();
+
+ //! Returns the length of the vector.
+ T length() const;
+
+ //! Returns the squared length of the vector.
+ T length_squared() const;
+
+ //! Returns the dot product (inwendig product) of this vector and another vector.
+ T dot(const Vector2<T> & other) const;
+
+ //! Returns the distance between this vector and another vector.
+ T distance(const Vector2<T> & other) const;
+
+ //! Returns the squared distance between this vector and another vector.
+ T distance_squared(const Vector2<T> & other) const;
+
+ //! Returns the perpendicular vector to this vector.
+ Vector2 perpendicular() const;
};
} // namespace crepe
diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp
index cad15f8..ff53cb0 100644
--- a/src/crepe/api/Vector2.hpp
+++ b/src/crepe/api/Vector2.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include <cmath>
+
#include "Vector2.h"
namespace crepe {
@@ -115,4 +117,50 @@ bool Vector2<T>::operator!=(const Vector2<T> & other) const {
return !(*this == other);
}
+template <class T>
+void Vector2<T>::truncate(T max) {
+ if (length() > max) {
+ normalize();
+ *this *= max;
+ }
+}
+
+template <class T>
+void Vector2<T>::normalize() {
+ T len = length();
+ if (len > 0) {
+ *this /= len;
+ }
+}
+
+template <class T>
+T Vector2<T>::length() const {
+ return std::sqrt(x * x + y * y);
+}
+
+template <class T>
+T Vector2<T>::length_squared() const {
+ return x * x + y * y;
+}
+
+template <class T>
+T Vector2<T>::dot(const Vector2<T> & other) const {
+ return x * other.x + y * other.y;
+}
+
+template <class T>
+T Vector2<T>::distance(const Vector2<T> & other) const {
+ return (*this - other).length();
+}
+
+template <class T>
+T Vector2<T>::distance_squared(const Vector2<T> & other) const {
+ return (*this - other).length_squared();
+}
+
+template <class T>
+Vector2<T> Vector2<T>::perpendicular() const {
+ return {-y, x};
+}
+
} // namespace crepe
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt
index 4cc53bc..0598e16 100644
--- a/src/crepe/facade/CMakeLists.txt
+++ b/src/crepe/facade/CMakeLists.txt
@@ -1,5 +1,6 @@
target_sources(crepe PUBLIC
Sound.cpp
+ Texture.cpp
SoundContext.cpp
SDLContext.cpp
DB.cpp
@@ -7,6 +8,7 @@ target_sources(crepe PUBLIC
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Sound.h
+ Texture.h
SoundContext.h
SDLContext.h
DB.h
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 8d8a87d..1dbd6a5 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -6,35 +6,30 @@
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_video.h>
+#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <functional>
-#include <iostream>
#include <memory>
#include <stdexcept>
#include "../api/Camera.h"
+#include "../api/Color.h"
#include "../api/Config.h"
#include "../api/Sprite.h"
-#include "../api/Texture.h"
#include "../util/Log.h"
+#include "manager/Mediator.h"
#include "SDLContext.h"
-#include "api/Color.h"
+#include "Texture.h"
#include "types.h"
using namespace crepe;
using namespace std;
-SDLContext & SDLContext::get_instance() {
- static SDLContext instance;
- return instance;
-}
-
-SDLContext::SDLContext() {
+SDLContext::SDLContext(Mediator & mediator) {
dbg_trace();
-
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError()));
}
@@ -62,6 +57,8 @@ SDLContext::SDLContext() {
if (!(IMG_Init(img_flags) & img_flags)) {
throw runtime_error("SDLContext: SDL_image could not initialize!");
}
+
+ mediator.sdl_context = *this;
}
SDLContext::~SDLContext() {
@@ -76,75 +73,218 @@ SDLContext::~SDLContext() {
IMG_Quit();
SDL_Quit();
}
-void SDLContext::handle_events(bool & running) {
- //TODO: wouter i need events
- /*
- SDL_Event event;
- SDL_PollEvent(&event);
- switch (event.type) {
- case SDL_QUIT:
- running = false;
- break;
- case SDL_KEYDOWN:
- triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym)));
- break;
- case SDL_MOUSEBUTTONDOWN:
- int x, y;
- SDL_GetMouseState(&x, &y);
- triggerEvent(MousePressedEvent(x, y));
- break;
+
+Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) {
+ static const std::array<Keycode, SDL_NUM_SCANCODES> LOOKUP_TABLE = [] {
+ std::array<Keycode, SDL_NUM_SCANCODES> table{};
+ table.fill(Keycode::NONE);
+
+ table[SDL_SCANCODE_SPACE] = Keycode::SPACE;
+ table[SDL_SCANCODE_APOSTROPHE] = Keycode::APOSTROPHE;
+ table[SDL_SCANCODE_COMMA] = Keycode::COMMA;
+ table[SDL_SCANCODE_MINUS] = Keycode::MINUS;
+ table[SDL_SCANCODE_PERIOD] = Keycode::PERIOD;
+ table[SDL_SCANCODE_SLASH] = Keycode::SLASH;
+ table[SDL_SCANCODE_0] = Keycode::D0;
+ table[SDL_SCANCODE_1] = Keycode::D1;
+ table[SDL_SCANCODE_2] = Keycode::D2;
+ table[SDL_SCANCODE_3] = Keycode::D3;
+ table[SDL_SCANCODE_4] = Keycode::D4;
+ table[SDL_SCANCODE_5] = Keycode::D5;
+ table[SDL_SCANCODE_6] = Keycode::D6;
+ table[SDL_SCANCODE_7] = Keycode::D7;
+ table[SDL_SCANCODE_8] = Keycode::D8;
+ table[SDL_SCANCODE_9] = Keycode::D9;
+ table[SDL_SCANCODE_SEMICOLON] = Keycode::SEMICOLON;
+ table[SDL_SCANCODE_EQUALS] = Keycode::EQUAL;
+ table[SDL_SCANCODE_A] = Keycode::A;
+ table[SDL_SCANCODE_B] = Keycode::B;
+ table[SDL_SCANCODE_C] = Keycode::C;
+ table[SDL_SCANCODE_D] = Keycode::D;
+ table[SDL_SCANCODE_E] = Keycode::E;
+ table[SDL_SCANCODE_F] = Keycode::F;
+ table[SDL_SCANCODE_G] = Keycode::G;
+ table[SDL_SCANCODE_H] = Keycode::H;
+ table[SDL_SCANCODE_I] = Keycode::I;
+ table[SDL_SCANCODE_J] = Keycode::J;
+ table[SDL_SCANCODE_K] = Keycode::K;
+ table[SDL_SCANCODE_L] = Keycode::L;
+ table[SDL_SCANCODE_M] = Keycode::M;
+ table[SDL_SCANCODE_N] = Keycode::N;
+ table[SDL_SCANCODE_O] = Keycode::O;
+ table[SDL_SCANCODE_P] = Keycode::P;
+ table[SDL_SCANCODE_Q] = Keycode::Q;
+ table[SDL_SCANCODE_R] = Keycode::R;
+ table[SDL_SCANCODE_S] = Keycode::S;
+ table[SDL_SCANCODE_T] = Keycode::T;
+ table[SDL_SCANCODE_U] = Keycode::U;
+ table[SDL_SCANCODE_V] = Keycode::V;
+ table[SDL_SCANCODE_W] = Keycode::W;
+ table[SDL_SCANCODE_X] = Keycode::X;
+ table[SDL_SCANCODE_Y] = Keycode::Y;
+ table[SDL_SCANCODE_Z] = Keycode::Z;
+ table[SDL_SCANCODE_LEFTBRACKET] = Keycode::LEFT_BRACKET;
+ table[SDL_SCANCODE_BACKSLASH] = Keycode::BACKSLASH;
+ table[SDL_SCANCODE_RIGHTBRACKET] = Keycode::RIGHT_BRACKET;
+ table[SDL_SCANCODE_GRAVE] = Keycode::GRAVE_ACCENT;
+ table[SDL_SCANCODE_ESCAPE] = Keycode::ESCAPE;
+ table[SDL_SCANCODE_RETURN] = Keycode::ENTER;
+ table[SDL_SCANCODE_TAB] = Keycode::TAB;
+ table[SDL_SCANCODE_BACKSPACE] = Keycode::BACKSPACE;
+ table[SDL_SCANCODE_INSERT] = Keycode::INSERT;
+ table[SDL_SCANCODE_DELETE] = Keycode::DELETE;
+ table[SDL_SCANCODE_RIGHT] = Keycode::RIGHT;
+ table[SDL_SCANCODE_LEFT] = Keycode::LEFT;
+ table[SDL_SCANCODE_DOWN] = Keycode::DOWN;
+ table[SDL_SCANCODE_UP] = Keycode::UP;
+ table[SDL_SCANCODE_PAGEUP] = Keycode::PAGE_UP;
+ table[SDL_SCANCODE_PAGEDOWN] = Keycode::PAGE_DOWN;
+ table[SDL_SCANCODE_HOME] = Keycode::HOME;
+ table[SDL_SCANCODE_END] = Keycode::END;
+ table[SDL_SCANCODE_CAPSLOCK] = Keycode::CAPS_LOCK;
+ table[SDL_SCANCODE_SCROLLLOCK] = Keycode::SCROLL_LOCK;
+ table[SDL_SCANCODE_NUMLOCKCLEAR] = Keycode::NUM_LOCK;
+ table[SDL_SCANCODE_PRINTSCREEN] = Keycode::PRINT_SCREEN;
+ table[SDL_SCANCODE_PAUSE] = Keycode::PAUSE;
+ table[SDL_SCANCODE_F1] = Keycode::F1;
+ table[SDL_SCANCODE_F2] = Keycode::F2;
+ table[SDL_SCANCODE_F3] = Keycode::F3;
+ table[SDL_SCANCODE_F4] = Keycode::F4;
+ table[SDL_SCANCODE_F5] = Keycode::F5;
+ table[SDL_SCANCODE_F6] = Keycode::F6;
+ table[SDL_SCANCODE_F7] = Keycode::F7;
+ table[SDL_SCANCODE_F8] = Keycode::F8;
+ table[SDL_SCANCODE_F9] = Keycode::F9;
+ table[SDL_SCANCODE_F10] = Keycode::F10;
+ table[SDL_SCANCODE_F11] = Keycode::F11;
+ table[SDL_SCANCODE_F12] = Keycode::F12;
+ table[SDL_SCANCODE_KP_0] = Keycode::KP0;
+ table[SDL_SCANCODE_KP_1] = Keycode::KP1;
+ table[SDL_SCANCODE_KP_2] = Keycode::KP2;
+ table[SDL_SCANCODE_KP_3] = Keycode::KP3;
+ table[SDL_SCANCODE_KP_4] = Keycode::KP4;
+ table[SDL_SCANCODE_KP_5] = Keycode::KP5;
+ table[SDL_SCANCODE_KP_6] = Keycode::KP6;
+ table[SDL_SCANCODE_KP_7] = Keycode::KP7;
+ table[SDL_SCANCODE_KP_8] = Keycode::KP8;
+ table[SDL_SCANCODE_KP_9] = Keycode::KP9;
+ table[SDL_SCANCODE_LSHIFT] = Keycode::LEFT_SHIFT;
+ table[SDL_SCANCODE_LCTRL] = Keycode::LEFT_CONTROL;
+ table[SDL_SCANCODE_LALT] = Keycode::LEFT_ALT;
+ table[SDL_SCANCODE_LGUI] = Keycode::LEFT_SUPER;
+ table[SDL_SCANCODE_RSHIFT] = Keycode::RIGHT_SHIFT;
+ table[SDL_SCANCODE_RCTRL] = Keycode::RIGHT_CONTROL;
+ table[SDL_SCANCODE_RALT] = Keycode::RIGHT_ALT;
+ table[SDL_SCANCODE_RGUI] = Keycode::RIGHT_SUPER;
+ table[SDL_SCANCODE_MENU] = Keycode::MENU;
+
+ return table;
+ }();
+
+ if (sdl_key < 0 || sdl_key >= SDL_NUM_SCANCODES) {
+ return Keycode::NONE;
}
- */
+
+ return LOOKUP_TABLE[sdl_key];
}
-void SDLContext::clear_screen() {
- SDL_SetRenderDrawColor(this->game_renderer.get(), 0, 0, 0, 255);
- SDL_RenderClear(this->game_renderer.get());
+MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) {
+ static const std::array<MouseButton, 5> MOUSE_BUTTON_LOOKUP_TABLE = [] {
+ std::array<MouseButton, 5> table{};
+ table.fill(MouseButton::NONE);
+
+ table[SDL_BUTTON_LEFT] = MouseButton::LEFT_MOUSE;
+ table[SDL_BUTTON_RIGHT] = MouseButton::RIGHT_MOUSE;
+ table[SDL_BUTTON_MIDDLE] = MouseButton::MIDDLE_MOUSE;
+ table[SDL_BUTTON_X1] = MouseButton::X1_MOUSE;
+ table[SDL_BUTTON_X2] = MouseButton::X2_MOUSE;
+
+ return table;
+ }();
+
+ if (sdl_button >= MOUSE_BUTTON_LOOKUP_TABLE.size()) {
+ // Return NONE for invalid or unmapped button
+ return MouseButton::NONE;
+ }
+
+ return MOUSE_BUTTON_LOOKUP_TABLE[sdl_button];
}
-void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); }
-
-SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const {
- return SDL_Rect{
- .x = sprite.mask.x,
- .y = sprite.mask.y,
- .w = sprite.mask.w,
- .h = sprite.mask.h,
- };
+
+void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); }
+void SDLContext::present_screen() {
+ SDL_SetRenderDrawColor(this->game_renderer.get(), 0, 0, 0, 255);
+ SDL_RenderFillRectF(this->game_renderer.get(), &black_bars[0]);
+ SDL_RenderFillRectF(this->game_renderer.get(), &black_bars[1]);
+ SDL_RenderPresent(this->game_renderer.get());
}
-SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam,
- const vec2 & cam_pos, const double & img_scale) const {
- int width = sprite.height * sprite.aspect_ratio;
- int height = sprite.height;
+SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const {
+
+ const Sprite::Data & data = ctx.sprite.data;
+
+ float aspect_ratio
+ = (ctx.sprite.aspect_ratio == 0) ? ctx.texture.get_ratio() : ctx.sprite.aspect_ratio;
+
+ vec2 size = data.size;
+ if (data.size.x == 0 && data.size.y != 0) {
+ size.x = data.size.y * aspect_ratio;
+ }
+ if (data.size.y == 0 && data.size.x != 0) {
+ size.y = data.size.x / aspect_ratio;
+ }
+
+ const CameraValues & cam = ctx.cam;
- width *= img_scale * cam.zoom;
- height *= img_scale * cam.zoom;
+ size *= cam.render_scale * ctx.img_scale * data.scale_offset;
- return SDL_Rect{
- .x = static_cast<int>((pos.x - cam_pos.x + (cam.viewport_size.x / 2) - width / 2)),
- .y = static_cast<int>((pos.y - cam_pos.y + (cam.viewport_size.y / 2) - height / 2)),
- .w = width,
- .h = height,
+ vec2 screen_pos
+ = (ctx.pos + data.position_offset - cam.cam_pos + (cam.zoomed_viewport) / 2)
+ * cam.render_scale
+ - size / 2 + cam.bar_size;
+
+ return SDL_FRect{
+ .x = screen_pos.x,
+ .y = screen_pos.y,
+ .w = size.x,
+ .h = size.y,
};
}
void SDLContext::draw(const RenderContext & ctx) {
-
+ const Sprite::Data & data = ctx.sprite.data;
SDL_RendererFlip render_flip
- = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * ctx.sprite.flip.flip_x)
- | (SDL_FLIP_VERTICAL * ctx.sprite.flip.flip_y));
+ = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x)
+ | (SDL_FLIP_VERTICAL * data.flip.flip_y));
+
+ SDL_Rect srcrect;
+ SDL_Rect * srcrect_ptr = NULL;
+ if (ctx.sprite.mask.w != 0 || ctx.sprite.mask.h != 0) {
+ srcrect.w = ctx.sprite.mask.w;
+ srcrect.h = ctx.sprite.mask.h;
+ srcrect.x = ctx.sprite.mask.x;
+ srcrect.y = ctx.sprite.mask.y;
+ srcrect_ptr = &srcrect;
+ }
+
+ SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{
+ .sprite = ctx.sprite,
+ .texture = ctx.texture,
+ .cam = ctx.cam,
+ .pos = ctx.pos,
+ .img_scale = ctx.scale,
+ });
- SDL_Rect srcrect = this->get_src_rect(ctx.sprite);
- SDL_Rect dstrect
- = this->get_dst_rect(ctx.sprite, ctx.pos, ctx.cam, ctx.cam_pos, ctx.scale);
+ double angle = ctx.angle + data.angle_offset;
- this->set_color_texture(ctx.sprite.sprite_image, ctx.sprite.color);
- SDL_RenderCopyEx(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(),
- &srcrect, &dstrect, ctx.angle, NULL, render_flip);
+ this->set_color_texture(ctx.texture, ctx.sprite.data.color);
+ SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.get_img(), srcrect_ptr, &dstrect,
+ angle, NULL, render_flip);
}
-void SDLContext::set_camera(const Camera & cam) {
+SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) {
+ const Camera::Data & cam_data = cam.data;
+ CameraValues ret_cam;
// resize window
int w, h;
SDL_GetWindowSize(this->game_window.get(), &w, &h);
@@ -152,42 +292,52 @@ void SDLContext::set_camera(const Camera & cam) {
SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y);
}
- double screen_aspect = cam.screen.x / cam.screen.y;
- double viewport_aspect = cam.viewport_size.x / cam.viewport_size.y;
+ vec2 & zoomed_viewport = ret_cam.zoomed_viewport;
+ vec2 & bar_size = ret_cam.bar_size;
+ vec2 & render_scale = ret_cam.render_scale;
+
+ zoomed_viewport = cam.viewport_size * cam_data.zoom;
+ float screen_aspect = static_cast<float>(cam.screen.x) / cam.screen.y;
+ float viewport_aspect = zoomed_viewport.x / zoomed_viewport.y;
- SDL_Rect view;
// calculate black bars
if (screen_aspect > viewport_aspect) {
- // letterboxing
- view.h = cam.screen.y / cam.zoom;
- view.w = cam.screen.y * viewport_aspect;
- view.x = (cam.screen.x - view.w) / 2;
- view.y = 0;
- } else {
// pillarboxing
- view.h = cam.screen.x / viewport_aspect;
- view.w = cam.screen.x / cam.zoom;
- view.x = 0;
- view.y = (cam.screen.y - view.h) / 2;
+ float scale = cam.screen.y / zoomed_viewport.y;
+ float adj_width = zoomed_viewport.x * scale;
+ float bar_width = (cam.screen.x - adj_width) / 2;
+ this->black_bars[0] = {0, 0, bar_width, (float) cam.screen.y};
+ this->black_bars[1] = {(cam.screen.x - bar_width), 0, bar_width, (float) cam.screen.y};
+
+ bar_size = {bar_width, 0};
+ render_scale.x = render_scale.y = scale;
+ } else {
+ // letterboxing
+ float scale = cam.screen.x / (cam.viewport_size.x * cam_data.zoom);
+ float adj_height = cam.viewport_size.y * scale;
+ float bar_height = (cam.screen.y - adj_height) / 2;
+ this->black_bars[0] = {0, 0, (float) cam.screen.x, bar_height};
+ this->black_bars[1]
+ = {0, (cam.screen.y - bar_height), (float) cam.screen.x, bar_height};
+
+ bar_size = {0, bar_height};
+ render_scale.x = render_scale.y = scale;
}
- // set drawing area
- SDL_RenderSetViewport(this->game_renderer.get(), &view);
-
- SDL_RenderSetLogicalSize(this->game_renderer.get(), static_cast<int>(cam.viewport_size.x),
- static_cast<int>(cam.viewport_size.y));
- // set bg color
- SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g,
- cam.bg_color.b, cam.bg_color.a);
+ SDL_SetRenderDrawColor(this->game_renderer.get(), cam_data.bg_color.r, cam_data.bg_color.g,
+ cam_data.bg_color.b, cam_data.bg_color.a);
SDL_Rect bg = {
.x = 0,
.y = 0,
- .w = static_cast<int>(cam.viewport_size.x),
- .h = static_cast<int>(cam.viewport_size.y),
+ .w = cam.screen.x,
+ .h = cam.screen.y,
};
+
// fill bg color
SDL_RenderFillRect(this->game_renderer.get(), &bg);
+
+ return ret_cam;
}
uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); }
@@ -216,16 +366,75 @@ SDLContext::texture_from_path(const std::string & path) {
return img_texture;
}
-
-ivec2 SDLContext::get_size(const Texture & ctx){
+ivec2 SDLContext::get_size(const Texture & ctx) {
ivec2 size;
- SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y);
+ SDL_QueryTexture(ctx.get_img(), NULL, NULL, &size.x, &size.y);
return size;
}
void SDLContext::delay(int ms) const { SDL_Delay(ms); }
+std::vector<SDLContext::EventData> SDLContext::get_events() {
+ std::vector<SDLContext::EventData> event_list;
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ switch (event.type) {
+ case SDL_QUIT:
+ event_list.push_back(EventData{
+ .event_type = SDLContext::EventType::SHUTDOWN,
+ });
+ break;
+ case SDL_KEYDOWN:
+ event_list.push_back(EventData{
+ .event_type = SDLContext::EventType::KEYDOWN,
+ .key = sdl_to_keycode(event.key.keysym.scancode),
+ .key_repeat = (event.key.repeat != 0),
+ });
+ break;
+ case SDL_KEYUP:
+ event_list.push_back(EventData{
+ .event_type = SDLContext::EventType::KEYUP,
+ .key = sdl_to_keycode(event.key.keysym.scancode),
+ });
+ break;
+ case SDL_MOUSEBUTTONDOWN:
+ event_list.push_back(EventData{
+ .event_type = SDLContext::EventType::MOUSEDOWN,
+ .mouse_button = sdl_to_mousebutton(event.button.button),
+ .mouse_position = {event.button.x, event.button.y},
+ });
+ break;
+ case SDL_MOUSEBUTTONUP: {
+ int x, y;
+ SDL_GetMouseState(&x, &y);
+ event_list.push_back(EventData{
+ .event_type = SDLContext::EventType::MOUSEUP,
+ .mouse_button = sdl_to_mousebutton(event.button.button),
+ .mouse_position = {event.button.x, event.button.y},
+ });
+ } break;
+
+ case SDL_MOUSEMOTION: {
+ event_list.push_back(
+ EventData{.event_type = SDLContext::EventType::MOUSEMOVE,
+ .mouse_position = {event.motion.x, event.motion.y},
+ .rel_mouse_move = {event.motion.xrel, event.motion.yrel}});
+ } break;
+
+ case SDL_MOUSEWHEEL: {
+ event_list.push_back(EventData{
+ .event_type = SDLContext::EventType::MOUSEWHEEL,
+ .mouse_position = {event.motion.x, event.motion.y},
+ // TODO: why is this needed?
+ .scroll_direction = event.wheel.y < 0 ? -1 : 1,
+ .scroll_delta = event.wheel.preciseY,
+ });
+ } break;
+ }
+ }
+ return event_list;
+}
void SDLContext::set_color_texture(const Texture & texture, const Color & color) {
- SDL_SetTextureColorMod(texture.texture.get(), color.r, color.g, color.b);
- SDL_SetTextureAlphaMod(texture.texture.get(), color.a);
+ SDL_SetTextureColorMod(texture.get_img(), color.r, color.g, color.b);
+ SDL_SetTextureAlphaMod(texture.get_img(), color.a);
}
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index e49ca78..46b779f 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -1,5 +1,6 @@
#pragma once
+#include <SDL2/SDL.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
@@ -9,63 +10,145 @@
#include <memory>
#include <string>
-#include "../api/Camera.h"
-#include "../api/Sprite.h"
-
+#include "api/Camera.h"
#include "api/Color.h"
-#include "api/Texture.h"
+#include "api/KeyCodes.h"
+#include "api/Sprite.h"
+#include "api/Transform.h"
+
#include "types.h"
namespace crepe {
-// TODO: SDL_Keycode is defined in a header not distributed with crepe, which means this
-// typedef is unusable when crepe is packaged. Wouter will fix this later.
-typedef SDL_Keycode CREPE_KEYCODES;
+class Texture;
+class Mediator;
/**
* \class SDLContext
* \brief Facade for the SDL library
- *
+ *
* SDLContext is a singleton that handles the SDL window and renderer, provides methods for
* event handling, and rendering to the screen. It is never used directly by the user
*/
class SDLContext {
public:
+ //! data that the camera component cannot hold
+ struct CameraValues {
+
+ //! zoomed in viewport in game_units
+ vec2 zoomed_viewport;
+
+ /**
+ * \brief scaling factor
+ *
+ * depending on the black bars type will the scaling be different.
+ * - letterboxing --> scaling on the y-as
+ * - pillarboxing --> scaling on the x-as
+ */
+ vec2 render_scale;
+
+ /**
+ * \brief size of calculated black bars
+ *
+ * depending on the black bars type will the size be different
+ * - lettorboxing --> {0, bar_height}
+ * - pillarboxing --> {bar_width , 0}
+ */
+ vec2 bar_size;
+
+ //! Calculated camera position
+ vec2 cam_pos;
+ };
+
+ //! rendering data needed to render on screen
struct RenderContext {
const Sprite & sprite;
- const Camera & cam;
- const vec2 & cam_pos;
+ const Texture & texture;
+ const CameraValues & cam;
const vec2 & pos;
const double & angle;
const double & scale;
};
public:
- /**
- * \brief Gets the singleton instance of SDLContext.
- * \return Reference to the SDLContext instance.
- */
- static SDLContext & get_instance();
+ //! EventType enum for passing eventType
+ enum EventType {
+ NONE = 0,
+ MOUSEDOWN,
+ MOUSEUP,
+ MOUSEMOVE,
+ MOUSEWHEEL,
+ KEYUP,
+ KEYDOWN,
+ SHUTDOWN,
+
+ };
+ //! EventData struct for passing event data from facade
+ struct EventData {
+ SDLContext::EventType event_type = SDLContext::EventType::NONE;
+ Keycode key = Keycode::NONE;
+ bool key_repeat = false;
+ MouseButton mouse_button = MouseButton::NONE;
+ ivec2 mouse_position = {-1, -1};
+ int scroll_direction = -1;
+ float scroll_delta = INFINITY;
+ ivec2 rel_mouse_move = {-1, -1};
+ };
+public:
SDLContext(const SDLContext &) = delete;
SDLContext(SDLContext &&) = delete;
SDLContext & operator=(const SDLContext &) = delete;
SDLContext & operator=(SDLContext &&) = delete;
-private:
- //! will only use handle_events
- friend class LoopManager;
+public:
/**
- * \brief Handles SDL events such as window close and input.
- * \param running Reference to a boolean flag that controls the main loop.
+ * \brief Constructs an SDLContext instance.
+ * Initializes SDL, creates a window and renderer.
*/
- void handle_events(bool & running);
+ SDLContext(Mediator & mediator);
-private:
- //! Will only use get_ticks
- friend class AnimatorSystem;
- //! Will only use delay
- friend class LoopTimer;
+ /**
+ * \brief Destroys the SDLContext instance.
+ * Cleans up SDL resources, including the window and renderer.
+ */
+ ~SDLContext();
+
+public:
+ /**
+ * \brief Retrieves a list of all events from the SDL context.
+ *
+ * This method retrieves all the events from the SDL context that are currently
+ * available. It is primarily used by the InputSystem to process various
+ * input events such as mouse clicks, mouse movements, and keyboard presses.
+ *
+ * \return Events that occurred since last call to `get_events()`
+ */
+ std::vector<SDLContext::EventData> get_events();
+
+ /**
+ * \brief Converts an SDL key code to the custom Keycode type.
+ *
+ * This method maps an SDL key code to the corresponding `Keycode` enum value,
+ * which is used internally by the system to identify the keys.
+ *
+ * \param sdl_key The SDL key code to convert.
+ * \return The corresponding `Keycode` value or `Keycode::NONE` if the key is unrecognized.
+ */
+ Keycode sdl_to_keycode(SDL_Keycode sdl_key);
+
+ /**
+ * \brief Converts an SDL mouse button code to the custom MouseButton type.
+ *
+ * This method maps an SDL mouse button code to the corresponding `MouseButton`
+ * enum value, which is used internally by the system to identify mouse buttons.
+ *
+ * \param sdl_button The SDL mouse button code to convert.
+ * \return The corresponding `MouseButton` value or `MouseButton::NONE` if the key is unrecognized
+ */
+ MouseButton sdl_to_mousebutton(Uint8 sdl_button);
+
+public:
/**
* \brief Gets the current SDL ticks since the program started.
* \return Current ticks in milliseconds as a constant uint64_t.
@@ -81,23 +164,7 @@ private:
*/
void delay(int ms) const;
-private:
- /**
- * \brief Constructs an SDLContext instance.
- * Initializes SDL, creates a window and renderer.
- */
- SDLContext();
-
- /**
- * \brief Destroys the SDLContext instance.
- * Cleans up SDL resources, including the window and renderer.
- */
- ~SDLContext();
-
-private:
- //! Will use the funtions: texture_from_path, get_width,get_height.
- friend class Texture;
-
+public:
/**
* \brief Loads a texture from a file path.
* \param path Path to the image file.
@@ -108,17 +175,14 @@ private:
/**
* \brief Gets the size of a texture.
* \param texture Reference to the Texture object.
- * \return Width and height of the texture as an integer.
+ * \return Width and height of the texture as an integer in pixels.
*/
ivec2 get_size(const Texture & ctx);
-private:
- //! Will use draw,clear_screen, present_screen, camera.
- friend class RenderSystem;
-
+public:
/**
* \brief Draws a sprite to the screen using the specified transform and camera.
- * \param RenderCtx Reference to rendering data to draw
+ * \param RenderContext Reference to rendering data to draw
*/
void draw(const RenderContext & ctx);
@@ -131,34 +195,31 @@ private:
/**
* \brief sets the background of the camera (will be adjusted in future PR)
* \param camera Reference to the Camera object.
+ * \return camera data the component cannot store
*/
- void set_camera(const Camera & camera);
+ CameraValues set_camera(const Camera & camera);
-private:
- /**
- * \brief calculates the sqaure size of the image
- *
- * \param sprite Reference to the sprite to calculate the rectangle
- * \return sdl rectangle to draw a src image
- */
- SDL_Rect get_src_rect(const Sprite & sprite) const;
+public:
+ //! the data needed to construct a sdl dst rectangle
+ struct DestinationRectangleData {
+ const Sprite & sprite;
+ const Texture & texture;
+ const CameraValues & cam;
+ const vec2 & pos;
+ const double & img_scale;
+ };
/**
* \brief calculates the sqaure size of the image for destination
*
- * \param sprite Reference to the sprite to calculate rectangle
- * \param pos the pos in world units
- * \param cam the camera of the current scene
- * \param cam_pos the current postion of the camera
- * \param img_scale the image multiplier for increasing img size
+ * \param data needed to calculate a destination rectangle
* \return sdl rectangle to draw a dst image to draw on the screen
*/
- SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam,
- const vec2 & cam_pos, const double & img_scale) const;
+ SDL_FRect get_dst_rect(const DestinationRectangleData & data) const;
/**
* \brief Set an additional color value multiplied into render copy operations.
*
- * \param texture the given texture to adjust
+ * \param texture the given texture to adjust
* \param color the color data for the texture
*/
void set_color_texture(const Texture & texture, const Color & color);
@@ -169,6 +230,9 @@ private:
//! renderer for the crepe engine
std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer *)>> game_renderer;
+
+ //! black bars rectangle to draw
+ SDL_FRect black_bars[2] = {};
};
} // namespace crepe
diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp
index 4d3abf5..97e455e 100644
--- a/src/crepe/facade/Sound.cpp
+++ b/src/crepe/facade/Sound.cpp
@@ -1,59 +1,13 @@
+#include "../api/Asset.h"
#include "../util/Log.h"
#include "Sound.h"
-#include "SoundContext.h"
using namespace crepe;
using namespace std;
-Sound::Sound(unique_ptr<Asset> res) {
+Sound::Sound(const Asset & src, Mediator & mediator) : Resource(src, mediator) {
+ this->sample.load(src.get_path().c_str());
dbg_trace();
- this->load(std::move(res));
-}
-
-Sound::Sound(const char * src) {
- dbg_trace();
- this->load(make_unique<Asset>(src));
-}
-
-void Sound::load(unique_ptr<Asset> res) { this->sample.load(res->get_path().c_str()); }
-
-void Sound::play() {
- SoundContext & ctx = SoundContext::get_instance();
- if (ctx.engine.getPause(this->handle)) {
- // resume if paused
- ctx.engine.setPause(this->handle, false);
- } else {
- // or start new sound
- this->handle = ctx.engine.play(this->sample, this->volume);
- ctx.engine.setLooping(this->handle, this->looping);
- }
-}
-
-void Sound::pause() {
- SoundContext & ctx = SoundContext::get_instance();
- if (ctx.engine.getPause(this->handle)) return;
- ctx.engine.setPause(this->handle, true);
-}
-
-void Sound::rewind() {
- SoundContext & ctx = SoundContext::get_instance();
- if (!ctx.engine.isValidVoiceHandle(this->handle)) return;
- ctx.engine.seek(this->handle, 0);
-}
-
-void Sound::set_volume(float volume) {
- this->volume = volume;
-
- SoundContext & ctx = SoundContext::get_instance();
- if (!ctx.engine.isValidVoiceHandle(this->handle)) return;
- ctx.engine.setVolume(this->handle, this->volume);
-}
-
-void Sound::set_looping(bool looping) {
- this->looping = looping;
-
- SoundContext & ctx = SoundContext::get_instance();
- if (!ctx.engine.isValidVoiceHandle(this->handle)) return;
- ctx.engine.setLooping(this->handle, this->looping);
}
+Sound::~Sound() { dbg_trace(); }
diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h
index 4c68f32..4a5d692 100644
--- a/src/crepe/facade/Sound.h
+++ b/src/crepe/facade/Sound.h
@@ -1,84 +1,31 @@
#pragma once
-#include <memory>
#include <soloud/soloud.h>
#include <soloud/soloud_wav.h>
-#include "../api/Asset.h"
+#include "../Resource.h"
namespace crepe {
+class SoundContext;
+class Mediator;
+
/**
* \brief Sound resource facade
*
- * This class is a wrapper around a \c SoLoud::Wav instance, which holds a
- * single sample. It is part of the sound facade.
+ * This class is a wrapper around a \c SoLoud::Wav instance, which holds a single sample. It is
+ * part of the sound facade.
*/
-class Sound {
-public:
- /**
- * \brief Pause this sample
- *
- * Pauses this sound if it is playing, or does nothing if it is already paused. The playhead
- * position is saved, such that calling \c play() after this function makes the sound resume.
- */
- void pause();
- /**
- * \brief Play this sample
- *
- * Resume playback if this sound is paused, or start from the beginning of the sample.
- *
- * \note This class only saves a reference to the most recent 'voice' of this sound. Calling
- * \c play() while the sound is already playing causes multiple instances of the sample to
- * play simultaniously. The sample started last is the one that is controlled afterwards.
- */
- void play();
- /**
- * \brief Reset playhead position
- *
- * Resets the playhead position so that calling \c play() after this function makes it play
- * from the start of the sample. If the sound is not paused before calling this function,
- * this function will stop playback.
- */
- void rewind();
- /**
- * \brief Set playback volume / gain
- *
- * \param volume Volume (0 = muted, 1 = full volume)
- */
- void set_volume(float volume);
- /**
- * \brief Get playback volume / gain
- *
- * \return Volume
- */
- float get_volume() const { return this->volume; }
- /**
- * \brief Set looping behavior for this sample
- *
- * \param looping Looping behavior (false = one-shot, true = loop)
- */
- void set_looping(bool looping);
- /**
- * \brief Get looping behavior
- *
- * \return true if looping, false if one-shot
- */
- bool get_looping() const { return this->looping; }
-
+class Sound : public Resource {
public:
- Sound(const char * src);
- Sound(std::unique_ptr<Asset> res);
-
-private:
- void load(std::unique_ptr<Asset> res);
+ Sound(const Asset & src, Mediator & mediator);
+ ~Sound(); // dbg_trace
private:
+ //! Deserialized resource (soloud)
SoLoud::Wav sample;
- SoLoud::handle handle;
-
- float volume = 1.0f;
- bool looping = false;
+ //! SoundContext uses \c sample
+ friend class SoundContext;
};
} // namespace crepe
diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp
index deb2b62..b1f8cb3 100644
--- a/src/crepe/facade/SoundContext.cpp
+++ b/src/crepe/facade/SoundContext.cpp
@@ -4,17 +4,33 @@
using namespace crepe;
-SoundContext & SoundContext::get_instance() {
- static SoundContext instance;
- return instance;
-}
-
SoundContext::SoundContext() {
dbg_trace();
- engine.init();
+ this->engine.init();
+ this->engine.setMaxActiveVoiceCount(this->config.audio.voices);
}
SoundContext::~SoundContext() {
dbg_trace();
- engine.deinit();
+ this->engine.deinit();
+}
+
+SoundHandle SoundContext::play(Sound & resource) {
+ SoLoud::handle real_handle = this->engine.play(resource.sample, 1.0f);
+ SoundHandle handle = this->next_handle;
+ this->registry[handle] = real_handle;
+ this->next_handle++;
+ return handle;
+}
+
+void SoundContext::stop(const SoundHandle & handle) {
+ this->engine.stop(this->registry[handle]);
+}
+
+void SoundContext::set_volume(const SoundHandle & handle, float volume) {
+ this->engine.setVolume(this->registry[handle], volume);
+}
+
+void SoundContext::set_loop(const SoundHandle & handle, bool loop) {
+ this->engine.setLooping(this->registry[handle], loop);
}
diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h
index d703c16..d986c59 100644
--- a/src/crepe/facade/SoundContext.h
+++ b/src/crepe/facade/SoundContext.h
@@ -2,30 +2,80 @@
#include <soloud/soloud.h>
+#include "../api/Config.h"
+
#include "Sound.h"
+#include "SoundHandle.h"
namespace crepe {
/**
* \brief Sound engine facade
*
- * This class is a wrapper around a \c SoLoud::Soloud instance, which provides
- * the methods for playing \c Sound instances. It is part of the sound facade.
+ * This class is a wrapper around a \c SoLoud::Soloud instance, which provides the methods for
+ * playing \c Sound instances. It is part of the sound facade.
*/
class SoundContext {
-private:
- // singleton
+public:
SoundContext();
virtual ~SoundContext();
+
SoundContext(const SoundContext &) = delete;
SoundContext(SoundContext &&) = delete;
SoundContext & operator=(const SoundContext &) = delete;
SoundContext & operator=(SoundContext &&) = delete;
+ /**
+ * \brief Play a sample
+ *
+ * Plays a Sound from the beginning of the sample and returns a handle to control it later.
+ *
+ * \param resource Sound instance to play
+ *
+ * \returns Handle to control this voice
+ */
+ virtual SoundHandle play(Sound & resource);
+ /**
+ * \brief Stop a voice immediately if it is still playing
+ *
+ * \note This function does nothing if the handle is invalid or if the sound is already
+ * stopped / finished playing.
+ *
+ * \param handle Voice handle returned by SoundContext::play
+ */
+ virtual void stop(const SoundHandle & handle);
+ /**
+ * \brief Change the volume of a voice
+ *
+ * \note This function does nothing if the handle is invalid or if the sound is already
+ * stopped / finished playing.
+ *
+ * \param handle Voice handle returned by SoundContext::play
+ * \param volume New gain value (0=silent, 1=default)
+ */
+ virtual void set_volume(const SoundHandle & handle, float volume);
+ /**
+ * \brief Set the looping behavior of a voice
+ *
+ * \note This function does nothing if the handle is invalid or if the sound is already
+ * stopped / finished playing.
+ *
+ * \param handle Voice handle returned by SoundContext::play
+ * \param loop Looping behavior (false=oneshot, true=loop)
+ */
+ virtual void set_loop(const SoundHandle & handle, bool loop);
+
private:
- static SoundContext & get_instance();
+ //! Abstracted class
SoLoud::Soloud engine;
- friend class Sound;
+
+ //! Config reference
+ Config & config = Config::get_instance();
+
+ //! Sound handle registry
+ std::unordered_map<SoundHandle, SoLoud::handle> registry;
+ //! Unique handle counter
+ SoundHandle next_handle = 0;
};
} // namespace crepe
diff --git a/src/crepe/facade/SoundHandle.h b/src/crepe/facade/SoundHandle.h
new file mode 100644
index 0000000..b7925fc
--- /dev/null
+++ b/src/crepe/facade/SoundHandle.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <cstddef>
+
+namespace crepe {
+
+/**
+ * \brief Voice handle returned by
+ */
+typedef size_t SoundHandle;
+
+} // namespace crepe
diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp
new file mode 100644
index 0000000..b63403d
--- /dev/null
+++ b/src/crepe/facade/Texture.cpp
@@ -0,0 +1,28 @@
+#include "../util/Log.h"
+#include "facade/SDLContext.h"
+#include "manager/Mediator.h"
+
+#include "Resource.h"
+#include "Texture.h"
+#include "types.h"
+
+using namespace crepe;
+using namespace std;
+
+Texture::Texture(const Asset & src, Mediator & mediator) : Resource(src, mediator) {
+ dbg_trace();
+ SDLContext & ctx = mediator.sdl_context;
+ this->texture = ctx.texture_from_path(src.get_path());
+ this->size = ctx.get_size(*this);
+ this->aspect_ratio = static_cast<float>(this->size.x) / this->size.y;
+}
+
+Texture::~Texture() {
+ dbg_trace();
+ this->texture.reset();
+}
+
+const ivec2 & Texture::get_size() const noexcept { return this->size; }
+const float & Texture::get_ratio() const noexcept { return this->aspect_ratio; }
+
+SDL_Texture * Texture::get_img() const noexcept { return this->texture.get(); }
diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h
new file mode 100644
index 0000000..cdacac4
--- /dev/null
+++ b/src/crepe/facade/Texture.h
@@ -0,0 +1,69 @@
+#pragma once
+
+#include <SDL2/SDL_render.h>
+#include <memory>
+
+#include "../Resource.h"
+
+#include "types.h"
+
+namespace crepe {
+
+class Mediator;
+class Asset;
+
+/**
+ * \class Texture
+ * \brief Manages texture loading and properties.
+ *
+ * The Texture class is responsible for loading an image from a source and providing access to
+ * its dimensions. Textures can be used for rendering.
+ */
+class Texture : public Resource {
+
+public:
+ /**
+ * \brief Constructs a Texture from an Asset resource.
+ * \param src Asset with texture data to load.
+ * \param mediator use the SDLContext reference to load the image
+ */
+ Texture(const Asset & src, Mediator & mediator);
+
+ /**
+ * \brief Destroys the Texture instance
+ */
+ ~Texture();
+
+ /**
+ * \brief get width and height of image in pixels
+ * \return pixel size width and height
+ *
+ */
+ const ivec2 & get_size() const noexcept;
+
+ /**
+ * \brief aspect_ratio of image
+ * \return ratio
+ *
+ */
+ const float & get_ratio() const noexcept;
+
+ /**
+ * \brief get the image texture
+ * \return SDL_Texture
+ *
+ */
+ SDL_Texture * get_img() const noexcept;
+
+private:
+ //! The texture of the class from the library
+ std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> texture;
+
+ // texture size in pixel
+ ivec2 size;
+
+ //! ratio of image
+ float aspect_ratio;
+};
+
+} // namespace crepe
diff --git a/src/crepe/manager/CMakeLists.txt b/src/crepe/manager/CMakeLists.txt
new file mode 100644
index 0000000..480c8ee
--- /dev/null
+++ b/src/crepe/manager/CMakeLists.txt
@@ -0,0 +1,23 @@
+target_sources(crepe PUBLIC
+ ComponentManager.cpp
+ EventManager.cpp
+ Manager.cpp
+ SaveManager.cpp
+ SceneManager.cpp
+ ResourceManager.cpp
+)
+
+target_sources(crepe PUBLIC FILE_SET HEADERS FILES
+ ComponentManager.h
+ ComponentManager.hpp
+ EventManager.h
+ EventManager.hpp
+ Manager.h
+ Mediator.h
+ SaveManager.h
+ SceneManager.h
+ SceneManager.hpp
+ ResourceManager.h
+ ResourceManager.hpp
+)
+
diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/manager/ComponentManager.cpp
index 5b73009..df30d27 100644
--- a/src/crepe/ComponentManager.cpp
+++ b/src/crepe/manager/ComponentManager.cpp
@@ -1,13 +1,17 @@
-#include "api/GameObject.h"
-#include "util/Log.h"
+#include "../api/GameObject.h"
+#include "../api/Metadata.h"
+#include "../types.h"
+#include "../util/Log.h"
#include "ComponentManager.h"
-#include "types.h"
using namespace crepe;
using namespace std;
-ComponentManager::ComponentManager() { dbg_trace(); }
+ComponentManager::ComponentManager(Mediator & mediator) : Manager(mediator) {
+ mediator.component_manager = *this;
+ dbg_trace();
+}
ComponentManager::~ComponentManager() { dbg_trace(); }
void ComponentManager::delete_all_components_of_id(game_object_id_t id) {
@@ -58,3 +62,13 @@ GameObject ComponentManager::new_object(const string & name, const string & tag,
void ComponentManager::set_persistent(game_object_id_t id, bool persistent) {
this->persistent[id] = persistent;
}
+
+set<game_object_id_t> ComponentManager::get_objects_by_name(const string & name) const {
+ return this->get_objects_by_predicate<Metadata>(
+ [name](const Metadata & data) { return data.name == name; });
+}
+
+set<game_object_id_t> ComponentManager::get_objects_by_tag(const string & tag) const {
+ return this->get_objects_by_predicate<Metadata>(
+ [tag](const Metadata & data) { return data.tag == tag; });
+}
diff --git a/src/crepe/ComponentManager.h b/src/crepe/manager/ComponentManager.h
index 480124f..19a8e81 100644
--- a/src/crepe/ComponentManager.h
+++ b/src/crepe/manager/ComponentManager.h
@@ -1,12 +1,15 @@
#pragma once
#include <memory>
+#include <set>
#include <typeindex>
#include <unordered_map>
#include <vector>
-#include "Component.h"
-#include "types.h"
+#include "../Component.h"
+#include "../types.h"
+
+#include "Manager.h"
namespace crepe {
@@ -14,10 +17,10 @@ class GameObject;
/**
* \brief Manages all components
- *
+ *
* This class manages all components. It provides methods to add, delete and get components.
*/
-class ComponentManager {
+class ComponentManager : public Manager {
// TODO: This relation should be removed! I (loek) believe that the scene manager should
// create/destroy components because the GameObject's are stored in concrete Scene classes,
// which will in turn call GameObject's destructor, which will in turn call
@@ -26,7 +29,7 @@ class ComponentManager {
friend class SceneManager;
public:
- ComponentManager(); // dbg_trace
+ ComponentManager(Mediator & mediator);
~ComponentManager(); // dbg_trace
/**
@@ -54,10 +57,10 @@ protected:
friend class GameObject;
/**
* \brief Add a component to the ComponentManager
- *
+ *
* This method adds a component to the ComponentManager. The component is created with the
* given arguments and added to the ComponentManager.
- *
+ *
* \tparam T The type of the component
* \tparam Args The types of the arguments
* \param id The id of the GameObject this component belongs to
@@ -68,9 +71,9 @@ protected:
T & add_component(game_object_id_t id, Args &&... args);
/**
* \brief Delete all components of a specific type and id
- *
+ *
* This method deletes all components of a specific type and id.
- *
+ *
* \tparam T The type of the component
* \param id The id of the GameObject this component belongs to
*/
@@ -78,24 +81,24 @@ protected:
void delete_components_by_id(game_object_id_t id);
/**
* \brief Delete all components of a specific type
- *
+ *
* This method deletes all components of a specific type.
- *
+ *
* \tparam T The type of the component
*/
template <typename T>
void delete_components();
/**
* \brief Delete all components of a specific id
- *
+ *
* This method deletes all components of a specific id.
- *
+ *
* \param id The id of the GameObject this component belongs to
*/
void delete_all_components_of_id(game_object_id_t id);
/**
* \brief Delete all components
- *
+ *
* This method deletes all components.
*/
void delete_all_components();
@@ -113,9 +116,9 @@ protected:
public:
/**
* \brief Get all components of a specific type and id
- *
+ *
* This method gets all components of a specific type and id.
- *
+ *
* \tparam T The type of the component
* \param id The id of the GameObject this component belongs to
* \return A vector of all components of the specific type and id
@@ -124,19 +127,88 @@ public:
RefVector<T> get_components_by_id(game_object_id_t id) const;
/**
* \brief Get all components of a specific type
- *
+ *
* This method gets all components of a specific type.
- *
+ *
* \tparam T The type of the component
* \return A vector of all components of the specific type
*/
template <typename T>
RefVector<T> get_components_by_type() const;
+ /**
+ * \brief Get all components of a specific type on a GameObject with name \c name
+ *
+ * \tparam T The type of the component
+ * \param name Metadata::name for the same game_object_id as the returned components
+ * \return Components matching criteria
+ */
+ template <typename T>
+ RefVector<T> get_components_by_name(const std::string & name) const;
+ /**
+ * \brief Get all components of a specific type on a GameObject with tag \c tag
+ *
+ * \tparam T The type of the component
+ * \param name Metadata::tag for the same game_object_id as the returned components
+ * \return Components matching criteria
+ */
+ template <typename T>
+ RefVector<T> get_components_by_tag(const std::string & tag) const;
private:
/**
+ * \brief Get object IDs by predicate function
+ *
+ * This function calls the predicate function \c pred for all components matching type \c T,
+ * and adds their parent game_object_id to a \c std::set if the predicate returns true.
+ *
+ * \tparam T The type of the component to check the predicate against
+ * \param pred Predicate function
+ *
+ * \note The predicate function may be called for multiple components with the same \c
+ * game_object_id. In this case, the ID is added if *any* call returns \c true.
+ *
+ * \returns game_object_id for all components where the predicate returned true
+ */
+ template <typename T>
+ std::set<game_object_id_t>
+ get_objects_by_predicate(const std::function<bool(const T &)> & pred) const;
+
+ /**
+ * \brief Get components of type \c T for multiple game object IDs
+ *
+ * \tparam T The type of the components to return
+ * \param ids The object IDs
+ *
+ * \return All components matching type \c T and one of the IDs in \c ids
+ */
+ template <typename T>
+ RefVector<T> get_components_by_ids(const std::set<game_object_id_t> & ids) const;
+
+ /**
+ * \brief Get object IDs for objects with name \c name
+ *
+ * \param name Object name to match
+ * \returns Object IDs where Metadata::name is equal to \c name
+ */
+ std::set<game_object_id_t> get_objects_by_name(const std::string & name) const;
+ /**
+ * \brief Get object IDs for objects with tag \c tag
+ *
+ * \param tag Object tag to match
+ * \returns Object IDs where Metadata::tag is equal to \c tag
+ */
+ std::set<game_object_id_t> get_objects_by_tag(const std::string & tag) const;
+
+private:
+ //! By Component \c std::type_index (readability helper type)
+ template <typename T>
+ using by_type = std::unordered_map<std::type_index, T>;
+ //! By \c game_object_id index (readability helper type)
+ template <typename T>
+ using by_id_index = std::vector<T>;
+ /**
* \brief The components
- *
+ *
* This unordered_map stores all components. The key is the type of the component and the
* value is a vector of vectors of unique pointers to the components.
*
@@ -144,8 +216,7 @@ private:
* The first vector is for the ids of the GameObjects and the second vector is for the
* components (because a GameObject might have multiple components).
*/
- std::unordered_map<std::type_index, std::vector<std::vector<std::unique_ptr<Component>>>>
- components;
+ by_type<by_id_index<std::vector<std::unique_ptr<Component>>>> components;
//! Persistent flag for each GameObject
std::unordered_map<game_object_id_t, bool> persistent;
diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/manager/ComponentManager.hpp
index ffb38ec..9e70865 100644
--- a/src/crepe/ComponentManager.hpp
+++ b/src/crepe/manager/ComponentManager.hpp
@@ -95,32 +95,25 @@ template <typename T>
RefVector<T> ComponentManager::get_components_by_id(game_object_id_t id) const {
using namespace std;
- // Determine the type of T (this is used as the key of the unordered_map<>)
- type_index type = typeid(T);
-
- // Create an empty vector<>
- RefVector<T> component_vector;
-
- if (this->components.find(type) == this->components.end()) return component_vector;
-
- // Get the correct vector<>
- const vector<vector<unique_ptr<Component>>> & component_array = this->components.at(type);
-
- // Make sure that the id (that we are looking for) is within the boundaries of the vector<>
- if (id >= component_array.size()) return component_vector;
-
- // Loop trough the whole vector<>
- for (const unique_ptr<Component> & component_ptr : component_array[id]) {
- // Cast the unique_ptr to a raw pointer
- T * casted_component = static_cast<T *>(component_ptr.get());
-
- if (casted_component == nullptr) continue;
+ static_assert(is_base_of<Component, T>::value,
+ "get_components_by_id must recieve a derivative class of Component");
- // Add the dereferenced raw pointer to the vector<>
- component_vector.push_back(*casted_component);
+ type_index type = typeid(T);
+ if (!this->components.contains(type)) return {};
+
+ const by_id_index<vector<unique_ptr<Component>>> & components_by_id
+ = this->components.at(type);
+ if (id >= components_by_id.size()) return {};
+
+ RefVector<T> out = {};
+ const vector<unique_ptr<Component>> & components = components_by_id.at(id);
+ for (auto & component_ptr : components) {
+ if (component_ptr == nullptr) continue;
+ Component & component = *component_ptr.get();
+ out.push_back(static_cast<T &>(component));
}
- return component_vector;
+ return out;
}
template <typename T>
@@ -158,4 +151,46 @@ RefVector<T> ComponentManager::get_components_by_type() const {
return component_vector;
}
+template <typename T>
+std::set<game_object_id_t>
+ComponentManager::get_objects_by_predicate(const std::function<bool(const T &)> & pred) const {
+ using namespace std;
+
+ set<game_object_id_t> objects = {};
+ RefVector<T> components = this->get_components_by_type<T>();
+
+ for (const T & component : components) {
+ game_object_id_t id = dynamic_cast<const Component &>(component).game_object_id;
+ if (objects.contains(id)) continue;
+ if (!pred(component)) continue;
+ objects.insert(id);
+ }
+
+ return objects;
+}
+
+template <typename T>
+RefVector<T>
+ComponentManager::get_components_by_ids(const std::set<game_object_id_t> & ids) const {
+ using namespace std;
+
+ RefVector<T> out = {};
+ for (game_object_id_t id : ids) {
+ RefVector<T> components = get_components_by_id<T>(id);
+ out.insert(out.end(), components.begin(), components.end());
+ }
+
+ return out;
+}
+
+template <typename T>
+RefVector<T> ComponentManager::get_components_by_name(const std::string & name) const {
+ return this->get_components_by_ids<T>(this->get_objects_by_name(name));
+}
+
+template <typename T>
+RefVector<T> ComponentManager::get_components_by_tag(const std::string & tag) const {
+ return this->get_components_by_ids<T>(this->get_objects_by_tag(tag));
+}
+
} // namespace crepe
diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/manager/EventManager.cpp
index 20f0dd3..20f0dd3 100644
--- a/src/crepe/api/EventManager.cpp
+++ b/src/crepe/manager/EventManager.cpp
diff --git a/src/crepe/api/EventManager.h b/src/crepe/manager/EventManager.h
index 1a33023..ba5e98b 100644
--- a/src/crepe/api/EventManager.h
+++ b/src/crepe/manager/EventManager.h
@@ -5,8 +5,8 @@
#include <unordered_map>
#include <vector>
-#include "Event.h"
-#include "EventHandler.h"
+#include "../api/Event.h"
+#include "../api/EventHandler.h"
namespace crepe {
@@ -24,7 +24,7 @@ typedef size_t event_channel_t;
/**
* \class EventManager
* \brief Manages event subscriptions, triggers, and queues, enabling decoupled event handling.
- *
+ *
* The `EventManager` acts as a centralized event system. It allows for registering callbacks
* for specific event types, triggering events synchronously, queueing events for later
* processing, and managing subscriptions via unique identifiers.
@@ -35,20 +35,20 @@ public:
/**
* \brief Get the singleton instance of the EventManager.
- *
+ *
* This method returns the unique instance of the EventManager, creating it if it
* doesn't already exist. Ensures only one instance is active in the program.
- *
+ *
* \return Reference to the singleton instance of the EventManager.
*/
static EventManager & get_instance();
/**
* \brief Subscribe to a specific event type.
- *
+ *
* Registers a callback for a given event type and optional channel. Each callback
* is assigned a unique subscription ID that can be used for later unsubscription.
- *
+ *
* \tparam EventType The type of the event to subscribe to.
* \param callback The callback function to be invoked when the event is triggered.
* \param channel The channel number to subscribe to (default is CHANNEL_ALL, which listens to all channels).
@@ -60,18 +60,18 @@ public:
/**
* \brief Unsubscribe a previously registered callback.
- *
+ *
* Removes a callback from the subscription list based on its unique subscription ID.
- *
+ *
* \param event_id The unique subscription ID of the callback to remove.
*/
void unsubscribe(subscription_t event_id);
/**
* \brief Trigger an event immediately.
- *
+ *
* Synchronously invokes all registered callbacks for the given event type on the specified channel.
- *
+ *
* \tparam EventType The type of the event to trigger.
* \param event The event instance to pass to the callbacks.
* \param channel The channel to trigger the event on (default is CHANNEL_ALL, which triggers on all channels).
@@ -81,9 +81,9 @@ public:
/**
* \brief Queue an event for later processing.
- *
+ *
* Adds an event to the event queue to be processed during the next call to `dispatch_events`.
- *
+ *
* \tparam EventType The type of the event to queue.
* \param event The event instance to queue.
* \param channel The channel to associate with the event (default is CHANNEL_ALL).
@@ -93,7 +93,7 @@ public:
/**
* \brief Process all queued events.
- *
+ *
* Iterates through the event queue and triggers callbacks for each queued event.
* Events are removed from the queue once processed.
*/
@@ -101,7 +101,7 @@ public:
/**
* \brief Clear all subscriptions.
- *
+ *
* Removes all registered event handlers and clears the subscription list.
*/
void clear();
@@ -109,7 +109,7 @@ public:
private:
/**
* \brief Default constructor for the EventManager.
- *
+ *
* Constructor is private to enforce the singleton pattern.
*/
EventManager() = default;
diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/manager/EventManager.hpp
index a5f4556..a5f4556 100644
--- a/src/crepe/api/EventManager.hpp
+++ b/src/crepe/manager/EventManager.hpp
diff --git a/src/crepe/manager/Manager.cpp b/src/crepe/manager/Manager.cpp
new file mode 100644
index 0000000..1182785
--- /dev/null
+++ b/src/crepe/manager/Manager.cpp
@@ -0,0 +1,5 @@
+#include "Manager.h"
+
+using namespace crepe;
+
+Manager::Manager(Mediator & mediator) : mediator(mediator) {}
diff --git a/src/crepe/manager/Manager.h b/src/crepe/manager/Manager.h
new file mode 100644
index 0000000..84d80fe
--- /dev/null
+++ b/src/crepe/manager/Manager.h
@@ -0,0 +1,24 @@
+#pragma once
+
+#include "Mediator.h"
+
+namespace crepe {
+
+/**
+ * \brief Base manager class
+ *
+ * Managers are used for various tasks that fall outside the ECS system category. All managers
+ * are required to register themselves to the mediator passed to the constructor, and this
+ * mutable reference is saved for convenience, even though not all managers use the mediator
+ * directly.
+ */
+class Manager {
+public:
+ Manager(Mediator & mediator);
+ virtual ~Manager() = default;
+
+protected:
+ Mediator & mediator;
+};
+
+} // namespace crepe
diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h
new file mode 100644
index 0000000..628154a
--- /dev/null
+++ b/src/crepe/manager/Mediator.h
@@ -0,0 +1,39 @@
+#pragma once
+
+#include "../util/OptionalRef.h"
+
+// TODO: remove these singletons:
+#include "EventManager.h"
+
+namespace crepe {
+
+class ComponentManager;
+class SceneManager;
+class SaveManager;
+class ResourceManager;
+class SDLContext;
+class LoopTimer;
+
+/**
+ * Struct to pass references to classes that would otherwise need to be singletons down to
+ * other classes within the engine hierarchy. Made to prevent constant changes to subclasses to
+ * pass specific references through dependency injection. All references on this struct
+ * *should* be explicitly checked for availability as this struct does not guarantee anything.
+ *
+ * \note Dereferencing members of this struct should be deferred. If you are a user of this
+ * class, keep a reference to this mediator instead of just picking references from it when you
+ * receive an instance.
+ *
+ * \warning This class should never be directly accessible from the API
+ */
+struct Mediator {
+ OptionalRef<SDLContext> sdl_context;
+ OptionalRef<ComponentManager> component_manager;
+ OptionalRef<SceneManager> scene_manager;
+ OptionalRef<SaveManager> save_manager;
+ OptionalRef<EventManager> event_manager = EventManager::get_instance();
+ OptionalRef<ResourceManager> resource_manager;
+ OptionalRef<LoopTimer> timer;
+};
+
+} // namespace crepe
diff --git a/src/crepe/manager/ResourceManager.cpp b/src/crepe/manager/ResourceManager.cpp
new file mode 100644
index 0000000..a141a46
--- /dev/null
+++ b/src/crepe/manager/ResourceManager.cpp
@@ -0,0 +1,30 @@
+#include "util/Log.h"
+
+#include "ResourceManager.h"
+
+using namespace crepe;
+using namespace std;
+
+ResourceManager::ResourceManager(Mediator & mediator) : Manager(mediator) {
+ dbg_trace();
+ mediator.resource_manager = *this;
+}
+ResourceManager::~ResourceManager() { dbg_trace(); }
+
+void ResourceManager::clear() {
+ std::erase_if(this->resources, [](const pair<const Asset, CacheEntry> & pair) {
+ const CacheEntry & entry = pair.second;
+ return entry.persistent == false;
+ });
+}
+
+void ResourceManager::clear_all() { this->resources.clear(); }
+
+void ResourceManager::set_persistent(const Asset & asset, bool persistent) {
+ this->get_entry(asset).persistent = persistent;
+}
+
+ResourceManager::CacheEntry & ResourceManager::get_entry(const Asset & asset) {
+ if (!this->resources.contains(asset)) this->resources[asset] = {};
+ return this->resources.at(asset);
+}
diff --git a/src/crepe/manager/ResourceManager.h b/src/crepe/manager/ResourceManager.h
new file mode 100644
index 0000000..84b275d
--- /dev/null
+++ b/src/crepe/manager/ResourceManager.h
@@ -0,0 +1,78 @@
+#pragma once
+
+#include <memory>
+#include <unordered_map>
+
+#include "../Resource.h"
+#include "../api/Asset.h"
+
+#include "Manager.h"
+
+namespace crepe {
+
+/**
+ * \brief Owner of concrete Resource instances
+ *
+ * ResourceManager caches concrete Resource instances per Asset. Concrete resources are
+ * destroyed at the end of scenes by default, unless the game programmer marks them as
+ * persistent.
+ */
+class ResourceManager : public Manager {
+public:
+ ResourceManager(Mediator & mediator);
+ virtual ~ResourceManager(); // dbg_trace
+
+private:
+ //! Cache entry
+ struct CacheEntry {
+ //! Concrete resource instance
+ std::unique_ptr<Resource> resource = nullptr;
+ //! Prevent ResourceManager::clear from removing this entry
+ bool persistent = false;
+ };
+ //! Internal cache
+ std::unordered_map<const Asset, CacheEntry> resources;
+ /**
+ * \brief Ensure a cache entry exists for this asset and return a mutable reference to it
+ *
+ * \param asset Asset the concrete resource is instantiated from
+ *
+ * \returns Mutable reference to cache entry
+ */
+ CacheEntry & get_entry(const Asset & asset);
+
+public:
+ /**
+ * \brief Mark a resource as persistent (i.e. used across multiple scenes)
+ *
+ * \param asset Asset the concrete resource is instantiated from
+ * \param persistent Whether this resource is persistent (true=keep, false=destroy)
+ */
+ void set_persistent(const Asset & asset, bool persistent);
+
+ /**
+ * \brief Retrieve reference to concrete Resource by Asset
+ *
+ * \param asset Asset the concrete resource is instantiated from
+ * \tparam Resource Concrete derivative of Resource
+ *
+ * This class instantiates the concrete resource if it is not yet stored in the internal
+ * cache, or returns a reference to the cached resource if it already exists.
+ *
+ * \returns Reference to concrete resource
+ *
+ * \throws std::runtime_error if the \c Resource parameter does not match with the actual
+ * type of the resource stored in the cache for this Asset
+ */
+ template <typename Resource>
+ Resource & get(const Asset & asset);
+
+ //! Clear non-persistent resources from cache
+ void clear();
+ //! Clear all resources from cache regardless of persistence
+ void clear_all();
+};
+
+} // namespace crepe
+
+#include "ResourceManager.hpp"
diff --git a/src/crepe/manager/ResourceManager.hpp b/src/crepe/manager/ResourceManager.hpp
new file mode 100644
index 0000000..cf5c949
--- /dev/null
+++ b/src/crepe/manager/ResourceManager.hpp
@@ -0,0 +1,27 @@
+#pragma once
+
+#include <format>
+
+#include "ResourceManager.h"
+
+namespace crepe {
+
+template <typename T>
+T & ResourceManager::get(const Asset & asset) {
+ using namespace std;
+ static_assert(is_base_of<Resource, T>::value,
+ "cache must recieve a derivative class of Resource");
+
+ CacheEntry & entry = this->get_entry(asset);
+ if (entry.resource == nullptr) entry.resource = make_unique<T>(asset, this->mediator);
+
+ T * concrete_resource = dynamic_cast<T *>(entry.resource.get());
+ if (concrete_resource == nullptr)
+ throw runtime_error(format("ResourceManager: mismatch between requested type and "
+ "actual type of resource ({})",
+ asset.get_path()));
+
+ return *concrete_resource;
+}
+
+} // namespace crepe
diff --git a/src/crepe/api/SaveManager.cpp b/src/crepe/manager/SaveManager.cpp
index c5f43ea..691ea2f 100644
--- a/src/crepe/api/SaveManager.cpp
+++ b/src/crepe/manager/SaveManager.cpp
@@ -1,13 +1,25 @@
+#include "../ValueBroker.h"
+#include "../api/Config.h"
#include "../facade/DB.h"
-#include "../util/Log.h"
-#include "Config.h"
#include "SaveManager.h"
-#include "ValueBroker.h"
using namespace std;
using namespace crepe;
+SaveManager::SaveManager(Mediator & mediator) : Manager(mediator) {
+ mediator.save_manager = *this;
+}
+
+DB & SaveManager::get_db() {
+ if (this->db == nullptr) {
+ Config & cfg = Config::get_instance();
+ this->db
+ = {new DB(cfg.savemgr.location), [](void * db) { delete static_cast<DB *>(db); }};
+ }
+ return *static_cast<DB *>(this->db.get());
+}
+
template <>
string SaveManager::serialize(const string & value) const noexcept {
return value;
@@ -90,22 +102,6 @@ int32_t SaveManager::deserialize(const string & value) const noexcept {
return deserialize<int64_t>(value);
}
-SaveManager::SaveManager() { dbg_trace(); }
-
-SaveManager & SaveManager::get_instance() {
- dbg_trace();
- static SaveManager instance;
- return instance;
-}
-
-DB & SaveManager::get_db() {
- Config & cfg = Config::get_instance();
- // TODO: make this path relative to XDG_DATA_HOME on Linux and whatever the
- // default equivalent is on Windows using some third party library
- static DB db(cfg.savemgr.location);
- return db;
-}
-
bool SaveManager::has(const string & key) {
DB & db = this->get_db();
return db.has(key);
@@ -155,7 +151,8 @@ ValueBroker<T> SaveManager::get(const string & key) {
return {
[this, key](const T & target) { this->set<T>(key, target); },
[this, key, value]() mutable -> const T & {
- value = this->deserialize<T>(this->get_db().get(key));
+ DB & db = this->get_db();
+ value = this->deserialize<T>(db.get(key));
return value;
},
};
diff --git a/src/crepe/api/SaveManager.h b/src/crepe/manager/SaveManager.h
index 3d8c852..61a978d 100644
--- a/src/crepe/api/SaveManager.h
+++ b/src/crepe/manager/SaveManager.h
@@ -1,9 +1,12 @@
#pragma once
+#include <functional>
#include <memory>
#include "../ValueBroker.h"
+#include "Manager.h"
+
namespace crepe {
class DB;
@@ -18,7 +21,7 @@ class DB;
*
* The underlying database is a key-value store.
*/
-class SaveManager {
+class SaveManager : public Manager {
public:
/**
* \brief Get a read/write reference to a value and initialize it if it does not yet exist
@@ -63,8 +66,8 @@ public:
*/
bool has(const std::string & key);
-private:
- SaveManager();
+public:
+ SaveManager(Mediator & mediator);
virtual ~SaveManager() = default;
private:
@@ -89,26 +92,13 @@ private:
template <typename T>
T deserialize(const std::string & value) const noexcept;
-public:
- // singleton
- static SaveManager & get_instance();
- SaveManager(const SaveManager &) = delete;
- SaveManager(SaveManager &&) = delete;
- SaveManager & operator=(const SaveManager &) = delete;
- SaveManager & operator=(SaveManager &&) = delete;
+protected:
+ //! Create or return DB
+ virtual DB & get_db();
private:
- /**
- * \brief Create an instance of DB and return its reference
- *
- * \returns DB instance
- *
- * This function exists because DB is a facade class, which can't directly be used in the API
- * without workarounds
- *
- * TODO: better solution
- */
- static DB & get_db();
+ //! Database
+ std::unique_ptr<void, std::function<void(void *)>> db = nullptr;
};
} // namespace crepe
diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/manager/SceneManager.cpp
index 1f783ad..50a9fbb 100644
--- a/src/crepe/api/SceneManager.cpp
+++ b/src/crepe/manager/SceneManager.cpp
@@ -1,14 +1,15 @@
#include <algorithm>
#include <memory>
-#include "../ComponentManager.h"
-
+#include "ComponentManager.h"
#include "SceneManager.h"
using namespace crepe;
using namespace std;
-SceneManager::SceneManager(ComponentManager & mgr) : component_manager(mgr) {}
+SceneManager::SceneManager(Mediator & mediator) : Manager(mediator) {
+ mediator.scene_manager = *this;
+}
void SceneManager::set_next_scene(const string & name) { next_scene = name; }
@@ -26,7 +27,7 @@ void SceneManager::load_next_scene() {
unique_ptr<Scene> & scene = *it;
// Delete all components of the current scene
- ComponentManager & mgr = this->component_manager;
+ ComponentManager & mgr = this->mediator.component_manager;
mgr.delete_all_components();
// Load the new scene
diff --git a/src/crepe/api/SceneManager.h b/src/crepe/manager/SceneManager.h
index f6f62cd..e0955c2 100644
--- a/src/crepe/api/SceneManager.h
+++ b/src/crepe/manager/SceneManager.h
@@ -3,7 +3,9 @@
#include <memory>
#include <vector>
-#include "Scene.h"
+#include "../api/Scene.h"
+
+#include "Manager.h"
namespace crepe {
@@ -15,10 +17,9 @@ class ComponentManager;
* This class manages scenes. It can add new scenes and load them. It also manages the current scene
* and the next scene.
*/
-class SceneManager {
+class SceneManager : public Manager {
public:
- //! \param mgr Reference to the ComponentManager
- SceneManager(ComponentManager & mgr);
+ SceneManager(Mediator & mediator);
public:
/**
@@ -44,8 +45,6 @@ private:
std::vector<std::unique_ptr<Scene>> scenes;
//! Next scene to load
std::string next_scene;
- //! Reference to the ComponentManager
- ComponentManager & component_manager;
};
} // namespace crepe
diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/manager/SceneManager.hpp
index 5c8e417..dff4e51 100644
--- a/src/crepe/api/SceneManager.hpp
+++ b/src/crepe/manager/SceneManager.hpp
@@ -12,7 +12,7 @@ void SceneManager::add_scene(Args &&... args) {
Scene * scene = new T(std::forward<Args>(args)...);
unique_ptr<Scene> unique_scene(scene);
- unique_scene->component_manager = this->component_manager;
+ unique_scene->mediator = this->mediator;
this->scenes.emplace_back(std::move(unique_scene));
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp
new file mode 100644
index 0000000..7f04432
--- /dev/null
+++ b/src/crepe/system/AISystem.cpp
@@ -0,0 +1,186 @@
+#include <algorithm>
+#include <cmath>
+
+#include "api/LoopTimer.h"
+#include "manager/ComponentManager.h"
+#include "manager/Mediator.h"
+
+#include "AISystem.h"
+
+using namespace crepe;
+
+void AISystem::update() {
+ const Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
+ LoopTimer & timer = mediator.timer;
+ RefVector<AI> ai_components = mgr.get_components_by_type<AI>();
+
+ //TODO: Use fixed loop dt (this is not available at master at the moment)
+ double dt = timer.get_delta_time();
+
+ // Loop through all AI components
+ for (AI & ai : ai_components) {
+ if (!ai.active) {
+ continue;
+ }
+
+ RefVector<Rigidbody> rigidbodies
+ = mgr.get_components_by_id<Rigidbody>(ai.game_object_id);
+ if (rigidbodies.empty()) {
+ throw std::runtime_error(
+ "AI component must be attached to a GameObject with a Rigidbody component");
+ }
+ Rigidbody & rigidbody = rigidbodies.front().get();
+ if (!rigidbody.active) {
+ continue;
+ }
+ if (rigidbody.data.mass <= 0) {
+ throw std::runtime_error("Mass must be greater than 0");
+ }
+
+ // Calculate the force to apply to the entity
+ vec2 force = this->calculate(ai, rigidbody);
+ // Calculate the acceleration (using the above calculated force)
+ vec2 acceleration = force / rigidbody.data.mass;
+ // Finally, update Rigidbody's velocity
+ rigidbody.data.linear_velocity += acceleration * dt;
+ }
+}
+
+vec2 AISystem::calculate(AI & ai, const Rigidbody & rigidbody) {
+ const Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
+ RefVector<Transform> transforms = mgr.get_components_by_id<Transform>(ai.game_object_id);
+ Transform & transform = transforms.front().get();
+
+ vec2 force;
+
+ // Run all the behaviors that are on, and stop if the force gets too high
+ if (ai.on(AI::BehaviorTypeMask::FLEE)) {
+ vec2 force_to_add = this->flee(ai, rigidbody, transform);
+
+ if (!this->accumulate_force(ai, force, force_to_add)) {
+ return force;
+ }
+ }
+ if (ai.on(AI::BehaviorTypeMask::ARRIVE)) {
+ vec2 force_to_add = this->arrive(ai, rigidbody, transform);
+
+ if (!this->accumulate_force(ai, force, force_to_add)) {
+ return force;
+ }
+ }
+ if (ai.on(AI::BehaviorTypeMask::SEEK)) {
+ vec2 force_to_add = this->seek(ai, rigidbody, transform);
+
+ if (!this->accumulate_force(ai, force, force_to_add)) {
+ return force;
+ }
+ }
+ if (ai.on(AI::BehaviorTypeMask::PATH_FOLLOW)) {
+ vec2 force_to_add = this->path_follow(ai, rigidbody, transform);
+
+ if (!this->accumulate_force(ai, force, force_to_add)) {
+ return force;
+ }
+ }
+
+ return force;
+}
+
+bool AISystem::accumulate_force(const AI & ai, vec2 & running_total, vec2 & force_to_add) {
+ float magnitude = running_total.length();
+ float magnitude_remaining = ai.max_force - magnitude;
+
+ if (magnitude_remaining <= 0.0f) {
+ // If the force is already at/above the max force, return false
+ return false;
+ }
+
+ float magnitude_to_add = force_to_add.length();
+ if (magnitude_to_add < magnitude_remaining) {
+ // If the force to add is less than the remaining force, add it
+ running_total += force_to_add;
+ } else {
+ // If the force to add is greater than the remaining force, add a fraction of it
+ force_to_add.normalize();
+ running_total += force_to_add * magnitude_remaining;
+ }
+
+ return true;
+}
+
+vec2 AISystem::seek(const AI & ai, const Rigidbody & rigidbody,
+ const Transform & transform) const {
+ // Calculate the desired velocity
+ vec2 desired_velocity = ai.seek_target - transform.position;
+ desired_velocity.normalize();
+ desired_velocity *= rigidbody.data.max_linear_velocity;
+
+ return desired_velocity - rigidbody.data.linear_velocity;
+}
+
+vec2 AISystem::flee(const AI & ai, const Rigidbody & rigidbody,
+ const Transform & transform) const {
+ // Calculate the desired velocity if the entity is within the panic distance
+ vec2 desired_velocity = transform.position - ai.flee_target;
+ if (desired_velocity.length_squared() > ai.square_flee_panic_distance) {
+ return vec2{0, 0};
+ }
+ desired_velocity.normalize();
+ desired_velocity *= rigidbody.data.max_linear_velocity;
+
+ return desired_velocity - rigidbody.data.linear_velocity;
+}
+
+vec2 AISystem::arrive(const AI & ai, const Rigidbody & rigidbody,
+ const Transform & transform) const {
+ // Calculate the desired velocity (taking into account the deceleration rate)
+ vec2 to_target = ai.arrive_target - transform.position;
+ float distance = to_target.length();
+ if (distance > 0.0f) {
+ if (ai.arrive_deceleration <= 0.0f) {
+ throw std::runtime_error("Deceleration rate must be greater than 0");
+ }
+
+ float speed = distance / ai.arrive_deceleration;
+ speed = std::min(speed, rigidbody.data.max_linear_velocity.length());
+ vec2 desired_velocity = to_target * (speed / distance);
+
+ return desired_velocity - rigidbody.data.linear_velocity;
+ }
+
+ return vec2{0, 0};
+}
+
+vec2 AISystem::path_follow(AI & ai, const Rigidbody & rigidbody, const Transform & transform) {
+ if (ai.path.empty()) {
+ return vec2{0, 0};
+ }
+
+ // Get the target node
+ vec2 target = ai.path.at(ai.path_index);
+ // Calculate the force to apply to the entity
+ vec2 to_target = target - transform.position;
+ if (to_target.length_squared() > ai.path_node_distance * ai.path_node_distance) {
+ // If the entity is not close enough to the target node, seek it
+ ai.seek_target = target;
+ ai.arrive_target = target;
+ } else {
+ // If the entity is close enough to the target node, move to the next node
+ ai.path_index++;
+ if (ai.path_index >= ai.path.size()) {
+ if (ai.path_loop) {
+ // If the path is looping, reset the path index
+ ai.path_index = 0;
+ } else {
+ // If the path is not looping, arrive at the last node
+ ai.path_index = ai.path.size() - 1;
+ return this->arrive(ai, rigidbody, transform);
+ }
+ }
+ }
+
+ // Seek the target node
+ return this->seek(ai, rigidbody, transform);
+}
diff --git a/src/crepe/system/AISystem.h b/src/crepe/system/AISystem.h
new file mode 100644
index 0000000..d5f8a8e
--- /dev/null
+++ b/src/crepe/system/AISystem.h
@@ -0,0 +1,81 @@
+#pragma once
+
+#include "api/AI.h"
+#include "api/Rigidbody.h"
+
+#include "System.h"
+#include "api/Transform.h"
+#include "types.h"
+
+namespace crepe {
+
+/**
+ * \brief The AISystem is used to control the movement of entities using AI.
+ *
+ * The AISystem is used to control the movement of entities using AI. The AISystem can be used to
+ * implement different behaviors such as seeking, fleeing, arriving, and path following.
+ */
+class AISystem : public System {
+public:
+ using System::System;
+
+ //! Update the AI system
+ void update() override;
+
+private:
+ /**
+ * \brief Calculate the total force to apply to the entity
+ *
+ * \param ai The AI component
+ * \param rigidbody The Rigidbody component
+ */
+ vec2 calculate(AI & ai, const Rigidbody & rigidbody);
+ /**
+ * \brief Accumulate the force to apply to the entity
+ *
+ * \param ai The AI component
+ * \param running_total The running total of the force
+ * \param force_to_add The force to add
+ * \return true if the force was added, false otherwise
+ */
+ bool accumulate_force(const AI & ai, vec2 & running_total, vec2 & force_to_add);
+
+ /**
+ * \brief Calculate the seek force
+ *
+ * \param ai The AI component
+ * \param rigidbody The Rigidbody component
+ * \param transform The Transform component
+ * \return The seek force
+ */
+ vec2 seek(const AI & ai, const Rigidbody & rigidbody, const Transform & transform) const;
+ /**
+ * \brief Calculate the flee force
+ *
+ * \param ai The AI component
+ * \param rigidbody The Rigidbody component
+ * \param transform The Transform component
+ * \return The flee force
+ */
+ vec2 flee(const AI & ai, const Rigidbody & rigidbody, const Transform & transform) const;
+ /**
+ * \brief Calculate the arrive force
+ *
+ * \param ai The AI component
+ * \param rigidbody The Rigidbody component
+ * \param transform The Transform component
+ * \return The arrive force
+ */
+ vec2 arrive(const AI & ai, const Rigidbody & rigidbody, const Transform & transform) const;
+ /**
+ * \brief Calculate the path follow force
+ *
+ * \param ai The AI component
+ * \param rigidbody The Rigidbody component
+ * \param transform The Transform component
+ * \return The path follow force
+ */
+ vec2 path_follow(AI & ai, const Rigidbody & rigidbody, const Transform & transform);
+};
+
+} // namespace crepe
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp
index 4c40940..d61ba35 100644
--- a/src/crepe/system/AnimatorSystem.cpp
+++ b/src/crepe/system/AnimatorSystem.cpp
@@ -1,24 +1,39 @@
-#include <cstdint>
-#include "api/Animator.h"
-#include "facade/SDLContext.h"
+
+#include "../api/Animator.h"
+#include "../manager/ComponentManager.h"
+#include "api/LoopTimer.h"
#include "AnimatorSystem.h"
-#include "ComponentManager.h"
using namespace crepe;
void AnimatorSystem::update() {
- ComponentManager & mgr = this->component_manager;
-
+ ComponentManager & mgr = this->mediator.component_manager;
+ LoopTimer & timer = this->mediator.timer;
RefVector<Animator> animations = mgr.get_components_by_type<Animator>();
- uint64_t tick = SDLContext::get_instance().get_ticks();
+ double elapsed_time = timer.get_current_time();
+
for (Animator & a : animations) {
if (!a.active) continue;
- // (10 frames per second)
- a.curr_row = (tick / 100) % a.row;
- a.spritesheet.mask.x = (a.curr_row * a.spritesheet.mask.w) + a.curr_col;
- a.spritesheet.mask = a.spritesheet.mask;
+
+ Animator::Data & ctx = a.data;
+ float frame_duration = 1.0f / ctx.fps;
+
+ int last_frame = ctx.row;
+
+ int cycle_end = (ctx.cycle_end == -1) ? a.grid_size.x : ctx.cycle_end;
+ int total_frames = cycle_end - ctx.cycle_start;
+
+ int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames;
+
+ ctx.row = ctx.cycle_start + curr_frame;
+ a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w;
+ a.spritesheet.mask.y = (ctx.col * a.spritesheet.mask.h);
+
+ if (!ctx.looping && curr_frame == ctx.cycle_start && last_frame == total_frames - 1) {
+ a.active = false;
+ }
}
}
diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h
index f8179a9..7d3f565 100644
--- a/src/crepe/system/AnimatorSystem.h
+++ b/src/crepe/system/AnimatorSystem.h
@@ -2,9 +2,6 @@
#include "System.h"
-//TODO:
-// control if flip works with animation system
-
namespace crepe {
/**
diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp
new file mode 100644
index 0000000..b1aa0f8
--- /dev/null
+++ b/src/crepe/system/AudioSystem.cpp
@@ -0,0 +1,62 @@
+#include "AudioSystem.h"
+
+#include "../manager/ComponentManager.h"
+#include "../manager/ResourceManager.h"
+#include "../types.h"
+
+using namespace crepe;
+using namespace std;
+
+void AudioSystem::update() {
+ ComponentManager & component_manager = this->mediator.component_manager;
+ ResourceManager & resource_manager = this->mediator.resource_manager;
+ RefVector<AudioSource> components
+ = component_manager.get_components_by_type<AudioSource>();
+
+ for (AudioSource & component : components) {
+ Sound & resource = resource_manager.get<Sound>(component.source);
+
+ this->diff_update(component, resource);
+
+ this->update_last(component);
+ }
+}
+
+void AudioSystem::diff_update(AudioSource & component, Sound & resource) {
+ SoundContext & context = this->get_context();
+
+ if (component.active != component.last_active) {
+ if (!component.active) {
+ context.stop(component.voice);
+ return;
+ }
+ if (component.play_on_awake) component.oneshot_play = true;
+ }
+ if (!component.active) return;
+
+ if (component.oneshot_play) {
+ component.voice = context.play(resource);
+ component.oneshot_play = false;
+ }
+ if (component.oneshot_stop) {
+ context.stop(component.voice);
+ component.oneshot_stop = false;
+ }
+ if (component.volume != component.last_volume) {
+ context.set_volume(component.voice, component.volume);
+ }
+ if (component.loop != component.last_loop) {
+ context.set_loop(component.voice, component.loop);
+ }
+}
+
+void AudioSystem::update_last(AudioSource & component) {
+ component.last_active = component.active;
+ component.last_loop = component.loop;
+ component.last_volume = component.volume;
+}
+
+SoundContext & AudioSystem::get_context() {
+ if (this->context == nullptr) this->context = make_unique<SoundContext>();
+ return *this->context.get();
+}
diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h
new file mode 100644
index 0000000..2ddc443
--- /dev/null
+++ b/src/crepe/system/AudioSystem.h
@@ -0,0 +1,51 @@
+#pragma once
+
+#include "../api/AudioSource.h"
+#include "../facade/Sound.h"
+#include "../facade/SoundContext.h"
+
+#include "System.h"
+
+namespace crepe {
+
+class AudioSystem : public System {
+public:
+ using System::System;
+ void update() override;
+
+private:
+ /**
+ * \brief Update `last_*` members of \c component
+ *
+ * Copies all component properties stored for comparison between AudioSystem::update() calls
+ *
+ * \param component AudioSource component to update
+ */
+ void update_last(AudioSource & component);
+
+ /**
+ * \brief Compare update component
+ *
+ * Compares properties of \c component and \c data, and calls SoundContext functions where
+ * applicable.
+ *
+ * \param component AudioSource component to update
+ * \param resource Sound instance for AudioSource's Asset
+ */
+ void diff_update(AudioSource & component, Sound & resource);
+
+protected:
+ /**
+ * \brief Get SoundContext
+ *
+ * SoundContext is retrieved through this function instead of being a direct member of
+ * AudioSystem to aid with testability.
+ */
+ virtual SoundContext & get_context();
+
+private:
+ //! SoundContext
+ std::unique_ptr<SoundContext> context = nullptr;
+};
+
+} // namespace crepe
diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt
index d658b25..0e2db76 100644
--- a/src/crepe/system/CMakeLists.txt
+++ b/src/crepe/system/CMakeLists.txt
@@ -5,7 +5,10 @@ target_sources(crepe PUBLIC
PhysicsSystem.cpp
CollisionSystem.cpp
RenderSystem.cpp
+ AudioSystem.cpp
AnimatorSystem.cpp
+ InputSystem.cpp
+ AISystem.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -14,5 +17,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
PhysicsSystem.h
CollisionSystem.h
RenderSystem.h
+ AudioSystem.h
AnimatorSystem.h
+ InputSystem.h
+ AISystem.h
)
diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp
index c74ca1d..44a0431 100644
--- a/src/crepe/system/CollisionSystem.cpp
+++ b/src/crepe/system/CollisionSystem.cpp
@@ -1,5 +1,551 @@
+#include <algorithm>
+#include <cmath>
+#include <cstddef>
+#include <functional>
+#include <optional>
+#include <utility>
+#include <variant>
+
+#include "../manager/ComponentManager.h"
+#include "../manager/EventManager.h"
+#include "api/BoxCollider.h"
+#include "api/CircleCollider.h"
+#include "api/Event.h"
+#include "api/Metadata.h"
+#include "api/Rigidbody.h"
+#include "api/Transform.h"
+#include "api/Vector2.h"
+
+#include "Collider.h"
#include "CollisionSystem.h"
+#include "types.h"
+#include "util/OptionalRef.h"
using namespace crepe;
-void CollisionSystem::update() {}
+void CollisionSystem::update() {
+ std::vector<CollisionInternal> all_colliders;
+ game_object_id_t id = 0;
+ ComponentManager & mgr = this->mediator.component_manager;
+ RefVector<Rigidbody> rigidbodies = mgr.get_components_by_type<Rigidbody>();
+ // Collisions can only happen on object with a rigidbody
+ for (Rigidbody & rigidbody : rigidbodies) {
+ if (!rigidbody.active) continue;
+ id = rigidbody.game_object_id;
+ Transform & transform = mgr.get_components_by_id<Transform>(id).front().get();
+ // Check if the boxcollider is active and has the same id as the rigidbody.
+ RefVector<BoxCollider> boxcolliders = mgr.get_components_by_type<BoxCollider>();
+ for (BoxCollider & boxcollider : boxcolliders) {
+ if (boxcollider.game_object_id != id) continue;
+ if (!boxcollider.active) continue;
+ all_colliders.push_back({.id = id,
+ .collider = collider_variant{boxcollider},
+ .transform = transform,
+ .rigidbody = rigidbody});
+ }
+ // Check if the circlecollider is active and has the same id as the rigidbody.
+ RefVector<CircleCollider> circlecolliders
+ = mgr.get_components_by_type<CircleCollider>();
+ for (CircleCollider & circlecollider : circlecolliders) {
+ if (circlecollider.game_object_id != id) continue;
+ if (!circlecollider.active) continue;
+ all_colliders.push_back({.id = id,
+ .collider = collider_variant{circlecollider},
+ .transform = transform,
+ .rigidbody = rigidbody});
+ }
+ }
+
+ // Check between all colliders if there is a collision
+ std::vector<std::pair<CollisionInternal, CollisionInternal>> collided
+ = this->gather_collisions(all_colliders);
+
+ // For both objects call the collision handler
+ for (auto & collision_pair : collided) {
+ this->collision_handler_request(collision_pair.first, collision_pair.second);
+ this->collision_handler_request(collision_pair.second, collision_pair.first);
+ }
+}
+
+void CollisionSystem::collision_handler_request(CollisionInternal & this_data,
+ CollisionInternal & other_data) {
+
+ CollisionInternalType type
+ = this->get_collider_type(this_data.collider, other_data.collider);
+ std::pair<vec2, CollisionSystem::Direction> resolution_data
+ = this->collision_handler(this_data, other_data, type);
+ ComponentManager & mgr = this->mediator.component_manager;
+ OptionalRef<Metadata> this_metadata
+ = mgr.get_components_by_id<Metadata>(this_data.id).front().get();
+ OptionalRef<Metadata> other_metadata
+ = mgr.get_components_by_id<Metadata>(other_data.id).front().get();
+ OptionalRef<Collider> this_collider;
+ OptionalRef<Collider> other_collider;
+ switch (type) {
+ case CollisionInternalType::BOX_BOX: {
+ this_collider = std::get<std::reference_wrapper<BoxCollider>>(this_data.collider);
+ other_collider
+ = std::get<std::reference_wrapper<BoxCollider>>(other_data.collider);
+ break;
+ }
+ case CollisionInternalType::BOX_CIRCLE: {
+ this_collider = std::get<std::reference_wrapper<BoxCollider>>(this_data.collider);
+ other_collider
+ = std::get<std::reference_wrapper<CircleCollider>>(other_data.collider);
+ break;
+ }
+ case CollisionInternalType::CIRCLE_BOX: {
+ this_collider
+ = std::get<std::reference_wrapper<CircleCollider>>(this_data.collider);
+ other_collider
+ = std::get<std::reference_wrapper<BoxCollider>>(other_data.collider);
+ break;
+ }
+ case CollisionInternalType::CIRCLE_CIRCLE: {
+ this_collider
+ = std::get<std::reference_wrapper<CircleCollider>>(this_data.collider);
+ other_collider
+ = std::get<std::reference_wrapper<CircleCollider>>(other_data.collider);
+ break;
+ }
+ }
+
+ // collision info
+ crepe::CollisionSystem::CollisionInfo collision_info{
+ .this_collider = this_collider,
+ .this_transform = this_data.transform,
+ .this_rigidbody = this_data.rigidbody,
+ .this_metadata = this_metadata,
+ .other_collider = other_collider,
+ .other_transform = other_data.transform,
+ .other_rigidbody = other_data.rigidbody,
+ .other_metadata = other_metadata,
+ .resolution = resolution_data.first,
+ .resolution_direction = resolution_data.second,
+ };
+
+ // Determine if static needs to be called
+ this->determine_collision_handler(collision_info);
+}
+
+std::pair<vec2, CollisionSystem::Direction>
+CollisionSystem::collision_handler(CollisionInternal & data1, CollisionInternal & data2,
+ CollisionInternalType type) {
+ vec2 resolution;
+ switch (type) {
+ case CollisionInternalType::BOX_BOX: {
+ const BoxCollider & collider1
+ = std::get<std::reference_wrapper<BoxCollider>>(data1.collider);
+ const BoxCollider & collider2
+ = std::get<std::reference_wrapper<BoxCollider>>(data2.collider);
+ vec2 collider_pos1 = this->get_current_position(collider1.offset, data1.transform,
+ data1.rigidbody);
+ vec2 collider_pos2 = this->get_current_position(collider2.offset, data2.transform,
+ data2.rigidbody);
+ resolution = this->get_box_box_resolution(collider1, collider2, collider_pos1,
+ collider_pos2);
+ break;
+ }
+ case CollisionInternalType::BOX_CIRCLE: {
+ const BoxCollider & collider1
+ = std::get<std::reference_wrapper<BoxCollider>>(data1.collider);
+ const CircleCollider & collider2
+ = std::get<std::reference_wrapper<CircleCollider>>(data2.collider);
+ vec2 collider_pos1 = this->get_current_position(collider1.offset, data1.transform,
+ data1.rigidbody);
+ vec2 collider_pos2 = this->get_current_position(collider2.offset, data2.transform,
+ data2.rigidbody);
+ resolution = -this->get_circle_box_resolution(collider2, collider1, collider_pos2,
+ collider_pos1);
+ break;
+ }
+ case CollisionInternalType::CIRCLE_CIRCLE: {
+ const CircleCollider & collider1
+ = std::get<std::reference_wrapper<CircleCollider>>(data1.collider);
+ const CircleCollider & collider2
+ = std::get<std::reference_wrapper<CircleCollider>>(data2.collider);
+ vec2 collider_pos1 = this->get_current_position(collider1.offset, data1.transform,
+ data1.rigidbody);
+ vec2 collider_pos2 = this->get_current_position(collider2.offset, data2.transform,
+ data2.rigidbody);
+ resolution = this->get_circle_circle_resolution(collider1, collider2,
+ collider_pos1, collider_pos2);
+ break;
+ }
+ case CollisionInternalType::CIRCLE_BOX: {
+ const CircleCollider & collider1
+ = std::get<std::reference_wrapper<CircleCollider>>(data1.collider);
+ const BoxCollider & collider2
+ = std::get<std::reference_wrapper<BoxCollider>>(data2.collider);
+ vec2 collider_pos1 = this->get_current_position(collider1.offset, data1.transform,
+ data1.rigidbody);
+ vec2 collider_pos2 = this->get_current_position(collider2.offset, data2.transform,
+ data2.rigidbody);
+ resolution = this->get_circle_box_resolution(collider1, collider2, collider_pos1,
+ collider_pos2);
+ break;
+ }
+ }
+
+ Direction resolution_direction = Direction::NONE;
+ if (resolution.x != 0 && resolution.y != 0) {
+ resolution_direction = Direction::BOTH;
+ } else if (resolution.x != 0) {
+ resolution_direction = Direction::X_DIRECTION;
+ if (data1.rigidbody.data.linear_velocity.y != 0)
+ resolution.y = data1.rigidbody.data.linear_velocity.y
+ * (resolution.x / data1.rigidbody.data.linear_velocity.x);
+ } else if (resolution.y != 0) {
+ resolution_direction = Direction::Y_DIRECTION;
+ if (data1.rigidbody.data.linear_velocity.x != 0)
+ resolution.x = data1.rigidbody.data.linear_velocity.x
+ * (resolution.y / data1.rigidbody.data.linear_velocity.y);
+ }
+
+ return std::make_pair(resolution, resolution_direction);
+}
+
+vec2 CollisionSystem::get_box_box_resolution(const BoxCollider & box_collider1,
+ const BoxCollider & box_collider2,
+ const vec2 & final_position1,
+ const vec2 & final_position2) const {
+ vec2 resolution; // Default resolution vector
+ vec2 delta = final_position2 - final_position1;
+
+ // Compute half-dimensions of the boxes
+ float half_width1 = box_collider1.dimensions.x / 2.0;
+ float half_height1 = box_collider1.dimensions.y / 2.0;
+ float half_width2 = box_collider2.dimensions.x / 2.0;
+ float half_height2 = box_collider2.dimensions.y / 2.0;
+
+ // Calculate overlaps along X and Y axes
+ float overlap_x = (half_width1 + half_width2) - std::abs(delta.x);
+ float overlap_y = (half_height1 + half_height2) - std::abs(delta.y);
+
+ // Check if there is a collision should always be true
+ if (overlap_x > 0 && overlap_y > 0) {
+ // Determine the direction of resolution
+ if (overlap_x < overlap_y) {
+ // Resolve along the X-axis (smallest overlap)
+ resolution.x = (delta.x > 0) ? -overlap_x : overlap_x;
+ } else if (overlap_y < overlap_x) {
+ // Resolve along the Y-axis (smallest overlap)
+ resolution.y = (delta.y > 0) ? -overlap_y : overlap_y;
+ } else {
+ // Equal overlap, resolve both directions with preference
+ resolution.x = (delta.x > 0) ? -overlap_x : overlap_x;
+ resolution.y = (delta.y > 0) ? -overlap_y : overlap_y;
+ }
+ }
+
+ return resolution;
+}
+
+vec2 CollisionSystem::get_circle_circle_resolution(const CircleCollider & circle_collider1,
+ const CircleCollider & circle_collider2,
+ const vec2 & final_position1,
+ const vec2 & final_position2) const {
+ vec2 delta = final_position2 - final_position1;
+
+ // Compute the distance between the two circle centers
+ float distance = std::sqrt(delta.x * delta.x + delta.y * delta.y);
+
+ // Compute the combined radii of the two circles
+ float combined_radius = circle_collider1.radius + circle_collider2.radius;
+
+ // Compute the penetration depth
+ float penetration_depth = combined_radius - distance;
+
+ // Normalize the delta vector to get the collision direction
+ vec2 collision_normal = delta / distance;
+
+ // Compute the resolution vector
+ vec2 resolution = -collision_normal * penetration_depth;
+
+ return resolution;
+}
+
+vec2 CollisionSystem::get_circle_box_resolution(const CircleCollider & circle_collider,
+ const BoxCollider & box_collider,
+ const vec2 & circle_position,
+ const vec2 & box_position) const {
+ vec2 delta = circle_position - box_position;
+
+ // Compute half-dimensions of the box
+ float half_width = box_collider.dimensions.x / 2.0f;
+ float half_height = box_collider.dimensions.y / 2.0f;
+
+ // Clamp circle center to the nearest point on the box
+ vec2 closest_point;
+ closest_point.x = std::clamp(delta.x, -half_width, half_width);
+ closest_point.y = std::clamp(delta.y, -half_height, half_height);
+
+ // Find the vector from the circle center to the closest point
+ vec2 closest_delta = delta - closest_point;
+
+ // Normalize the delta to get the collision direction
+ float distance
+ = std::sqrt(closest_delta.x * closest_delta.x + closest_delta.y * closest_delta.y);
+ vec2 collision_normal = closest_delta / distance;
+
+ // Compute penetration depth
+ float penetration_depth = circle_collider.radius - distance;
+
+ // Compute the resolution vector
+ vec2 resolution = collision_normal * penetration_depth;
+
+ return resolution;
+}
+
+void CollisionSystem::determine_collision_handler(CollisionInfo & info) {
+ // Check rigidbody type for static
+ if (info.this_rigidbody.data.body_type == Rigidbody::BodyType::STATIC) return;
+ // If second body is static perform the static collision handler in this system
+ if (info.other_rigidbody.data.body_type == Rigidbody::BodyType::STATIC) {
+ this->static_collision_handler(info);
+ };
+ // Call collision event for user
+ CollisionEvent data(info);
+ EventManager & emgr = this->mediator.event_manager;
+ emgr.trigger_event<CollisionEvent>(data, info.this_collider.game_object_id);
+}
+
+void CollisionSystem::static_collision_handler(CollisionInfo & info) {
+ // Move object back using calculate move back value
+ info.this_transform.position += info.resolution;
+
+ // If bounce is enabled mirror velocity
+ if (info.this_rigidbody.data.elastisity_coefficient > 0) {
+ if (info.resolution_direction == Direction::BOTH) {
+ info.this_rigidbody.data.linear_velocity.y
+ = -info.this_rigidbody.data.linear_velocity.y
+ * info.this_rigidbody.data.elastisity_coefficient;
+ info.this_rigidbody.data.linear_velocity.x
+ = -info.this_rigidbody.data.linear_velocity.x
+ * info.this_rigidbody.data.elastisity_coefficient;
+ } else if (info.resolution_direction == Direction::Y_DIRECTION) {
+ info.this_rigidbody.data.linear_velocity.y
+ = -info.this_rigidbody.data.linear_velocity.y
+ * info.this_rigidbody.data.elastisity_coefficient;
+ } else if (info.resolution_direction == Direction::X_DIRECTION) {
+ info.this_rigidbody.data.linear_velocity.x
+ = -info.this_rigidbody.data.linear_velocity.x
+ * info.this_rigidbody.data.elastisity_coefficient;
+ }
+ }
+ // Stop movement if bounce is disabled
+ else {
+ info.this_rigidbody.data.linear_velocity = {0, 0};
+ }
+}
+
+std::vector<std::pair<CollisionSystem::CollisionInternal, CollisionSystem::CollisionInternal>>
+CollisionSystem::gather_collisions(std::vector<CollisionInternal> & colliders) {
+
+ // TODO:
+ // If no colliders skip
+ // Check if colliders has rigidbody if not skip
+
+ // TODO:
+ // If amount is higer than lets say 16 for now use quadtree otwerwise skip
+ // Quadtree code
+ // Quadtree is placed over the input vector
+
+ // Return data of collided colliders which are variants
+ std::vector<std::pair<CollisionInternal, CollisionInternal>> collisions_ret;
+ //using visit to visit the variant to access the active and id.
+ for (size_t i = 0; i < colliders.size(); ++i) {
+ for (size_t j = i + 1; j < colliders.size(); ++j) {
+ if (colliders[i].id == colliders[j].id) continue;
+ if (!have_common_layer(colliders[i].rigidbody.data.collision_layers,
+ colliders[j].rigidbody.data.collision_layers))
+ continue;
+ CollisionInternalType type
+ = get_collider_type(colliders[i].collider, colliders[j].collider);
+ if (!get_collision(
+ {
+ .collider = colliders[i].collider,
+ .transform = colliders[i].transform,
+ .rigidbody = colliders[i].rigidbody,
+ },
+ {
+ .collider = colliders[j].collider,
+ .transform = colliders[j].transform,
+ .rigidbody = colliders[j].rigidbody,
+ },
+ type))
+ continue;
+ collisions_ret.emplace_back(colliders[i], colliders[j]);
+ }
+ }
+
+ return collisions_ret;
+}
+
+bool CollisionSystem::have_common_layer(const std::set<int> & layers1,
+ const std::set<int> & layers2) {
+
+ // Check if any number is equal in the layers
+ for (int num : layers1) {
+ if (layers2.contains(num)) {
+ // Common layer found
+ return true;
+ break;
+ }
+ }
+ // No common layer found
+ return false;
+}
+
+CollisionSystem::CollisionInternalType
+CollisionSystem::get_collider_type(const collider_variant & collider1,
+ const collider_variant & collider2) const {
+ if (std::holds_alternative<std::reference_wrapper<CircleCollider>>(collider1)) {
+ if (std::holds_alternative<std::reference_wrapper<CircleCollider>>(collider2)) {
+ return CollisionInternalType::CIRCLE_CIRCLE;
+ } else {
+ return CollisionInternalType::CIRCLE_BOX;
+ }
+ } else {
+ if (std::holds_alternative<std::reference_wrapper<CircleCollider>>(collider2)) {
+ return CollisionInternalType::BOX_CIRCLE;
+ } else {
+ return CollisionInternalType::BOX_BOX;
+ }
+ }
+}
+
+bool CollisionSystem::get_collision(const CollisionInternal & first_info,
+ const CollisionInternal & second_info,
+ CollisionInternalType type) const {
+ switch (type) {
+ case CollisionInternalType::BOX_BOX: {
+ const BoxCollider & box_collider1
+ = std::get<std::reference_wrapper<BoxCollider>>(first_info.collider);
+ const BoxCollider & box_collider2
+ = std::get<std::reference_wrapper<BoxCollider>>(second_info.collider);
+ return this->get_box_box_collision(box_collider1, box_collider2,
+ first_info.transform, second_info.transform,
+ second_info.rigidbody, second_info.rigidbody);
+ }
+ case CollisionInternalType::BOX_CIRCLE: {
+ const BoxCollider & box_collider
+ = std::get<std::reference_wrapper<BoxCollider>>(first_info.collider);
+ const CircleCollider & circle_collider
+ = std::get<std::reference_wrapper<CircleCollider>>(second_info.collider);
+ return this->get_box_circle_collision(
+ box_collider, circle_collider, first_info.transform, second_info.transform,
+ second_info.rigidbody, second_info.rigidbody);
+ }
+ case CollisionInternalType::CIRCLE_CIRCLE: {
+ const CircleCollider & circle_collider1
+ = std::get<std::reference_wrapper<CircleCollider>>(first_info.collider);
+ const CircleCollider & circle_collider2
+ = std::get<std::reference_wrapper<CircleCollider>>(second_info.collider);
+ return this->get_circle_circle_collision(
+ circle_collider1, circle_collider2, first_info.transform,
+ second_info.transform, second_info.rigidbody, second_info.rigidbody);
+ }
+ case CollisionInternalType::CIRCLE_BOX: {
+ const CircleCollider & circle_collider
+ = std::get<std::reference_wrapper<CircleCollider>>(first_info.collider);
+ const BoxCollider & box_collider
+ = std::get<std::reference_wrapper<BoxCollider>>(second_info.collider);
+ return this->get_box_circle_collision(
+ box_collider, circle_collider, first_info.transform, second_info.transform,
+ second_info.rigidbody, second_info.rigidbody);
+ }
+ }
+ return false;
+}
+
+bool CollisionSystem::get_box_box_collision(const BoxCollider & box1, const BoxCollider & box2,
+ const Transform & transform1,
+ const Transform & transform2,
+ const Rigidbody & rigidbody1,
+ const Rigidbody & rigidbody2) const {
+ // Get current positions of colliders
+ vec2 final_position1 = this->get_current_position(box1.offset, transform1, rigidbody1);
+ vec2 final_position2 = this->get_current_position(box2.offset, transform2, rigidbody2);
+
+ // Calculate half-extents (half width and half height)
+ float half_width1 = box1.dimensions.x / 2.0;
+ float half_height1 = box1.dimensions.y / 2.0;
+ float half_width2 = box2.dimensions.x / 2.0;
+ float half_height2 = box2.dimensions.y / 2.0;
+
+ // Check if the boxes overlap along the X and Y axes
+ return (final_position1.x + half_width1 > final_position2.x - half_width2
+ && final_position1.x - half_width1 < final_position2.x + half_width2
+ && final_position1.y + half_height1 > final_position2.y - half_height2
+ && final_position1.y - half_height1 < final_position2.y + half_height2);
+}
+
+bool CollisionSystem::get_box_circle_collision(const BoxCollider & box1,
+ const CircleCollider & circle2,
+ const Transform & transform1,
+ const Transform & transform2,
+ const Rigidbody & rigidbody1,
+ const Rigidbody & rigidbody2) const {
+ // Get current positions of colliders
+ vec2 final_position1 = this->get_current_position(box1.offset, transform1, rigidbody1);
+ vec2 final_position2 = this->get_current_position(circle2.offset, transform2, rigidbody2);
+
+ // Calculate box half-extents
+ float half_width = box1.dimensions.x / 2.0;
+ float half_height = box1.dimensions.y / 2.0;
+
+ // Find the closest point on the box to the circle's center
+ float closest_x = std::max(final_position1.x - half_width,
+ std::min(final_position2.x, final_position1.x + half_width));
+ float closest_y = std::max(final_position1.y - half_height,
+ std::min(final_position2.y, final_position1.y + half_height));
+
+ // Calculate the distance squared between the circle's center and the closest point on the box
+ float distance_x = final_position2.x - closest_x;
+ float distance_y = final_position2.y - closest_y;
+ float distance_squared = distance_x * distance_x + distance_y * distance_y;
+
+ // Compare distance squared with the square of the circle's radius
+ return distance_squared < circle2.radius * circle2.radius;
+}
+
+bool CollisionSystem::get_circle_circle_collision(const CircleCollider & circle1,
+ const CircleCollider & circle2,
+ const Transform & transform1,
+ const Transform & transform2,
+ const Rigidbody & rigidbody1,
+ const Rigidbody & rigidbody2) const {
+ // Get current positions of colliders
+ vec2 final_position1 = this->get_current_position(circle1.offset, transform1, rigidbody1);
+ vec2 final_position2 = this->get_current_position(circle2.offset, transform2, rigidbody2);
+
+ float distance_x = final_position1.x - final_position2.x;
+ float distance_y = final_position1.y - final_position2.y;
+ float distance_squared = distance_x * distance_x + distance_y * distance_y;
+
+ // Calculate the sum of the radii
+ float radius_sum = circle1.radius + circle2.radius;
+
+ // Check if the distance between the centers is less than or equal to the sum of the radii
+ return distance_squared < radius_sum * radius_sum;
+}
+
+vec2 CollisionSystem::get_current_position(const vec2 & collider_offset,
+ const Transform & transform,
+ const Rigidbody & rigidbody) const {
+ // Get the rotation in radians
+ float radians1 = transform.rotation * (M_PI / 180.0);
+
+ // Calculate total offset with scale
+ vec2 total_offset = (rigidbody.data.offset + collider_offset) * transform.scale;
+
+ // Rotate
+ float rotated_total_offset_x1
+ = total_offset.x * cos(radians1) - total_offset.y * sin(radians1);
+ float rotated_total_offset_y1
+ = total_offset.x * sin(radians1) + total_offset.y * cos(radians1);
+
+ // Final positions considering scaling and rotation
+ return (transform.position + vec2(rotated_total_offset_x1, rotated_total_offset_y1));
+}
diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h
index c1a70d8..5b136c6 100644
--- a/src/crepe/system/CollisionSystem.h
+++ b/src/crepe/system/CollisionSystem.h
@@ -1,13 +1,311 @@
#pragma once
+#include <optional>
+#include <variant>
+#include <vector>
+
+#include "api/BoxCollider.h"
+#include "api/CircleCollider.h"
+#include "api/Event.h"
+#include "api/Metadata.h"
+#include "api/Rigidbody.h"
+#include "api/Transform.h"
+#include "api/Vector2.h"
+
+#include "Collider.h"
#include "System.h"
namespace crepe {
+//! A system responsible for detecting and handling collisions between colliders.
class CollisionSystem : public System {
public:
using System::System;
+
+private:
+ //! A variant type that can hold either a BoxCollider or a CircleCollider.
+ using collider_variant = std::variant<std::reference_wrapper<BoxCollider>,
+ std::reference_wrapper<CircleCollider>>;
+
+ //! Enum representing the types of collider pairs for collision detection.
+ enum class CollisionInternalType {
+ BOX_BOX,
+ CIRCLE_CIRCLE,
+ BOX_CIRCLE,
+ CIRCLE_BOX,
+ };
+
+ /**
+ * \brief A structure to store the collision data of a single collider.
+ *
+ * This structure all components and id that are for needed within this system when calculating or handeling collisions.
+ * The transform and rigidbody are mostly needed for location and rotation.
+ * In rigidbody additional info is written about what the body of the object is,
+ * and how it should respond on a collision.
+ */
+ struct CollisionInternal {
+ game_object_id_t id = 0;
+ collider_variant collider;
+ Transform & transform;
+ Rigidbody & rigidbody;
+ };
+
+ //! Enum representing movement directions during collision resolution.
+ enum class Direction {
+ //! No movement required.
+ NONE,
+ //! Movement in the X direction.
+ X_DIRECTION,
+ //! Movement in the Y direction.
+ Y_DIRECTION,
+ //! Movement in both X and Y directions.
+ BOTH
+ };
+
+public:
+ /**
+ * \brief Structure representing detailed collision information between two colliders.
+ *
+ * Includes information about the colliding objects and the resolution data for handling the collision.
+ */
+ struct CollisionInfo {
+ Collider & this_collider;
+ Transform & this_transform;
+ Rigidbody & this_rigidbody;
+ Metadata & this_metadata;
+ Collider & other_collider;
+ Transform & other_transform;
+ Rigidbody & other_rigidbody;
+ Metadata & other_metadata;
+ //! The resolution vector for the collision.
+ vec2 resolution;
+ //! The direction of movement for resolving the collision.
+ Direction resolution_direction = Direction::NONE;
+ };
+
+public:
+ //! Updates the collision system by checking for collisions between colliders and handling them.
void update() override;
+
+private:
+ /**
+ * \brief Determines the type of collider pair from two colliders.
+ *
+ * Uses std::holds_alternative to identify the types of the provided colliders.
+ *
+ * \param collider1 First collider variant (BoxCollider or CircleCollider).
+ * \param collider2 Second collider variant (BoxCollider or CircleCollider).
+ * \return The combined type of the two colliders.
+ */
+ CollisionInternalType get_collider_type(const collider_variant & collider1,
+ const collider_variant & collider2) const;
+
+ /**
+ * \brief Calculates the current position of a collider.
+ *
+ * Combines the Collider offset, Transform position, and Rigidbody offset to compute the position of the collider.
+ *
+ * \param collider_offset The offset of the collider.
+ * \param transform The Transform of the associated game object.
+ * \param rigidbody The Rigidbody of the associated game object.
+ * \return The calculated position of the collider.
+ */
+ vec2 get_current_position(const vec2 & collider_offset, const Transform & transform,
+ const Rigidbody & rigidbody) const;
+
+private:
+ /**
+ * \brief Handles collision resolution between two colliders.
+ *
+ * Processes collision data and adjusts objects to resolve collisions and/or calls the user oncollision script function.
+ *
+ * \param data1 Collision data for the first collider.
+ * \param data2 Collision data for the second collider.
+ */
+ void collision_handler_request(CollisionInternal & data1, CollisionInternal & data2);
+
+ /**
+ * \brief Resolves collision between two colliders and calculates the movement required.
+ *
+ * Determines the displacement and direction needed to separate colliders based on their types.
+ *
+ * \param data1 Collision data for the first collider.
+ * \param data2 Collision data for the second collider.
+ * \param type The type of collider pair.
+ * \return A pair containing the resolution vector and direction for the first collider.
+ */
+ std::pair<vec2, Direction> collision_handler(CollisionInternal & data1,
+ CollisionInternal & data2,
+ CollisionInternalType type);
+
+ /**
+ * \brief Calculates the resolution vector for two BoxColliders.
+ *
+ * Computes the displacement required to separate two overlapping BoxColliders.
+ *
+ * \param box_collider1 The first BoxCollider.
+ * \param box_collider2 The second BoxCollider.
+ * \param position1 The position of the first BoxCollider.
+ * \param position2 The position of the second BoxCollider.
+ * \return The resolution vector for the collision.
+ */
+ vec2 get_box_box_resolution(const BoxCollider & box_collider1,
+ const BoxCollider & box_collider2, const vec2 & position1,
+ const vec2 & position2) const;
+
+ /**
+ * \brief Calculates the resolution vector for two CircleCollider.
+ *
+ * Computes the displacement required to separate two overlapping CircleCollider.
+ *
+ * \param circle_collider1 The first CircleCollider.
+ * \param circle_collider2 The second CircleCollider.
+ * \param final_position1 The position of the first CircleCollider.
+ * \param final_position2 The position of the second CircleCollider.
+ * \return The resolution vector for the collision.
+ */
+ vec2 get_circle_circle_resolution(const CircleCollider & circle_collider1,
+ const CircleCollider & circle_collider2,
+ const vec2 & final_position1,
+ const vec2 & final_position2) const;
+
+ /**
+ * \brief Calculates the resolution vector for two CircleCollider.
+ *
+ * Computes the displacement required to separate two overlapping CircleCollider.
+ *
+ * \param circle_collider The first CircleCollider.
+ * \param box_collider The second CircleCollider.
+ * \param circle_position The position of the CircleCollider.
+ * \param box_position The position of the BoxCollider.
+ * \return The resolution vector for the collision.
+ */
+ vec2 get_circle_box_resolution(const CircleCollider & circle_collider,
+ const BoxCollider & box_collider,
+ const vec2 & circle_position,
+ const vec2 & box_position) const;
+
+ /**
+ * \brief Determines the appropriate collision handler for a collision.
+ *
+ * Decides the correct resolution process based on the dynamic or static nature of the colliders involved.
+ *
+ * \param info Collision information containing data about both colliders.
+ */
+ void determine_collision_handler(CollisionInfo & info);
+
+ /**
+ * \brief Handles collisions involving static objects.
+ *
+ * Resolves collisions by adjusting positions and modifying velocities if bounce is enabled.
+ *
+ * \param info Collision information containing data about both colliders.
+ */
+ void static_collision_handler(CollisionInfo & info);
+
+private:
+ /**
+ * \brief Checks for collisions between colliders.
+ *
+ * Identifies collisions and generates pairs of colliding objects for further processing.
+ *
+ * \param colliders A collection of all active colliders.
+ * \return A list of collision pairs with their associated data.
+ */
+ std::vector<std::pair<CollisionInternal, CollisionInternal>>
+ gather_collisions(std::vector<CollisionInternal> & colliders);
+
+ /**
+ * \brief Checks if two collision layers have at least one common layer.
+ *
+ * This function checks if there is any overlapping layer between the two inputs.
+ * It compares each layer from the first input to see
+ * if it exists in the second input. If at least one common layer is found,
+ * the function returns true, indicating that the two colliders share a common
+ * collision layer.
+ *
+ * \param layers1 all collision layers for the first collider.
+ * \param layers2 all collision layers for the second collider.
+ * \return Returns true if there is at least one common layer, false otherwise.
+ */
+
+ bool have_common_layer(const std::set<int> & layers1, const std::set<int> & layers2);
+
+ /**
+ * \brief Checks for collision between two colliders.
+ *
+ * Calls the appropriate collision detection function based on the collider types.
+ *
+ * \param first_info Collision data for the first collider.
+ * \param second_info Collision data for the second collider.
+ * \param type The type of collider pair.
+ * \return True if a collision is detected, otherwise false.
+ */
+ bool get_collision(const CollisionInternal & first_info,
+ const CollisionInternal & second_info,
+ CollisionInternalType type) const;
+
+ /**
+ * \brief Detects collisions between two BoxColliders.
+ *
+ * \param box1 The first BoxCollider.
+ * \param box2 The second BoxCollider.
+ * \param transform1 Transform of the first object.
+ * \param transform2 Transform of the second object.
+ * \param rigidbody1 Rigidbody of the first object.
+ * \param rigidbody2 Rigidbody of the second object.
+ * \return True if a collision is detected, otherwise false.
+ */
+ bool get_box_box_collision(const BoxCollider & box1, const BoxCollider & box2,
+ const Transform & transform1, const Transform & transform2,
+ const Rigidbody & rigidbody1,
+ const Rigidbody & rigidbody2) const;
+
+ /**
+ * \brief Check collision for box on circle collider
+ *
+ * \param box1 The BoxCollider
+ * \param circle2 The CircleCollider
+ * \param transform1 Transform of the first object.
+ * \param transform2 Transform of the second object.
+ * \param rigidbody1 Rigidbody of the first object.
+ * \param rigidbody2 Rigidbody of the second object.
+ * \return True if a collision is detected, otherwise false.
+ */
+ bool get_box_circle_collision(const BoxCollider & box1, const CircleCollider & circle2,
+ const Transform & transform1, const Transform & transform2,
+ const Rigidbody & rigidbody1,
+ const Rigidbody & rigidbody2) const;
+
+ /**
+ * \brief Check collision for circle on circle collider
+ *
+ * \param circle1 First CircleCollider
+ * \param circle2 Second CircleCollider
+ * \param transform1 Transform of the first object.
+ * \param transform2 Transform of the second object.
+ * \param rigidbody1 Rigidbody of the first object.
+ * \param rigidbody2 Rigidbody of the second object.
+ * \return True if a collision is detected, otherwise false.
+ *
+ * \return status of collision
+ */
+ bool get_circle_circle_collision(const CircleCollider & circle1,
+ const CircleCollider & circle2,
+ const Transform & transform1,
+ const Transform & transform2,
+ const Rigidbody & rigidbody1,
+ const Rigidbody & rigidbody2) const;
+};
+
+/**
+ * \brief Event triggered during a collision between objects.
+ */
+class CollisionEvent : public Event {
+public:
+ crepe::CollisionSystem::CollisionInfo info;
+ CollisionEvent(const crepe::CollisionSystem::CollisionInfo & collisionInfo)
+ : info(collisionInfo) {}
};
} // namespace crepe
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp
new file mode 100644
index 0000000..a710ae2
--- /dev/null
+++ b/src/crepe/system/InputSystem.cpp
@@ -0,0 +1,190 @@
+#include "../api/Button.h"
+#include "../manager/ComponentManager.h"
+#include "../manager/EventManager.h"
+#include "facade/SDLContext.h"
+#include "util/Log.h"
+
+#include "InputSystem.h"
+
+using namespace crepe;
+
+void InputSystem::update() {
+ ComponentManager & mgr = this->mediator.component_manager;
+ EventManager & event_mgr = this->mediator.event_manager;
+ SDLContext & context = this->mediator.sdl_context;
+ std::vector<SDLContext::EventData> event_list = context.get_events();
+ RefVector<Button> buttons = mgr.get_components_by_type<Button>();
+ RefVector<Camera> cameras = mgr.get_components_by_type<Camera>();
+ OptionalRef<Camera> curr_cam_ref;
+ // Find the active camera
+ for (Camera & cam : cameras) {
+ if (!cam.active) continue;
+ curr_cam_ref = cam;
+ break;
+ }
+ if (!curr_cam_ref) return;
+ Camera & current_cam = curr_cam_ref;
+ RefVector<Transform> transform_vec
+ = mgr.get_components_by_id<Transform>(current_cam.game_object_id);
+ Transform & cam_transform = transform_vec.front().get();
+ int camera_origin_x = cam_transform.position.x + current_cam.data.postion_offset.x
+ - (current_cam.viewport_size.x / 2);
+ int camera_origin_y = cam_transform.position.y + current_cam.data.postion_offset.y
+ - (current_cam.viewport_size.y / 2);
+
+ for (const SDLContext::EventData & event : event_list) {
+ int world_mouse_x = event.mouse_position.x + camera_origin_x;
+ int world_mouse_y = event.mouse_position.y + camera_origin_y;
+ // check if the mouse is within the viewport
+ bool mouse_in_viewport
+ = !(world_mouse_x < camera_origin_x
+ || world_mouse_x > camera_origin_x + current_cam.viewport_size.x
+ || world_mouse_y < camera_origin_y
+ || world_mouse_y > camera_origin_y + current_cam.viewport_size.y);
+
+ switch (event.event_type) {
+ case SDLContext::EventType::KEYDOWN:
+ event_mgr.queue_event<KeyPressEvent>(KeyPressEvent{
+ .repeat = event.key_repeat,
+ .key = event.key,
+ });
+ break;
+ case SDLContext::EventType::KEYUP:
+ event_mgr.queue_event<KeyReleaseEvent>(KeyReleaseEvent{
+ .key = event.key,
+ });
+ break;
+ case SDLContext::EventType::MOUSEDOWN:
+ if (!mouse_in_viewport) {
+ break;
+ }
+ event_mgr.queue_event<MousePressEvent>(MousePressEvent{
+ .mouse_x = world_mouse_x,
+ .mouse_y = world_mouse_y,
+ .button = event.mouse_button,
+ });
+ this->last_mouse_down_position = {world_mouse_x, world_mouse_y};
+ this->last_mouse_button = event.mouse_button;
+ break;
+ case SDLContext::EventType::MOUSEUP: {
+ if (!mouse_in_viewport) {
+ break;
+ }
+ event_mgr.queue_event<MouseReleaseEvent>(MouseReleaseEvent{
+ .mouse_x = world_mouse_x,
+ .mouse_y = world_mouse_y,
+ .button = event.mouse_button,
+ });
+ //check if its a click by checking the last button down
+ int delta_x = world_mouse_x - this->last_mouse_down_position.x;
+ int delta_y = world_mouse_y - this->last_mouse_down_position.y;
+
+ if (this->last_mouse_button == event.mouse_button
+ && std::abs(delta_x) <= click_tolerance
+ && std::abs(delta_y) <= click_tolerance) {
+ event_mgr.queue_event<MouseClickEvent>(MouseClickEvent{
+ .mouse_x = world_mouse_x,
+ .mouse_y = world_mouse_y,
+ .button = event.mouse_button,
+ });
+
+ this->handle_click(event.mouse_button, world_mouse_x, world_mouse_y);
+ }
+ } break;
+ case SDLContext::EventType::MOUSEMOVE:
+ if (!mouse_in_viewport) {
+ break;
+ }
+ event_mgr.queue_event<MouseMoveEvent>(MouseMoveEvent{
+ .mouse_x = world_mouse_x,
+ .mouse_y = world_mouse_y,
+ .delta_x = event.rel_mouse_move.x,
+ .delta_y = event.rel_mouse_move.y,
+ });
+ this->handle_move(event, world_mouse_x, world_mouse_y);
+ break;
+ case SDLContext::EventType::MOUSEWHEEL:
+ event_mgr.queue_event<MouseScrollEvent>(MouseScrollEvent{
+ .mouse_x = world_mouse_x,
+ .mouse_y = world_mouse_y,
+ .scroll_direction = event.scroll_direction,
+ .scroll_delta = event.scroll_delta,
+ });
+ break;
+ case SDLContext::EventType::SHUTDOWN:
+ event_mgr.queue_event<ShutDownEvent>(ShutDownEvent{});
+ break;
+ default:
+ break;
+ }
+ }
+}
+void InputSystem::handle_move(const SDLContext::EventData & event_data,
+ const int world_mouse_x, const int world_mouse_y) {
+ ComponentManager & mgr = this->mediator.component_manager;
+
+ RefVector<Button> buttons = mgr.get_components_by_type<Button>();
+
+ for (Button & button : buttons) {
+ RefVector<Transform> transform_vec
+ = mgr.get_components_by_id<Transform>(button.game_object_id);
+ Transform & transform(transform_vec.front().get());
+
+ bool was_hovering = button.hover;
+ if (button.active
+ && this->is_mouse_inside_button(world_mouse_x, world_mouse_y, button, transform)) {
+ button.hover = true;
+ if (!was_hovering && button.on_mouse_enter) {
+ button.on_mouse_enter();
+ }
+ } else {
+ button.hover = false;
+ // Trigger the on_exit callback if the hover state just changed to false
+ if (was_hovering && button.on_mouse_exit) {
+ button.on_mouse_exit();
+ }
+ }
+ }
+}
+
+void InputSystem::handle_click(const MouseButton & mouse_button, const int world_mouse_x,
+ const int world_mouse_y) {
+ ComponentManager & mgr = this->mediator.component_manager;
+
+ RefVector<Button> buttons = mgr.get_components_by_type<Button>();
+
+ for (Button & button : buttons) {
+ RefVector<Transform> transform_vec
+ = mgr.get_components_by_id<Transform>(button.game_object_id);
+ Transform & transform = transform_vec.front().get();
+
+ if (button.active
+ && this->is_mouse_inside_button(world_mouse_x, world_mouse_y, button, transform)) {
+ this->handle_button_press(button);
+ }
+ }
+}
+
+bool InputSystem::is_mouse_inside_button(const int mouse_x, const int mouse_y,
+ const Button & button, const Transform & transform) {
+ int actual_x = transform.position.x + button.offset.x;
+ int actual_y = transform.position.y + button.offset.y;
+
+ int half_width = button.dimensions.x / 2;
+ int half_height = button.dimensions.y / 2;
+
+ // Check if the mouse is within the button's boundaries
+ return mouse_x >= actual_x - half_width && mouse_x <= actual_x + half_width
+ && mouse_y >= actual_y - half_height && mouse_y <= actual_y + half_height;
+}
+
+void InputSystem::handle_button_press(Button & button) {
+ if (button.is_toggle) {
+ if (!button.is_pressed && button.on_click) {
+ button.on_click();
+ }
+ button.is_pressed = !button.is_pressed;
+ } else if (button.on_click) {
+ button.on_click();
+ }
+}
diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h
new file mode 100644
index 0000000..87e86f8
--- /dev/null
+++ b/src/crepe/system/InputSystem.h
@@ -0,0 +1,85 @@
+#pragma once
+
+#include "../facade/SDLContext.h"
+#include "../types.h"
+#include "../util/OptionalRef.h"
+
+#include "System.h"
+
+namespace crepe {
+
+class Camera;
+class Button;
+class Transform;
+
+/**
+ * \brief Handles the processing of input events created by SDLContext
+ *
+ * This system processes events such as mouse clicks, mouse movement, and keyboard
+ * actions. It is responsible for detecting interactions with UI buttons and
+ * passing the corresponding events to the registered listeners.
+ */
+class InputSystem : public System {
+public:
+ using System::System;
+
+ /**
+ * \brief Updates the system, processing all input events.
+ * This method processes all events and triggers corresponding actions.
+ */
+ void update() override;
+
+private:
+ //! Stores the last position of the mouse when the button was pressed.
+ ivec2 last_mouse_down_position;
+ // TODO: specify world/hud space and make regular `vec2`
+
+ //! Stores the last mouse button pressed.
+ MouseButton last_mouse_button = MouseButton::NONE;
+
+ //! The maximum allowable distance between mouse down and mouse up to register as a click.
+ const int click_tolerance = 5;
+
+ /**
+ * \brief Handles the mouse click event.
+ * \param mouse_button The mouse button involved in the click.
+ * \param world_mouse_x The X coordinate of the mouse in world space.
+ * \param world_mouse_y The Y coordinate of the mouse in world space.
+ *
+ * This method processes the mouse click event and triggers the corresponding button action.
+ */
+ void handle_click(const MouseButton & mouse_button, const int world_mouse_x,
+ const int world_mouse_y);
+
+ /**
+ * \brief Handles the mouse movement event.
+ * \param event_data The event data containing information about the mouse movement.
+ * \param world_mouse_x The X coordinate of the mouse in world space.
+ * \param world_mouse_y The Y coordinate of the mouse in world space.
+ *
+ * This method processes the mouse movement event and updates the button hover state.
+ */
+ void handle_move(const SDLContext::EventData & event_data, const int world_mouse_x,
+ const int world_mouse_y);
+
+ /**
+ * \brief Checks if the mouse position is inside the bounds of the button.
+ * \param world_mouse_x The X coordinate of the mouse in world space.
+ * \param world_mouse_y The Y coordinate of the mouse in world space.
+ * \param button The button to check.
+ * \param transform The transform component of the button.
+ * \return True if the mouse is inside the button, false otherwise.
+ */
+ bool is_mouse_inside_button(const int world_mouse_x, const int world_mouse_y,
+ const Button & button, const Transform & transform);
+
+ /**
+ * \brief Handles the button press event, calling the on_click callback if necessary.
+ * \param button The button being pressed.
+ *
+ * This method triggers the on_click action for the button when it is pressed.
+ */
+ void handle_button_press(Button & button);
+};
+
+} // namespace crepe
diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp
index 0e62a57..b14c52f 100644
--- a/src/crepe/system/ParticleSystem.cpp
+++ b/src/crepe/system/ParticleSystem.cpp
@@ -2,17 +2,17 @@
#include <cstdlib>
#include <ctime>
-#include "api/ParticleEmitter.h"
-#include "api/Transform.h"
+#include "../api/ParticleEmitter.h"
+#include "../api/Transform.h"
+#include "../manager/ComponentManager.h"
-#include "ComponentManager.h"
#include "ParticleSystem.h"
using namespace crepe;
void ParticleSystem::update() {
// Get all emitters
- ComponentManager & mgr = this->component_manager;
+ ComponentManager & mgr = this->mediator.component_manager;
RefVector<ParticleEmitter> emitters = mgr.get_components_by_type<ParticleEmitter>();
for (ParticleEmitter & emitter : emitters) {
diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h
index c647284..068f01c 100644
--- a/src/crepe/system/ParticleSystem.h
+++ b/src/crepe/system/ParticleSystem.h
@@ -25,7 +25,7 @@ public:
private:
/**
* \brief Emits a particle from the specified emitter based on its emission properties.
- *
+ *
* \param emitter Reference to the ParticleEmitter.
* \param transform Const reference to the Transform component associated with the emitter.
*/
@@ -34,7 +34,7 @@ private:
/**
* \brief Calculates the number of times particles should be emitted based on emission rate
* and update count.
- *
+ *
* \param count Current update count.
* \param emission Emission rate.
* \return The number of particles to emit.
@@ -44,7 +44,7 @@ private:
/**
* \brief Checks whether particles are within the emitter’s boundary, resets or stops
* particles if they exit.
- *
+ *
* \param emitter Reference to the ParticleEmitter.
* \param transform Const reference to the Transform component associated with the emitter.
*/
@@ -52,7 +52,7 @@ private:
/**
* \brief Generates a random angle for particle emission within the specified range.
- *
+ *
* \param min_angle Minimum emission angle in degrees.
* \param max_angle Maximum emission angle in degrees.
* \return Random angle in degrees.
@@ -61,7 +61,7 @@ private:
/**
* \brief Generates a random speed for particle emission within the specified range.
- *
+ *
* \param min_speed Minimum emission speed.
* \param max_speed Maximum emission speed.
* \return Random speed.
diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp
index 514a4b3..ebf4439 100644
--- a/src/crepe/system/PhysicsSystem.cpp
+++ b/src/crepe/system/PhysicsSystem.cpp
@@ -1,17 +1,17 @@
#include <cmath>
-#include "../ComponentManager.h"
#include "../api/Config.h"
#include "../api/Rigidbody.h"
#include "../api/Transform.h"
#include "../api/Vector2.h"
+#include "../manager/ComponentManager.h"
#include "PhysicsSystem.h"
using namespace crepe;
void PhysicsSystem::update() {
- ComponentManager & mgr = this->component_manager;
+ ComponentManager & mgr = this->mediator.component_manager;
RefVector<Rigidbody> rigidbodies = mgr.get_components_by_type<Rigidbody>();
RefVector<Transform> transforms = mgr.get_components_by_type<Transform>();
@@ -25,17 +25,20 @@ void PhysicsSystem::update() {
if (transform.game_object_id == rigidbody.game_object_id) {
// Add gravity
- if (rigidbody.data.use_gravity) {
+ if (rigidbody.data.gravity_scale > 0) {
rigidbody.data.linear_velocity.y
+= (rigidbody.data.mass * rigidbody.data.gravity_scale
* gravity);
}
// Add damping
- if (rigidbody.data.angular_damping != 0) {
- rigidbody.data.angular_velocity *= rigidbody.data.angular_damping;
+ if (rigidbody.data.angular_velocity_coefficient > 0) {
+ rigidbody.data.angular_velocity
+ *= rigidbody.data.angular_velocity_coefficient;
}
- if (rigidbody.data.linear_damping != vec2{0, 0}) {
- rigidbody.data.linear_velocity *= rigidbody.data.linear_damping;
+ if (rigidbody.data.linear_velocity_coefficient.x > 0
+ && rigidbody.data.linear_velocity_coefficient.y > 0) {
+ rigidbody.data.linear_velocity
+ *= rigidbody.data.linear_velocity_coefficient;
}
// Max velocity check
diff --git a/src/crepe/system/PhysicsSystem.h b/src/crepe/system/PhysicsSystem.h
index 227ab69..26152a5 100644
--- a/src/crepe/system/PhysicsSystem.h
+++ b/src/crepe/system/PhysicsSystem.h
@@ -6,7 +6,7 @@ namespace crepe {
/**
* \brief System that controls all physics
- *
+ *
* This class is a physics system that uses a rigidbody and transform to add physics to a game
* object.
*/
@@ -15,7 +15,7 @@ public:
using System::System;
/**
* \brief updates the physics system.
- *
+ *
* It calculates new velocties and changes the postion in the transform.
*/
void update() override;
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index 11c9669..51340fb 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -5,25 +5,33 @@
#include <stdexcept>
#include <vector>
-#include "../ComponentManager.h"
#include "../api/Camera.h"
#include "../api/ParticleEmitter.h"
#include "../api/Sprite.h"
#include "../api/Transform.h"
#include "../facade/SDLContext.h"
+#include "../facade/Texture.h"
+#include "../manager/ComponentManager.h"
+#include "../manager/ResourceManager.h"
#include "RenderSystem.h"
using namespace crepe;
using namespace std;
-void RenderSystem::clear_screen() { this->context.clear_screen(); }
-
-void RenderSystem::present_screen() { this->context.present_screen(); }
+void RenderSystem::clear_screen() {
+ SDLContext & ctx = this->mediator.sdl_context;
+ ctx.clear_screen();
+}
-const Camera & RenderSystem::update_camera() {
- ComponentManager & mgr = this->component_manager;
+void RenderSystem::present_screen() {
+ SDLContext & ctx = this->mediator.sdl_context;
+ ctx.present_screen();
+}
+SDLContext::CameraValues RenderSystem::update_camera() {
+ ComponentManager & mgr = this->mediator.component_manager;
+ SDLContext & ctx = this->mediator.sdl_context;
RefVector<Camera> cameras = mgr.get_components_by_type<Camera>();
if (cameras.size() == 0) throw std::runtime_error("No cameras in current scene");
@@ -32,16 +40,18 @@ const Camera & RenderSystem::update_camera() {
if (!cam.active) continue;
const Transform & transform
= mgr.get_components_by_id<Transform>(cam.game_object_id).front().get();
- this->context.set_camera(cam);
- this->cam_pos = transform.position + cam.offset;
- return cam;
+ SDLContext::CameraValues cam_val = ctx.set_camera(cam);
+ cam_val.cam_pos = transform.position + cam.data.postion_offset;
+ return cam_val;
}
throw std::runtime_error("No active cameras in current scene");
}
bool sorting_comparison(const Sprite & a, const Sprite & b) {
- if (a.sorting_in_layer < b.sorting_in_layer) return true;
- if (a.sorting_in_layer == b.sorting_in_layer) return a.order_in_layer < b.order_in_layer;
+ if (a.data.sorting_in_layer != b.data.sorting_in_layer)
+ return a.data.sorting_in_layer < b.data.sorting_in_layer;
+ if (a.data.order_in_layer != b.data.order_in_layer)
+ return a.data.order_in_layer < b.data.order_in_layer;
return false;
}
@@ -59,10 +69,13 @@ void RenderSystem::update() {
this->present_screen();
}
-bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam,
+bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam,
const double & scale) {
- ComponentManager & mgr = this->component_manager;
+ ComponentManager & mgr = this->mediator.component_manager;
+ SDLContext & ctx = this->mediator.sdl_context;
+ ResourceManager & resource_manager = this->mediator.resource_manager;
+ Texture & res = resource_manager.get<Texture>(sprite.source);
vector<reference_wrapper<ParticleEmitter>> emitters
= mgr.get_components_by_id<ParticleEmitter>(sprite.game_object_id);
@@ -77,10 +90,10 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam,
for (const Particle & p : em.data.particles) {
if (!p.active) continue;
- this->context.draw(SDLContext::RenderContext{
+ ctx.draw(SDLContext::RenderContext{
.sprite = sprite,
+ .texture = res,
.cam = cam,
- .cam_pos = this->cam_pos,
.pos = p.position,
.angle = p.angle,
.scale = scale,
@@ -89,12 +102,16 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam,
}
return rendering_particles;
}
-void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam,
+void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam,
const Transform & tm) {
- this->context.draw(SDLContext::RenderContext{
+ SDLContext & ctx = this->mediator.sdl_context;
+ ResourceManager & resource_manager = this->mediator.resource_manager;
+ const Texture & res = resource_manager.get<Texture>(sprite.source);
+
+ ctx.draw(SDLContext::RenderContext{
.sprite = sprite,
+ .texture = res,
.cam = cam,
- .cam_pos = this->cam_pos,
.pos = tm.position,
.angle = tm.rotation,
.scale = tm.scale,
@@ -102,8 +119,8 @@ void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam,
}
void RenderSystem::render() {
- ComponentManager & mgr = this->component_manager;
- const Camera & cam = this->update_camera();
+ ComponentManager & mgr = this->mediator.component_manager;
+ const SDLContext::CameraValues & cam = this->update_camera();
RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>();
RefVector<Sprite> sorted_sprites = this->sort(sprites);
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index 096d058..e270a6b 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -18,7 +18,7 @@ class Transform;
* \brief Manages rendering operations for all game objects.
*
* RenderSystem is responsible for rendering, clearing and presenting the screen, and
- * managing the active camera.
+ * managing the active camera.
*/
class RenderSystem : public System {
public:
@@ -37,7 +37,7 @@ private:
void present_screen();
//! Updates the active camera used for rendering.
- const Camera & update_camera();
+ SDLContext::CameraValues update_camera();
//! Renders the whole screen
void render();
@@ -52,20 +52,22 @@ private:
* constructor is now protected i cannot make tmp inside
* \return true if particles have been rendered
*/
- bool render_particle(const Sprite & sprite, const Camera & cam, const double & scale);
+ bool render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam,
+ const double & scale);
/**
- * \brief renders a sprite with a Transform component on the screen
+ * \brief renders a sprite with a Transform component on the screen
*
* \param sprite the sprite component that holds all the data
- * \param tm the Transform component that holds the position,rotation and scale
+ * \param tm the Transform component that holds the position,rotation and scale
*/
- void render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm);
+ void render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam,
+ const Transform & tm);
/**
* \brief sort a vector sprite objects with
*
- * \param objs the vector that will do a sorting algorithm on
+ * \param objs the vector that will do a sorting algorithm on
* \return returns a sorted reference vector
*/
RefVector<Sprite> sort(RefVector<Sprite> & objs) const;
@@ -75,13 +77,6 @@ private:
* \todo Implement a text component and a button component.
* \todo Consider adding text input functionality.
*/
-
-private:
- // FIXME: retrieve sdlcontext via mediator after #PR57
- SDLContext & context = SDLContext::get_instance();
-
- //! camera postion in the current scene
- vec2 cam_pos;
};
} // namespace crepe
diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp
index 20a83f7..d6b2ca1 100644
--- a/src/crepe/system/ScriptSystem.cpp
+++ b/src/crepe/system/ScriptSystem.cpp
@@ -1,6 +1,6 @@
-#include "../ComponentManager.h"
#include "../api/BehaviorScript.h"
#include "../api/Script.h"
+#include "../manager/ComponentManager.h"
#include "ScriptSystem.h"
@@ -10,7 +10,7 @@ using namespace crepe;
void ScriptSystem::update() {
dbg_trace();
- ComponentManager & mgr = this->component_manager;
+ ComponentManager & mgr = this->mediator.component_manager;
RefVector<BehaviorScript> behavior_scripts = mgr.get_components_by_type<BehaviorScript>();
for (BehaviorScript & behavior_script : behavior_scripts) {
diff --git a/src/crepe/system/ScriptSystem.h b/src/crepe/system/ScriptSystem.h
index 936e9ca..3db1b1e 100644
--- a/src/crepe/system/ScriptSystem.h
+++ b/src/crepe/system/ScriptSystem.h
@@ -8,7 +8,7 @@ class Script;
/**
* \brief Script system
- *
+ *
* The script system is responsible for all \c BehaviorScript components, and
* calls the methods on classes derived from \c Script.
*/
diff --git a/src/crepe/system/System.cpp b/src/crepe/system/System.cpp
index 937a423..ecc740d 100644
--- a/src/crepe/system/System.cpp
+++ b/src/crepe/system/System.cpp
@@ -1,7 +1,5 @@
-#include "../util/Log.h"
-
#include "System.h"
using namespace crepe;
-System::System(ComponentManager & mgr) : component_manager(mgr) { dbg_trace(); }
+System::System(const Mediator & mediator) : mediator(mediator) {}
diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h
index 28ea20e..063dfbf 100644
--- a/src/crepe/system/System.h
+++ b/src/crepe/system/System.h
@@ -1,5 +1,7 @@
#pragma once
+#include "../manager/Mediator.h"
+
namespace crepe {
class ComponentManager;
@@ -7,9 +9,8 @@ class ComponentManager;
/**
* \brief Base ECS system class
*
- * This class is used as the base for all system classes. Classes derived from
- * System must implement the System::update() method and copy Script::Script
- * with the `using`-syntax.
+ * This class is used as the base for all system classes. Classes derived from System must
+ * implement the System::update() method and copy Script::Script with the `using`-syntax.
*/
class System {
public:
@@ -19,11 +20,11 @@ public:
virtual void update() = 0;
public:
- System(ComponentManager &);
+ System(const Mediator & m);
virtual ~System() = default;
protected:
- ComponentManager & component_manager;
+ const Mediator & mediator;
};
} // namespace crepe
diff --git a/src/crepe/util/OptionalRef.h b/src/crepe/util/OptionalRef.h
index 3201667..1b2cb3f 100644
--- a/src/crepe/util/OptionalRef.h
+++ b/src/crepe/util/OptionalRef.h
@@ -25,7 +25,7 @@ public:
*/
OptionalRef<T> & operator=(T & ref);
/**
- * \brief Retrieve this reference
+ * \brief Retrieve this reference (cast)
*
* \returns Internal reference if it is set
*
@@ -33,6 +33,14 @@ public:
*/
operator T &() const;
/**
+ * \brief Retrieve this reference (member access)
+ *
+ * \returns Internal reference if it is set
+ *
+ * \throws std::runtime_error if this function is called while the reference it not set
+ */
+ T * operator->() const;
+ /**
* \brief Check if this reference is not empty
*
* \returns `true` if reference is set, or `false` if it is not
diff --git a/src/crepe/util/OptionalRef.hpp b/src/crepe/util/OptionalRef.hpp
index 4608c9e..5e36b3a 100644
--- a/src/crepe/util/OptionalRef.hpp
+++ b/src/crepe/util/OptionalRef.hpp
@@ -19,6 +19,13 @@ OptionalRef<T>::operator T &() const {
}
template <typename T>
+T * OptionalRef<T>::operator->() const {
+ if (this->ref == nullptr)
+ throw std::runtime_error("OptionalRef: attempt to dereference nullptr");
+ return this->ref;
+}
+
+template <typename T>
OptionalRef<T> & OptionalRef<T>::operator=(T & ref) {
this->ref = &ref;
return *this;
diff --git a/src/doc/feature/animator_creation.dox b/src/doc/feature/animator_creation.dox
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/doc/feature/animator_creation.dox
diff --git a/src/doc/feature/scene.dox b/src/doc/feature/scene.dox
index d81df4c..4124e37 100644
--- a/src/doc/feature/scene.dox
+++ b/src/doc/feature/scene.dox
@@ -6,10 +6,11 @@ namespace crepe {
\ingroup feature
\brief User-defined scenes
-Scenes can be used to implement game environments, and allow arbitrary game objects to be organized
-as part of the game structure. Scenes are implemented as derivative classes of Scene, which are
-added to the game using the SceneManager. Scenes describe the start of a Scene and cannot modify
-GameObjects during runtime of a Scene (use \ref feature_script "Scripting" for this purpose).
+Scenes can be used to implement game environments, and allow arbitrary game
+objects to be organized as part of the game structure. Scenes are implemented as
+derivative classes of Scene, which are added to the game using the SceneManager.
+Scenes describe the start of a Scene and cannot modify GameObjects during
+runtime of a Scene (use \ref feature_script for this purpose).
\see SceneManager
\see GameObject
@@ -18,19 +19,25 @@ GameObjects during runtime of a Scene (use \ref feature_script "Scripting" for t
\par Example
-This example demonstrates how to define and add scenes to the loop/scene manager in the `crepe` framework.
-Each concrete scene should be derived from Scene. In the example below, the concrete scene is named MyScene.
-A concrete scene should, at least, implement (override) two methods, namely load_scene() and get_name(). The
-scene is build (using GameObjects) in the load_scene() method. GameObjects should be made using the
-component_manager::new_object(). In the example below, two GameObjects (named object1 and object2) are added
-to MyScene. object1 and object2 do not have any non-default Components attached to them, however, if needed,
-this should also be done in load_scene(). Each concrete scene must have a unique name. This unique name is
-used to load a new concrete scene (via a Script). The unique name is set using the get_name() method. In the
-example below, MyScene's unique name is my_scene.
-After setting up one or more concrete scene(s), the concrete scene(s) should be added to the loop/scene manager.
-This is done in your main(). Firstly, the LoopManager should be instantiated. Than, all the concrete scene(s)
-should be added to the loop/scene manger via loop_mgr::add_scene<>(). The templated argument should define the
-concrete scene to be added.
+This example demonstrates how to define and add scenes to the loop/scene manager
+in the `crepe` framework. Each concrete scene should be derived from Scene. In
+the example below, the concrete scene is named MyScene. A concrete scene should,
+at least, implement (override) two methods, namely load_scene() and get_name().
+The scene is build (using GameObjects) in the load_scene() method. GameObjects
+should be made using the component_manager::new_object().
+
+In the example below, two GameObjects (named object1 and object2) are added to
+MyScene. object1 and object2 do not have any non-default Components attached to
+them, however, if needed, this should also be done in load_scene(). Each
+concrete scene must have a unique name. This unique name is used to load a new
+concrete scene (via a Script). The unique name is set using the get_name()
+method. In the example below, MyScene's unique name is my_scene.
+
+After setting up one or more concrete scene(s), the concrete scene(s) should be
+added to the loop/scene manager. This is done in your main(). Firstly, the
+LoopManager should be instantiated. Than, all the concrete scene(s) should be
+added to the loop/scene manger via loop_mgr::add_scene<>(). The templated
+argument should define the concrete scene to be added.
```cpp
#include <crepe/api/LoopManager.h>
diff --git a/src/doc/features.dox b/src/doc/features.dox
index 4786bed..21a040a 100644
--- a/src/doc/features.dox
+++ b/src/doc/features.dox
@@ -1,10 +1,28 @@
// vim:ft=doxygen
/**
+\htmlonly
+<style>
+table.memberdecls { display: none; }
+ul { margin: 1ex 0pt; }
+</style>
+\endhtmlonly
+
\defgroup feature Features
\brief Engine components
This page lists engine features and contains usage instructions for each
feature.
+\par Features
+
+- Scripting
+ - \ref feature_script \n\copybrief feature_script
+
+- Game flow management
+ - \ref feature_scene \n\copybrief feature_scene
+
+- Entity
+ - \ref feature_gameobject \n\copybrief feature_gameobject
+
*/
diff --git a/src/doc/index.dox b/src/doc/index.dox
index 5ec7889..7796f34 100644
--- a/src/doc/index.dox
+++ b/src/doc/index.dox
@@ -8,3 +8,10 @@ Welcome to the documentation for the crêpe game engine.
\see feature
*/
+
+/**
+
+\namespace crepe
+\brief Engine namespace
+
+*/
diff --git a/src/doc/layout.xml b/src/doc/layout.xml
index fb4cc0c..6336655 100644
--- a/src/doc/layout.xml
+++ b/src/doc/layout.xml
@@ -11,7 +11,7 @@
<tab type="modulelist" visible="yes" title="" intro=""/>
<tab type="modulemembers" visible="yes" title="" intro=""/>
</tab>
- <tab type="namespaces" visible="no" title="">
+ <tab type="namespaces" visible="yes" title="">
<tab type="namespacelist" visible="yes" title="" intro=""/>
<tab type="namespacemembers" visible="yes" title="" intro=""/>
</tab>
@@ -56,10 +56,10 @@
<interfaces title=""/>
<publicslots title=""/>
<signals title=""/>
- <publicmethods title=""/>
- <publicstaticmethods title=""/>
<publicattributes title=""/>
<publicstaticattributes title=""/>
+ <publicmethods title=""/>
+ <publicstaticmethods title=""/>
<protectedtypes title=""/>
<protectedslots title=""/>
<protectedmethods title=""/>
@@ -102,11 +102,12 @@
</class>
<namespace>
<briefdescription visible="yes"/>
+ <detaileddescription title=""/>
<memberdecl>
<nestednamespaces visible="yes" title=""/>
<constantgroups visible="yes" title=""/>
<interfaces visible="yes" title=""/>
- <classes visible="yes" title=""/>
+ <classes visible="no" title=""/>
<concepts visible="yes" title=""/>
<structs visible="yes" title=""/>
<exceptions visible="yes" title=""/>
@@ -119,7 +120,6 @@
<properties title=""/>
<membergroups visible="yes"/>
</memberdecl>
- <detaileddescription title=""/>
<memberdef>
<inlineclasses title=""/>
<typedefs title=""/>
diff --git a/src/doc/style.css b/src/doc/style.css
index daabd39..c12240c 100644
--- a/src/doc/style.css
+++ b/src/doc/style.css
@@ -1,6 +1,6 @@
#titlearea,
-address {
- display: none;
-}
+address,
+a[href="namespaces.html"]
+{ display: none; }
h2.groupheader { margin-top: revert; }
diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp
new file mode 100644
index 0000000..f4efc9f
--- /dev/null
+++ b/src/example/AITest.cpp
@@ -0,0 +1,86 @@
+#include <crepe/api/AI.h>
+#include <crepe/api/BehaviorScript.h>
+#include <crepe/api/Camera.h>
+#include <crepe/api/Color.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/LoopManager.h>
+#include <crepe/api/Rigidbody.h>
+#include <crepe/api/Scene.h>
+#include <crepe/api/Script.h>
+#include <crepe/api/Sprite.h>
+#include <crepe/api/Texture.h>
+#include <crepe/manager/Mediator.h>
+#include <crepe/types.h>
+
+using namespace crepe;
+using namespace std;
+
+class Script1 : public Script {
+ bool shutdown(const ShutDownEvent & event) {
+ // Very dirty way of shutting down the game
+ throw "ShutDownEvent";
+ return true;
+ }
+
+ bool mousemove(const MouseMoveEvent & event) {
+ /*RefVector<AI> aivec = this->get_components<AI>();
+ AI & ai = aivec.front().get();
+ ai.flee_target
+ = vec2{static_cast<float>(event.mouse_x), static_cast<float>(event.mouse_y)};*/
+ return true;
+ }
+
+ void init() {
+ subscribe<ShutDownEvent>(
+ [this](const ShutDownEvent & ev) -> bool { return this->shutdown(ev); });
+ subscribe<MouseMoveEvent>(
+ [this](const MouseMoveEvent & ev) -> bool { return this->mousemove(ev); });
+ }
+};
+
+class Scene1 : public Scene {
+public:
+ void load_scene() override {
+ Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
+
+ GameObject game_object1 = mgr.new_object("", "", vec2{0, 0}, 0, 1);
+ GameObject game_object2 = mgr.new_object("", "", vec2{0, 0}, 0, 1);
+
+ Texture img = Texture("asset/texture/test_ap43.png");
+ game_object1.add_component<Sprite>(img, Sprite::Data{
+ .color = Color::MAGENTA,
+ .flip = Sprite::FlipSettings{false, false},
+ .sorting_in_layer = 1,
+ .order_in_layer = 1,
+ .size = {0, 195},
+ });
+ AI & ai = game_object1.add_component<AI>(3000);
+ // ai.arrive_on();
+ // ai.flee_on();
+ ai.path_follow_on();
+ ai.make_oval_path(500, 1000, {0, -1000}, 1.5708, true);
+ ai.make_oval_path(1000, 500, {0, 500}, 4.7124, false);
+ game_object1.add_component<Rigidbody>(Rigidbody::Data{
+ .mass = 0.1f,
+ .max_linear_velocity = {40, 40},
+ });
+ game_object1.add_component<BehaviorScript>().set_script<Script1>();
+
+ game_object2.add_component<Camera>(ivec2{1080, 720}, vec2{5000, 5000},
+ Camera::Data{
+ .bg_color = Color::WHITE,
+ .zoom = 1,
+ });
+ }
+
+ string get_name() const override { return "Scene1"; }
+};
+
+int main() {
+ LoopManager engine;
+ engine.add_scene<Scene1>();
+ engine.start();
+
+ return 0;
+}
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index 560e2bc..187ed46 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -16,8 +16,7 @@ function(add_example target_name)
add_dependencies(examples ${target_name})
endfunction()
-add_example(asset_manager)
-add_example(savemgr)
add_example(rendering_particle)
-add_example(gameloop)
-
+add_example(game)
+add_example(button)
+add_example(AITest)
diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp
deleted file mode 100644
index 917b547..0000000
--- a/src/example/asset_manager.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-#include <crepe/api/AssetManager.h>
-#include <crepe/api/Texture.h>
-#include <crepe/facade/Sound.h>
-
-using namespace crepe;
-
-int main() {
-
- // this needs to be called before the asset manager otherwise the destructor of sdl is not in
- // the right order
- { Texture test("../asset/texture/img.png"); }
- // FIXME: make it so the issue described by the above comment is not possible (i.e. the order
- // in which internal classes are instantiated should not impact the way the engine works).
-
- auto & mgr = AssetManager::get_instance();
-
- {
- // TODO: [design] the Sound class can't be directly included by the user as it includes
- // SoLoud headers.
- auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg");
- auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav");
- auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav");
-
- auto img = mgr.cache<Texture>("../asset/texture/img.png");
- auto img1 = mgr.cache<Texture>("../asset/texture/second.png");
- }
-
- {
- auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg");
- auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav");
- auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav");
-
- auto img = mgr.cache<Texture>("../asset/texture/img.png");
- auto img1 = mgr.cache<Texture>("../asset/texture/second.png");
- }
-}
diff --git a/src/example/button.cpp b/src/example/button.cpp
new file mode 100644
index 0000000..00bdc28
--- /dev/null
+++ b/src/example/button.cpp
@@ -0,0 +1,54 @@
+#include <SDL2/SDL_timer.h>
+#include <chrono>
+#include <crepe/Component.h>
+#include <crepe/ComponentManager.h>
+#include <crepe/api/Animator.h>
+#include <crepe/api/Button.h>
+#include <crepe/api/Camera.h>
+#include <crepe/api/Color.h>
+#include <crepe/api/EventManager.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Sprite.h>
+#include <crepe/api/Texture.h>
+#include <crepe/api/Transform.h>
+#include <crepe/system/AnimatorSystem.h>
+#include <crepe/system/InputSystem.h>
+#include <crepe/system/RenderSystem.h>
+#include <crepe/types.h>
+#include <iostream>
+using namespace crepe;
+using namespace std;
+
+int main(int argc, char * argv[]) {
+ ComponentManager mgr;
+ RenderSystem sys{mgr};
+ EventManager & event_mgr = EventManager::get_instance();
+ InputSystem input_sys{mgr};
+ AnimatorSystem asys{mgr};
+ GameObject camera_obj = mgr.new_object("", "", vec2{1000, 1000}, 0, 1);
+ camera_obj.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f);
+
+ GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
+ auto s2 = Texture("asset/texture/test_ap43.png");
+ bool button_clicked = false;
+ auto & sprite2 = button_obj.add_component<Sprite>(
+ s2, Color::GREEN, Sprite::FlipSettings{false, false}, 2, 1, 100);
+ std::function<void()> on_click = [&]() { std::cout << "button clicked" << std::endl; };
+ std::function<void()> on_enter = [&]() { std::cout << "enter" << std::endl; };
+ std::function<void()> on_exit = [&]() { std::cout << "exit" << std::endl; };
+ auto & button
+ = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false);
+ button.on_mouse_enter = on_enter;
+ button.on_mouse_exit = on_exit;
+ button.is_toggle = true;
+ button.active = true;
+ auto start = std::chrono::steady_clock::now();
+ while (true) {
+ input_sys.update();
+ sys.update();
+ asys.update();
+ event_mgr.dispatch_events();
+ SDL_Delay(30);
+ }
+ return 0;
+}
diff --git a/src/example/game.cpp b/src/example/game.cpp
new file mode 100644
index 0000000..4239c15
--- /dev/null
+++ b/src/example/game.cpp
@@ -0,0 +1,255 @@
+#include "api/CircleCollider.h"
+#include "api/Scene.h"
+#include "manager/ComponentManager.h"
+#include "manager/Mediator.h"
+#include <crepe/api/BoxCollider.h>
+#include <crepe/api/Camera.h>
+#include <crepe/api/Color.h>
+#include <crepe/api/Event.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/LoopManager.h>
+#include <crepe/api/Rigidbody.h>
+#include <crepe/api/Script.h>
+#include <crepe/api/Sprite.h>
+#include <crepe/api/Texture.h>
+#include <crepe/api/Transform.h>
+#include <crepe/api/Vector2.h>
+
+using namespace crepe;
+
+using namespace std;
+
+class MyScript1 : public Script {
+ bool flip = false;
+ bool oncollision(const CollisionEvent & test) {
+ Log::logf("Box {} script on_collision()", test.info.this_collider.game_object_id);
+ return true;
+ }
+ bool keypressed(const KeyPressEvent & test) {
+ Log::logf("Box script keypressed()");
+ switch (test.key) {
+ case Keycode::A: {
+ Transform & tf = this->get_component<Transform>();
+ tf.position.x -= 1;
+ break;
+ }
+ case Keycode::W: {
+ Transform & tf = this->get_component<Transform>();
+ tf.position.y -= 1;
+ break;
+ }
+ case Keycode::S: {
+ Transform & tf = this->get_component<Transform>();
+ tf.position.y += 1;
+ break;
+ }
+ case Keycode::D: {
+ Transform & tf = this->get_component<Transform>();
+ tf.position.x += 1;
+ break;
+ }
+ case Keycode::E: {
+ if (flip) {
+ flip = false;
+ this->get_component<BoxCollider>().active = true;
+ this->get_components<Sprite>()[0].get().active = true;
+ this->get_component<CircleCollider>().active = false;
+ this->get_components<Sprite>()[1].get().active = false;
+ } else {
+ flip = true;
+ this->get_component<BoxCollider>().active = false;
+ this->get_components<Sprite>()[0].get().active = false;
+ this->get_component<CircleCollider>().active = true;
+ this->get_components<Sprite>()[1].get().active = true;
+ }
+
+ //add collider switch
+ break;
+ }
+ default:
+ break;
+ }
+ return false;
+ }
+
+ void init() {
+ Log::logf("init");
+ subscribe<CollisionEvent>(
+ [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); });
+ subscribe<KeyPressEvent>(
+ [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); });
+ }
+ void update() {
+ // Retrieve component from the same GameObject this script is on
+ }
+};
+
+class MyScript2 : public Script {
+ bool flip = false;
+ bool oncollision(const CollisionEvent & test) {
+ Log::logf("Box {} script on_collision()", test.info.this_collider.game_object_id);
+ return true;
+ }
+ bool keypressed(const KeyPressEvent & test) {
+ Log::logf("Box script keypressed()");
+ switch (test.key) {
+ case Keycode::LEFT: {
+ Transform & tf = this->get_component<Transform>();
+ tf.position.x -= 1;
+ break;
+ }
+ case Keycode::UP: {
+ Transform & tf = this->get_component<Transform>();
+ tf.position.y -= 1;
+ break;
+ }
+ case Keycode::DOWN: {
+ Transform & tf = this->get_component<Transform>();
+ tf.position.y += 1;
+ break;
+ }
+ case Keycode::RIGHT: {
+ Transform & tf = this->get_component<Transform>();
+ tf.position.x += 1;
+ break;
+ }
+ case Keycode::PAUSE: {
+ if (flip) {
+ flip = false;
+ this->get_component<BoxCollider>().active = true;
+ this->get_components<Sprite>()[0].get().active = true;
+ this->get_component<CircleCollider>().active = false;
+ this->get_components<Sprite>()[1].get().active = false;
+ } else {
+ flip = true;
+ this->get_component<BoxCollider>().active = false;
+ this->get_components<Sprite>()[0].get().active = false;
+ this->get_component<CircleCollider>().active = true;
+ this->get_components<Sprite>()[1].get().active = true;
+ }
+
+ //add collider switch
+ break;
+ }
+ default:
+ break;
+ }
+ return false;
+ }
+
+ void init() {
+ Log::logf("init");
+ subscribe<CollisionEvent>(
+ [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); });
+ subscribe<KeyPressEvent>(
+ [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); });
+ }
+ void update() {
+ // Retrieve component from the same GameObject this script is on
+ }
+};
+
+class ConcreteScene1 : public Scene {
+public:
+ using Scene::Scene;
+
+ void load_scene() {
+
+ Mediator & m = this->mediator;
+ ComponentManager & mgr = m.component_manager;
+ Color color(0, 0, 0, 255);
+
+ float screen_size_width = 320;
+ float screen_size_height = 240;
+ float world_collider = 1000;
+ //define playable world
+ GameObject world = mgr.new_object(
+ "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1);
+ world.add_component<Rigidbody>(Rigidbody::Data{
+ .mass = 0,
+ .gravity_scale = 0,
+ .body_type = Rigidbody::BodyType::STATIC,
+ .offset = {0, 0},
+ .collision_layers = {0},
+ });
+ world.add_component<BoxCollider>(
+ vec2{0, 0 - (screen_size_height / 2 + world_collider / 2)},
+ vec2{world_collider, world_collider});
+ ; // Top
+ world.add_component<BoxCollider>(vec2{0, screen_size_height / 2 + world_collider / 2},
+ vec2{world_collider, world_collider}); // Bottom
+ world.add_component<BoxCollider>(
+ vec2{0 - (screen_size_width / 2 + world_collider / 2), 0},
+ vec2{world_collider, world_collider}); // Left
+ world.add_component<BoxCollider>(vec2{screen_size_width / 2 + world_collider / 2, 0},
+ vec2{world_collider, world_collider}); // right
+ world.add_component<Camera>(
+ Color::WHITE,
+ ivec2{static_cast<int>(screen_size_width), static_cast<int>(screen_size_height)},
+ vec2{screen_size_width, screen_size_height}, 1.0f);
+
+ GameObject game_object1 = mgr.new_object(
+ "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1);
+ game_object1.add_component<Rigidbody>(Rigidbody::Data{
+ .mass = 1,
+ .gravity_scale = 0,
+ .body_type = Rigidbody::BodyType::DYNAMIC,
+ .linear_velocity = {0, 0},
+ .constraints = {0, 0, 0},
+ .elastisity_coefficient = 1,
+ .offset = {0, 0},
+ .collision_layers = {0},
+ });
+ // add box with boxcollider
+ game_object1.add_component<BoxCollider>(vec2{0, 0}, vec2{20, 20});
+ game_object1.add_component<BehaviorScript>().set_script<MyScript1>();
+ auto img1 = Texture("asset/texture/square.png");
+ game_object1.add_component<Sprite>(img1, color, Sprite::FlipSettings{false, false}, 1,
+ 1, 20);
+
+ //add circle with cirlcecollider deactiveated
+ game_object1.add_component<CircleCollider>(vec2{0, 0}, 10).active = false;
+ auto img2 = Texture("asset/texture/circle.png");
+ game_object1
+ .add_component<Sprite>(img2, color, Sprite::FlipSettings{false, false}, 1, 1, 20)
+ .active
+ = false;
+
+ GameObject game_object2 = mgr.new_object(
+ "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1);
+ game_object2.add_component<Rigidbody>(Rigidbody::Data{
+ .mass = 1,
+ .gravity_scale = 0,
+ .body_type = Rigidbody::BodyType::STATIC,
+ .linear_velocity = {0, 0},
+ .constraints = {0, 0, 0},
+ .elastisity_coefficient = 1,
+ .offset = {0, 0},
+ .collision_layers = {0},
+ });
+ // add box with boxcollider
+ game_object2.add_component<BoxCollider>(vec2{0, 0}, vec2{20, 20});
+ game_object2.add_component<BehaviorScript>().set_script<MyScript2>();
+ auto img3 = Texture("asset/texture/square.png");
+ game_object2.add_component<Sprite>(img3, color, Sprite::FlipSettings{false, false}, 1,
+ 1, 20);
+
+ //add circle with cirlcecollider deactiveated
+ game_object2.add_component<CircleCollider>(vec2{0, 0}, 10).active = false;
+ auto img4 = Texture("asset/texture/circle.png");
+ game_object2
+ .add_component<Sprite>(img4, color, Sprite::FlipSettings{false, false}, 1, 1, 20)
+ .active
+ = false;
+ }
+
+ string get_name() const { return "scene1"; }
+};
+
+int main(int argc, char * argv[]) {
+
+ LoopManager gameloop;
+ gameloop.add_scene<ConcreteScene1>();
+ gameloop.start();
+ return 0;
+}
diff --git a/src/example/gameloop.cpp b/src/example/gameloop.cpp
deleted file mode 100644
index a676f20..0000000
--- a/src/example/gameloop.cpp
+++ /dev/null
@@ -1,7 +0,0 @@
-#include "crepe/api/LoopManager.h"
-using namespace crepe;
-int main() {
- LoopManager gameloop;
- gameloop.start();
- return 1;
-}
diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp
index 349d11e..bd4ef95 100644
--- a/src/example/rendering_particle.cpp
+++ b/src/example/rendering_particle.cpp
@@ -1,43 +1,23 @@
-#include "api/Animator.h"
-#include "api/Camera.h"
-#include "system/AnimatorSystem.h"
-#include "system/ParticleSystem.h"
-#include <SDL2/SDL_timer.h>
-#include <crepe/ComponentManager.h>
-
+#include "api/Asset.h"
#include <crepe/Component.h>
+#include <crepe/api/Animator.h>
+#include <crepe/api/Camera.h>
#include <crepe/api/Color.h>
#include <crepe/api/GameObject.h>
+#include <crepe/api/LoopManager.hpp>
#include <crepe/api/ParticleEmitter.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Sprite.h>
-#include <crepe/api/Texture.h>
#include <crepe/api/Transform.h>
-#include <crepe/system/RenderSystem.h>
+#include <crepe/manager/ComponentManager.h>
+#include <crepe/manager/Mediator.h>
#include <crepe/types.h>
-
-#include <chrono>
+#include <iostream>
using namespace crepe;
using namespace std;
-int main(int argc, char * argv[]) {
- ComponentManager mgr;
- GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1);
- RenderSystem sys{mgr};
- ParticleSystem psys{mgr};
- AnimatorSystem asys{mgr};
-
- Color color(255, 255, 255, 100);
-
- auto img = Texture("asset/texture/test_ap43.png");
- Sprite & test_sprite = game_object.add_component<Sprite>(
- img, color, Sprite::FlipSettings{true, true}, 1, 1, 500);
-
- //game_object.add_component<Animator>(test_sprite, 4, 1, 0).active = true;
- game_object.add_component<Animator>(test_sprite, 1, 1, 0).active = true;
-
- /*
+/*
auto & test = game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{
.position = {0, 0},
.max_particles = 10,
@@ -59,25 +39,45 @@ int main(int argc, char * argv[]) {
});
*/
- auto & cam = game_object.add_component<Camera>(Color::RED, ivec2{1080, 720},
- vec2{2000, 2000}, 1.0f);
+class TestScene : public Scene {
+public:
+ void load_scene() {
- /*
- game_object
- .add_component<Sprite>(make_shared<Texture>("asset/texture/img.png"), color,
- .add_component<Sprite>(make_shared<Texture>("asset/texture/img.png"), color,
- FlipSettings{false, false})
- .order_in_layer
- = 6;
- */
+ cout << "TestScene" << endl;
+ Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
+ GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1);
+
+ Color color(255, 255, 255, 255);
+
+ Asset img{"asset/spritesheet/spritesheet_test.png"};
+
+ Sprite & test_sprite = game_object.add_component<Sprite>(
+ img, Sprite::Data{
+ .color = color,
+ .flip = Sprite::FlipSettings{false, false},
+ .sorting_in_layer = 2,
+ .order_in_layer = 2,
+ .size = {0, 100},
+ .angle_offset = 0,
+ .position_offset = {0, 0},
+ });
- auto start = std::chrono::steady_clock::now();
- while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) {
- psys.update();
- asys.update();
- sys.update();
- SDL_Delay(10);
+ //auto & anim = game_object.add_component<Animator>(test_sprite,ivec2{32, 64}, uvec2{4,1}, Animator::Data{});
+ //anim.set_anim(0);
+
+ auto & cam = game_object.add_component<Camera>(ivec2{1280, 720}, vec2{400, 400},
+ Camera::Data{
+ .bg_color = Color::WHITE,
+ });
}
+ string get_name() const { return "TestScene"; };
+};
+
+int main(int argc, char * argv[]) {
+ LoopManager engine;
+ engine.add_scene<TestScene>();
+ engine.start();
return 0;
}
diff --git a/src/example/savemgr.cpp b/src/example/savemgr.cpp
deleted file mode 100644
index 65c4a34..0000000
--- a/src/example/savemgr.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
-/** \file
- *
- * Standalone example for usage of the save manager
- */
-
-#include <cassert>
-#include <crepe/api/Config.h>
-#include <crepe/api/SaveManager.h>
-#include <crepe/util/Log.h>
-#include <crepe/util/Proxy.h>
-
-using namespace crepe;
-
-// unrelated setup code
-int _ = []() {
- // make sure all log messages get printed
- auto & cfg = Config::get_instance();
- cfg.log.level = Log::Level::TRACE;
-
- return 0; // satisfy compiler
-}();
-
-int main() {
- const char * key = "mygame.test";
-
- SaveManager & mgr = SaveManager::get_instance();
-
- dbg_logf("has key = {}", mgr.has(key));
- ValueBroker<int> prop = mgr.get<int>(key, 0);
- Proxy<int> val = mgr.get<int>(key, 0);
-
- dbg_logf("val = {}", mgr.get<int>(key).get());
- prop.set(1);
- dbg_logf("val = {}", mgr.get<int>(key).get());
- val = 2;
- dbg_logf("val = {}", mgr.get<int>(key).get());
- mgr.set<int>(key, 3);
- dbg_logf("val = {}", mgr.get<int>(key).get());
-
- dbg_logf("has key = {}", mgr.has(key));
- assert(true == mgr.has(key));
-
- return 0;
-}
diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp
new file mode 100644
index 0000000..48bba1b
--- /dev/null
+++ b/src/test/AudioTest.cpp
@@ -0,0 +1,161 @@
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <crepe/api/AudioSource.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/manager/ComponentManager.h>
+#include <crepe/manager/ResourceManager.h>
+#include <crepe/system/AudioSystem.h>
+
+using namespace std;
+using namespace std::chrono_literals;
+using namespace crepe;
+using namespace testing;
+
+class AudioTest : public Test {
+private:
+ class TestSoundContext : public SoundContext {
+ public:
+ MOCK_METHOD(SoundHandle, play, (Sound & resource), (override));
+ MOCK_METHOD(void, stop, (const SoundHandle &), (override));
+ MOCK_METHOD(void, set_volume, (const SoundHandle &, float), (override));
+ MOCK_METHOD(void, set_loop, (const SoundHandle &, bool), (override));
+ };
+
+ class TestAudioSystem : public AudioSystem {
+ public:
+ using AudioSystem::AudioSystem;
+ StrictMock<TestSoundContext> context;
+ virtual SoundContext & get_context() { return this->context; }
+ };
+
+private:
+ Mediator mediator;
+ ComponentManager component_manager{mediator};
+ ResourceManager resource_manager{mediator};
+
+public:
+ TestAudioSystem system{mediator};
+ TestSoundContext & context = system.context;
+
+private:
+ GameObject entity = component_manager.new_object("name");
+
+public:
+ AudioSource & component = entity.add_component<AudioSource>("mwe/audio/bgm.ogg");
+};
+
+TEST_F(AudioTest, Default) {
+ EXPECT_CALL(context, play(_)).Times(0);
+ EXPECT_CALL(context, stop(_)).Times(0);
+ EXPECT_CALL(context, set_volume(_, _)).Times(0);
+ EXPECT_CALL(context, set_loop(_, _)).Times(0);
+ system.update();
+}
+
+TEST_F(AudioTest, Play) {
+ system.update();
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(context, play(_)).Times(0);
+ component.play();
+ }
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(context, play(_)).Times(1);
+ system.update();
+ }
+}
+
+TEST_F(AudioTest, Stop) {
+ system.update();
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(context, stop(_)).Times(0);
+ component.stop();
+ }
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(context, stop(_)).Times(1);
+ system.update();
+ }
+}
+
+TEST_F(AudioTest, Volume) {
+ system.update();
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(context, set_volume(_, _)).Times(0);
+ component.volume += 0.2;
+ }
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(context, set_volume(_, component.volume)).Times(1);
+ system.update();
+ }
+}
+
+TEST_F(AudioTest, Looping) {
+ system.update();
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(context, set_loop(_, _)).Times(0);
+ component.loop = !component.loop;
+ }
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(context, set_loop(_, component.loop)).Times(1);
+ system.update();
+ }
+}
+
+TEST_F(AudioTest, StopOnDeactivate) {
+ system.update();
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(context, stop(_)).Times(1);
+ component.active = false;
+ system.update();
+ }
+}
+
+TEST_F(AudioTest, PlayOnActive) {
+ component.active = false;
+ component.play_on_awake = true;
+ system.update();
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(context, play(_)).Times(1);
+ component.active = true;
+ system.update();
+ }
+}
+
+TEST_F(AudioTest, PlayImmediately) {
+ component.play_on_awake = false;
+ component.play();
+
+ EXPECT_CALL(context, play(_)).Times(1);
+
+ system.update();
+}
diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt
index d310f6a..7196404 100644
--- a/src/test/CMakeLists.txt
+++ b/src/test/CMakeLists.txt
@@ -1,9 +1,12 @@
target_sources(test_main PUBLIC
main.cpp
+ CollisionTest.cpp
PhysicsTest.cpp
ScriptTest.cpp
ParticleTest.cpp
+ AudioTest.cpp
AssetTest.cpp
+ ResourceManagerTest.cpp
OptionalRefTest.cpp
RenderSystemTest.cpp
EventTest.cpp
@@ -12,4 +15,11 @@ target_sources(test_main PUBLIC
ValueBrokerTest.cpp
DBTest.cpp
Vector2Test.cpp
+ InputTest.cpp
+ ScriptEventTest.cpp
+ ScriptSceneTest.cpp
+ Profiling.cpp
+ SaveManagerTest.cpp
+ ScriptSaveManagerTest.cpp
+ ScriptECSTest.cpp
)
diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp
new file mode 100644
index 0000000..dd45eb6
--- /dev/null
+++ b/src/test/CollisionTest.cpp
@@ -0,0 +1,390 @@
+#include "api/BoxCollider.h"
+#include "manager/Mediator.h"
+#include <cmath>
+#include <cstddef>
+#include <gtest/gtest.h>
+
+#define private public
+#define protected public
+
+#include <crepe/api/Event.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Rigidbody.h>
+#include <crepe/api/Script.h>
+#include <crepe/api/Transform.h>
+#include <crepe/manager/ComponentManager.h>
+#include <crepe/manager/EventManager.h>
+#include <crepe/manager/Mediator.h>
+#include <crepe/system/CollisionSystem.h>
+#include <crepe/system/ScriptSystem.h>
+#include <crepe/types.h>
+#include <crepe/util/Log.h>
+
+using namespace std;
+using namespace std::chrono_literals;
+using namespace crepe;
+using namespace testing;
+
+class CollisionHandler : public Script {
+public:
+ int box_id;
+ function<void(const CollisionEvent & ev)> test_fn = [](const CollisionEvent & ev) {};
+
+ CollisionHandler(int box_id) { this->box_id = box_id; }
+
+ bool on_collision(const CollisionEvent & ev) {
+ //Log::logf("Box {} script on_collision()", box_id);
+ test_fn(ev);
+ return true;
+ }
+
+ void init() {
+ subscribe<CollisionEvent>(
+ [this](const CollisionEvent & ev) -> bool { return this->on_collision(ev); });
+ }
+ void update() {
+ // Retrieve component from the same GameObject this script is on
+ }
+};
+
+class CollisionTest : public Test {
+public:
+ Mediator m;
+ ComponentManager mgr{m};
+ CollisionSystem collision_sys{m};
+ ScriptSystem script_sys{m};
+
+ GameObject world = mgr.new_object("world", "", {50, 50});
+ GameObject game_object1 = mgr.new_object("object1", "", {50, 50});
+ GameObject game_object2 = mgr.new_object("object2", "", {50, 30});
+
+ CollisionHandler * script_object1_ref = nullptr;
+ CollisionHandler * script_object2_ref = nullptr;
+
+ void SetUp() override {
+ world.add_component<Rigidbody>(Rigidbody::Data{
+ // TODO: remove unrelated properties:
+ .body_type = Rigidbody::BodyType::STATIC,
+ .offset = {0, 0},
+ });
+ // Create a box with an inner size of 10x10 units
+ world.add_component<BoxCollider>(vec2{0, -100}, vec2{100, 100}); // Top
+ world.add_component<BoxCollider>(vec2{0, 100}, vec2{100, 100}); // Bottom
+ world.add_component<BoxCollider>(vec2{-100, 0}, vec2{100, 100}); // Left
+ world.add_component<BoxCollider>(vec2{100, 0}, vec2{100, 100}); // right
+
+ game_object1.add_component<Rigidbody>(Rigidbody::Data{
+ .mass = 1,
+ .gravity_scale = 0.01,
+ .body_type = Rigidbody::BodyType::DYNAMIC,
+ .linear_velocity = {0, 0},
+ .constraints = {0, 0, 0},
+ .elastisity_coefficient = 1,
+ .offset = {0, 0},
+ .collision_layers = {0},
+ });
+ game_object1.add_component<BoxCollider>(vec2{0, 0}, vec2{10, 10});
+ BehaviorScript & script_object1
+ = game_object1.add_component<BehaviorScript>().set_script<CollisionHandler>(1);
+ script_object1_ref = static_cast<CollisionHandler *>(script_object1.script.get());
+ ASSERT_NE(script_object1_ref, nullptr);
+
+ game_object2.add_component<Rigidbody>(Rigidbody::Data{
+ .mass = 1,
+ .gravity_scale = 0.01,
+ .body_type = Rigidbody::BodyType::DYNAMIC,
+ .linear_velocity = {0, 0},
+ .constraints = {0, 0, 0},
+ .elastisity_coefficient = 1,
+ .offset = {0, 0},
+ .collision_layers = {0},
+ });
+ game_object2.add_component<BoxCollider>(vec2{0, 0}, vec2{10, 10});
+ BehaviorScript & script_object2
+ = game_object2.add_component<BehaviorScript>().set_script<CollisionHandler>(2);
+ script_object2_ref = static_cast<CollisionHandler *>(script_object2.script.get());
+ ASSERT_NE(script_object2_ref, nullptr);
+
+ // Ensure Script::init() is called on all BehaviorScript instances
+ script_sys.update();
+ }
+};
+
+TEST_F(CollisionTest, collision_example) {
+ bool collision_happend = false;
+ script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
+ };
+ script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 2);
+ };
+ EXPECT_FALSE(collision_happend);
+ collision_sys.update();
+ EXPECT_FALSE(collision_happend);
+}
+
+TEST_F(CollisionTest, collision_box_box_dynamic_both_no_velocity) {
+ bool collision_happend = false;
+ script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
+ EXPECT_EQ(ev.info.resolution.x, 10);
+ EXPECT_EQ(ev.info.resolution.y, 10);
+ EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::BOTH);
+ };
+ script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 2);
+ EXPECT_EQ(ev.info.resolution.x, 10);
+ EXPECT_EQ(ev.info.resolution.y, 10);
+ EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::BOTH);
+ };
+ EXPECT_FALSE(collision_happend);
+ Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get();
+ tf.position = {50, 30};
+ collision_sys.update();
+ EXPECT_TRUE(collision_happend);
+}
+
+TEST_F(CollisionTest, collision_box_box_dynamic_x_direction_no_velocity) {
+ bool collision_happend = false;
+ script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
+ EXPECT_EQ(ev.info.resolution.x, -5);
+ EXPECT_EQ(ev.info.resolution.y, 0);
+ EXPECT_EQ(ev.info.resolution_direction,
+ crepe::CollisionSystem::Direction::X_DIRECTION);
+ };
+ script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 2);
+ EXPECT_EQ(ev.info.resolution.x, 5);
+ EXPECT_EQ(ev.info.resolution.y, 0);
+ EXPECT_EQ(ev.info.resolution_direction,
+ crepe::CollisionSystem::Direction::X_DIRECTION);
+ };
+ EXPECT_FALSE(collision_happend);
+ Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get();
+ tf.position = {45, 30};
+ collision_sys.update();
+ EXPECT_TRUE(collision_happend);
+}
+
+TEST_F(CollisionTest, collision_box_box_dynamic_y_direction_no_velocity) {
+ bool collision_happend = false;
+ script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
+ EXPECT_EQ(ev.info.resolution.x, 0);
+ EXPECT_EQ(ev.info.resolution.y, -5);
+ EXPECT_EQ(ev.info.resolution_direction,
+ crepe::CollisionSystem::Direction::Y_DIRECTION);
+ };
+ script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 2);
+ EXPECT_EQ(ev.info.resolution.x, 0);
+ EXPECT_EQ(ev.info.resolution.y, 5);
+ EXPECT_EQ(ev.info.resolution_direction,
+ crepe::CollisionSystem::Direction::Y_DIRECTION);
+ };
+ EXPECT_FALSE(collision_happend);
+ Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get();
+ tf.position = {50, 25};
+ collision_sys.update();
+ EXPECT_TRUE(collision_happend);
+}
+
+TEST_F(CollisionTest, collision_box_box_dynamic_both) {
+ bool collision_happend = false;
+ script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
+ EXPECT_EQ(ev.info.resolution.x, 10);
+ EXPECT_EQ(ev.info.resolution.y, 10);
+ EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::BOTH);
+ };
+ script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 2);
+ EXPECT_EQ(ev.info.resolution.x, 10);
+ EXPECT_EQ(ev.info.resolution.y, 10);
+ EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::BOTH);
+ };
+ EXPECT_FALSE(collision_happend);
+ Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get();
+ tf.position = {50, 30};
+ Rigidbody & rg1 = this->mgr.get_components_by_id<Rigidbody>(1).front().get();
+ rg1.data.linear_velocity = {10, 10};
+ Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get();
+ rg2.data.linear_velocity = {10, 10};
+ collision_sys.update();
+ EXPECT_TRUE(collision_happend);
+}
+
+TEST_F(CollisionTest, collision_box_box_dynamic_x_direction) {
+ bool collision_happend = false;
+ script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
+ EXPECT_EQ(ev.info.resolution.x, -5);
+ EXPECT_EQ(ev.info.resolution.y, -5);
+ EXPECT_EQ(ev.info.resolution_direction,
+ crepe::CollisionSystem::Direction::X_DIRECTION);
+ };
+ script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 2);
+ EXPECT_EQ(ev.info.resolution.x, 5);
+ EXPECT_EQ(ev.info.resolution.y, 5);
+ EXPECT_EQ(ev.info.resolution_direction,
+ crepe::CollisionSystem::Direction::X_DIRECTION);
+ };
+ EXPECT_FALSE(collision_happend);
+ Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get();
+ tf.position = {45, 30};
+ Rigidbody & rg1 = this->mgr.get_components_by_id<Rigidbody>(1).front().get();
+ rg1.data.linear_velocity = {10, 10};
+ Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get();
+ rg2.data.linear_velocity = {10, 10};
+ collision_sys.update();
+ EXPECT_TRUE(collision_happend);
+}
+
+TEST_F(CollisionTest, collision_box_box_dynamic_y_direction) {
+ bool collision_happend = false;
+ script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
+ EXPECT_EQ(ev.info.resolution.x, -5);
+ EXPECT_EQ(ev.info.resolution.y, -5);
+ EXPECT_EQ(ev.info.resolution_direction,
+ crepe::CollisionSystem::Direction::Y_DIRECTION);
+ };
+ script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 2);
+ EXPECT_EQ(ev.info.resolution.x, 5);
+ EXPECT_EQ(ev.info.resolution.y, 5);
+ EXPECT_EQ(ev.info.resolution_direction,
+ crepe::CollisionSystem::Direction::Y_DIRECTION);
+ };
+ EXPECT_FALSE(collision_happend);
+ Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get();
+ tf.position = {50, 25};
+ Rigidbody & rg1 = this->mgr.get_components_by_id<Rigidbody>(1).front().get();
+ rg1.data.linear_velocity = {10, 10};
+ Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get();
+ rg2.data.linear_velocity = {10, 10};
+ collision_sys.update();
+ EXPECT_TRUE(collision_happend);
+}
+
+TEST_F(CollisionTest, collision_box_box_static_both) {
+ bool collision_happend = false;
+ script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
+ EXPECT_EQ(ev.info.resolution.x, 10);
+ EXPECT_EQ(ev.info.resolution.y, 10);
+ EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::BOTH);
+ };
+ script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ // is static should not be called
+ FAIL();
+ };
+ EXPECT_FALSE(collision_happend);
+ Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get();
+ tf.position = {50, 30};
+ Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get();
+ rg2.data.body_type = crepe::Rigidbody::BodyType::STATIC;
+ collision_sys.update();
+ EXPECT_TRUE(collision_happend);
+}
+
+TEST_F(CollisionTest, collision_box_box_static_x_direction) {
+ bool collision_happend = false;
+ script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
+ EXPECT_EQ(ev.info.resolution.x, -5);
+ EXPECT_EQ(ev.info.resolution.y, -5);
+ EXPECT_EQ(ev.info.resolution_direction,
+ crepe::CollisionSystem::Direction::X_DIRECTION);
+ };
+ script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ // is static should not be called
+ FAIL();
+ };
+ EXPECT_FALSE(collision_happend);
+ Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get();
+ tf.position = {45, 30};
+ Rigidbody & rg1 = this->mgr.get_components_by_id<Rigidbody>(1).front().get();
+ rg1.data.linear_velocity = {10, 10};
+ Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get();
+ rg2.data.body_type = crepe::Rigidbody::BodyType::STATIC;
+ collision_sys.update();
+ EXPECT_TRUE(collision_happend);
+}
+
+TEST_F(CollisionTest, collision_box_box_static_y_direction) {
+ bool collision_happend = false;
+ script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
+ EXPECT_EQ(ev.info.resolution.x, -5);
+ EXPECT_EQ(ev.info.resolution.y, -5);
+ EXPECT_EQ(ev.info.resolution_direction,
+ crepe::CollisionSystem::Direction::Y_DIRECTION);
+ };
+ script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) {
+ // is static should not be called
+ FAIL();
+ };
+ EXPECT_FALSE(collision_happend);
+ Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get();
+ tf.position = {50, 25};
+ Rigidbody & rg1 = this->mgr.get_components_by_id<Rigidbody>(1).front().get();
+ rg1.data.linear_velocity = {10, 10};
+ Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get();
+ rg2.data.body_type = crepe::Rigidbody::BodyType::STATIC;
+ collision_sys.update();
+ EXPECT_TRUE(collision_happend);
+}
+
+TEST_F(CollisionTest, collision_box_box_static_multiple) { //todo check visually
+ bool collision_happend = false;
+ float offset_value = 0;
+ float resolution = 0;
+ script_object1_ref->test_fn = [&](const CollisionEvent & ev) {
+ collision_happend = true;
+ EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
+ EXPECT_EQ(ev.info.this_collider.offset.x, offset_value);
+ EXPECT_EQ(ev.info.resolution.x, resolution);
+ };
+ script_object2_ref->test_fn = [&](const CollisionEvent & ev) {
+ // is static should not be called
+ FAIL();
+ };
+ EXPECT_FALSE(collision_happend);
+ Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get();
+ tf.position = {45, 30};
+ Rigidbody & rg1 = this->mgr.get_components_by_id<Rigidbody>(1).front().get();
+ rg1.data.linear_velocity = {10, 10};
+ Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get();
+ rg2.data.body_type = crepe::Rigidbody::BodyType::STATIC;
+ BoxCollider & bxc = this->mgr.get_components_by_id<BoxCollider>(1).front().get();
+ bxc.offset = {5, 0};
+ this->game_object1.add_component<BoxCollider>(vec2{-5, 0}, vec2{10, 10});
+ offset_value = 5;
+ resolution = 10;
+ collision_sys.update();
+ offset_value = -5;
+ resolution = 10;
+ tf.position = {55, 30};
+ collision_sys.update();
+ EXPECT_TRUE(collision_happend);
+}
diff --git a/src/test/DBTest.cpp b/src/test/DBTest.cpp
index e80814c..99dedff 100644
--- a/src/test/DBTest.cpp
+++ b/src/test/DBTest.cpp
@@ -1,6 +1,7 @@
-#include <crepe/facade/DB.h>
#include <gtest/gtest.h>
+#include <crepe/facade/DB.h>
+
using namespace std;
using namespace crepe;
using namespace testing;
diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp
index af9d6f2..af2b7b0 100644
--- a/src/test/ECSTest.cpp
+++ b/src/test/ECSTest.cpp
@@ -1,19 +1,26 @@
#include <gtest/gtest.h>
#define protected public
+#define private public
-#include <crepe/ComponentManager.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Metadata.h>
#include <crepe/api/Transform.h>
#include <crepe/api/Vector2.h>
+#include <crepe/manager/ComponentManager.h>
using namespace std;
using namespace crepe;
class ECSTest : public ::testing::Test {
+ Mediator m;
+
public:
- ComponentManager mgr{};
+ ComponentManager mgr{m};
+
+ class TestComponent : public Component {
+ using Component::Component;
+ };
};
TEST_F(ECSTest, createGameObject) {
@@ -385,3 +392,77 @@ TEST_F(ECSTest, resetPersistent) {
EXPECT_EQ(metadata.size(), 0);
EXPECT_EQ(transform.size(), 0);
}
+
+TEST_F(ECSTest, IDByName) {
+ GameObject foo = mgr.new_object("foo");
+ GameObject bar = mgr.new_object("bar");
+
+ {
+ auto objects = mgr.get_objects_by_name("");
+ EXPECT_EQ(objects.size(), 0);
+ }
+
+ {
+ auto objects = mgr.get_objects_by_name("foo");
+ EXPECT_EQ(objects.size(), 1);
+ EXPECT_TRUE(objects.contains(foo.id));
+ }
+}
+
+TEST_F(ECSTest, IDByTag) {
+ GameObject foo = mgr.new_object("foo", "common tag");
+ GameObject bar = mgr.new_object("bar", "common tag");
+
+ {
+ auto objects = mgr.get_objects_by_tag("");
+ EXPECT_EQ(objects.size(), 0);
+ }
+
+ {
+ auto objects = mgr.get_objects_by_tag("common tag");
+ EXPECT_EQ(objects.size(), 2);
+ EXPECT_TRUE(objects.contains(foo.id));
+ EXPECT_TRUE(objects.contains(bar.id));
+ }
+}
+
+TEST_F(ECSTest, ComponentsByName) {
+ GameObject foo = mgr.new_object("foo");
+ foo.add_component<TestComponent>();
+ GameObject bar = mgr.new_object("bar");
+ bar.add_component<TestComponent>();
+ bar.add_component<TestComponent>();
+
+ {
+ auto objects = mgr.get_components_by_name<TestComponent>("");
+ EXPECT_EQ(objects.size(), 0);
+ }
+
+ {
+ auto objects = mgr.get_components_by_name<TestComponent>("foo");
+ EXPECT_EQ(objects.size(), 1);
+ }
+
+ {
+ auto objects = mgr.get_components_by_name<TestComponent>("bar");
+ EXPECT_EQ(objects.size(), 2);
+ }
+}
+
+TEST_F(ECSTest, ComponentsByTag) {
+ GameObject foo = mgr.new_object("foo", "common tag");
+ foo.add_component<TestComponent>();
+ GameObject bar = mgr.new_object("bar", "common tag");
+ bar.add_component<TestComponent>();
+ bar.add_component<TestComponent>();
+
+ {
+ auto objects = mgr.get_components_by_tag<TestComponent>("");
+ EXPECT_EQ(objects.size(), 0);
+ }
+
+ {
+ auto objects = mgr.get_components_by_tag<TestComponent>("common tag");
+ EXPECT_EQ(objects.size(), 3);
+ }
+}
diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp
index b0e6c9c..4a4872d 100644
--- a/src/test/EventTest.cpp
+++ b/src/test/EventTest.cpp
@@ -1,10 +1,11 @@
-
-#include "api/Event.h"
-#include "api/EventManager.h"
-#include "api/IKeyListener.h"
-#include "api/IMouseListener.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
+
+#include <crepe/api/Event.h>
+#include <crepe/api/IKeyListener.h>
+#include <crepe/api/IMouseListener.h>
+#include <crepe/manager/EventManager.h>
+
using namespace std;
using namespace std::chrono_literals;
using namespace crepe;
@@ -36,10 +37,7 @@ public:
};
TEST_F(EventManagerTest, EventSubscription) {
- EventHandler<KeyPressEvent> key_handler = [](const KeyPressEvent & e) {
- std::cout << "Key Event Triggered" << std::endl;
- return true;
- };
+ EventHandler<KeyPressEvent> key_handler = [](const KeyPressEvent & e) { return true; };
// Subscribe to KeyPressEvent
EventManager::get_instance().subscribe<KeyPressEvent>(key_handler, 1);
@@ -158,29 +156,37 @@ TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) {
bool triggered1 = false;
bool triggered2 = false;
int test_channel = 1;
- EventHandler<MouseClickEvent> mouse_handler1 = [&](const MouseClickEvent & e) {
+
+ // Adjusted to use KeyPressEvent with repeat as the first variable
+ EventHandler<KeyPressEvent> key_handler1 = [&](const KeyPressEvent & e) {
triggered1 = true;
- EXPECT_EQ(e.mouse_x, 100);
- EXPECT_EQ(e.mouse_y, 200);
- EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE);
+ EXPECT_EQ(e.repeat, false); // Expecting repeat to be false
+ EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code
return false; // Allows propagation
};
- EventHandler<MouseClickEvent> mouse_handler2 = [&](const MouseClickEvent & e) {
+
+ EventHandler<KeyPressEvent> key_handler2 = [&](const KeyPressEvent & e) {
triggered2 = true;
- EXPECT_EQ(e.mouse_x, 100);
- EXPECT_EQ(e.mouse_y, 200);
- EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE);
+ EXPECT_EQ(e.repeat, false); // Expecting repeat to be false
+ EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code
return false; // Allows propagation
};
- event_manager.subscribe<MouseClickEvent>(mouse_handler1);
- event_manager.subscribe<MouseClickEvent>(mouse_handler2, test_channel);
- event_manager.queue_event<MouseClickEvent>(
- MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE});
- event_manager.queue_event<MouseClickEvent>(
- MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE},
+ // Subscribe handlers to KeyPressEvent
+ event_manager.subscribe<KeyPressEvent>(key_handler1);
+ event_manager.subscribe<KeyPressEvent>(key_handler2, test_channel);
+
+ // Queue a KeyPressEvent instead of KeyDownEvent
+ event_manager.queue_event<KeyPressEvent>(KeyPressEvent{
+ .repeat = false, .key = Keycode::A}); // Adjust event with repeat flag first
+
+ event_manager.queue_event<KeyPressEvent>(
+ KeyPressEvent{.repeat = false,
+ .key = Keycode::A}, // Adjust event for second subscription
test_channel);
+
event_manager.dispatch_events();
+
EXPECT_TRUE(triggered1);
EXPECT_TRUE(triggered2);
}
diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp
new file mode 100644
index 0000000..4c05e56
--- /dev/null
+++ b/src/test/InputTest.cpp
@@ -0,0 +1,294 @@
+#include <gtest/gtest.h>
+#define protected public
+#define private public
+#include "api/KeyCodes.h"
+#include "manager/ComponentManager.h"
+#include "manager/EventManager.h"
+#include "manager/Mediator.h"
+#include "system/InputSystem.h"
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_keycode.h>
+#include <crepe/api/Button.h>
+#include <crepe/api/Camera.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Metadata.h>
+#include <crepe/api/Transform.h>
+#include <crepe/api/Vector2.h>
+#include <gmock/gmock.h>
+
+using namespace std;
+using namespace std::chrono_literals;
+using namespace crepe;
+
+class InputTest : public ::testing::Test {
+public:
+ Mediator mediator;
+ ComponentManager mgr{mediator};
+ SDLContext sdl_context{mediator};
+
+ InputSystem input_system{mediator};
+
+ EventManager & event_manager = EventManager::get_instance();
+ //GameObject camera;
+
+protected:
+ void SetUp() override {
+ mediator.event_manager = event_manager;
+ mediator.component_manager = mgr;
+ event_manager.clear();
+ }
+
+ void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) {
+ SDL_Event event;
+
+ // Simulate Mouse Button Down event
+ SDL_zero(event);
+ event.type = SDL_MOUSEBUTTONDOWN;
+ event.button.x = mouse_x;
+ event.button.y = mouse_y;
+ event.button.button = mouse_button;
+ SDL_PushEvent(&event);
+
+ // Simulate Mouse Button Up event
+ SDL_zero(event);
+ event.type = SDL_MOUSEBUTTONUP;
+ event.button.x = mouse_x;
+ event.button.y = mouse_y;
+ event.button.button = mouse_button;
+ SDL_PushEvent(&event);
+ }
+};
+
+TEST_F(InputTest, MouseDown) {
+ GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1);
+ auto & camera = obj.add_component<Camera>(
+ ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
+ camera.active = true;
+ bool mouse_triggered = false;
+ EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent & event) {
+ mouse_triggered = true;
+ //middle of the screen = 0,0
+ EXPECT_EQ(event.mouse_x, 0);
+ EXPECT_EQ(event.mouse_y, 0);
+ EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE);
+ return false;
+ };
+ event_manager.subscribe<MousePressEvent>(on_mouse_down);
+
+ SDL_Event event;
+ SDL_zero(event);
+ event.type = SDL_MOUSEBUTTONDOWN;
+ // middle of the screen of a 500*500 camera = 250*250
+ event.button.x = 250;
+ event.button.y = 250;
+ event.button.button = SDL_BUTTON_LEFT;
+ SDL_PushEvent(&event);
+
+ input_system.update();
+ event_manager.dispatch_events();
+ EXPECT_TRUE(mouse_triggered);
+}
+
+TEST_F(InputTest, MouseUp) {
+ GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1);
+ auto & camera = obj.add_component<Camera>(
+ ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
+ camera.active = true;
+ bool function_triggered = false;
+ EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent & e) {
+ function_triggered = true;
+ EXPECT_EQ(e.mouse_x, 0);
+ EXPECT_EQ(e.mouse_y, 0);
+ EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE);
+ return false;
+ };
+ event_manager.subscribe<MouseReleaseEvent>(on_mouse_release);
+
+ SDL_Event event;
+ SDL_zero(event);
+ event.type = SDL_MOUSEBUTTONUP;
+ event.button.x = 250;
+ event.button.y = 250;
+ event.button.button = SDL_BUTTON_LEFT;
+ SDL_PushEvent(&event);
+
+ input_system.update();
+ event_manager.dispatch_events();
+ EXPECT_TRUE(function_triggered);
+}
+
+TEST_F(InputTest, MouseMove) {
+ GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1);
+ auto & camera = obj.add_component<Camera>(
+ ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
+ camera.active = true;
+ bool function_triggered = false;
+ EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent & e) {
+ function_triggered = true;
+ EXPECT_EQ(e.mouse_x, 0);
+ EXPECT_EQ(e.mouse_y, 0);
+ EXPECT_EQ(e.delta_x, 10);
+ EXPECT_EQ(e.delta_y, 10);
+ return false;
+ };
+ event_manager.subscribe<MouseMoveEvent>(on_mouse_move);
+
+ SDL_Event event;
+ SDL_zero(event);
+ event.type = SDL_MOUSEMOTION;
+ event.motion.x = 250;
+ event.motion.y = 250;
+ event.motion.xrel = 10;
+ event.motion.yrel = 10;
+ SDL_PushEvent(&event);
+
+ input_system.update();
+ event_manager.dispatch_events();
+ EXPECT_TRUE(function_triggered);
+}
+
+TEST_F(InputTest, KeyDown) {
+ GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1);
+ auto & camera = obj.add_component<Camera>(
+ ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
+ camera.active = true;
+ bool function_triggered = false;
+
+ // Define event handler for KeyPressEvent
+ EventHandler<KeyPressEvent> on_key_press = [&](const KeyPressEvent & event) {
+ function_triggered = true;
+ EXPECT_EQ(event.key, Keycode::B); // Validate the key is 'B'
+ EXPECT_EQ(event.repeat, true); // Validate repeat flag
+ return false;
+ };
+
+ event_manager.subscribe<KeyPressEvent>(on_key_press);
+
+ // Simulate SDL_KEYDOWN event
+ SDL_Event test_event;
+ SDL_zero(test_event);
+ test_event.type = SDL_KEYDOWN; // Key down event
+ test_event.key.keysym.scancode = SDL_SCANCODE_B; // Set scancode for 'B'
+ test_event.key.repeat = 1; // Set repeat flag
+ SDL_PushEvent(&test_event);
+
+ input_system.update(); // Process the event
+ event_manager.dispatch_events(); // Dispatch events to handlers
+
+ EXPECT_TRUE(function_triggered); // Check if the handler was triggered
+}
+
+TEST_F(InputTest, KeyUp) {
+ GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1);
+ auto & camera = obj.add_component<Camera>(
+ ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
+ camera.active = true;
+ bool function_triggered = false;
+ EventHandler<KeyReleaseEvent> on_key_release = [&](const KeyReleaseEvent & event) {
+ function_triggered = true;
+ EXPECT_EQ(event.key, Keycode::B);
+ return false;
+ };
+ event_manager.subscribe<KeyReleaseEvent>(on_key_release);
+
+ SDL_Event event;
+ SDL_zero(event);
+ event.type = SDL_KEYUP;
+ event.key.keysym.scancode = SDL_SCANCODE_B;
+ SDL_PushEvent(&event);
+
+ input_system.update();
+ event_manager.dispatch_events();
+ EXPECT_TRUE(function_triggered);
+}
+
+TEST_F(InputTest, MouseClick) {
+ GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1);
+ auto & camera = obj.add_component<Camera>(
+ ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
+ camera.active = true;
+ bool on_click_triggered = false;
+ EventHandler<MouseClickEvent> on_mouse_click = [&](const MouseClickEvent & event) {
+ on_click_triggered = true;
+ EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE);
+ EXPECT_EQ(event.mouse_x, 0);
+ EXPECT_EQ(event.mouse_y, 0);
+ return false;
+ };
+ event_manager.subscribe<MouseClickEvent>(on_mouse_click);
+
+ this->simulate_mouse_click(250, 250, SDL_BUTTON_LEFT);
+ input_system.update();
+ event_manager.dispatch_events();
+ EXPECT_TRUE(on_click_triggered);
+}
+
+TEST_F(InputTest, testButtonClick) {
+ GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1);
+ auto & camera = obj.add_component<Camera>(
+ ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
+ camera.active = true;
+ GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
+ bool button_clicked = false;
+ std::function<void()> on_click = [&]() { button_clicked = true; };
+ auto & button
+ = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false);
+
+ bool hover = false;
+ button.active = true;
+
+ button.is_pressed = false;
+ button.is_toggle = false;
+ this->simulate_mouse_click(999, 999, SDL_BUTTON_LEFT);
+ input_system.update();
+ event_manager.dispatch_events();
+ EXPECT_FALSE(button_clicked);
+
+ this->simulate_mouse_click(250, 250, SDL_BUTTON_LEFT);
+ input_system.update();
+ event_manager.dispatch_events();
+ EXPECT_TRUE(button_clicked);
+}
+
+TEST_F(InputTest, testButtonHover) {
+ GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1);
+ auto & camera = obj.add_component<Camera>(
+ ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
+ camera.active = true;
+ GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
+ bool button_clicked = false;
+ std::function<void()> on_click = [&]() { button_clicked = true; };
+ auto & button
+ = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false);
+ button.active = true;
+ button.is_pressed = false;
+ button.is_toggle = false;
+
+ // Mouse not on button
+ SDL_Event event;
+ SDL_zero(event);
+ event.type = SDL_MOUSEMOTION;
+ event.motion.x = 700;
+ event.motion.y = 700;
+ event.motion.xrel = 10;
+ event.motion.yrel = 10;
+ SDL_PushEvent(&event);
+
+ input_system.update();
+ event_manager.dispatch_events();
+ EXPECT_FALSE(button.hover);
+
+ // Mouse on button
+ SDL_Event hover_event;
+ SDL_zero(hover_event);
+ hover_event.type = SDL_MOUSEMOTION;
+ hover_event.motion.x = 250;
+ hover_event.motion.y = 250;
+ hover_event.motion.xrel = 10;
+ hover_event.motion.yrel = 10;
+ SDL_PushEvent(&hover_event);
+
+ input_system.update();
+ event_manager.dispatch_events();
+ EXPECT_TRUE(button.hover);
+}
diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp
index 976f9a1..9112a3f 100644
--- a/src/test/ParticleTest.cpp
+++ b/src/test/ParticleTest.cpp
@@ -1,5 +1,4 @@
-#include "api/Texture.h"
-#include <crepe/ComponentManager.h>
+#include "api/Asset.h"
#include <crepe/Particle.h>
#include <crepe/api/Config.h>
#include <crepe/api/GameObject.h>
@@ -7,6 +6,7 @@
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Transform.h>
+#include <crepe/manager/ComponentManager.h>
#include <crepe/system/ParticleSystem.h>
#include <gtest/gtest.h>
#include <math.h>
@@ -16,9 +16,11 @@ using namespace std::chrono_literals;
using namespace crepe;
class ParticlesTest : public ::testing::Test {
+ Mediator m;
+
public:
- ComponentManager component_manager;
- ParticleSystem particle_system{component_manager};
+ ComponentManager component_manager{m};
+ ParticleSystem particle_system{m};
void SetUp() override {
ComponentManager & mgr = this->component_manager;
@@ -28,9 +30,13 @@ public:
GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 0);
Color color(0, 0, 0, 0);
- auto s1 = Texture("asset/texture/img.png");
+ auto s1 = Asset("asset/texture/img.png");
Sprite & test_sprite = game_object.add_component<Sprite>(
- s1, color, Sprite::FlipSettings{true, true}, 1, 1, 100);
+ s1, Sprite::Data{
+ .color = color,
+ .flip = Sprite::FlipSettings{true, true},
+ .size = {10, 10},
+ });
game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{
.position = {0, 0},
diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp
index 33b6020..43d2931 100644
--- a/src/test/PhysicsTest.cpp
+++ b/src/test/PhysicsTest.cpp
@@ -1,8 +1,8 @@
-#include <crepe/ComponentManager.h>
#include <crepe/api/Config.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Transform.h>
+#include <crepe/manager/ComponentManager.h>
#include <crepe/system/PhysicsSystem.h>
#include <gtest/gtest.h>
@@ -11,9 +11,11 @@ using namespace std::chrono_literals;
using namespace crepe;
class PhysicsTest : public ::testing::Test {
+ Mediator m;
+
public:
- ComponentManager component_manager;
- PhysicsSystem system{component_manager};
+ ComponentManager component_manager{m};
+ PhysicsSystem system{m};
void SetUp() override {
ComponentManager & mgr = this->component_manager;
@@ -28,8 +30,6 @@ public:
.max_linear_velocity = vec2{10, 10},
.max_angular_velocity = 10,
.constraints = {0, 0},
- .use_gravity = true,
- .bounce = false,
});
}
transforms = mgr.get_components_by_id<Transform>(0);
@@ -105,16 +105,16 @@ TEST_F(PhysicsTest, movement) {
EXPECT_EQ(transform.position.y, 1);
EXPECT_EQ(transform.rotation, 1);
- rigidbody.data.linear_damping.x = 0.5;
- rigidbody.data.linear_damping.y = 0.5;
- rigidbody.data.angular_damping = 0.5;
+ rigidbody.data.linear_velocity_coefficient.x = 0.5;
+ rigidbody.data.linear_velocity_coefficient.y = 0.5;
+ rigidbody.data.angular_velocity_coefficient = 0.5;
system.update();
EXPECT_EQ(rigidbody.data.linear_velocity.x, 0.5);
EXPECT_EQ(rigidbody.data.linear_velocity.y, 0.5);
EXPECT_EQ(rigidbody.data.angular_velocity, 0.5);
rigidbody.data.constraints = {1, 1, 0};
- rigidbody.data.angular_damping = 0;
+ rigidbody.data.angular_velocity_coefficient = 0;
rigidbody.data.max_angular_velocity = 1000;
rigidbody.data.angular_velocity = 360;
system.update();
diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp
new file mode 100644
index 0000000..cc4c637
--- /dev/null
+++ b/src/test/Profiling.cpp
@@ -0,0 +1,247 @@
+#include <chrono>
+#include <cmath>
+#include <crepe/api/Asset.h>
+#include <crepe/manager/Mediator.h>
+#include <crepe/manager/ResourceManager.h>
+#include <crepe/system/ParticleSystem.h>
+#include <crepe/system/PhysicsSystem.h>
+#include <crepe/system/RenderSystem.h>
+#include <gtest/gtest.h>
+
+#define private public
+#define protected public
+
+#include <crepe/api/Event.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/ParticleEmitter.h>
+#include <crepe/api/Rigidbody.h>
+#include <crepe/api/Script.h>
+#include <crepe/api/Transform.h>
+#include <crepe/manager/ComponentManager.h>
+#include <crepe/manager/EventManager.h>
+#include <crepe/system/CollisionSystem.h>
+#include <crepe/system/ScriptSystem.h>
+#include <crepe/types.h>
+#include <crepe/util/Log.h>
+
+using namespace std;
+using namespace std::chrono_literals;
+using namespace crepe;
+using namespace testing;
+
+class TestScript : public Script {
+ bool oncollision(const CollisionEvent & test) {
+ Log::logf("Box {} script on_collision()", test.info.this_collider.game_object_id);
+ return true;
+ }
+ void init() {
+ subscribe<CollisionEvent>(
+ [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); });
+ }
+ void update() {
+ // Retrieve component from the same GameObject this script is on
+ }
+};
+
+class DISABLED_ProfilingTest : public Test {
+public:
+ // Config for test
+ // Minimum amount to let test pass
+ const int min_gameobject_count = 100;
+ // Maximum amount to stop test
+ const int max_gameobject_count = 150;
+ // Amount of times a test runs to calculate average
+ const int average = 5;
+ // Maximum duration to stop test
+ const std::chrono::microseconds duration = 16000us;
+
+ Mediator m;
+ SDLContext sdl_context{m};
+ ResourceManager resman{m};
+ ComponentManager mgr{m};
+ // Add system used for profling tests
+ CollisionSystem collision_sys{m};
+ PhysicsSystem physics_sys{m};
+ ParticleSystem particle_sys{m};
+ RenderSystem render_sys{m};
+ ScriptSystem script_sys{m};
+
+ // Test data
+ std::map<std::string, std::chrono::microseconds> timings;
+ int game_object_count = 0;
+ std::chrono::microseconds total_time = 0us;
+
+ void SetUp() override {
+
+ GameObject do_not_use = mgr.new_object("DO_NOT_USE", "", {0, 0});
+ do_not_use.add_component<Camera>(ivec2{1080, 720}, vec2{2000, 2000},
+ Camera::Data{
+ .bg_color = Color::WHITE,
+ .zoom = 1.0f,
+ });
+ // initialize systems here:
+ //calls init
+ script_sys.update();
+ //creates window
+ render_sys.update();
+ }
+
+ // Helper function to time an update call and store its duration
+ template <typename Func>
+ std::chrono::microseconds time_function(const std::string & name, Func && func) {
+ auto start = std::chrono::steady_clock::now();
+ func();
+ auto end = std::chrono::steady_clock::now();
+ std::chrono::microseconds duration
+ = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
+ timings[name] += duration;
+ return duration;
+ }
+
+ // Run and profile all systems, return the total time in milliseconds
+ std::chrono::microseconds run_all_systems() {
+ std::chrono::microseconds total_microseconds = 0us;
+ total_microseconds += time_function("PhysicsSystem", [&]() { physics_sys.update(); });
+ total_microseconds
+ += time_function("CollisionSystem", [&]() { collision_sys.update(); });
+ total_microseconds
+ += time_function("ParticleSystem", [&]() { particle_sys.update(); });
+ total_microseconds += time_function("RenderSystem", [&]() { render_sys.update(); });
+ return total_microseconds;
+ }
+
+ // Print timings of all functions
+ void log_timings() const {
+ std::string result = "\nFunction timings:\n";
+
+ for (const auto & [name, duration] : timings) {
+ result += name + " took " + std::to_string(duration.count() / 1000.0 / average)
+ + " ms (" + std::to_string(duration.count() / average) + " µs).\n";
+ }
+
+ result += "Total time: " + std::to_string(this->total_time.count() / 1000.0 / average)
+ + " ms (" + std::to_string(this->total_time.count() / average) + " µs)\n";
+
+ result += "Amount of gameobjects: " + std::to_string(game_object_count) + "\n";
+
+ GTEST_LOG_(INFO) << result;
+ }
+
+ void clear_timings() {
+ for (auto & [key, value] : timings) {
+ value = std::chrono::microseconds(0);
+ }
+ }
+};
+
+TEST_F(DISABLED_ProfilingTest, Profiling_1) {
+ while (this->total_time / this->average < this->duration) {
+
+ {
+ //define gameobject used for testing
+ GameObject gameobject = mgr.new_object("gameobject", "", {0, 0});
+ }
+
+ this->game_object_count++;
+
+ this->total_time = 0us;
+ clear_timings();
+
+ for (int amount = 0; amount < this->average; amount++) {
+ this->total_time += run_all_systems();
+ }
+
+ if (this->game_object_count >= this->max_gameobject_count) break;
+ }
+ log_timings();
+ EXPECT_GE(this->game_object_count, this->min_gameobject_count);
+}
+
+TEST_F(DISABLED_ProfilingTest, Profiling_2) {
+ while (this->total_time / this->average < this->duration) {
+
+ {
+ //define gameobject used for testing
+ GameObject gameobject = mgr.new_object(
+ "gameobject", "", {static_cast<float>(game_object_count * 2), 0});
+ gameobject.add_component<Rigidbody>(Rigidbody::Data{
+ .gravity_scale = 0.0,
+ .body_type = Rigidbody::BodyType::STATIC,
+ });
+ gameobject.add_component<BoxCollider>(vec2{0, 0}, vec2{1, 1});
+
+ gameobject.add_component<BehaviorScript>().set_script<TestScript>();
+ Sprite & test_sprite = gameobject.add_component<Sprite>(
+ Asset{"asset/texture/square.png"},
+ Sprite::Data{
+ .color = {0, 0, 0, 0},
+ .flip = {.flip_x = false, .flip_y = false},
+ .sorting_in_layer = 1,
+ .order_in_layer = 1,
+ .size = {.y = 500},
+ });
+ }
+
+ this->game_object_count++;
+
+ this->total_time = 0us;
+ clear_timings();
+ for (int amount = 0; amount < this->average; amount++) {
+ this->total_time += run_all_systems();
+ }
+
+ if (this->game_object_count >= this->max_gameobject_count) break;
+ }
+ log_timings();
+ EXPECT_GE(this->game_object_count, this->min_gameobject_count);
+}
+
+TEST_F(DISABLED_ProfilingTest, Profiling_3) {
+ while (this->total_time / this->average < this->duration) {
+
+ {
+ //define gameobject used for testing
+ GameObject gameobject = mgr.new_object(
+ "gameobject", "", {static_cast<float>(game_object_count * 2), 0});
+ gameobject.add_component<Rigidbody>(Rigidbody::Data{
+ .gravity_scale = 0,
+ .body_type = Rigidbody::BodyType::STATIC,
+ });
+ gameobject.add_component<BoxCollider>(vec2{0, 0}, vec2{1, 1});
+ gameobject.add_component<BehaviorScript>().set_script<TestScript>();
+ Sprite & test_sprite = gameobject.add_component<Sprite>(
+ Asset{"asset/texture/square.png"},
+ Sprite::Data{
+ .color = {0, 0, 0, 0},
+ .flip = {.flip_x = false, .flip_y = false},
+ .sorting_in_layer = 1,
+ .order_in_layer = 1,
+ .size = {.y = 500},
+ });
+ auto & test = gameobject.add_component<ParticleEmitter>(ParticleEmitter::Data{
+ .max_particles = 10,
+ .emission_rate = 100,
+ .end_lifespan = 100000,
+ .boundary{
+ .width = 1000,
+ .height = 1000,
+ .offset = vec2{0, 0},
+ .reset_on_exit = false,
+ },
+ .sprite = test_sprite,
+ });
+ }
+ render_sys.update();
+ this->game_object_count++;
+
+ this->total_time = 0us;
+ clear_timings();
+ for (int amount = 0; amount < this->average; amount++) {
+ this->total_time += run_all_systems();
+ }
+
+ if (this->game_object_count >= this->max_gameobject_count) break;
+ }
+ log_timings();
+ EXPECT_GE(this->game_object_count, this->min_gameobject_count);
+}
diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp
index bb5b81a..b4519cb 100644
--- a/src/test/RenderSystemTest.cpp
+++ b/src/test/RenderSystemTest.cpp
@@ -1,3 +1,6 @@
+#include "api/Asset.h"
+#include "facade/SDLContext.h"
+#include "manager/ResourceManager.h"
#include "types.h"
#include <functional>
#include <gtest/gtest.h>
@@ -7,12 +10,11 @@
#define private public
#define protected public
-#include "crepe/api/Camera.h"
-#include <crepe/ComponentManager.h>
+#include <crepe/api/Camera.h>
#include <crepe/api/Color.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Sprite.h>
-#include <crepe/api/Texture.h>
+#include <crepe/manager/ComponentManager.h>
#include <crepe/system/RenderSystem.h>
@@ -21,54 +23,67 @@ using namespace crepe;
using namespace testing;
class RenderSystemTest : public Test {
+ Mediator m;
+
public:
- ComponentManager mgr{};
- RenderSystem sys{mgr};
+ ComponentManager mgr{m};
+ SDLContext ctx{m};
+ ResourceManager resource_manager{m};
+ RenderSystem sys{m};
GameObject entity1 = this->mgr.new_object("name");
GameObject entity2 = this->mgr.new_object("name");
GameObject entity3 = this->mgr.new_object("name");
GameObject entity4 = this->mgr.new_object("name");
void SetUp() override {
- auto s1 = Texture("asset/texture/img.png");
- auto s2 = Texture("asset/texture/img.png");
- auto s3 = Texture("asset/texture/img.png");
- auto s4 = Texture("asset/texture/img.png");
- auto & sprite1 = entity1.add_component<Sprite>(
- s1, Color(0, 0, 0, 0), Sprite::FlipSettings{false, false}, 5, 5, 100);
- ASSERT_NE(sprite1.sprite_image.texture.get(), nullptr);
- EXPECT_EQ(sprite1.order_in_layer, 5);
- EXPECT_EQ(sprite1.sorting_in_layer, 5);
- auto & sprite2 = entity2.add_component<Sprite>(
- s2, Color(0, 0, 0, 0), Sprite::FlipSettings{false, false}, 2, 1, 100);
- ASSERT_NE(sprite2.sprite_image.texture.get(), nullptr);
- EXPECT_EQ(sprite2.sorting_in_layer, 2);
- EXPECT_EQ(sprite2.order_in_layer, 1);
-
- auto & sprite3 = entity3.add_component<Sprite>(
- s3, Color(0, 0, 0, 0), Sprite::FlipSettings{false, false}, 1, 2, 100);
- ASSERT_NE(sprite3.sprite_image.texture.get(), nullptr);
- EXPECT_EQ(sprite3.sorting_in_layer, 1);
- EXPECT_EQ(sprite3.order_in_layer, 2);
-
- auto & sprite4 = entity4.add_component<Sprite>(
- s4, Color(0, 0, 0, 0), Sprite::FlipSettings{false, false}, 1, 1, 100);
- ASSERT_NE(sprite4.sprite_image.texture.get(), nullptr);
- EXPECT_EQ(sprite4.sorting_in_layer, 1);
- EXPECT_EQ(sprite4.order_in_layer, 1);
+ auto s1 = Asset("asset/texture/img.png");
+ auto s2 = Asset("asset/texture/img.png");
+ auto s3 = Asset("asset/texture/img.png");
+ auto s4 = Asset("asset/texture/img.png");
+ auto & sprite1
+ = entity1.add_component<Sprite>(s1, Sprite::Data{
+ .color = Color(0, 0, 0, 0),
+ .flip = Sprite::FlipSettings{false, false},
+ .sorting_in_layer = 5,
+ .order_in_layer = 5,
+ .size = {10, 10},
+ });
+
+ EXPECT_EQ(sprite1.data.order_in_layer, 5);
+ EXPECT_EQ(sprite1.data.sorting_in_layer, 5);
+ auto & sprite2
+ = entity2.add_component<Sprite>(s2, Sprite::Data{
+ .color = Color(0, 0, 0, 0),
+ .flip = Sprite::FlipSettings{false, false},
+ .sorting_in_layer = 2,
+ .order_in_layer = 1,
+ });
+ EXPECT_EQ(sprite2.data.sorting_in_layer, 2);
+ EXPECT_EQ(sprite2.data.order_in_layer, 1);
+
+ auto & sprite3
+ = entity3.add_component<Sprite>(s3, Sprite::Data{
+ .color = Color(0, 0, 0, 0),
+ .flip = Sprite::FlipSettings{false, false},
+ .sorting_in_layer = 1,
+ .order_in_layer = 2,
+ });
+ EXPECT_EQ(sprite3.data.sorting_in_layer, 1);
+ EXPECT_EQ(sprite3.data.order_in_layer, 2);
+
+ auto & sprite4
+ = entity4.add_component<Sprite>(s4, Sprite::Data{
+ .color = Color(0, 0, 0, 0),
+ .flip = Sprite::FlipSettings{false, false},
+ .sorting_in_layer = 1,
+ .order_in_layer = 1,
+ });
+ EXPECT_EQ(sprite4.data.sorting_in_layer, 1);
+ EXPECT_EQ(sprite4.data.order_in_layer, 1);
}
};
-TEST_F(RenderSystemTest, expected_throws) {
- GameObject entity1 = this->mgr.new_object("NAME");
-
- // no texture img
- EXPECT_ANY_THROW({
- auto test = Texture("");
- entity1.add_component<Sprite>(test, Color(0, 0, 0, 0),
- Sprite::FlipSettings{false, false}, 1, 1, 100);
- });
-
+TEST_F(RenderSystemTest, NoCamera) {
// No camera
EXPECT_ANY_THROW({ this->sys.update(); });
}
@@ -88,32 +103,33 @@ TEST_F(RenderSystemTest, sorting_sprites) {
// 3. sorting_in_layer: 2, order_in_layer: 1 (entity2)
// 4. sorting_in_layer: 5, order_in_layer: 5 (entity1)
- EXPECT_EQ(sorted_sprites[0].get().sorting_in_layer, 1);
- EXPECT_EQ(sorted_sprites[0].get().order_in_layer, 1);
+ EXPECT_EQ(sorted_sprites[0].get().data.sorting_in_layer, 1);
+ EXPECT_EQ(sorted_sprites[0].get().data.order_in_layer, 1);
- EXPECT_EQ(sorted_sprites[1].get().sorting_in_layer, 1);
- EXPECT_EQ(sorted_sprites[1].get().order_in_layer, 2);
+ EXPECT_EQ(sorted_sprites[1].get().data.sorting_in_layer, 1);
+ EXPECT_EQ(sorted_sprites[1].get().data.order_in_layer, 2);
- EXPECT_EQ(sorted_sprites[2].get().sorting_in_layer, 2);
- EXPECT_EQ(sorted_sprites[2].get().order_in_layer, 1);
+ EXPECT_EQ(sorted_sprites[2].get().data.sorting_in_layer, 2);
+ EXPECT_EQ(sorted_sprites[2].get().data.order_in_layer, 1);
- EXPECT_EQ(sorted_sprites[3].get().sorting_in_layer, 5);
- EXPECT_EQ(sorted_sprites[3].get().order_in_layer, 5);
+ EXPECT_EQ(sorted_sprites[3].get().data.sorting_in_layer, 5);
+ EXPECT_EQ(sorted_sprites[3].get().data.order_in_layer, 5);
for (size_t i = 1; i < sorted_sprites.size(); ++i) {
const Sprite & prev = sorted_sprites[i - 1].get();
const Sprite & curr = sorted_sprites[i].get();
- if (prev.sorting_in_layer == curr.sorting_in_layer) {
- EXPECT_LE(prev.order_in_layer, curr.order_in_layer);
+ if (prev.data.sorting_in_layer == curr.data.sorting_in_layer) {
+ EXPECT_LE(prev.data.order_in_layer, curr.data.order_in_layer);
} else {
- EXPECT_LE(prev.sorting_in_layer, curr.sorting_in_layer);
+ EXPECT_LE(prev.data.sorting_in_layer, curr.data.sorting_in_layer);
}
}
}
TEST_F(RenderSystemTest, Update) {
- entity1.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f);
+ entity1.add_component<Camera>(ivec2{100, 100}, vec2{100, 100},
+ Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
{
vector<reference_wrapper<Sprite>> sprites = this->mgr.get_components_by_type<Sprite>();
ASSERT_EQ(sprites.size(), 4);
@@ -141,7 +157,9 @@ TEST_F(RenderSystemTest, Camera) {
EXPECT_NE(cameras.size(), 1);
}
{
- entity1.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f);
+ entity1.add_component<Camera>(ivec2{100, 100}, vec2{100, 100},
+ Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
+
auto cameras = this->mgr.get_components_by_type<Camera>();
EXPECT_EQ(cameras.size(), 1);
}
@@ -149,18 +167,20 @@ TEST_F(RenderSystemTest, Camera) {
//TODO improve with newer version
}
TEST_F(RenderSystemTest, Color) {
- entity1.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f);
+ entity1.add_component<Camera>(ivec2{100, 100}, vec2{100, 100},
+ Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
+
auto & sprite = this->mgr.get_components_by_id<Sprite>(entity1.id).front().get();
- ASSERT_NE(sprite.sprite_image.texture.get(), nullptr);
+ //ASSERT_NE(sprite.texture.texture.get(), nullptr);
- sprite.color = Color::GREEN;
- EXPECT_EQ(sprite.color.r, Color::GREEN.r);
- EXPECT_EQ(sprite.color.g, Color::GREEN.g);
- EXPECT_EQ(sprite.color.b, Color::GREEN.b);
- EXPECT_EQ(sprite.color.a, Color::GREEN.a);
+ sprite.data.color = Color::GREEN;
+ EXPECT_EQ(sprite.data.color.r, Color::GREEN.r);
+ EXPECT_EQ(sprite.data.color.g, Color::GREEN.g);
+ EXPECT_EQ(sprite.data.color.b, Color::GREEN.b);
+ EXPECT_EQ(sprite.data.color.a, Color::GREEN.a);
this->sys.update();
- EXPECT_EQ(sprite.color.r, Color::GREEN.r);
- EXPECT_EQ(sprite.color.g, Color::GREEN.g);
- EXPECT_EQ(sprite.color.b, Color::GREEN.b);
- EXPECT_EQ(sprite.color.a, Color::GREEN.a);
+ EXPECT_EQ(sprite.data.color.r, Color::GREEN.r);
+ EXPECT_EQ(sprite.data.color.g, Color::GREEN.g);
+ EXPECT_EQ(sprite.data.color.b, Color::GREEN.b);
+ EXPECT_EQ(sprite.data.color.a, Color::GREEN.a);
}
diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp
new file mode 100644
index 0000000..965eeab
--- /dev/null
+++ b/src/test/ResourceManagerTest.cpp
@@ -0,0 +1,84 @@
+#include "manager/Mediator.h"
+#include <gtest/gtest.h>
+
+#define private public
+#define protected public
+
+#include <crepe/api/GameObject.h>
+#include <crepe/manager/ResourceManager.h>
+#include <crepe/util/Log.h>
+
+using namespace std;
+using namespace crepe;
+using namespace testing;
+
+class ResourceManagerTest : public Test {
+ Mediator mediator;
+
+public:
+ ResourceManager resource_manager{mediator};
+
+ class Unrelated : public Resource {
+ using Resource::Resource;
+ };
+
+ Asset asset_a{"asset/texture/img.png"};
+ Asset asset_b{"asset/texture/ERROR.png"};
+
+ class TestResource : public Resource {
+ public:
+ static unsigned instances;
+
+ public:
+ const unsigned instance;
+ TestResource(const Asset & src, Mediator & mediator)
+ : Resource(src, mediator),
+ instance(this->instances++) {}
+ ~TestResource() { this->instances--; }
+ bool operator==(const TestResource & other) const {
+ return this->instance == other.instance;
+ }
+ };
+
+private:
+ void SetUp() override { TestResource::instances = 0; }
+};
+unsigned ResourceManagerTest::TestResource::instances = 0;
+
+TEST_F(ResourceManagerTest, Default) {
+ TestResource & res_1 = resource_manager.get<TestResource>(asset_a);
+ TestResource & res_2 = resource_manager.get<TestResource>(asset_a);
+ TestResource & res_3 = resource_manager.get<TestResource>(asset_b);
+ TestResource & res_4 = resource_manager.get<TestResource>(asset_b);
+
+ ASSERT_EQ(res_1, res_2);
+ ASSERT_EQ(res_3, res_4);
+ EXPECT_NE(res_1, res_3);
+
+ EXPECT_EQ(TestResource::instances, 2);
+
+ resource_manager.clear();
+}
+
+TEST_F(ResourceManagerTest, Persistent) {
+ resource_manager.set_persistent(asset_a, true);
+ EXPECT_EQ(TestResource::instances, 0);
+
+ resource_manager.get<TestResource>(asset_a);
+ resource_manager.get<TestResource>(asset_a);
+ resource_manager.get<TestResource>(asset_b);
+ resource_manager.get<TestResource>(asset_b);
+ EXPECT_EQ(TestResource::instances, 2);
+
+ resource_manager.clear();
+ EXPECT_EQ(TestResource::instances, 1);
+
+ resource_manager.clear_all();
+ EXPECT_EQ(TestResource::instances, 0);
+}
+
+TEST_F(ResourceManagerTest, UnmatchedType) {
+ EXPECT_NO_THROW({ resource_manager.get<TestResource>(asset_a); });
+
+ EXPECT_THROW({ resource_manager.get<Unrelated>(asset_a); }, runtime_error);
+}
diff --git a/src/test/SaveManagerTest.cpp b/src/test/SaveManagerTest.cpp
new file mode 100644
index 0000000..e9b0c29
--- /dev/null
+++ b/src/test/SaveManagerTest.cpp
@@ -0,0 +1,40 @@
+#include <gtest/gtest.h>
+
+#include <crepe/ValueBroker.h>
+#include <crepe/facade/DB.h>
+#include <crepe/manager/SaveManager.h>
+
+using namespace std;
+using namespace crepe;
+using namespace testing;
+
+class SaveManagerTest : public Test {
+ Mediator m;
+ class TestSaveManager : public SaveManager {
+ using SaveManager::SaveManager;
+
+ // in-memory database for testing
+ DB db{};
+ virtual DB & get_db() override { return this->db; }
+ };
+
+public:
+ TestSaveManager mgr{m};
+};
+
+TEST_F(SaveManagerTest, ReadWrite) {
+ ASSERT_FALSE(mgr.has("foo"));
+ mgr.set<string>("foo", "bar");
+ ASSERT_TRUE(mgr.has("foo"));
+
+ ValueBroker value = mgr.get<string>("foo");
+ EXPECT_EQ(value.get(), "bar");
+}
+
+TEST_F(SaveManagerTest, DefaultValue) {
+ ValueBroker value = mgr.get<int>("foo", 3);
+
+ ASSERT_EQ(value.get(), 3);
+ value.set(5);
+ ASSERT_EQ(value.get(), 5);
+}
diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp
index 62b7d33..9bb260c 100644
--- a/src/test/SceneManagerTest.cpp
+++ b/src/test/SceneManagerTest.cpp
@@ -1,12 +1,13 @@
-#include "types.h"
-#include <crepe/ComponentManager.h>
+#include <gtest/gtest.h>
+
#include <crepe/api/GameObject.h>
#include <crepe/api/Metadata.h>
#include <crepe/api/Scene.h>
-#include <crepe/api/SceneManager.h>
#include <crepe/api/Transform.h>
#include <crepe/api/Vector2.h>
-#include <gtest/gtest.h>
+#include <crepe/manager/ComponentManager.h>
+#include <crepe/manager/SceneManager.h>
+#include <crepe/types.h>
using namespace std;
using namespace crepe;
@@ -14,7 +15,8 @@ using namespace crepe;
class ConcreteScene1 : public Scene {
public:
void load_scene() {
- ComponentManager & mgr = this->component_manager;
+ Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
GameObject object1 = mgr.new_object("scene_1", "tag_scene_1", vec2{0, 0}, 0, 1);
GameObject object2 = mgr.new_object("scene_1", "tag_scene_1", vec2{1, 0}, 0, 1);
GameObject object3 = mgr.new_object("scene_1", "tag_scene_1", vec2{2, 0}, 0, 1);
@@ -26,7 +28,8 @@ public:
class ConcreteScene2 : public Scene {
public:
void load_scene() {
- ComponentManager & mgr = this->component_manager;
+ Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
GameObject object1 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 0}, 0, 1);
GameObject object2 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 1}, 0, 1);
GameObject object3 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 2}, 0, 1);
@@ -41,7 +44,8 @@ public:
ConcreteScene3(const string & name) : name(name) {}
void load_scene() {
- ComponentManager & mgr = this->component_manager;
+ Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
GameObject object1 = mgr.new_object("scene_3", "tag_scene_3", vec2{0, 0}, 0, 1);
}
@@ -52,9 +56,11 @@ private:
};
class SceneManagerTest : public ::testing::Test {
+ Mediator m;
+
public:
- ComponentManager component_mgr{};
- SceneManager scene_mgr{component_mgr};
+ ComponentManager component_mgr{m};
+ SceneManager scene_mgr{m};
};
TEST_F(SceneManagerTest, loadScene) {
diff --git a/src/test/ScriptECSTest.cpp b/src/test/ScriptECSTest.cpp
new file mode 100644
index 0000000..1ec33ba
--- /dev/null
+++ b/src/test/ScriptECSTest.cpp
@@ -0,0 +1,41 @@
+#include <gtest/gtest.h>
+
+#define protected public
+
+#include <crepe/api/BehaviorScript.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Metadata.h>
+#include <crepe/api/Script.h>
+#include <crepe/manager/ComponentManager.h>
+#include <crepe/system/ScriptSystem.h>
+
+#include "ScriptTest.h"
+
+using namespace std;
+using namespace crepe;
+using namespace testing;
+
+class ScriptECSTest : public ScriptTest {
+public:
+ class TestComponent : public Component {
+ using Component::Component;
+ };
+};
+
+TEST_F(ScriptECSTest, GetOwnComponent) {
+ MyScript & script = this->script;
+ Metadata & metadata = script.get_component<Metadata>();
+
+ EXPECT_EQ(metadata.name, OBJ_NAME);
+}
+
+TEST_F(ScriptECSTest, GetOwnComponents) {
+ const unsigned COUNT = 4;
+
+ for (unsigned i = 0; i < COUNT; i++) entity.add_component<TestComponent>();
+
+ MyScript & script = this->script;
+ RefVector<TestComponent> components = script.get_components<TestComponent>();
+
+ EXPECT_EQ(components.size(), COUNT);
+}
diff --git a/src/test/ScriptEventTest.cpp b/src/test/ScriptEventTest.cpp
new file mode 100644
index 0000000..c1b4028
--- /dev/null
+++ b/src/test/ScriptEventTest.cpp
@@ -0,0 +1,50 @@
+#include <gtest/gtest.h>
+
+// stupid hack to allow access to private/protected members under test
+#define private public
+#define protected public
+
+#include <crepe/api/BehaviorScript.h>
+#include <crepe/api/Event.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Script.h>
+#include <crepe/api/Vector2.h>
+#include <crepe/manager/ComponentManager.h>
+#include <crepe/manager/EventManager.h>
+#include <crepe/system/ScriptSystem.h>
+
+#include "ScriptTest.h"
+
+using namespace std;
+using namespace crepe;
+using namespace testing;
+
+class ScriptEventTest : public ScriptTest {
+public:
+ EventManager & event_manager = mediator.event_manager;
+
+ class MyEvent : public Event {};
+};
+
+TEST_F(ScriptEventTest, Default) {
+ BehaviorScript & behaviorscript = this->behaviorscript;
+ MyScript & script = this->script;
+ EventManager & evmgr = this->event_manager;
+
+ unsigned event_count = 0;
+ script.subscribe<MyEvent>([&](const MyEvent &) {
+ event_count++;
+ return true;
+ });
+
+ system.update();
+ behaviorscript.active = false;
+ EXPECT_EQ(0, event_count);
+
+ evmgr.trigger_event<MyEvent>();
+ EXPECT_EQ(0, event_count);
+
+ behaviorscript.active = true;
+ evmgr.trigger_event<MyEvent>();
+ EXPECT_EQ(1, event_count);
+}
diff --git a/src/test/ScriptSaveManagerTest.cpp b/src/test/ScriptSaveManagerTest.cpp
new file mode 100644
index 0000000..64403c4
--- /dev/null
+++ b/src/test/ScriptSaveManagerTest.cpp
@@ -0,0 +1,35 @@
+#include <gtest/gtest.h>
+
+// stupid hack to allow access to private/protected members under test
+#define private public
+#define protected public
+
+#include <crepe/facade/DB.h>
+#include <crepe/manager/SaveManager.h>
+
+#include "ScriptTest.h"
+
+using namespace std;
+using namespace crepe;
+using namespace testing;
+
+class ScriptSaveManagerTest : public ScriptTest {
+public:
+ class TestSaveManager : public SaveManager {
+ using SaveManager::SaveManager;
+
+ // in-memory database for testing
+ DB db{};
+ virtual DB & get_db() override { return this->db; }
+ };
+
+ TestSaveManager save_mgr{mediator};
+};
+
+TEST_F(ScriptSaveManagerTest, GetSaveManager) {
+ MyScript & script = this->script;
+
+ SaveManager & mgr = script.get_save_manager();
+
+ EXPECT_EQ(&mgr, &save_mgr);
+}
diff --git a/src/test/ScriptSceneTest.cpp b/src/test/ScriptSceneTest.cpp
new file mode 100644
index 0000000..2568049
--- /dev/null
+++ b/src/test/ScriptSceneTest.cpp
@@ -0,0 +1,30 @@
+#include <gtest/gtest.h>
+
+// stupid hack to allow access to private/protected members under test
+#define private public
+#define protected public
+
+#include "ScriptTest.h"
+#include <crepe/manager/SceneManager.h>
+
+using namespace std;
+using namespace crepe;
+using namespace testing;
+
+class ScriptSceneTest : public ScriptTest {
+public:
+ SceneManager scene_manager{mediator};
+
+ class MyScene : public Scene {};
+};
+
+TEST_F(ScriptSceneTest, Default) {
+ BehaviorScript & behaviorscript = this->behaviorscript;
+ MyScript & script = this->script;
+
+ const char * non_default_value = "foo";
+ ASSERT_NE(non_default_value, scene_manager.next_scene);
+
+ script.set_next_scene(non_default_value);
+ EXPECT_EQ(non_default_value, scene_manager.next_scene);
+}
diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp
index 78d5061..acdae70 100644
--- a/src/test/ScriptTest.cpp
+++ b/src/test/ScriptTest.cpp
@@ -1,129 +1,75 @@
+#include <gmock/gmock.h>
#include <gtest/gtest.h>
// stupid hack to allow access to private/protected members under test
#define private public
#define protected public
-#include <crepe/ComponentManager.h>
-#include <crepe/api/BehaviorScript.h>
-#include <crepe/api/Event.h>
-#include <crepe/api/EventManager.h>
-#include <crepe/api/GameObject.h>
-#include <crepe/api/Script.h>
-#include <crepe/api/Vector2.h>
-#include <crepe/system/ScriptSystem.h>
+#include "ScriptTest.h"
using namespace std;
using namespace crepe;
using namespace testing;
-class MyEvent : public Event {};
-
-class ScriptTest : public Test {
-public:
- ComponentManager component_manager{};
- ScriptSystem system{component_manager};
- EventManager & event_manager = EventManager::get_instance();
-
- class MyScript : public Script {
- // NOTE: default (private) visibility of init and update shouldn't cause
- // issues!
- void init() {
- this->init_count++;
-
- subscribe<MyEvent>([this](const MyEvent &) {
- this->event_count++;
- return true;
- });
-
- // init should never be called more than once
- EXPECT_LE(this->init_count, 1);
- }
- void update() { this->update_count++; }
-
- public:
- unsigned init_count = 0;
- unsigned update_count = 0;
- unsigned event_count = 0;
- };
-
- OptionalRef<BehaviorScript> behaviorscript;
- OptionalRef<MyScript> script;
-
- void SetUp() override {
- auto & mgr = this->component_manager;
- GameObject entity = mgr.new_object("name");
- BehaviorScript & component = entity.add_component<BehaviorScript>();
-
- this->behaviorscript = component;
- ASSERT_TRUE(this->behaviorscript);
- EXPECT_EQ(component.script.get(), nullptr);
- component.set_script<MyScript>();
- ASSERT_NE(component.script.get(), nullptr);
-
- this->script = *(MyScript *) component.script.get();
- ASSERT_TRUE(this->script);
-
- // sanity
- MyScript & script = this->script;
- ASSERT_EQ(script.init_count, 0);
- ASSERT_EQ(script.update_count, 0);
- ASSERT_EQ(script.event_count, 0);
- }
-};
+void ScriptTest::SetUp() {
+ auto & mgr = this->component_manager;
+ BehaviorScript & component = entity.add_component<BehaviorScript>();
+
+ this->behaviorscript = component;
+ ASSERT_TRUE(this->behaviorscript);
+ EXPECT_EQ(component.script.get(), nullptr);
+ component.set_script<NiceMock<MyScript>>();
+ ASSERT_NE(component.script.get(), nullptr);
+
+ this->script = *(MyScript *) component.script.get();
+ ASSERT_TRUE(this->script);
+}
TEST_F(ScriptTest, Default) {
MyScript & script = this->script;
- EXPECT_EQ(0, script.init_count);
- EXPECT_EQ(0, script.update_count);
- EXPECT_EQ(0, script.event_count);
+ EXPECT_CALL(script, init()).Times(0);
+ EXPECT_CALL(script, update()).Times(0);
}
TEST_F(ScriptTest, UpdateOnce) {
MyScript & script = this->script;
- system.update();
- EXPECT_EQ(1, script.init_count);
- EXPECT_EQ(1, script.update_count);
- EXPECT_EQ(0, script.event_count);
+ {
+ InSequence seq;
+
+ EXPECT_CALL(script, init()).Times(1);
+ EXPECT_CALL(script, update()).Times(1);
+ system.update();
+ }
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(script, init()).Times(0);
+ EXPECT_CALL(script, update()).Times(1);
+ system.update();
+ }
}
TEST_F(ScriptTest, UpdateInactive) {
BehaviorScript & behaviorscript = this->behaviorscript;
MyScript & script = this->script;
- behaviorscript.active = false;
- system.update();
- EXPECT_EQ(0, script.init_count);
- EXPECT_EQ(0, script.update_count);
- EXPECT_EQ(0, script.event_count);
-
- behaviorscript.active = true;
- system.update();
- EXPECT_EQ(1, script.init_count);
- EXPECT_EQ(1, script.update_count);
- EXPECT_EQ(0, script.event_count);
-}
+ {
+ InSequence seq;
-TEST_F(ScriptTest, EventInactive) {
- BehaviorScript & behaviorscript = this->behaviorscript;
- MyScript & script = this->script;
- EventManager & evmgr = this->event_manager;
-
- system.update();
- behaviorscript.active = false;
- EXPECT_EQ(1, script.init_count);
- EXPECT_EQ(1, script.update_count);
- EXPECT_EQ(0, script.event_count);
-
- evmgr.trigger_event<MyEvent>();
- EXPECT_EQ(1, script.init_count);
- EXPECT_EQ(1, script.update_count);
- EXPECT_EQ(0, script.event_count);
-
- behaviorscript.active = true;
- evmgr.trigger_event<MyEvent>();
- EXPECT_EQ(1, script.init_count);
- EXPECT_EQ(1, script.update_count);
- EXPECT_EQ(1, script.event_count);
+ EXPECT_CALL(script, init()).Times(0);
+ EXPECT_CALL(script, update()).Times(0);
+ behaviorscript.active = false;
+ system.update();
+ }
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(script, init()).Times(1);
+ EXPECT_CALL(script, update()).Times(1);
+ behaviorscript.active = true;
+ system.update();
+ }
}
diff --git a/src/test/ScriptTest.h b/src/test/ScriptTest.h
new file mode 100644
index 0000000..309e016
--- /dev/null
+++ b/src/test/ScriptTest.h
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <crepe/api/BehaviorScript.h>
+#include <crepe/api/Script.h>
+#include <crepe/manager/ComponentManager.h>
+#include <crepe/system/ScriptSystem.h>
+
+class ScriptTest : public testing::Test {
+protected:
+ crepe::Mediator mediator;
+ static constexpr const char * OBJ_NAME = "foo";
+
+public:
+ crepe::ComponentManager component_manager{mediator};
+ crepe::ScriptSystem system{mediator};
+ crepe::GameObject entity = component_manager.new_object(OBJ_NAME);
+
+ class MyScript : public crepe::Script {
+ // NOTE: explicitly stating `public:` is not required on actual scripts
+
+ public:
+ MOCK_METHOD(void, init, (), (override));
+ MOCK_METHOD(void, update, (), (override));
+ };
+
+ crepe::OptionalRef<crepe::BehaviorScript> behaviorscript;
+ crepe::OptionalRef<MyScript> script;
+
+ virtual void SetUp();
+};
diff --git a/src/test/Vector2Test.cpp b/src/test/Vector2Test.cpp
index 17bca41..1e21af9 100644
--- a/src/test/Vector2Test.cpp
+++ b/src/test/Vector2Test.cpp
@@ -382,3 +382,151 @@ TEST_F(Vector2Test, NotEquals) {
EXPECT_FALSE(long_vec1 != long_vec1);
EXPECT_TRUE(long_vec1 != long_vec2);
}
+
+TEST_F(Vector2Test, Truncate) {
+ Vector2<int> vec = {3, 4};
+ vec.truncate(3);
+ EXPECT_EQ(vec.x, 0);
+ EXPECT_EQ(vec.y, 0);
+
+ Vector2<double> vec2 = {3.0, 4.0};
+ vec2.truncate(3.0);
+ EXPECT_FLOAT_EQ(vec2.x, 1.8);
+ EXPECT_FLOAT_EQ(vec2.y, 2.4);
+
+ Vector2<long> vec3 = {3, 4};
+ vec3.truncate(3);
+ EXPECT_EQ(vec3.x, 0);
+ EXPECT_EQ(vec3.y, 0);
+
+ Vector2<float> vec4 = {3.0f, 4.0f};
+ vec4.truncate(3.0f);
+ EXPECT_FLOAT_EQ(vec4.x, 1.8f);
+ EXPECT_FLOAT_EQ(vec4.y, 2.4f);
+}
+
+TEST_F(Vector2Test, Normalize) {
+ Vector2<int> vec = {3, 4};
+ vec.normalize();
+ EXPECT_EQ(vec.x, 0);
+ EXPECT_EQ(vec.y, 0);
+
+ Vector2<double> vec2 = {3.0, 4.0};
+ vec2.normalize();
+ EXPECT_FLOAT_EQ(vec2.x, 0.6);
+ EXPECT_FLOAT_EQ(vec2.y, 0.8);
+
+ Vector2<long> vec3 = {3, 4};
+ vec3.normalize();
+ EXPECT_EQ(vec3.x, 0);
+ EXPECT_EQ(vec3.y, 0);
+
+ Vector2<float> vec4 = {3.0f, 4.0f};
+ vec4.normalize();
+ EXPECT_FLOAT_EQ(vec4.x, 0.6f);
+ EXPECT_FLOAT_EQ(vec4.y, 0.8f);
+}
+
+TEST_F(Vector2Test, Length) {
+ Vector2<int> vec = {3, 4};
+ EXPECT_EQ(vec.length(), 5);
+
+ Vector2<double> vec2 = {3.0, 4.0};
+ EXPECT_FLOAT_EQ(vec2.length(), 5.0);
+
+ Vector2<long> vec3 = {3, 4};
+ EXPECT_EQ(vec3.length(), 5);
+
+ Vector2<float> vec4 = {3.0f, 4.0f};
+ EXPECT_FLOAT_EQ(vec4.length(), 5.0f);
+}
+
+TEST_F(Vector2Test, LengthSquared) {
+ Vector2<int> vec = {3, 4};
+ EXPECT_EQ(vec.length_squared(), 25);
+
+ Vector2<double> vec2 = {3.0, 4.0};
+ EXPECT_FLOAT_EQ(vec2.length_squared(), 25.0);
+
+ Vector2<long> vec3 = {3, 4};
+ EXPECT_EQ(vec3.length_squared(), 25);
+
+ Vector2<float> vec4 = {3.0f, 4.0f};
+ EXPECT_FLOAT_EQ(vec4.length_squared(), 25.0f);
+}
+
+TEST_F(Vector2Test, Dot) {
+ Vector2<int> vec1 = {3, 4};
+ Vector2<int> vec2 = {5, 6};
+ EXPECT_EQ(vec1.dot(vec2), 39);
+
+ Vector2<double> vec3 = {3.0, 4.0};
+ Vector2<double> vec4 = {5.0, 6.0};
+ EXPECT_FLOAT_EQ(vec3.dot(vec4), 39.0);
+
+ Vector2<long> vec5 = {3, 4};
+ Vector2<long> vec6 = {5, 6};
+ EXPECT_EQ(vec5.dot(vec6), 39);
+
+ Vector2<float> vec7 = {3.0f, 4.0f};
+ Vector2<float> vec8 = {5.0f, 6.0f};
+ EXPECT_FLOAT_EQ(vec7.dot(vec8), 39.0f);
+}
+
+TEST_F(Vector2Test, Distance) {
+ Vector2<int> vec1 = {1, 1};
+ Vector2<int> vec2 = {4, 5};
+ EXPECT_EQ(vec1.distance(vec2), 5);
+
+ Vector2<double> vec3 = {1.0, 1.0};
+ Vector2<double> vec4 = {4.0, 5.0};
+ EXPECT_FLOAT_EQ(vec3.distance(vec4), 5.0);
+
+ Vector2<long> vec5 = {1, 1};
+ Vector2<long> vec6 = {4, 5};
+ EXPECT_EQ(vec5.distance(vec6), 5);
+
+ Vector2<float> vec7 = {1.0f, 1.0f};
+ Vector2<float> vec8 = {4.0f, 5.0f};
+ EXPECT_FLOAT_EQ(vec7.distance(vec8), 5.0f);
+}
+
+TEST_F(Vector2Test, DistanceSquared) {
+ Vector2<int> vec1 = {3, 4};
+ Vector2<int> vec2 = {5, 6};
+ EXPECT_EQ(vec1.distance_squared(vec2), 8);
+
+ Vector2<double> vec3 = {3.0, 4.0};
+ Vector2<double> vec4 = {5.0, 6.0};
+ EXPECT_FLOAT_EQ(vec3.distance_squared(vec4), 8.0);
+
+ Vector2<long> vec5 = {3, 4};
+ Vector2<long> vec6 = {5, 6};
+ EXPECT_EQ(vec5.distance_squared(vec6), 8);
+
+ Vector2<float> vec7 = {3.0f, 4.0f};
+ Vector2<float> vec8 = {5.0f, 6.0f};
+ EXPECT_FLOAT_EQ(vec7.distance_squared(vec8), 8.0f);
+}
+
+TEST_F(Vector2Test, Perpendicular) {
+ Vector2<int> vec = {3, 4};
+ Vector2<int> result = vec.perpendicular();
+ EXPECT_EQ(result.x, -4);
+ EXPECT_EQ(result.y, 3);
+
+ Vector2<double> vec2 = {3.0, 4.0};
+ Vector2<double> result2 = vec2.perpendicular();
+ EXPECT_FLOAT_EQ(result2.x, -4.0);
+ EXPECT_FLOAT_EQ(result2.y, 3.0);
+
+ Vector2<long> vec3 = {3, 4};
+ Vector2<long> result3 = vec3.perpendicular();
+ EXPECT_EQ(result3.x, -4);
+ EXPECT_EQ(result3.y, 3);
+
+ Vector2<float> vec4 = {3.0f, 4.0f};
+ Vector2<float> result4 = vec4.perpendicular();
+ EXPECT_FLOAT_EQ(result4.x, -4.0f);
+ EXPECT_FLOAT_EQ(result4.y, 3.0f);
+}
diff --git a/src/test/main.cpp b/src/test/main.cpp
index aece72d..ed2aed5 100644
--- a/src/test/main.cpp
+++ b/src/test/main.cpp
@@ -1,9 +1,5 @@
-#include <gtest/gtest.h>
-
-#define protected public
-#define private public
-
#include <crepe/api/Config.h>
+#include <gtest/gtest.h>
using namespace crepe;
using namespace testing;
@@ -11,12 +7,14 @@ using namespace testing;
class GlobalConfigReset : public EmptyTestEventListener {
public:
Config & cfg = Config::get_instance();
- Config cfg_default = Config();
// This function is called before each test
void OnTestStart(const TestInfo &) override {
- cfg = cfg_default;
- cfg.log.level = Log::Level::WARNING;
+ cfg = {
+ .log = {
+ .level = Log::Level::WARNING,
+ },
+ };
}
};