aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.clang-tidy2
-rw-r--r--contributing.md282
-rw-r--r--src/crepe/Collider.cpp2
-rw-r--r--src/crepe/Collider.h2
-rw-r--r--src/crepe/Component.cpp3
-rw-r--r--src/crepe/Component.h30
-rw-r--r--src/crepe/ComponentManager.cpp7
-rw-r--r--src/crepe/ComponentManager.h103
-rw-r--r--src/crepe/ComponentManager.hpp59
-rw-r--r--src/crepe/api/AudioSource.h2
-rw-r--r--src/crepe/api/BehaviorScript.h5
-rw-r--r--src/crepe/api/CMakeLists.txt11
-rw-r--r--src/crepe/api/CircleCollider.h2
-rw-r--r--src/crepe/api/Config.h10
-rw-r--r--src/crepe/api/Force.cpp21
-rw-r--r--src/crepe/api/Force.h17
-rw-r--r--src/crepe/api/GameObject.cpp28
-rw-r--r--src/crepe/api/GameObject.h57
-rw-r--r--src/crepe/api/GameObject.hpp2
-rw-r--r--src/crepe/api/Metadata.cpp7
-rw-r--r--src/crepe/api/Metadata.h43
-rw-r--r--src/crepe/api/ParticleEmitter.cpp17
-rw-r--r--src/crepe/api/ParticleEmitter.h2
-rw-r--r--src/crepe/api/Point.h11
-rw-r--r--src/crepe/api/Rigidbody.cpp14
-rw-r--r--src/crepe/api/Rigidbody.h108
-rw-r--r--src/crepe/api/Scene.cpp5
-rw-r--r--src/crepe/api/Scene.h17
-rw-r--r--src/crepe/api/SceneManager.cpp38
-rw-r--r--src/crepe/api/SceneManager.h50
-rw-r--r--src/crepe/api/SceneManager.hpp18
-rw-r--r--src/crepe/api/Sprite.cpp4
-rw-r--r--src/crepe/api/Sprite.h6
-rw-r--r--src/crepe/api/Transform.cpp11
-rw-r--r--src/crepe/api/Transform.h32
-rw-r--r--src/crepe/api/Vector2.cpp57
-rw-r--r--src/crepe/api/Vector2.h47
-rw-r--r--src/crepe/facade/CMakeLists.txt2
-rw-r--r--src/crepe/facade/SDLApp.cpp71
-rw-r--r--src/crepe/facade/SDLApp.h26
-rw-r--r--src/crepe/system/PhysicsSystem.cpp106
-rw-r--r--src/crepe/system/PhysicsSystem.h17
-rw-r--r--src/crepe/system/RenderSystem.cpp2
-rw-r--r--src/crepe/system/ScriptSystem.cpp8
-rw-r--r--src/crepe/system/ScriptSystem.h5
-rw-r--r--src/crepe/system/System.h14
-rw-r--r--src/crepe/types.h9
-rw-r--r--src/example/CMakeLists.txt3
-rw-r--r--src/example/asset_manager.cpp2
-rw-r--r--src/example/audio_internal.cpp2
-rw-r--r--src/example/ecs.cpp55
-rw-r--r--src/example/particle.cpp102
-rw-r--r--src/example/physics.cpp28
-rw-r--r--src/example/rendering.cpp24
-rw-r--r--src/example/scene_manager.cpp75
-rw-r--r--src/example/script.cpp10
-rw-r--r--src/makefile102
-rw-r--r--src/test/CMakeLists.txt1
-rw-r--r--src/test/PhysicsTest.cpp120
59 files changed, 1424 insertions, 492 deletions
diff --git a/.clang-tidy b/.clang-tidy
index 3408f75..4d8170b 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -20,6 +20,8 @@ CheckOptions:
value: '_.*'
- key: 'readability-identifier-naming.ConstantParameterCase'
value: 'lower_case'
+ - key: 'readability-identifier-naming.ConstantMemberCase'
+ value: 'lower_case'
- key: 'readability-identifier-naming.VariableCase'
value: 'lower_case'
- key: 'readability-identifier-naming.VariableIgnoredRegexp'
diff --git a/contributing.md b/contributing.md
index 2fe46f7..8799057 100644
--- a/contributing.md
+++ b/contributing.md
@@ -49,11 +49,13 @@ that you can click on to open them.
class Cars {};
```
</td></tr></table></details>
-- Source files contain the following types of comments:
+- Source files (`.cpp`, `.hpp`) contain the following types of comments:
- What is the code supposed to do (optional)
- Implementation details (if applicable)
-- Header files contain the following types of comments:
- - Usage documentation (required)
+- Header files (`.h`) contain the following types of comments:
+ - [Usage documentation](#documentation) (required)
+ > [!NOTE]
+ > Constructors/destructors aren't required to have a `\brief` description
- Implementation details (if they affect the header)
- Design/data structure decisions (if applicable)
- <details><summary>
@@ -76,8 +78,8 @@ that you can click on to open them.
}
```
</td></tr></table></details>
-- Header includes are split into paragraphs separated by a blank line. The
- order is:
+- Header includes (at the top of files) are split into paragraphs separated by
+ a blank line. The order is:
1. system headers (using `<`brackets`>`)
2. relative headers NOT in the same folder as the current file
3. relative headers in the same folder as the current file
@@ -110,7 +112,54 @@ that you can click on to open them.
```
</td></tr></table></details>
- <details><summary>
- <code>using namespace</code> may not be used in header files, only in source files.
+ If there is one, the matching template header (<code>.hpp</code>) is included
+ at the bottom of the regular header (<code>.h</code>)
+ </summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
+
+ Foo.h:
+ ```cpp
+ #pragma once
+
+ template <typename T>
+ void foo();
+
+ #include "Foo.hpp"
+ ```
+
+ Foo.hpp:
+ ```cpp
+ #pragma once
+ #include "Foo.h"
+
+ template <typename T>
+ void foo() {
+ // ...
+ }
+ ```
+ </td><td>
+
+ Foo.h:
+ ```cpp
+ #pragma once
+
+ template <typename T>
+ void foo();
+ ```
+
+ Foo.hpp:
+ ```cpp
+ #pragma once
+ #include "Foo.h"
+
+ template <typename T>
+ void foo() {
+ // ...
+ }
+ ```
+ </td></tr></table></details>
+- <details><summary>
+ <code>using namespace</code> may not be used in header files (.h, .hpp), only
+ in source files (.cpp).
</summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
example.h:
@@ -277,7 +326,7 @@ that you can click on to open them.
```cpp
struct Foo {
- int bar;
+ int bar = 0;
std::string baz;
};
```
@@ -285,7 +334,7 @@ that you can click on to open them.
```cpp
struct Foo {
- int bar = 0;
+ int bar;
std::string baz;
};
```
@@ -386,6 +435,223 @@ that you can click on to open them.
#endif
```
</td></tr></table></details>
+- <details><summary>
+ Variables that are being moved always use the fully qualified <code>std::move</code>
+ </summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
+
+ ```cpp
+ using namespace std;
+ string foo = "bar";
+ ref_fn(std::move(foo));
+ ```
+ </td><td>
+
+ ```cpp
+ using namespace std;
+ string foo = "bar";
+ ref_fn(move(foo));
+ ```
+ </td></tr></table></details>
+- <details><summary>
+ If possible, classes and structs are passed to functions by (const) reference
+ </summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
+
+ ```cpp
+ void foo(const Point & p);
+ ```
+ </td><td>
+
+ ```cpp
+ void foo(Point & p);
+ void bar(Point p);
+ ```
+ </td></tr></table></details>
+- <details><summary>
+ Follow the rule of five
+ </summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
+
+ ```cpp
+ class Foo {
+ public:
+ Foo();
+ ~Foo();
+ Foo(Foo &&) noexcept;
+ Foo & operator = (const Foo &);
+ Foo & operator = (Foo &&) noexcept;
+ };
+ ```
+ </td><td>
+
+ ```cpp
+ class Foo {
+ public:
+ Foo();
+ ~Foo();
+ };
+ ```
+ </td></tr></table></details>
+- <details><summary>
+ Ensure const-correctness
+ </summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
+
+ ```cpp
+ class Foo {
+ public:
+ int get_value() const;
+ void set_value(int new_value);
+ const std::string & get_name() const;
+ void set_name(const std::string & new_name);
+ private:
+ int value;
+ std::string name;
+ };
+ ```
+ </td><td>
+
+ ```cpp
+ class Foo {
+ public:
+ int get_value();
+ void set_value(int new_value);
+ std::string get_name();
+ void set_name(std::string new_name);
+ private:
+ int value;
+ std::string name;
+ };
+ ```
+ </td></tr></table></details>
+- <details><summary>
+ Files should be named after the class/struct/interface they implement
+ </summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
+
+ ```cpp
+ MyClass.h
+ MyClass.cpp
+ MyClass.hpp
+ ```
+ </td><td>
+
+ ```cpp
+ my_class.h
+ myClass.cpp
+ my-class.hpp
+ ```
+ </td></tr></table></details>
+- <details><summary>
+ Implementations are not allowed in header files, except if the implementation
+
+ - is `= default`
+ - is `= delete`
+ - is `{}` (empty)
+ - only returns a constant literal
+ </summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
+
+ ```cpp
+ class Foo {
+ public:
+ int get_value() const { return 42; }
+ };
+ ```
+ </td><td>
+
+ ```cpp
+ class Foo {
+ public:
+ int calculate_value() const {
+ int result = 0;
+ // complex calculation
+ return result;
+ }
+ };
+ ```
+ </td></tr></table></details>
+- <details><summary>
+ Use angle brackets (<code><></code>) only for including system headers and
+ double quotes (<code>""</code>) for including other engine files.
+
+ > [!NOTE]
+ > Only files in the examples folder should include engine headers with angle
+ > brackets
+ </summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
+
+ ```cpp
+ #include <iostream>
+
+ #include "facade/Sound.h"
+ ```
+ </td><td>
+
+ ```cpp
+ #include <iostream>
+ #include <crepe/facade/Sound.h>
+ ```
+ </td></tr></table></details>
+- <details><summary>
+ Ensure exception safety by using RAII classes
+ </summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
+
+ ```cpp
+ auto foo = std::make_unique<Foo>();
+ ```
+ </td><td>
+
+ ```cpp
+ Foo* foo = new Foo();
+ // ...
+ delete foo;
+ ```
+ </td></tr></table></details>
+- <details><summary>
+ Do not use C-style memory management APIs (<code>malloc</code>,
+ <code>calloc</code>, <code>free</code>)
+ </summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
+
+ ```cpp
+ Foo * foo = new Foo();
+ delete foo;
+ ```
+ </td><td>
+
+ ```cpp
+ Foo * foo = (Foo *) malloc(sizeof(Foo));
+ free(foo);
+ ```
+ </td></tr></table></details>
+- <details><summary>
+ Prefix all class members with <code>this-></code>
+ </summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
+
+ ```cpp
+ void Foo::set_value(int value) {
+ this->value = value;
+ }
+ ```
+ </td><td>
+
+ ```cpp
+ void Foo::set_value(int new_value) {
+ value = new_value;
+ }
+ ```
+ </td></tr></table></details>
+- <details><summary>
+ Assigning booleans should be done with the
+ <code>true</code>/<code>false</code> literals instead of
+ <code>0</code>/<code>1</code>
+ </summary><table><tr><th>Good</th><th>Bad</th></tr><tr><td>
+
+ ```cpp
+ bool foo = true;
+ bool bar = false;
+ ```
+ </td><td>
+
+ ```cpp
+ bool foo = 1;
+ bool bar = 0;
+ ```
+ </td></tr></table></details>
## CMakeLists-specific
diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp
index 13a3f33..bbec488 100644
--- a/src/crepe/Collider.cpp
+++ b/src/crepe/Collider.cpp
@@ -2,4 +2,4 @@
using namespace crepe;
-Collider::Collider(uint32_t gameObjectId) : Component(gameObjectId) {}
+Collider::Collider(game_object_id_t id) : Component(id) {}
diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h
index 68a7d1d..827f83d 100644
--- a/src/crepe/Collider.h
+++ b/src/crepe/Collider.h
@@ -6,7 +6,7 @@ namespace crepe {
class Collider : public Component {
public:
- Collider(uint32_t game_object_id);
+ Collider(game_object_id_t id);
int size;
};
diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp
index 358ce31..73c2d07 100644
--- a/src/crepe/Component.cpp
+++ b/src/crepe/Component.cpp
@@ -1,5 +1,6 @@
#include "Component.h"
+#include "types.h"
using namespace crepe;
-Component::Component(uint32_t id) : game_object_id(id), active(true) {}
+Component::Component(game_object_id_t id) : game_object_id(id) {}
diff --git a/src/crepe/Component.h b/src/crepe/Component.h
index bc44865..0fe60b2 100644
--- a/src/crepe/Component.h
+++ b/src/crepe/Component.h
@@ -1,22 +1,46 @@
#pragma once
+#include "types.h"
+
#include <cstdint>
namespace crepe {
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.
+ */
class Component {
protected:
+ //! Only the ComponentManager can create components
friend class crepe::ComponentManager;
- Component(uint32_t id);
+ /**
+ * \param id The id of the GameObject this component belongs to
+ */
+ Component(game_object_id_t id);
public:
virtual ~Component() = default;
+ /**
+ * \brief Get the maximum number of instances for this component
+ *
+ * This method returns -1 by default, which means that there is no limit
+ * for the number of instances. Concrete components can override this method
+ * to set a limit.
+ *
+ * \return The maximum number of instances for this component
+ */
+ virtual int get_instances_max() const { return -1; }
public:
- uint32_t game_object_id;
- bool active;
+ //! The id of the GameObject this component belongs to
+ const game_object_id_t game_object_id;
+ //! Whether the component is active
+ bool active = true;
};
} // namespace crepe
diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp
index 8bde33a..85149c8 100644
--- a/src/crepe/ComponentManager.cpp
+++ b/src/crepe/ComponentManager.cpp
@@ -1,6 +1,7 @@
-#include "ComponentManager.h"
#include "util/log.h"
+#include "ComponentManager.h"
+
using namespace crepe;
ComponentManager & ComponentManager::get_instance() {
@@ -8,9 +9,9 @@ ComponentManager & ComponentManager::get_instance() {
return instance;
}
-void ComponentManager::delete_all_components_of_id(uint32_t id) {
+void ComponentManager::delete_all_components_of_id(game_object_id_t id) {
// Loop through all the types (in the unordered_map<>)
- for (auto & [type, componentArray] : components) {
+ for (auto & [type, componentArray] : this->components) {
// Make sure that the id (that we are looking for) is within the boundaries of the vector<>
if (id < componentArray.size()) {
// Clear the components at this specific id
diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h
index 2b5e1df..c8c196c 100644
--- a/src/crepe/ComponentManager.h
+++ b/src/crepe/ComponentManager.h
@@ -10,47 +10,110 @@
namespace crepe {
+/**
+ * \brief Manages all components
+ *
+ * This class manages all components. It provides methods to add, delete and get
+ * components.
+ */
class ComponentManager {
public:
- // Singleton
+ /**
+ * \brief Get the instance of the ComponentManager
+ *
+ * \return The instance of the ComponentManager
+ */
static ComponentManager & get_instance();
ComponentManager(const ComponentManager &) = delete;
ComponentManager(ComponentManager &&) = delete;
ComponentManager & operator=(const ComponentManager &) = delete;
ComponentManager & operator=(ComponentManager &&) = delete;
+ ~ComponentManager();
-public:
- //! Add a component of a specific type
+ /**
+ * \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
+ * \param args The arguments to create the component
+ * \return The created component
+ */
template <typename T, typename... Args>
- T & add_component(uint32_t id, Args &&... args);
- //! Deletes all components of a specific type and id
+ 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
+ */
template <typename T>
- void delete_components_by_id(uint32_t id);
- //! Deletes all components of a specific type
+ 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();
- //! Deletes all components of a specific id
- void delete_all_components_of_id(uint32_t id);
- //! Deletes all 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();
-
- //! Get a vector<> of all components at specific type and id
+ /**
+ * \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
+ */
template <typename T>
std::vector<std::reference_wrapper<T>>
- get_components_by_id(uint32_t id) const;
- //! Get a vector<> of all components of a specific type
+ 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>
std::vector<std::reference_wrapper<T>> get_components_by_type() const;
private:
ComponentManager();
- virtual ~ComponentManager();
- /*
- * The std::unordered_map<std::type_index, std::vector<std::vector<std::unique_ptr<Component>>>> below might seem a bit strange, let me explain this structure:
- * The std::unordered_map<> has a key and value. The key is a std::type_index and the value is a std::vector. So, a new std::vector will be created for each new std::type_index.
- * The first std::vector<> stores another vector<>. This first vector<> is to bind the entity's id to a component.
- * The second std::vector<> stores unique_ptrs. Each component can be gathered via an unique_ptr. This second vector<> allows multiple components of the same std::type_index for one entity (id).
+private:
+ /**
+ * \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.
+ * Every component type has its own vector of vectors of unique pointers to
+ * the components. 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>>>>
diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp
index 9b07f13..98efb49 100644
--- a/src/crepe/ComponentManager.hpp
+++ b/src/crepe/ComponentManager.hpp
@@ -3,11 +3,12 @@
#include <type_traits>
#include "ComponentManager.h"
+#include "types.h"
namespace crepe {
template <class T, typename... Args>
-T & ComponentManager::add_component(uint32_t id, Args &&... args) {
+T & ComponentManager::add_component(game_object_id_t id, Args &&... args) {
using namespace std;
static_assert(is_base_of<Component, T>::value,
@@ -17,38 +18,51 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) {
type_index type = typeid(T);
// Check if this component type is already in the unordered_map<>
- if (components.find(type) == components.end()) {
+ if (this->components.find(type) == this->components.end()) {
//If not, create a new (empty) vector<> of vector<unique_ptr<Component>>
- components[type] = vector<vector<unique_ptr<Component>>>();
+ this->components[type] = vector<vector<unique_ptr<Component>>>();
}
// Resize the vector<> if the id is greater than the current size
- if (id >= components[type].size()) {
+ if (id >= this->components[type].size()) {
// Initialize new slots to nullptr (resize does automatically init to nullptr)
- components[type].resize(id + 1);
+ this->components[type].resize(id + 1);
}
// Create a new component of type T (arguments directly forwarded). The
// constructor must be called by ComponentManager.
- T * instance = new T(id, forward<Args>(args)...);
+ T * instance_ptr = new T(id, forward<Args>(args)...);
+ if (instance_ptr == nullptr) throw std::bad_alloc();
+
+ T & instance_ref = *instance_ptr;
+ unique_ptr<T> instance = unique_ptr<T>(instance_ptr);
+
+ // Check if the vector size is not greater than get_instances_max
+ int max_instances = instance->get_instances_max();
+ if (max_instances != -1 && components[type][id].size() >= max_instances) {
+ // TODO: Exception
+ throw std::runtime_error(
+ "Exceeded maximum number of instances for this component type");
+ }
+
// store its unique_ptr in the vector<>
- components[type][id].push_back(unique_ptr<T>(instance));
+ this->components[type][id].push_back(std::move(instance));
- return *instance;
+ return instance_ref;
}
template <typename T>
-void ComponentManager::delete_components_by_id(uint32_t id) {
+void ComponentManager::delete_components_by_id(game_object_id_t id) {
using namespace std;
// Determine the type of T (this is used as the key of the unordered_map<>)
type_index type = typeid(T);
// Find the type (in the unordered_map<>)
- if (components.find(type) != components.end()) {
+ if (this->components.find(type) != this->components.end()) {
// Get the correct vector<>
vector<vector<unique_ptr<Component>>> & component_array
- = components[type];
+ = this->components[type];
// Make sure that the id (that we are looking for) is within the boundaries of the vector<>
if (id < component_array.size()) {
@@ -63,14 +77,14 @@ void ComponentManager::delete_components() {
// Determine the type of T (this is used as the key of the unordered_map<>)
std::type_index type = typeid(T);
- if (components.find(type) == components.end()) return;
+ if (this->components.find(type) == this->components.end()) return;
- components[type].clear();
+ this->components[type].clear();
}
template <typename T>
std::vector<std::reference_wrapper<T>>
-ComponentManager::get_components_by_id(uint32_t id) const {
+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<>)
@@ -79,11 +93,12 @@ ComponentManager::get_components_by_id(uint32_t id) const {
// Create an empty vector<>
vector<reference_wrapper<T>> component_vector;
- if (components.find(type) == components.end()) return 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
- = components.at(type);
+ = 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;
@@ -112,15 +127,14 @@ ComponentManager::get_components_by_type() const {
// Create an empty vector<>
vector<reference_wrapper<T>> component_vector;
- // Set the id to 0 (the id will also be stored in the returned vector<>)
- // uint32_t id = 0;
// Find the type (in the unordered_map<>)
- if (components.find(type) == components.end()) return 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
- = components.at(type);
+ = this->components.at(type);
// Loop through the whole vector<>
for (const vector<unique_ptr<Component>> & component : component_array) {
@@ -132,12 +146,9 @@ ComponentManager::get_components_by_type() const {
// Ensure that the cast was successful
if (casted_component == nullptr) continue;
- // Pair the dereferenced raw pointer and the id and add it to the vector<>
+ // Add the dereferenced raw pointer to the vector<>
component_vector.emplace_back(ref(*casted_component));
}
-
- // Increase the id (the id will also be stored in the returned vector<>)
- //++id;
}
// Return the vector<>
diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h
index 42add50..1e24ae8 100644
--- a/src/crepe/api/AudioSource.h
+++ b/src/crepe/api/AudioSource.h
@@ -10,7 +10,7 @@ namespace crepe {
class Sound;
//! Audio source component
-class AudioSource : Component {
+class AudioSource : public Component {
public:
AudioSource(std::unique_ptr<Asset> audio_clip);
virtual ~AudioSource() = default;
diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h
index 21638f4..6b1fec7 100644
--- a/src/crepe/api/BehaviorScript.h
+++ b/src/crepe/api/BehaviorScript.h
@@ -5,12 +5,9 @@
#include "../Component.h"
namespace crepe {
+
class ScriptSystem;
class ComponentManager;
-} // namespace crepe
-
-namespace crepe {
-
class Script;
class BehaviorScript : public Component {
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index f55bd9e..3b20142 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -4,7 +4,6 @@ target_sources(crepe PUBLIC
Script.cpp
GameObject.cpp
Rigidbody.cpp
- Force.cpp
ParticleEmitter.cpp
Transform.cpp
Color.cpp
@@ -13,6 +12,10 @@ target_sources(crepe PUBLIC
Sprite.cpp
SaveManager.cpp
Config.cpp
+ Metadata.cpp
+ Scene.cpp
+ SceneManager.cpp
+ Vector2.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -25,10 +28,14 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
GameObject.hpp
Rigidbody.h
Sprite.h
- Point.h
+ Vector2.h
Color.h
Texture.h
AssetManager.h
AssetManager.hpp
SaveManager.h
+ Scene.h
+ Metadata.h
+ SceneManager.h
+ SceneManager.hpp
)
diff --git a/src/crepe/api/CircleCollider.h b/src/crepe/api/CircleCollider.h
index 931b012..caa7e43 100644
--- a/src/crepe/api/CircleCollider.h
+++ b/src/crepe/api/CircleCollider.h
@@ -5,7 +5,7 @@ namespace crepe {
class CircleCollider : public Collider {
public:
- CircleCollider(uint32_t game_object_id, int radius)
+ CircleCollider(game_object_id_t game_object_id, int radius)
: Collider(game_object_id), radius(radius) {}
int radius;
};
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index bbcca87..56e3af5 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -47,6 +47,16 @@ public:
*/
std::string location = "save.crepe.db";
} savemgr;
+
+ //! physics-related settings
+ struct {
+ /**
+ * \brief gravity value of physics system
+ *
+ * Gravity value of game.
+ */
+ double gravity = 1;
+ } physics;
};
} // namespace crepe
diff --git a/src/crepe/api/Force.cpp b/src/crepe/api/Force.cpp
deleted file mode 100644
index 3c33ad3..0000000
--- a/src/crepe/api/Force.cpp
+++ /dev/null
@@ -1,21 +0,0 @@
-#include <cmath>
-
-#include "Force.h"
-
-namespace crepe {
-
-Force::Force(uint32_t game_object_id, uint32_t magnitude, uint32_t direction)
- : Component(game_object_id) {
- // TODO: A standard angle unit should be established for the entire engine
- // and assumed to be the default everywhere. Only conversion functions should
- // explicitly contain the unit (i.e. `deg_to_rad()` & `rad_to_deg()`)
-
- // Convert direction from degrees to radians
- float radian_direction = static_cast<float>(direction) * (M_PI / 180.0f);
- force_x = static_cast<int32_t>(
- std::round(magnitude * std::cos(radian_direction)));
- force_y = static_cast<int32_t>(
- std::round(magnitude * std::sin(radian_direction)));
-}
-
-} // namespace crepe
diff --git a/src/crepe/api/Force.h b/src/crepe/api/Force.h
deleted file mode 100644
index c08a8b9..0000000
--- a/src/crepe/api/Force.h
+++ /dev/null
@@ -1,17 +0,0 @@
-#pragma once
-
-#include <cstdint>
-
-#include "../Component.h"
-
-namespace crepe {
-
-class Force : public Component {
-public:
- Force(uint32_t game_object_id, uint32_t magnitude, uint32_t direction);
-
- int32_t force_x;
- int32_t force_y;
-};
-
-} // namespace crepe
diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp
index 445a60d..d252e77 100644
--- a/src/crepe/api/GameObject.cpp
+++ b/src/crepe/api/GameObject.cpp
@@ -1,7 +1,31 @@
+#include "api/Transform.h"
+
#include "GameObject.h"
+#include "Metadata.h"
using namespace crepe;
using namespace std;
-GameObject::GameObject(uint32_t id, string name, string tag, int layer)
- : id(id), name(name), tag(tag), active(true), layer(layer) {}
+GameObject::GameObject(game_object_id_t id, const std::string & name,
+ const std::string & tag, const Vector2 & position,
+ double rotation, double scale)
+ : id(id) {
+ // Add Transform and Metadata components
+ ComponentManager & mgr = ComponentManager::get_instance();
+ mgr.add_component<Transform>(this->id, position, rotation, scale);
+ mgr.add_component<Metadata>(this->id, name, tag);
+}
+
+void GameObject::set_parent(const GameObject & parent) {
+ ComponentManager & mgr = ComponentManager::get_instance();
+
+ // Set parent on own Metadata component
+ vector<reference_wrapper<Metadata>> this_metadata
+ = mgr.get_components_by_id<Metadata>(this->id);
+ this_metadata.at(0).get().parent = parent.id;
+
+ // Add own id to children list of parent's Metadata component
+ vector<reference_wrapper<Metadata>> parent_metadata
+ = mgr.get_components_by_id<Metadata>(parent.id);
+ parent_metadata.at(0).get().children.push_back(this->id);
+}
diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h
index b5d6399..d703730 100644
--- a/src/crepe/api/GameObject.h
+++ b/src/crepe/api/GameObject.h
@@ -1,22 +1,63 @@
#pragma once
-#include <cstdint>
#include <string>
+#include "types.h"
+
namespace crepe {
+class Vector2;
+
+/**
+ * \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.
+ */
class GameObject {
public:
- GameObject(uint32_t id, std::string name, std::string tag, int layer);
-
+ /**
+ * This constructor creates a new GameObject. It creates a new
+ * Transform and Metadata component and adds them to the ComponentManager.
+ *
+ * \param id The id of the GameObject
+ * \param name The name of the GameObject
+ * \param tag The tag of the GameObject
+ * \param position The position of the GameObject
+ * \param rotation The rotation of the GameObject
+ * \param scale The scale of the GameObject
+ */
+ GameObject(game_object_id_t id, const std::string & name,
+ const std::string & tag, const Vector2 & position,
+ double rotation, double scale);
+ /**
+ * \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
+ * \return The created component
+ */
template <typename T, typename... Args>
T & add_component(Args &&... args);
- uint32_t id;
- std::string name;
- std::string tag;
- bool active;
- int layer;
+public:
+ //! The id of the GameObject
+ const game_object_id_t id;
};
} // namespace crepe
diff --git a/src/crepe/api/GameObject.hpp b/src/crepe/api/GameObject.hpp
index 77cf40e..bfba7fe 100644
--- a/src/crepe/api/GameObject.hpp
+++ b/src/crepe/api/GameObject.hpp
@@ -8,7 +8,7 @@ namespace crepe {
template <typename T, typename... Args>
T & GameObject::add_component(Args &&... args) {
- auto & mgr = ComponentManager::get_instance();
+ ComponentManager & mgr = ComponentManager::get_instance();
return mgr.add_component<T>(this->id, std::forward<Args>(args)...);
}
diff --git a/src/crepe/api/Metadata.cpp b/src/crepe/api/Metadata.cpp
new file mode 100644
index 0000000..76f11d7
--- /dev/null
+++ b/src/crepe/api/Metadata.cpp
@@ -0,0 +1,7 @@
+#include "Metadata.h"
+
+using namespace crepe;
+using namespace std;
+
+Metadata::Metadata(game_object_id_t id, const string & name, const string & tag)
+ : Component(id), name(name), tag(tag) {}
diff --git a/src/crepe/api/Metadata.h b/src/crepe/api/Metadata.h
new file mode 100644
index 0000000..c61e006
--- /dev/null
+++ b/src/crepe/api/Metadata.h
@@ -0,0 +1,43 @@
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "../Component.h"
+
+namespace crepe {
+
+/**
+ * \brief Metadata component
+ *
+ * This class represents the Metadata component. It stores the name, tag, parent
+ * and children of a GameObject.
+ */
+class Metadata : public Component {
+public:
+ /**
+ * \param game_object_id The id of the GameObject this component belongs to
+ * \param name The name of the GameObject
+ * \param tag The tag of the GameObject
+ */
+ Metadata(game_object_id_t id, const std::string & name,
+ const std::string & tag);
+ /**
+ * \brief Get the maximum number of instances for this component
+ *
+ * \return The maximum number of instances for this component
+ */
+ virtual int get_instances_max() const { return 1; }
+
+public:
+ //! The name of the GameObject
+ const std::string name;
+ //! The tag of the GameObject
+ const std::string tag;
+ //! The id of the parent GameObject (-1 if no parent)
+ game_object_id_t parent = -1;
+ //! The ids of the children GameObjects
+ std::vector<game_object_id_t> children;
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp
index 0b3a9ee..3b2e2f2 100644
--- a/src/crepe/api/ParticleEmitter.cpp
+++ b/src/crepe/api/ParticleEmitter.cpp
@@ -6,15 +6,14 @@
using namespace crepe;
-ParticleEmitter::ParticleEmitter(uint32_t game_object_id,
- uint32_t max_particles, uint32_t emission_rate,
- uint32_t speed, uint32_t speed_offset,
- uint32_t angle, uint32_t angleOffset,
- float begin_lifespan, float end_lifespan)
- : Component(game_object_id), max_particles(max_particles),
- emission_rate(emission_rate), speed(speed), speed_offset(speed_offset),
- position{0, 0}, begin_lifespan(begin_lifespan),
- end_lifespan(end_lifespan) {
+ParticleEmitter::ParticleEmitter(game_object_id_t id, uint32_t max_particles,
+ uint32_t emission_rate, uint32_t speed,
+ uint32_t speed_offset, uint32_t angle,
+ uint32_t angleOffset, float begin_lifespan,
+ float end_lifespan)
+ : Component(id), max_particles(max_particles), emission_rate(emission_rate),
+ speed(speed), speed_offset(speed_offset), position{0, 0},
+ begin_lifespan(begin_lifespan), end_lifespan(end_lifespan) {
std::srand(
static_cast<uint32_t>(std::time(nullptr))); // initialize random seed
std::cout << "Create emitter" << std::endl;
diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h
index 2e2e95b..5939723 100644
--- a/src/crepe/api/ParticleEmitter.h
+++ b/src/crepe/api/ParticleEmitter.h
@@ -10,7 +10,7 @@ namespace crepe {
class ParticleEmitter : public Component {
public:
- ParticleEmitter(uint32_t game_object_id, uint32_t max_particles,
+ ParticleEmitter(game_object_id_t id, uint32_t max_particles,
uint32_t emission_rate, uint32_t speed,
uint32_t speed_offset, uint32_t angle, uint32_t angleOffset,
float begin_lifespan, float end_lifespan);
diff --git a/src/crepe/api/Point.h b/src/crepe/api/Point.h
deleted file mode 100644
index 575d624..0000000
--- a/src/crepe/api/Point.h
+++ /dev/null
@@ -1,11 +0,0 @@
-#pragma once
-
-namespace crepe {
-
-class Point {
-public:
- double x;
- double y;
-};
-
-} // namespace crepe
diff --git a/src/crepe/api/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp
index 0a6262a..cbf1325 100644
--- a/src/crepe/api/Rigidbody.cpp
+++ b/src/crepe/api/Rigidbody.cpp
@@ -2,7 +2,13 @@
using namespace crepe;
-Rigidbody::Rigidbody(uint32_t game_object_id, int mass, int gravity_scale,
- BodyType bodyType)
- : Component(game_object_id), mass(mass), gravity_scale(gravity_scale),
- body_type(bodyType) {}
+crepe::Rigidbody::Rigidbody(uint32_t game_object_id, const Data & data)
+ : Component(game_object_id), data(data) {}
+
+void crepe::Rigidbody::add_force_linear(const Vector2 & force) {
+ this->data.linear_velocity += force;
+}
+
+void crepe::Rigidbody::add_force_angular(double force) {
+ this->data.angular_velocity += force;
+}
diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h
index 518ed94..68481f4 100644
--- a/src/crepe/api/Rigidbody.h
+++ b/src/crepe/api/Rigidbody.h
@@ -4,27 +4,101 @@
#include "../Component.h"
-namespace crepe {
+#include "Vector2.h"
-// FIXME: can't this enum be defined inside the class declaration of Rigidbody?
-enum class BodyType {
- //! Does not move (e.g. walls, ground ...)
- STATIC,
- //! Moves and responds to forces (e.g. player, physics objects ...)
- DYNAMIC,
- //! Moves but does not respond to forces (e.g. moving platforms ...)
- KINEMATIC,
-};
+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.
+ */
class Rigidbody : public Component {
public:
- Rigidbody(uint32_t game_object_id, int mass, int gravity_scale,
- BodyType body_type);
- int32_t velocity_x;
- int32_t velocity_y;
- int mass;
- int gravity_scale;
- BodyType body_type;
+ /**
+ * \brief BodyType enum
+ *
+ * This enum provides three bodytypes the physics sytem and collision system use.
+ */
+ enum class BodyType {
+ //! Does not move (e.g. walls, ground ...)
+ STATIC,
+ //! Moves and responds to forces (e.g. player, physics objects ...)
+ DYNAMIC,
+ //! Moves but does not respond to forces (e.g. moving platforms ...)
+ KINEMATIC,
+ };
+ /**
+ * \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
+ bool x = false;
+ //! Y constraint
+ bool y = false;
+ //! rotation constraint
+ 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
+ BodyType body_type = BodyType::DYNAMIC;
+ //! linear velocity of object
+ Vector2 linear_velocity;
+ //! maximum linear velocity of object
+ Vector2 max_linear_velocity;
+ //! linear damping of object
+ Vector2 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
+ PhysicsConstraints constraints;
+ //! if gravity applies
+ bool use_gravity = true;
+ //! if object bounces
+ bool bounce = false;
+ };
+
+public:
+ /**
+ * \param game_object_id id of the gameobject the rigibody is added to.
+ * \param data struct to configure the rigidbody.
+ */
+ Rigidbody(uint32_t game_object_id, const Data & data);
+ //! struct to hold data of rigidbody
+ 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 Vector2 & 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);
};
} // namespace crepe
diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp
new file mode 100644
index 0000000..933edf4
--- /dev/null
+++ b/src/crepe/api/Scene.cpp
@@ -0,0 +1,5 @@
+#include "Scene.h"
+
+using namespace crepe;
+
+Scene::Scene(const std::string & name) : name(name) {}
diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h
new file mode 100644
index 0000000..f8bcc3d
--- /dev/null
+++ b/src/crepe/api/Scene.h
@@ -0,0 +1,17 @@
+#pragma once
+
+#include <string>
+
+namespace crepe {
+
+class Scene {
+public:
+ Scene(const std::string & name);
+ virtual ~Scene() = default;
+ virtual void load_scene() = 0;
+
+public:
+ std::string name;
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp
new file mode 100644
index 0000000..dfed6ee
--- /dev/null
+++ b/src/crepe/api/SceneManager.cpp
@@ -0,0 +1,38 @@
+#include <algorithm>
+#include <memory>
+
+#include "../ComponentManager.h"
+
+#include "SceneManager.h"
+
+using namespace crepe;
+using namespace std;
+
+SceneManager & SceneManager::get_instance() {
+ static SceneManager instance;
+ return instance;
+}
+
+void SceneManager::set_next_scene(const string & name) { next_scene = name; }
+
+void SceneManager::load_next_scene() {
+ // next scene not set
+ if (this->next_scene.empty()) return;
+
+ auto it
+ = find_if(this->scenes.begin(), this->scenes.end(),
+ [&next_scene = this->next_scene](unique_ptr<Scene> & scene) {
+ return scene->name == next_scene;
+ });
+
+ // next scene not found
+ if (it == this->scenes.end()) return;
+ unique_ptr<Scene> & scene = *it;
+
+ // Delete all components of the current scene
+ ComponentManager & mgr = ComponentManager::get_instance();
+ mgr.delete_all_components();
+
+ // Load the new scene
+ scene->load_scene();
+}
diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h
new file mode 100644
index 0000000..1e0e670
--- /dev/null
+++ b/src/crepe/api/SceneManager.h
@@ -0,0 +1,50 @@
+#pragma once
+
+#include <memory>
+#include <queue>
+#include <vector>
+
+#include "Scene.h"
+
+namespace crepe {
+
+class SceneManager {
+public:
+ // Singleton
+ static SceneManager & get_instance();
+ SceneManager(const SceneManager &) = delete;
+ SceneManager(SceneManager &&) = delete;
+ SceneManager & operator=(const SceneManager &) = delete;
+ SceneManager & operator=(SceneManager &&) = delete;
+
+public:
+ /**
+ * \brief Add a new concrete scene to the scene manager
+ *
+ * \tparam T Type of concrete scene
+ * \param name Name of new scene
+ */
+ template <typename T>
+ void add_scene(const std::string & name);
+ /**
+ * \brief Set the next scene
+ *
+ * This scene will be loaded at the end of the frame
+ *
+ * \param name Name of the next scene
+ */
+ void set_next_scene(const std::string & name);
+ //! Load a new scene (if there is one)
+ void load_next_scene();
+
+private:
+ SceneManager() = default;
+
+private:
+ std::vector<std::unique_ptr<Scene>> scenes;
+ std::string next_scene;
+};
+
+} // namespace crepe
+
+#include "SceneManager.hpp"
diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp
new file mode 100644
index 0000000..8bad7b2
--- /dev/null
+++ b/src/crepe/api/SceneManager.hpp
@@ -0,0 +1,18 @@
+#include "SceneManager.h"
+
+namespace crepe {
+
+template <typename T>
+void SceneManager::add_scene(const std::string & name) {
+ static_assert(std::is_base_of<Scene, T>::value,
+ "T must be derived from Scene");
+
+ scenes.emplace_back(make_unique<T>(name));
+
+ // The first scene added, is the one that will be loaded at the beginning
+ if (next_scene.empty()) {
+ next_scene = name;
+ }
+}
+
+} // namespace crepe
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
index 3dd44f2..d3465c7 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -10,8 +10,8 @@
using namespace std;
using namespace crepe;
-Sprite::Sprite(uint32_t id, shared_ptr<Texture> image, const Color & color,
- const FlipSettings & flip)
+Sprite::Sprite(game_object_id_t id, shared_ptr<Texture> image,
+ const Color & color, const FlipSettings & flip)
: Component(id), color(color), flip(flip), sprite_image(image) {
dbg_trace();
}
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
index bdb4da9..00dcb27 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -12,14 +12,14 @@
namespace crepe {
struct FlipSettings {
- bool flip_x = 1;
- bool flip_y = 1;
+ bool flip_x = true;
+ bool flip_y = true;
};
class Sprite : public Component {
public:
- Sprite(uint32_t game_id, std::shared_ptr<Texture> image,
+ Sprite(game_object_id_t id, std::shared_ptr<Texture> image,
const Color & color, const FlipSettings & flip);
~Sprite();
std::shared_ptr<Texture> sprite_image;
diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp
index 70b16bd..a244bc5 100644
--- a/src/crepe/api/Transform.cpp
+++ b/src/crepe/api/Transform.cpp
@@ -1,16 +1,11 @@
-#include <cstdint>
-
-#include "api/Point.h"
#include "util/log.h"
-#include "Component.h"
#include "Transform.h"
using namespace crepe;
-Transform::Transform(uint32_t game_id, const Point & point, double rot,
- double scale)
- : Component(game_id), position(point), rotation(rot), scale(scale) {
+Transform::Transform(game_object_id_t id, const Vector2 & point,
+ double rotation, double scale)
+ : Component(id), position(point), rotation(rotation), scale(scale) {
dbg_trace();
}
-Transform::~Transform() { dbg_trace(); }
diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h
index d416088..d7a5b8a 100644
--- a/src/crepe/api/Transform.h
+++ b/src/crepe/api/Transform.h
@@ -1,23 +1,37 @@
#pragma once
-#include <cstdint>
-
-#include "api/Point.h"
+#include "api/Vector2.h"
#include "Component.h"
namespace crepe {
+/**
+ * \brief Transform component
+ *
+ * This class represents the Transform component. It stores the position,
+ * rotation and scale of a GameObject.
+ */
class Transform : public Component {
- // FIXME: What's the difference between the `Point` and `Position`
- // classes/structs? How about we replace both with a universal `Vec2` that
- // works similar (or the same) as those found in GLSL?
+public:
+ /**
+ * \param id The id of the GameObject this component belongs to
+ * \param point The position of the GameObject
+ * \param rotation The rotation of the GameObject
+ * \param scale The scale of the GameObject
+ */
+ Transform(game_object_id_t id, const Vector2 & point, double rotation,
+ double scale);
+ /**
+ * \brief Get the maximum number of instances for this component
+ *
+ * \return The maximum number of instances for this component
+ */
+ virtual int get_instances_max() const { return 1; }
public:
- Transform(uint32_t id, const Point &, double, double);
- ~Transform();
//! Translation (shift)
- Point position;
+ Vector2 position;
//! Rotation, in radians
double rotation;
//! Multiplication factor
diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp
new file mode 100644
index 0000000..09bb59b
--- /dev/null
+++ b/src/crepe/api/Vector2.cpp
@@ -0,0 +1,57 @@
+#include "Vector2.h"
+
+namespace crepe {
+
+// Constructor with initial values
+Vector2::Vector2(float x, float y) : x(x), y(y) {}
+
+// Subtracts another vector from this vector and returns the result.
+Vector2 Vector2::operator-(const Vector2 & other) const {
+ return {x - other.x, y - other.y};
+}
+
+// Adds another vector to this vector and returns the result.
+Vector2 Vector2::operator+(const Vector2 & other) const {
+ return {x + other.x, y + other.y};
+}
+
+// Multiplies this vector by a scalar and returns the result.
+Vector2 Vector2::operator*(float scalar) const {
+ return {x * scalar, y * scalar};
+}
+
+// Multiplies this vector by another vector element-wise and updates this vector.
+Vector2 & Vector2::operator*=(const Vector2 & other) {
+ x *= other.x;
+ y *= other.y;
+ return *this;
+}
+
+// Adds another vector to this vector and updates this vector.
+Vector2 & Vector2::operator+=(const Vector2 & other) {
+ x += other.x;
+ y += other.y;
+ return *this;
+}
+
+// Adds a scalar value to both components of this vector and updates this vector.
+Vector2 & Vector2::operator+=(float other) {
+ x += other;
+ y += other;
+ return *this;
+}
+
+// Returns the negation of this vector.
+Vector2 Vector2::operator-() const { return {-x, -y}; }
+
+// Checks if this vector is equal to another vector.
+bool Vector2::operator==(const Vector2 & other) const {
+ return x == other.x && y == other.y;
+}
+
+// Checks if this vector is not equal to another vector.
+bool Vector2::operator!=(const Vector2 & other) const {
+ return !(*this == other);
+}
+
+} // namespace crepe
diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h
new file mode 100644
index 0000000..741951b
--- /dev/null
+++ b/src/crepe/api/Vector2.h
@@ -0,0 +1,47 @@
+#pragma once
+
+namespace crepe {
+
+//! Vector2 class
+class Vector2 {
+public:
+ //! X component of the vector
+ float x;
+ //! Y component of the vector
+ float y;
+
+ //! Default constructor
+ Vector2() = default;
+
+ //! Constructor with initial values
+ Vector2(float x, float y);
+
+ //! Subtracts another vector from this vector and returns the result.
+ Vector2 operator-(const Vector2 & other) const;
+
+ //! Adds another vector to this vector and returns the result.
+ Vector2 operator+(const Vector2 & other) const;
+
+ //! Multiplies this vector by a scalar and returns the result.
+ Vector2 operator*(float scalar) const;
+
+ //! Multiplies this vector by another vector element-wise and updates this vector.
+ Vector2 & operator*=(const Vector2 & other);
+
+ //! Adds another vector to this vector and updates this vector.
+ Vector2 & operator+=(const Vector2 & other);
+
+ //! Adds a scalar value to both components of this vector and updates this vector.
+ Vector2 & operator+=(float other);
+
+ //! Returns the negation of this vector.
+ Vector2 operator-() const;
+
+ //! Checks if this vector is equal to another vector.
+ bool operator==(const Vector2 & other) const;
+
+ //! Checks if this vector is not equal to another vector.
+ bool operator!=(const Vector2 & other) const;
+};
+
+} // namespace crepe
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt
index bb52e7a..4cc53bc 100644
--- a/src/crepe/facade/CMakeLists.txt
+++ b/src/crepe/facade/CMakeLists.txt
@@ -1,7 +1,6 @@
target_sources(crepe PUBLIC
Sound.cpp
SoundContext.cpp
- SDLApp.cpp
SDLContext.cpp
DB.cpp
)
@@ -10,7 +9,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Sound.h
SoundContext.h
SDLContext.h
- SDLContext.h
DB.h
)
diff --git a/src/crepe/facade/SDLApp.cpp b/src/crepe/facade/SDLApp.cpp
deleted file mode 100644
index c6ddeaa..0000000
--- a/src/crepe/facade/SDLApp.cpp
+++ /dev/null
@@ -1,71 +0,0 @@
-#include <iostream>
-
-#include "SDLApp.h"
-
-SDLApp::SDLApp(int window_width, int window_height)
- : window_width(window_width), window_height(window_height), window(nullptr),
- renderer(nullptr) {}
-
-// FIXME: why is there clean_up and ~SDLApp?
-SDLApp::~SDLApp() { clean_up(); }
-
-bool SDLApp::initialize() {
- if (SDL_Init(SDL_INIT_VIDEO) != 0) {
- // FIXME: throw exception
- std::cerr << "SDL Initialization Error: " << SDL_GetError()
- << std::endl;
- return false;
- }
-
- window = SDL_CreateWindow("Particle System", SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED, window_width,
- window_height, SDL_WINDOW_SHOWN);
- if (!window) {
- // FIXME: throw exception
- std::cerr << "Window Creation Error: " << SDL_GetError() << std::endl;
- return false;
- }
-
- renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
- if (!renderer) {
- // FIXME: throw exception
- std::cerr << "Renderer Creation Error: " << SDL_GetError() << std::endl;
- return false;
- }
-
- return true;
-}
-
-void SDLApp::handle_events(bool & running) {
- SDL_Event event;
- while (SDL_PollEvent(&event)) {
- if (event.type == SDL_QUIT) {
- running = false;
- }
- }
-}
-
-void SDLApp::clear_screen() {
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
- SDL_RenderClear(renderer);
-}
-
-void SDLApp::present_screen() { SDL_RenderPresent(renderer); }
-
-void SDLApp::draw_square(int x, int y, int size) {
- SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
- SDL_Rect rect = {x, y, size, size};
- SDL_RenderFillRect(renderer, &rect);
-}
-
-SDL_Texture * square_texture = nullptr; // Load this with an image or create it
-
-void SDLApp::clean_up() {
- if (renderer) {
- SDL_DestroyRenderer(renderer);
- }
- if (window) {
- SDL_DestroyWindow(window);
- }
- SDL_Quit();
-}
diff --git a/src/crepe/facade/SDLApp.h b/src/crepe/facade/SDLApp.h
deleted file mode 100644
index 6d8f3f4..0000000
--- a/src/crepe/facade/SDLApp.h
+++ /dev/null
@@ -1,26 +0,0 @@
-#pragma once
-
-#include <SDL2/SDL.h>
-
-#include "../api/ParticleEmitter.h"
-
-class SDLApp {
-public:
- SDLApp(int window_width, int window_height);
- ~SDLApp();
-
- bool initialize();
- void handle_events(bool & running);
- void clear_screen();
- void present_screen();
- void draw_square(int x, int y, int size);
- void clean_up();
- void draw_particles(const std::vector<crepe::ParticleEmitter> & emitters);
- void draw_multiple_squares(const std::vector<SDL_Rect> & squares);
-
-private:
- int window_width;
- int window_height;
- SDL_Window * window;
- SDL_Renderer * renderer;
-};
diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp
index cea8062..eb54ad3 100644
--- a/src/crepe/system/PhysicsSystem.cpp
+++ b/src/crepe/system/PhysicsSystem.cpp
@@ -1,16 +1,15 @@
-#include <iostream>
+#include <cmath>
#include "../ComponentManager.h"
-#include "../api/Force.h"
+#include "../api/Config.h"
#include "../api/Rigidbody.h"
#include "../api/Transform.h"
+#include "../api/Vector2.h"
#include "PhysicsSystem.h"
using namespace crepe;
-PhysicsSystem::PhysicsSystem() {}
-
void PhysicsSystem::update() {
ComponentManager & mgr = ComponentManager::get_instance();
std::vector<std::reference_wrapper<Rigidbody>> rigidbodies
@@ -18,41 +17,86 @@ void PhysicsSystem::update() {
std::vector<std::reference_wrapper<Transform>> transforms
= mgr.get_components_by_type<Transform>();
+ double gravity = Config::get_instance().physics.gravity;
for (Rigidbody & rigidbody : rigidbodies) {
+ if (!rigidbody.active) continue;
- switch (rigidbody.body_type) {
- case BodyType::DYNAMIC:
+ switch (rigidbody.data.body_type) {
+ case Rigidbody::BodyType::DYNAMIC:
for (Transform & transform : transforms) {
if (transform.game_object_id == rigidbody.game_object_id) {
- rigidbody.velocity_x = 0;
- rigidbody.velocity_y = 0;
- std::vector<std::reference_wrapper<Force>> forces
- = mgr.get_components_by_id<Force>(
- rigidbody.game_object_id);
- rigidbody.velocity_y
- += rigidbody.gravity_scale * 1 * rigidbody.mass;
-
- for (Force & force : forces) {
- rigidbody.velocity_x += force.force_x;
- rigidbody.velocity_y += force.force_y;
- }
-
- std::cout << "before transform.postion.x "
- << transform.position.x << std::endl;
- std::cout << "before transform.postion.y "
- << transform.position.y << std::endl;
- transform.position.x += rigidbody.velocity_x;
- transform.position.y += rigidbody.velocity_y;
- std::cout << "after transform.postion.x "
- << transform.position.x << std::endl;
- std::cout << "after transform.postion.y "
- << transform.position.y << std::endl;
+
+ // Add gravity
+ if (rigidbody.data.use_gravity) {
+ 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.linear_damping != Vector2{0, 0}) {
+ rigidbody.data.linear_velocity
+ *= rigidbody.data.linear_damping;
+ }
+
+ // Max velocity check
+ if (rigidbody.data.angular_velocity
+ > rigidbody.data.max_angular_velocity) {
+ rigidbody.data.angular_velocity
+ = rigidbody.data.max_angular_velocity;
+ } else if (rigidbody.data.angular_velocity
+ < -rigidbody.data.max_angular_velocity) {
+ rigidbody.data.angular_velocity
+ = -rigidbody.data.max_angular_velocity;
+ }
+
+ if (rigidbody.data.linear_velocity.x
+ > rigidbody.data.max_linear_velocity.x) {
+ rigidbody.data.linear_velocity.x
+ = rigidbody.data.max_linear_velocity.x;
+ } else if (rigidbody.data.linear_velocity.x
+ < -rigidbody.data.max_linear_velocity.x) {
+ rigidbody.data.linear_velocity.x
+ = -rigidbody.data.max_linear_velocity.x;
+ }
+
+ if (rigidbody.data.linear_velocity.y
+ > rigidbody.data.max_linear_velocity.y) {
+ rigidbody.data.linear_velocity.y
+ = rigidbody.data.max_linear_velocity.y;
+ } else if (rigidbody.data.linear_velocity.y
+ < -rigidbody.data.max_linear_velocity.y) {
+ rigidbody.data.linear_velocity.y
+ = -rigidbody.data.max_linear_velocity.y;
+ }
+
+ // Move object
+ if (!rigidbody.data.constraints.rotation) {
+ transform.rotation
+ += rigidbody.data.angular_velocity;
+ transform.rotation
+ = std::fmod(transform.rotation, 360.0);
+ if (transform.rotation < 0) {
+ transform.rotation += 360.0;
+ }
+ }
+ if (!rigidbody.data.constraints.x) {
+ transform.position.x
+ += rigidbody.data.linear_velocity.x;
+ }
+ if (!rigidbody.data.constraints.y) {
+ transform.position.y
+ += rigidbody.data.linear_velocity.y;
+ }
}
}
break;
- case BodyType::KINEMATIC:
+ case Rigidbody::BodyType::KINEMATIC:
break; //(scripts)
- case BodyType::STATIC:
+ case Rigidbody::BodyType::STATIC:
break; //(unmoveable objects)
default:
break;
diff --git a/src/crepe/system/PhysicsSystem.h b/src/crepe/system/PhysicsSystem.h
index 33b4072..cc13b70 100644
--- a/src/crepe/system/PhysicsSystem.h
+++ b/src/crepe/system/PhysicsSystem.h
@@ -1,10 +1,23 @@
#pragma once
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.
+ */
class PhysicsSystem {
public:
- PhysicsSystem();
+ /**
+ * Constructor is default
+ */
+ PhysicsSystem() = default;
+ /**
+ * \brief updates the physics system.
+ *
+ * It calculates new velocties and changes the postion in the transform.
+ */
void update();
};
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index 96c94e9..5a07cc2 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -2,9 +2,9 @@
#include <vector>
#include "../ComponentManager.h"
-#include "../facade/SDLContext.h"
#include "../api/Sprite.h"
#include "../api/Transform.h"
+#include "../facade/SDLContext.h"
#include "../util/log.h"
#include "RenderSystem.h"
diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp
index f1fae4d..f2673e7 100644
--- a/src/crepe/system/ScriptSystem.cpp
+++ b/src/crepe/system/ScriptSystem.cpp
@@ -12,14 +12,6 @@
using namespace std;
using namespace crepe;
-ScriptSystem::ScriptSystem() { dbg_trace(); }
-ScriptSystem::~ScriptSystem() { dbg_trace(); }
-
-ScriptSystem & ScriptSystem::get_instance() {
- static ScriptSystem instance;
- return instance;
-}
-
void ScriptSystem::update() {
using namespace std;
dbg_trace();
diff --git a/src/crepe/system/ScriptSystem.h b/src/crepe/system/ScriptSystem.h
index 32e793c..4fa6141 100644
--- a/src/crepe/system/ScriptSystem.h
+++ b/src/crepe/system/ScriptSystem.h
@@ -10,14 +10,9 @@ class Script;
class ScriptSystem : public System {
public:
- static ScriptSystem & get_instance();
void update();
private:
- ScriptSystem();
- ~ScriptSystem();
-
-private:
// TODO: to forward_list<reference_wrapper>
std::forward_list<Script *> get_scripts();
};
diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h
index ecbb7f5..3b81bef 100644
--- a/src/crepe/system/System.h
+++ b/src/crepe/system/System.h
@@ -4,19 +4,11 @@ namespace crepe {
class System {
public:
- static System & get_instance();
virtual void update() = 0;
-protected:
- System() {};
- virtual ~System() {};
-
-private:
- // singleton
- System(const System &) = delete;
- System(System &&) = delete;
- System & operator=(const System &) = delete;
- System & operator=(System &&) = delete;
+public:
+ System() = default;
+ virtual ~System() = default;
};
} // namespace crepe
diff --git a/src/crepe/types.h b/src/crepe/types.h
new file mode 100644
index 0000000..0d459e8
--- /dev/null
+++ b/src/crepe/types.h
@@ -0,0 +1,9 @@
+#pragma once
+
+#include <cstdint>
+
+namespace crepe {
+
+typedef uint32_t game_object_id_t;
+
+}
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index 9698c41..36f9d4d 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -22,9 +22,10 @@ add_example(script)
add_example(log)
add_example(rendering)
add_example(asset_manager)
-add_example(particle)
add_example(physics)
add_example(savemgr)
add_example(proxy)
add_example(db)
+add_example(ecs)
+add_example(scene_manager)
diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp
index ba18b62..cf64f89 100644
--- a/src/example/asset_manager.cpp
+++ b/src/example/asset_manager.cpp
@@ -1,6 +1,6 @@
-#include <crepe/facade/Sound.h>
#include <crepe/api/AssetManager.h>
#include <crepe/api/Texture.h>
+#include <crepe/facade/Sound.h>
using namespace crepe;
diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp
index 0b36daa..1ea839d 100644
--- a/src/example/audio_internal.cpp
+++ b/src/example/audio_internal.cpp
@@ -3,8 +3,8 @@
* Standalone example for usage of the internal \c Sound class.
*/
-#include <crepe/facade/Sound.h>
#include <crepe/api/Config.h>
+#include <crepe/facade/Sound.h>
#include <crepe/util/log.h>
#include <thread>
diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp
new file mode 100644
index 0000000..e61c398
--- /dev/null
+++ b/src/example/ecs.cpp
@@ -0,0 +1,55 @@
+#include <iostream>
+
+#include <crepe/ComponentManager.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Metadata.h>
+#include <crepe/api/Transform.h>
+
+using namespace crepe;
+using namespace std;
+
+int main() {
+ // Create a few GameObjects
+ try {
+ GameObject body(0, "body", "person", Vector2{0, 0}, 0, 1);
+ GameObject right_leg(1, "rightLeg", "person", Vector2{1, 1}, 0, 1);
+ GameObject left_leg(2, "leftLeg", "person", Vector2{1, 1}, 0, 1);
+ GameObject right_foot(3, "rightFoot", "person", Vector2{2, 2}, 0, 1);
+ GameObject left_foot(4, "leftFoot", "person", Vector2{2, 2}, 0, 1);
+
+ // Set the parent of each GameObject
+ right_foot.set_parent(right_leg);
+ left_foot.set_parent(left_leg);
+ right_leg.set_parent(body);
+ left_leg.set_parent(body);
+
+ // Adding a second Transform component is not allowed and will invoke an exception
+ body.add_component<Transform>(Vector2{10, 10}, 0, 1);
+ } catch (const exception & e) {
+ cerr << e.what() << endl;
+ }
+
+ // Get the Metadata and Transform components of each GameObject
+ ComponentManager & mgr = ComponentManager::get_instance();
+ vector<reference_wrapper<Metadata>> metadata
+ = mgr.get_components_by_type<Metadata>();
+ vector<reference_wrapper<Transform>> transform
+ = mgr.get_components_by_type<Transform>();
+
+ // Print the Metadata and Transform components
+ for (auto & m : metadata) {
+ cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name
+ << " Tag: " << m.get().tag << " Parent: " << m.get().parent
+ << " Children: ";
+ for (auto & c : m.get().children) {
+ cout << c << " ";
+ }
+ cout << endl;
+ }
+ for (auto & t : transform) {
+ cout << "Id: " << t.get().game_object_id << " Position: ["
+ << t.get().position.x << ", " << t.get().position.y << "]" << endl;
+ }
+
+ return 0;
+}
diff --git a/src/example/particle.cpp b/src/example/particle.cpp
deleted file mode 100644
index 83b1e8a..0000000
--- a/src/example/particle.cpp
+++ /dev/null
@@ -1,102 +0,0 @@
-#include <chrono>
-#include <iostream>
-#include <thread>
-
-#include <crepe/Component.h>
-#include <crepe/ComponentManager.h>
-#include <crepe/Particle.h>
-#include <crepe/facade/SDLApp.h>
-#include <crepe/api/GameObject.h>
-#include <crepe/api/ParticleEmitter.h>
-#include <crepe/system/ParticleSystem.h>
-
-using namespace crepe;
-using namespace std;
-
-const int WINDOW_WIDTH = 800;
-const int WINDOW_HEIGHT = 600;
-
-int main(int argc, char * argv[]) {
- SDLApp app(WINDOW_WIDTH, WINDOW_HEIGHT);
-
- if (!app.initialize()) {
- cerr << "Failed to initialize SDLApp." << endl;
- return 1;
- }
-
- GameObject * game_object[1];
- game_object[0] = new GameObject(0, "Name", "Tag", 0);
-
- // FIXME: all systems are singletons, so this shouldn't even compile.
- ParticleSystem particle_system;
-
- unsigned int max_particles = 100; // maximum number of particles
- unsigned int emission_rate = 10; // particles created per second
- unsigned int speed = 50; // base speed of particles
- unsigned int speed_offset = 10; // random offset for particle speed
- unsigned int angle = 270; // base angle of particle emission
- unsigned int angle_offset = 30; // random offset for particle angle
- float begin_lifespan = 0.0f; // beginning lifespan of particles
- float end_lifespan = 6.0f; // ending lifespan of particles
-
- // Vector to hold all the emitters
- // vector<ParticleEmitter> emitters;
- game_object[0]->add_component<ParticleEmitter>(
- max_particles, emission_rate, speed, speed_offset, angle, angle_offset,
- begin_lifespan, end_lifespan);
-
- // Loop to create 1000 emitters
- // for (unsigned int i = 0; i < 1000; ++i) {
- // ParticleEmitter emitter(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan);
-
- // // Set a position for each emitter, modifying the position for demonstration
- // emitter.m_position = {static_cast<float>(200 + (i % 100)), static_cast<float>(200 + (i / 100) * 10)}; // Adjust position for each emitter
-
- // emitters.push_back(emitter); // Add the emitter to the vector
- // }
- float delta_time = 0.1f;
- bool running = true;
- cout << "start loop " << endl;
- while (running) {
- app.handle_events(running);
-
- // Start timing
- auto start = chrono::high_resolution_clock::now();
-
- // POC CODE
- particle_system.update();
- // POC CODE
-
- // End timing
- auto end = chrono::high_resolution_clock::now();
- chrono::duration<float, milli> duration
- = end - start; // get duration in milliseconds
-
- cout << "Update took " << duration.count() << " ms" << endl;
- app.clear_screen();
-
- start = chrono::high_resolution_clock::now();
- // render particles using the draw_square method from SDLApp
- ComponentManager & mgr = ComponentManager::get_instance();
- std::vector<std::reference_wrapper<ParticleEmitter>> emitters
- = mgr.get_components_by_type<ParticleEmitter>();
- for (const ParticleEmitter & emitter : emitters) {
- for (const Particle & particle : emitter.particles) {
- if (particle.active)
- app.draw_square(particle.position.x, particle.position.y,
- 5); // draw each particle
- }
- }
-
- app.present_screen();
- end = chrono::high_resolution_clock::now();
- duration = end - start; // get duration in milliseconds
-
- cout << "screen took " << duration.count() << " ms" << endl;
-
- this_thread::sleep_for(chrono::milliseconds(20)); // simulate ~50 FPS
- }
-
- app.clean_up();
- return 0;
-}
diff --git a/src/example/physics.cpp b/src/example/physics.cpp
index 2dbddc2..848f857 100644
--- a/src/example/physics.cpp
+++ b/src/example/physics.cpp
@@ -1,10 +1,5 @@
-#include <chrono>
-#include <iostream>
-#include <thread>
-
#include <crepe/Component.h>
#include <crepe/ComponentManager.h>
-#include <crepe/api/Force.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Transform.h>
@@ -14,17 +9,16 @@ using namespace crepe;
using namespace std;
int main(int argc, char * argv[]) {
- PhysicsSystem physics_system;
- GameObject * game_object[2];
- game_object[1] = new GameObject(2, "Name", "Tag", 0); // not found not used
- game_object[0] = new GameObject(5, "Name", "Tag", 0);
- Point point = {
- .x = 0,
- .y = 0,
- };
- game_object[0]->add_component<Transform>(point, 0, 0);
- game_object[0]->add_component<Rigidbody>(1, 1, BodyType::DYNAMIC);
- game_object[0]->add_component<Force>(1, 0);
- physics_system.update();
+ GameObject * game_object;
+ game_object = new GameObject(0, "Name", "Tag", Vector2{0, 0}, 0, 0);
+ game_object->add_component<Rigidbody>(Rigidbody::Data{
+ .mass = 1,
+ .gravity_scale = 1,
+ .body_type = Rigidbody::BodyType::DYNAMIC,
+ .constraints = {0, 0, 0},
+ .use_gravity = true,
+ .bounce = false,
+ });
+ delete game_object;
return 0;
}
diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp
index b0ab60a..d554a8a 100644
--- a/src/example/rendering.cpp
+++ b/src/example/rendering.cpp
@@ -5,10 +5,10 @@
#include <crepe/api/AssetManager.h>
#include <crepe/api/Color.h>
-#include <crepe/api/Point.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Texture.h>
#include <crepe/api/Transform.h>
+#include <crepe/api/Vector2.h>
#include <chrono>
#include <memory>
@@ -17,22 +17,16 @@ using namespace std;
using namespace crepe;
int main() {
-
dbg_trace();
- auto obj = GameObject(0, "name", "tag", 0);
- auto obj1 = GameObject(1, "name", "tag", 0);
- auto obj2 = GameObject(2, "name", "tag", 0);
+ auto obj = GameObject(0, "name", "tag", Vector2{0, 0}, 1, 1);
+ auto obj1 = GameObject(1, "name", "tag", Vector2{500, 0}, 1, 0.1);
+ auto obj2 = GameObject(2, "name", "tag", Vector2{800, 0}, 1, 0.1);
auto & mgr = AssetManager::get_instance();
// Normal adding components
{
Color color(0, 0, 0, 0);
- Point point = {
- .x = 0,
- .y = 0,
- };
- obj.add_component<Transform>(point, 1, 1);
obj.add_component<Sprite>(
make_shared<Texture>("../asset/texture/img.png"), color,
FlipSettings{true, true});
@@ -40,22 +34,12 @@ int main() {
{
Color color(0, 0, 0, 0);
- Point point = {
- .x = 500,
- .y = 0,
- };
- obj1.add_component<Transform>(point, 0, 0.1);
auto img = mgr.cache<Texture>("../asset/texture/second.png");
obj1.add_component<Sprite>(img, color, FlipSettings{true, true});
}
{
Color color(0, 0, 0, 0);
- Point point = {
- .x = 800,
- .y = 0,
- };
- //obj.add_component<Transform>(point, 0, 0.1);
auto img = mgr.cache<Texture>("../asset/texture/second.png");
obj2.add_component<Sprite>(img, color, FlipSettings{true, true});
}
diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp
new file mode 100644
index 0000000..f46dc36
--- /dev/null
+++ b/src/example/scene_manager.cpp
@@ -0,0 +1,75 @@
+#include <iostream>
+
+#include <crepe/ComponentManager.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Metadata.h>
+#include <crepe/api/Scene.h>
+#include <crepe/api/SceneManager.h>
+#include <crepe/api/Vector2.h>
+
+using namespace crepe;
+using namespace std;
+
+class ConcreteScene1 : public Scene {
+public:
+ ConcreteScene1(string name) : Scene(name) {}
+
+ void load_scene() {
+ GameObject object1(0, "scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1);
+ GameObject object2(1, "scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1);
+ GameObject object3(2, "scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1);
+ }
+};
+
+class ConcreteScene2 : public Scene {
+public:
+ ConcreteScene2(string name) : Scene(name) {}
+
+ void load_scene() {
+ GameObject object1(0, "scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1);
+ GameObject object2(1, "scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1);
+ GameObject object3(2, "scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1);
+ GameObject object4(3, "scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1);
+ }
+};
+
+int main() {
+ SceneManager & scene_mgr = SceneManager::get_instance();
+
+ // Add the scenes to the scene manager
+ scene_mgr.add_scene<ConcreteScene1>("scene1");
+ scene_mgr.add_scene<ConcreteScene2>("scene2");
+
+ // There is no need to call set_next_scene() at the beginnen, because the first scene will be automatically set as the next scene
+ // Load scene1 (the first scene added)
+ scene_mgr.load_next_scene();
+
+ // Get the Metadata components of each GameObject of Scene1
+ ComponentManager & component_mgr = ComponentManager::get_instance();
+ vector<reference_wrapper<Metadata>> metadata
+ = component_mgr.get_components_by_type<Metadata>();
+
+ cout << "Metadata components of Scene1:" << endl;
+ // Print the Metadata
+ for (auto & m : metadata) {
+ cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name
+ << " Tag: " << m.get().tag << endl;
+ }
+
+ // Set scene2 as the next scene
+ scene_mgr.set_next_scene("scene2");
+ // Load scene2
+ scene_mgr.load_next_scene();
+
+ // Get the Metadata components of each GameObject of Scene2
+ metadata = component_mgr.get_components_by_type<Metadata>();
+
+ cout << "Metadata components of Scene2:" << endl;
+ // Print the Metadata
+ for (auto & m : metadata) {
+ cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name
+ << " Tag: " << m.get().tag << endl;
+ }
+
+ return 0;
+}
diff --git a/src/example/script.cpp b/src/example/script.cpp
index dac7af3..9e8b147 100644
--- a/src/example/script.cpp
+++ b/src/example/script.cpp
@@ -36,18 +36,12 @@ class MyScript : public Script {
int main() {
// Create game object with Transform and BehaviorScript components
- auto obj = GameObject(0, "name", "tag", 0);
- obj.add_component<Transform>(
- Point{
- .x = 1.2,
- .y = 3.4,
- },
- 0, 0);
+ auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1);
obj.add_component<BehaviorScript>().set_script<MyScript>();
// Get ScriptSystem singleton instance (this would normally be done from the
// game loop)
- auto & sys = ScriptSystem::get_instance();
+ ScriptSystem sys;
// Update all scripts. This should result in MyScript::update being called
sys.update();
diff --git a/src/makefile b/src/makefile
index 3f74a2a..b9c44c8 100644
--- a/src/makefile
+++ b/src/makefile
@@ -1,8 +1,104 @@
.PHONY: FORCE
-FMT += $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp')
+# STEPS FOR BIG CLEANUP
+#
+# 1. Change TODO to your name (in capitals) for each file in the list below
+# that is yours (or you are going to fix)
+# 2. Update the name between parentheses below this list (see comment) to your
+# name
+# 3. Create a git commit at this point (ensure `git status` reports "working
+# tree clean")
+# 4. Run `make format` in the REPOSITORY ROOT DIRECTORY (NOT HERE), and start
+# fixing reported errors or miscorrections manually until everything works
+# again.
+# 5. Once everything is working again, create another git commit, and create a
+# pull request. Make sure to ask someone to review the code standards for
+# each ENTIRE FILE in this pull request.
+
+LOEK += crepe/Asset.cpp
+LOEK += crepe/Asset.h
+TODO += crepe/Collider.cpp
+TODO += crepe/Collider.h
+MAX += crepe/Component.cpp
+MAX += crepe/Component.h
+MAX += crepe/ComponentManager.cpp
+MAX += crepe/ComponentManager.h
+MAX += crepe/ComponentManager.hpp
+MAX += crepe/api/Metadata.cpp
+MAX += crepe/api/Metadata.h
+TODO += crepe/Particle.cpp
+TODO += crepe/Particle.h
+TODO += crepe/Position.h
+TODO += crepe/api/AssetManager.cpp
+TODO += crepe/api/AssetManager.h
+TODO += crepe/api/AssetManager.hpp
+LOEK += crepe/api/AudioSource.cpp
+LOEK += crepe/api/AudioSource.h
+LOEK += crepe/api/BehaviorScript.cpp
+LOEK += crepe/api/BehaviorScript.h
+LOEK += crepe/api/BehaviorScript.hpp
+TODO += crepe/api/CircleCollider.h
+TODO += crepe/api/Color.cpp
+TODO += crepe/api/Color.h
+LOEK += crepe/api/Config.h
+MAX += crepe/api/GameObject.cpp
+MAX += crepe/api/GameObject.h
+MAX += crepe/api/GameObject.hpp
+TODO += crepe/api/ParticleEmitter.cpp
+TODO += crepe/api/ParticleEmitter.h
+TODO += crepe/api/Vector2.h
+TODO += crepe/api/Vector2.cpp
+JARO += crepe/api/Rigidbody.cpp
+JARO += crepe/api/Rigidbody.h
+LOEK += crepe/api/Script.cpp
+LOEK += crepe/api/Script.h
+LOEK += crepe/api/Script.hpp
+TODO += crepe/api/Sprite.cpp
+TODO += crepe/api/Sprite.h
+TODO += crepe/api/Texture.cpp
+TODO += crepe/api/Texture.h
+MAX += crepe/api/Transform.cpp
+MAX += crepe/api/Transform.h
+TODO += crepe/facade/SDLContext.cpp
+TODO += crepe/facade/SDLContext.h
+LOEK += crepe/facade/Sound.cpp
+LOEK += crepe/facade/Sound.h
+LOEK += crepe/facade/SoundContext.cpp
+LOEK += crepe/facade/SoundContext.h
+TODO += crepe/system/CollisionSystem.cpp
+TODO += crepe/system/CollisionSystem.h
+TODO += crepe/system/ParticleSystem.cpp
+TODO += crepe/system/ParticleSystem.h
+JARO += crepe/system/PhysicsSystem.cpp
+JARO += crepe/system/PhysicsSystem.h
+TODO += crepe/system/RenderSystem.cpp
+TODO += crepe/system/RenderSystem.h
+LOEK += crepe/system/ScriptSystem.cpp
+LOEK += crepe/system/ScriptSystem.h
+LOEK += crepe/system/System.h
+LOEK += crepe/util/LogColor.cpp
+LOEK += crepe/util/LogColor.h
+LOEK += crepe/util/fmt.cpp
+LOEK += crepe/util/fmt.h
+LOEK += crepe/util/log.cpp
+LOEK += crepe/util/log.h
+TODO += example/asset_manager.cpp
+LOEK += example/audio_internal.cpp
+TODO += example/components_internal.cpp
+MAX += example/ecs.cpp
+LOEK += example/log.cpp
+TODO += example/particle.cpp
+JARO += example/physics.cpp
+TODO += example/rendering.cpp
+LOEK += example/script.cpp
+LOEK += test/audio.cpp
+LOEK += test/dummy.cpp
+JARO += test/PhysicsTest.cpp
+
+FMT := $(JARO) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2
format: FORCE
- # clang-tidy -p build/compile_commands.json --fix-errors $(FMT)
+ clang-tidy -p build/compile_commands.json --fix-errors $(FMT)
-# TODO: re-enable linter after 2024-11-10
+# FMT += $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp')
+# TODO: re-enable linter after all corrections
diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt
index 0d316d6..0e4eaed 100644
--- a/src/test/CMakeLists.txt
+++ b/src/test/CMakeLists.txt
@@ -1,5 +1,6 @@
target_sources(test_main PUBLIC
dummy.cpp
# audio.cpp
+ PhysicsTest.cpp
)
diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp
new file mode 100644
index 0000000..5385962
--- /dev/null
+++ b/src/test/PhysicsTest.cpp
@@ -0,0 +1,120 @@
+#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/system/PhysicsSystem.h>
+#include <gtest/gtest.h>
+
+using namespace std;
+using namespace std::chrono_literals;
+using namespace crepe;
+
+class PhysicsTest : public ::testing::Test {
+protected:
+ GameObject * game_object;
+ PhysicsSystem physics_system;
+ void SetUp() override {
+ ComponentManager & mgr = ComponentManager::get_instance();
+ std::vector<std::reference_wrapper<Transform>> transforms
+ = mgr.get_components_by_id<Transform>(0);
+ if (transforms.empty()) {
+ game_object = new GameObject(0, "", "", Vector2{0, 0}, 0, 0);
+ game_object->add_component<Rigidbody>(Rigidbody::Data{
+ .mass = 1,
+ .gravity_scale = 1,
+ .body_type = Rigidbody::BodyType::DYNAMIC,
+ .max_linear_velocity = Vector2{10, 10},
+ .max_angular_velocity = 10,
+ .constraints = {0, 0},
+ .use_gravity = true,
+ .bounce = false,
+ });
+ }
+ transforms = mgr.get_components_by_id<Transform>(0);
+ Transform & transform = transforms.front().get();
+ transform.position.x = 0.0;
+ transform.position.y = 0.0;
+ transform.rotation = 0.0;
+ std::vector<std::reference_wrapper<Rigidbody>> rigidbodies
+ = mgr.get_components_by_id<Rigidbody>(0);
+ Rigidbody & rigidbody = rigidbodies.front().get();
+ rigidbody.data.angular_velocity = 0;
+ rigidbody.data.linear_velocity.x = 0;
+ rigidbody.data.linear_velocity.y = 0;
+ }
+};
+
+TEST_F(PhysicsTest, gravity) {
+ Config::get_instance().physics.gravity = 1;
+ ComponentManager & mgr = ComponentManager::get_instance();
+ std::vector<std::reference_wrapper<Transform>> transforms
+ = mgr.get_components_by_id<Transform>(0);
+ const Transform & transform = transforms.front().get();
+ ASSERT_FALSE(transforms.empty());
+ EXPECT_EQ(transform.position.y, 0);
+ physics_system.update();
+ EXPECT_EQ(transform.position.y, 1);
+ physics_system.update();
+ EXPECT_EQ(transform.position.y, 3);
+}
+
+TEST_F(PhysicsTest, max_velocity) {
+ ComponentManager & mgr = ComponentManager::get_instance();
+ std::vector<std::reference_wrapper<Rigidbody>> rigidbodies
+ = mgr.get_components_by_id<Rigidbody>(0);
+ Rigidbody & rigidbody = rigidbodies.front().get();
+ ASSERT_FALSE(rigidbodies.empty());
+ EXPECT_EQ(rigidbody.data.linear_velocity.y, 0);
+ rigidbody.add_force_linear({100, 100});
+ rigidbody.add_force_angular(100);
+ physics_system.update();
+ EXPECT_EQ(rigidbody.data.linear_velocity.y, 10);
+ EXPECT_EQ(rigidbody.data.linear_velocity.x, 10);
+ EXPECT_EQ(rigidbody.data.angular_velocity, 10);
+ rigidbody.add_force_linear({-100, -100});
+ rigidbody.add_force_angular(-100);
+ physics_system.update();
+ EXPECT_EQ(rigidbody.data.linear_velocity.y, -10);
+ EXPECT_EQ(rigidbody.data.linear_velocity.x, -10);
+ EXPECT_EQ(rigidbody.data.angular_velocity, -10);
+}
+
+TEST_F(PhysicsTest, movement) {
+ Config::get_instance().physics.gravity = 0;
+ ComponentManager & mgr = ComponentManager::get_instance();
+ std::vector<std::reference_wrapper<Rigidbody>> rigidbodies
+ = mgr.get_components_by_id<Rigidbody>(0);
+ Rigidbody & rigidbody = rigidbodies.front().get();
+ std::vector<std::reference_wrapper<Transform>> transforms
+ = mgr.get_components_by_id<Transform>(0);
+ const Transform & transform = transforms.front().get();
+ ASSERT_FALSE(rigidbodies.empty());
+ ASSERT_FALSE(transforms.empty());
+ rigidbody.add_force_linear({1, 1});
+ rigidbody.add_force_angular(1);
+ physics_system.update();
+ EXPECT_EQ(transform.position.x, 1);
+ EXPECT_EQ(transform.position.y, 1);
+ EXPECT_EQ(transform.rotation, 1);
+ rigidbody.data.constraints = {1, 1, 1};
+ EXPECT_EQ(transform.position.x, 1);
+ 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;
+ physics_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.max_angular_velocity = 1000;
+ rigidbody.data.angular_velocity = 360;
+ physics_system.update();
+ EXPECT_EQ(transform.rotation, 1);
+ rigidbody.data.angular_velocity = -360;
+ physics_system.update();
+ EXPECT_EQ(transform.rotation, 1);
+}