diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-13 13:10:25 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-13 13:10:25 +0100 |
commit | fac06d488e635583307cd4a916a611500fb64c16 (patch) | |
tree | f62f79cc8ba540498d5dfa1deb4379469738a682 /src | |
parent | 9d58e897fb68ab8dd001a5085cde2fae4634c274 (diff) |
cleanup + fix examples
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/BehaviorScript.cpp | 7 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.h | 13 | ||||
-rw-r--r-- | src/crepe/api/GameObject.cpp | 6 | ||||
-rw-r--r-- | src/crepe/api/GameObject.h | 13 | ||||
-rw-r--r-- | src/crepe/api/Script.h | 3 | ||||
-rw-r--r-- | src/example/components_internal.cpp | 13 | ||||
-rw-r--r-- | src/example/ecs.cpp | 18 | ||||
-rw-r--r-- | src/example/physics.cpp | 9 | ||||
-rw-r--r-- | src/example/rendering.cpp | 10 | ||||
-rw-r--r-- | src/example/scene_manager.cpp | 4 |
10 files changed, 51 insertions, 45 deletions
diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp index c5fecef..7bbace0 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -1,8 +1,15 @@ #include "BehaviorScript.h" #include "Component.h" +#include "GameObject.h" using namespace crepe; BehaviorScript::BehaviorScript(game_object_id_t id, ComponentManager & mgr) : Component(id), component_manager(mgr) {} + +template <> +BehaviorScript & GameObject::add_component<BehaviorScript>() { + ComponentManager & mgr = this->component_manager; + return mgr.add_component<BehaviorScript>(this->id, mgr); +} diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index f156081..a49fc96 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -3,6 +3,7 @@ #include <memory> #include "../Component.h" +#include "GameObject.h" namespace crepe { @@ -44,6 +45,18 @@ private: friend class Script; }; +/** + * \brief Add a BehaviorScript component to this game object + * + * The \c BehaviorScript class is the only exception to the ECS harmony, and + * requires a reference to the component manager passed to its constructor in + * order to function normally. This is because the \c BehaviorScript (and \c + * Script) classes are the only component-related classes that store + * implemented member functions as data. + */ +template <> +BehaviorScript & GameObject::add_component<BehaviorScript>(); + } // namespace crepe #include "BehaviorScript.hpp" diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index e05cea1..4f3c639 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -33,9 +33,3 @@ void GameObject::set_parent(const GameObject & parent) { = mgr.get_components_by_id<Metadata>(parent.id); parent_metadata.at(0).get().children.push_back(this->id); } - -template <> -BehaviorScript & GameObject::add_component<BehaviorScript>() { - ComponentManager & mgr = this->component_manager; - return mgr.add_component<BehaviorScript>(this->id, mgr); -} diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index e4a2539..73ff0b8 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -8,7 +8,6 @@ namespace crepe { class ComponentManager; -class BehaviorScript; /** * \brief Represents a GameObject @@ -70,18 +69,6 @@ protected: ComponentManager & component_manager; }; -/** - * \brief Add a BehaviorScript component to this game object - * - * The \c BehaviorScript class is the only exception to the ECS harmony, and - * requires a reference to the component manager passed to its constructor in - * order to function normally. This is because the \c BehaviorScript (and \c - * Script) classes are the only component-related classes that store - * implemented member functions as data. - */ -template <> -BehaviorScript & GameObject::add_component<BehaviorScript>(); - } // namespace crepe #include "GameObject.hpp" diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 837420f..051ea00 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -32,7 +32,7 @@ protected: template <typename T> std::vector<std::reference_wrapper<T>> get_components(); -private: +protected: // NOTE: Script must have a constructor without arguments so the game // programmer doesn't need to manually add `using Script::Script` to their // concrete script class. @@ -40,6 +40,7 @@ private: //! Only \c BehaviorScript instantiates Script friend class BehaviorScript; +private: // These references are set by BehaviorScript immediately after calling the // constructor of Script. BehaviorScript * parent_ref = nullptr; diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp index ea1eaad..dd4c7df 100644 --- a/src/example/components_internal.cpp +++ b/src/example/components_internal.cpp @@ -23,17 +23,16 @@ using namespace std; int main() { dbg_trace(); - auto & mgr = ComponentManager::get_instance(); + ComponentManager mgr{}; auto start_adding = chrono::high_resolution_clock::now(); GameObject * game_object[OBJ_COUNT]; for (int i = 0; i < OBJ_COUNT; ++i) { - game_object[i] = new GameObject(i, "Name", "Tag", 0); - - game_object[i]->add_component<Sprite>("test"); - game_object[i]->add_component<Rigidbody>(0, 0, i); + GameObject & obj = mgr.new_object("Name", "Tag"); + obj.add_component<Sprite>("test"); + obj.add_component<Rigidbody>(0, 0, i); } auto stop_adding = chrono::high_resolution_clock::now(); @@ -45,10 +44,6 @@ int main() { auto stop_looping = chrono::high_resolution_clock::now(); - for (int i = 0; i < OBJ_COUNT; ++i) { - delete game_object[i]; - } - auto add_time = chrono::duration_cast<chrono::microseconds>(stop_adding - start_adding); auto loop_time = chrono::duration_cast<chrono::microseconds>(stop_looping diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index e61c398..9a008ea 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -9,13 +9,20 @@ using namespace crepe; using namespace std; int main() { + ComponentManager mgr{}; + // 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); + GameObject & body + = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + GameObject & right_leg + = mgr.new_object("rightLeg", "person", Vector2{1, 1}, 0, 1); + GameObject & left_leg + = mgr.new_object("leftLeg", "person", Vector2{1, 1}, 0, 1); + GameObject & right_foot + = mgr.new_object("rightFoot", "person", Vector2{2, 2}, 0, 1); + GameObject & left_foot + = mgr.new_object("leftFoot", "person", Vector2{2, 2}, 0, 1); // Set the parent of each GameObject right_foot.set_parent(right_leg); @@ -30,7 +37,6 @@ int main() { } // 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 diff --git a/src/example/physics.cpp b/src/example/physics.cpp index 848f857..2ebf779 100644 --- a/src/example/physics.cpp +++ b/src/example/physics.cpp @@ -9,9 +9,11 @@ using namespace crepe; using namespace std; int main(int argc, char * argv[]) { - GameObject * game_object; - game_object = new GameObject(0, "Name", "Tag", Vector2{0, 0}, 0, 0); - game_object->add_component<Rigidbody>(Rigidbody::Data{ + ComponentManager mgr{}; + + GameObject & game_object + = mgr.new_object("Name", "Tag", Vector2{0, 0}, 0, 0); + game_object.add_component<Rigidbody>(Rigidbody::Data{ .mass = 1, .gravity_scale = 1, .body_type = Rigidbody::BodyType::DYNAMIC, @@ -19,6 +21,5 @@ int main(int argc, char * argv[]) { .use_gravity = true, .bounce = false, }); - delete game_object; return 0; } diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 493169b..95d5dac 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -20,9 +20,12 @@ using namespace crepe; int main() { dbg_trace(); - 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); + ComponentManager mgr{}; + RenderSystem sys{mgr}; + + auto & obj = mgr.new_object("name", "tag", Vector2{0, 0}, 1, 1); + auto & obj1 = mgr.new_object("name", "tag", Vector2{500, 0}, 1, 0.1); + auto & obj2 = mgr.new_object("name", "tag", Vector2{800, 0}, 1, 0.1); // Normal adding components { @@ -47,7 +50,6 @@ int main() { } */ - auto & sys = crepe::AssetManager::get_instance(); auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { sys.update(); diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index f46dc36..5cd7336 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -34,7 +34,8 @@ public: }; int main() { - SceneManager & scene_mgr = SceneManager::get_instance(); + ComponentManager component_mgr{}; + SceneManager scene_mgr{component_mgr}; // Add the scenes to the scene manager scene_mgr.add_scene<ConcreteScene1>("scene1"); @@ -45,7 +46,6 @@ int main() { 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>(); |