aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-13 18:46:06 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-13 18:46:06 +0100
commit0aa97807cb9b7f1d1850449dd5a5e45b961445ce (patch)
tree8f6a808669a39f5b8d91079ecb30c3d1f11e9e02
parentbc2a31190b5afe7754b04e9c61fcda30e577d7d8 (diff)
fix scene_manager example
-rw-r--r--src/crepe/ComponentManager.cpp8
-rw-r--r--src/crepe/ComponentManager.h2
-rw-r--r--src/crepe/api/Scene.cpp2
-rw-r--r--src/crepe/api/Scene.h10
-rw-r--r--src/crepe/api/SceneManager.hpp9
-rw-r--r--src/example/components_internal.cpp2
-rw-r--r--src/example/ecs.cpp15
-rw-r--r--src/example/physics.cpp3
-rw-r--r--src/example/rendering.cpp6
-rw-r--r--src/example/scene_manager.cpp25
-rw-r--r--src/example/script.cpp2
11 files changed, 45 insertions, 39 deletions
diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp
index c6b658c..1e23609 100644
--- a/src/crepe/ComponentManager.cpp
+++ b/src/crepe/ComponentManager.cpp
@@ -25,13 +25,11 @@ void ComponentManager::delete_all_components() {
ComponentManager::ComponentManager() { dbg_trace(); }
ComponentManager::~ComponentManager() { dbg_trace(); }
-GameObject & ComponentManager::new_object(const string & name,
+GameObject ComponentManager::new_object(const string & name,
const string & tag,
const Vector2 & position,
double rotation, double scale) {
- GameObject * object = new GameObject(*this, this->next_id, name, tag,
- position, rotation, scale);
- this->objects.push_front(unique_ptr<GameObject>(object));
+ GameObject object{*this, this->next_id, name, tag, position, rotation, scale};
this->next_id++;
- return *object;
+ return object;
}
diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h
index 75606e0..31a8bfa 100644
--- a/src/crepe/ComponentManager.h
+++ b/src/crepe/ComponentManager.h
@@ -101,7 +101,7 @@ public:
std::vector<std::reference_wrapper<T>> get_components_by_type() const;
// TODO: doxygen
- GameObject & new_object(const std::string & name,
+ GameObject new_object(const std::string & name,
const std::string & tag = "",
const Vector2 & position = {0, 0},
double rotation = 0, double scale = 0);
diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp
index 933edf4..008d689 100644
--- a/src/crepe/api/Scene.cpp
+++ b/src/crepe/api/Scene.cpp
@@ -2,4 +2,4 @@
using namespace crepe;
-Scene::Scene(const std::string & name) : name(name) {}
+Scene::Scene(ComponentManager & mgr, const std::string & name) : component_manager(mgr), name(name) {}
diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h
index f8bcc3d..334f306 100644
--- a/src/crepe/api/Scene.h
+++ b/src/crepe/api/Scene.h
@@ -4,14 +4,20 @@
namespace crepe {
+class ComponentManager;
+
class Scene {
public:
- Scene(const std::string & name);
+ Scene(ComponentManager & mgr, const std::string & name);
virtual ~Scene() = default;
+
virtual void load_scene() = 0;
public:
- std::string name;
+ const std::string name;
+
+protected:
+ ComponentManager & component_manager;
};
} // namespace crepe
diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp
index 8bad7b2..714f690 100644
--- a/src/crepe/api/SceneManager.hpp
+++ b/src/crepe/api/SceneManager.hpp
@@ -1,13 +1,16 @@
+#pragma once
+
#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");
+ using namespace std;
+ static_assert(is_base_of<Scene, T>::value, "T must be derived from Scene");
- scenes.emplace_back(make_unique<T>(name));
+ Scene * scene = new T(this->component_manager, name);
+ this->scenes.emplace_back(unique_ptr<Scene>(scene));
// The first scene added, is the one that will be loaded at the beginning
if (next_scene.empty()) {
diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp
index 7a8a08a..eeecdc0 100644
--- a/src/example/components_internal.cpp
+++ b/src/example/components_internal.cpp
@@ -30,7 +30,7 @@ int main() {
GameObject * game_object[OBJ_COUNT];
for (int i = 0; i < OBJ_COUNT; ++i) {
- GameObject & obj = mgr.new_object("Name", "Tag");
+ GameObject obj = mgr.new_object("Name", "Tag");
obj.add_component<Sprite>("test");
obj.add_component<Rigidbody>(0, 0, i);
}
diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp
index 9a008ea..06fc563 100644
--- a/src/example/ecs.cpp
+++ b/src/example/ecs.cpp
@@ -13,16 +13,11 @@ int main() {
// Create a few GameObjects
try {
- 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);
+ 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);
diff --git a/src/example/physics.cpp b/src/example/physics.cpp
index 2ebf779..ad663a0 100644
--- a/src/example/physics.cpp
+++ b/src/example/physics.cpp
@@ -11,8 +11,7 @@ using namespace std;
int main(int argc, char * argv[]) {
ComponentManager mgr{};
- GameObject & game_object
- = mgr.new_object("Name", "Tag", Vector2{0, 0}, 0, 0);
+ 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,
diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp
index 74c00e2..abd11b1 100644
--- a/src/example/rendering.cpp
+++ b/src/example/rendering.cpp
@@ -23,9 +23,9 @@ int main() {
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);
+ GameObject obj = mgr.new_object("name", "tag", Vector2{0, 0}, 1, 1);
+ GameObject obj1 = mgr.new_object("name", "tag", Vector2{500, 0}, 1, 0.1);
+ GameObject obj2 = mgr.new_object("name", "tag", Vector2{800, 0}, 1, 0.1);
// Normal adding components
{
diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp
index 5cd7336..1c982aa 100644
--- a/src/example/scene_manager.cpp
+++ b/src/example/scene_manager.cpp
@@ -12,24 +12,26 @@ using namespace std;
class ConcreteScene1 : public Scene {
public:
- ConcreteScene1(string name) : Scene(name) {}
+ using Scene::Scene;
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);
+ auto & mgr = this->component_manager;
+ GameObject object1 = mgr.new_object("scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1);
+ GameObject object2 = mgr.new_object("scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1);
+ GameObject object3 = mgr.new_object("scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1);
}
};
class ConcreteScene2 : public Scene {
public:
- ConcreteScene2(string name) : Scene(name) {}
+ using Scene::Scene;
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);
+ auto & mgr = this->component_manager;
+ GameObject object1 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1);
+ GameObject object2 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1);
+ GameObject object3 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1);
+ GameObject object4 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1);
}
};
@@ -41,7 +43,9 @@ int main() {
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
+ // There is no need to call set_next_scene() at the beginning because the
+ // first scene will be automatically set as the next scene
+
// Load scene1 (the first scene added)
scene_mgr.load_next_scene();
@@ -73,3 +77,4 @@ int main() {
return 0;
}
+
diff --git a/src/example/script.cpp b/src/example/script.cpp
index c82764c..a23295b 100644
--- a/src/example/script.cpp
+++ b/src/example/script.cpp
@@ -39,7 +39,7 @@ int main() {
ScriptSystem system{component_manager};
// Create game object with Transform and BehaviorScript components
- auto & obj = component_manager.new_object("name");
+ GameObject obj = component_manager.new_object("name");
obj.add_component<BehaviorScript>().set_script<MyScript>();
// Update all scripts. This should result in MyScript::update being called