aboutsummaryrefslogtreecommitdiff
path: root/mwe/ecs-memory-efficient
diff options
context:
space:
mode:
authorMax-001 <80035972+Max-001@users.noreply.github.com>2024-10-05 11:05:59 +0200
committerMax-001 <80035972+Max-001@users.noreply.github.com>2024-10-05 11:05:59 +0200
commitc39adf3040c3f2f7cfcab5ce5b7e39b815e8131d (patch)
tree3dc49555635077ef8e59270b886866f4a2dce24c /mwe/ecs-memory-efficient
parent03b25facdf35117eb9f56c7a2edbec5b34b90203 (diff)
Setup ecs-memory-efficient
Diffstat (limited to 'mwe/ecs-memory-efficient')
-rw-r--r--mwe/ecs-memory-efficient/CMakeLists.txt17
-rw-r--r--mwe/ecs-memory-efficient/inc/ComponentManager.h48
-rw-r--r--mwe/ecs-memory-efficient/inc/ComponentManager.hpp88
-rw-r--r--mwe/ecs-memory-efficient/inc/Components.h33
-rw-r--r--mwe/ecs-memory-efficient/inc/GameObjectMax.h20
-rw-r--r--mwe/ecs-memory-efficient/inc/GameObjectMax.hpp6
-rw-r--r--mwe/ecs-memory-efficient/src/ComponentManager.cpp21
-rw-r--r--mwe/ecs-memory-efficient/src/Components.cpp10
-rw-r--r--mwe/ecs-memory-efficient/src/GameObjectMax.cpp5
-rw-r--r--mwe/ecs-memory-efficient/src/main.cpp108
10 files changed, 356 insertions, 0 deletions
diff --git a/mwe/ecs-memory-efficient/CMakeLists.txt b/mwe/ecs-memory-efficient/CMakeLists.txt
new file mode 100644
index 0000000..d072907
--- /dev/null
+++ b/mwe/ecs-memory-efficient/CMakeLists.txt
@@ -0,0 +1,17 @@
+cmake_minimum_required(VERSION 3.5)
+project(ecs-memory-efficient)
+
+# Set the C++ standard (optional, but good practice)
+set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_CXX_STANDARD_REQUIRED True)
+
+# Use the debug mode (otherwise breakpoints are not compiled)
+set(CMAKE_BUILD_TYPE Debug)
+
+add_executable(ecs-memory-efficient
+ src/main.cpp
+ src/ComponentManager.cpp
+ src/Components.cpp
+ src/GameObjectMax.cpp
+)
+target_include_directories(ecs-memory-efficient PRIVATE "${CMAKE_SOURCE_DIR}/inc")
diff --git a/mwe/ecs-memory-efficient/inc/ComponentManager.h b/mwe/ecs-memory-efficient/inc/ComponentManager.h
new file mode 100644
index 0000000..893aa56
--- /dev/null
+++ b/mwe/ecs-memory-efficient/inc/ComponentManager.h
@@ -0,0 +1,48 @@
+#pragma once
+
+#include "Components.h"
+#include <cstdint>
+#include <unordered_map>
+#include <vector>
+#include <typeindex>
+#include <memory>
+#include <utility>
+
+class ComponentManager {
+public:
+ static ComponentManager& GetInstance(); //Singleton
+
+ ComponentManager(const ComponentManager&) = delete; //Singleton
+ ComponentManager(ComponentManager&&) = delete; //Singleton
+ ComponentManager& operator=(const ComponentManager&) = delete; //Singleton
+ ComponentManager& operator=(ComponentManager&&) = delete; //Singleton
+
+ template <typename T, typename... Args>
+ void AddComponent(std::uint32_t id, Args&&... args); //Add a component of a specific type
+ template <typename T>
+ void DeleteComponentsById(std::uint32_t id); //Deletes all components of a specific type and id
+ template <typename T>
+ void DeleteComponents(); //Deletes all components of a specific type
+ void DeleteAllComponentsOfId(std::uint32_t id); //Deletes all components of a specific id
+ void DeleteAllComponents(); //Deletes all components
+
+ template <typename T>
+ std::vector<std::reference_wrapper<T>> GetComponentsByID(std::uint32_t id) const; //Get a vector<> of all components at specific type and id
+ template <typename T>
+ std::vector<std::pair<std::reference_wrapper<T>, std::uint32_t>> GetComponentsByType() const; //Get a vector<> of all components of a specific type
+
+private:
+ static ComponentManager mInstance; //Singleton
+
+ ComponentManager(); //Singleton
+
+ /*
+ * 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).
+ */
+ std::unordered_map<std::type_index, std::vector<std::vector<std::unique_ptr<Component>>>> mComponents;
+};
+
+#include "ComponentManager.hpp"
diff --git a/mwe/ecs-memory-efficient/inc/ComponentManager.hpp b/mwe/ecs-memory-efficient/inc/ComponentManager.hpp
new file mode 100644
index 0000000..53dfddd
--- /dev/null
+++ b/mwe/ecs-memory-efficient/inc/ComponentManager.hpp
@@ -0,0 +1,88 @@
+
+template <typename T, typename... Args>
+void ComponentManager::AddComponent(std::uint32_t id, Args&&... args) {
+ std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>)
+
+ if (mComponents.find(type) == mComponents.end()) { //Check if this component type is already in the unordered_map<>
+ mComponents[type] = std::vector<std::vector<std::unique_ptr<Component>>>(); //If not, create a new (empty) vector<> of vector<unique_ptr<Component>>
+ }
+
+ if (id >= mComponents[type].size()) { //Resize the vector<> if the id is greater than the current size
+ mComponents[type].resize(id + 1); //Initialize new slots to nullptr (resize does automatically init to nullptr)
+ }
+
+ mComponents[type][id].push_back(std::make_unique<T>(std::forward<Args>(args)...)); //Create a new component of type T using perfect forwarding and store its unique_ptr in the vector<>
+}
+
+template <typename T>
+void ComponentManager::DeleteComponentsById(std::uint32_t id) {
+ std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>)
+
+ if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_map<>)
+ std::vector<std::vector<std::unique_ptr<Component>>>& componentArray = mComponents[type]; //Get the correct vector<>
+
+ if (id < componentArray.size()) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<>
+ componentArray[id].clear(); //Clear the whole vector<> of this specific type and id
+ }
+ }
+}
+
+template <typename T>
+void ComponentManager::DeleteComponents() {
+ std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>)
+
+ if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_map<>)
+ mComponents[type].clear(); //Clear the whole vector<> of this specific type
+ }
+}
+
+template <typename T>
+std::vector<std::reference_wrapper<T>> ComponentManager::GetComponentsByID(std::uint32_t id) const {
+ std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>)
+
+ std::vector<std::reference_wrapper<T>> componentVector; //Create an empty vector<>
+
+ if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_map<>)
+
+ const std::vector<std::vector<std::unique_ptr<Component>>>& componentArray = mComponents.at(type); //Get the correct vector<>
+
+ if (id < componentArray.size()) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<>
+ for (const std::unique_ptr<Component>& componentPtr : componentArray[id]) { //Loop trough the whole vector<>
+ T* castedComponent = static_cast<T*>(componentPtr.get()); //Cast the unique_ptr to a raw pointer
+
+ if (castedComponent) { //Ensure that the cast was successful
+ componentVector.push_back(*castedComponent); //Add the dereferenced raw pointer to the vector<>
+ }
+ }
+ }
+ }
+
+ return componentVector; //Return the vector<>
+}
+
+template <typename T>
+std::vector<std::pair<std::reference_wrapper<T>, std::uint32_t>> ComponentManager::GetComponentsByType() const {
+ std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>)
+
+ std::vector<std::pair<std::reference_wrapper<T>, std::uint32_t>> componentVector; //Create an empty vector<>
+ std::uint32_t id = 0; //Set the id to 0 (the id will also be stored in the returned vector<>)
+
+ if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_map<>)
+
+ const std::vector<std::vector<std::unique_ptr<Component>>>& componentArray = mComponents.at(type); //Get the correct vector<>
+
+ for (const std::vector<std::unique_ptr<Component>>& component : componentArray) { //Loop through the whole vector<>
+ for (const std::unique_ptr<Component>& componentPtr : component) { //Loop trough the whole vector<>
+ T* castedComponent = static_cast<T*>(componentPtr.get()); //Cast the unique_ptr to a raw pointer
+
+ if (castedComponent) { //Ensure that the cast was successful
+ componentVector.emplace_back(std::ref(*castedComponent), id); //Pair the dereferenced raw pointer and the id and add it to the vector<>
+ }
+ }
+
+ ++id; //Increase the id (the id will also be stored in the returned vector<>)
+ }
+ }
+
+ return componentVector; //Return the vector<>
+}
diff --git a/mwe/ecs-memory-efficient/inc/Components.h b/mwe/ecs-memory-efficient/inc/Components.h
new file mode 100644
index 0000000..98c5fe7
--- /dev/null
+++ b/mwe/ecs-memory-efficient/inc/Components.h
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <string>
+
+class Component {
+public:
+ Component();
+
+ bool mActive;
+};
+
+class Sprite : public Component {
+public:
+ Sprite(std::string path);
+
+ std::string mPath;
+};
+
+class Rigidbody : public Component {
+public:
+ Rigidbody(int mass, int gravityScale, int bodyType);
+
+ int mMass;
+ int mGravityScale;
+ int mBodyType;
+};
+
+class Colider : public Component {
+public:
+ Colider(int size);
+
+ int mSize;
+};
diff --git a/mwe/ecs-memory-efficient/inc/GameObjectMax.h b/mwe/ecs-memory-efficient/inc/GameObjectMax.h
new file mode 100644
index 0000000..f0bcec9
--- /dev/null
+++ b/mwe/ecs-memory-efficient/inc/GameObjectMax.h
@@ -0,0 +1,20 @@
+#pragma once
+
+#include <cstdint>
+#include <string>
+
+class GameObject {
+public:
+ GameObject(std::uint32_t id, std::string name, std::string tag, int layer);
+
+ template <typename T, typename... Args>
+ void AddComponent(Args&&... args);
+
+ std::uint32_t mId;
+ std::string mName;
+ std::string mTag;
+ bool mActive;
+ int mLayer;
+};
+
+#include "GameObjectMax.hpp"
diff --git a/mwe/ecs-memory-efficient/inc/GameObjectMax.hpp b/mwe/ecs-memory-efficient/inc/GameObjectMax.hpp
new file mode 100644
index 0000000..1e952ba
--- /dev/null
+++ b/mwe/ecs-memory-efficient/inc/GameObjectMax.hpp
@@ -0,0 +1,6 @@
+#include "ComponentManager.h"
+
+template <typename T, typename... Args>
+void GameObject::AddComponent(Args&&... args) {
+ ComponentManager::GetInstance().AddComponent<T>(mId, std::forward<Args>(args)...);
+}
diff --git a/mwe/ecs-memory-efficient/src/ComponentManager.cpp b/mwe/ecs-memory-efficient/src/ComponentManager.cpp
new file mode 100644
index 0000000..16cc2b6
--- /dev/null
+++ b/mwe/ecs-memory-efficient/src/ComponentManager.cpp
@@ -0,0 +1,21 @@
+#include "ComponentManager.h"
+
+ComponentManager ComponentManager::mInstance;
+
+ComponentManager& ComponentManager::GetInstance() {
+ return mInstance;
+}
+
+ComponentManager::ComponentManager() {}
+
+void ComponentManager::DeleteAllComponentsOfId(std::uint32_t id) {
+ for(auto& [type, componentArray] : mComponents) { //Loop through all the types (in the unordered_map<>)
+ if (id < componentArray.size()) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<>
+ componentArray[id].clear(); //Clear the components at this specific id
+ }
+ }
+}
+
+void ComponentManager::DeleteAllComponents() {
+ mComponents.clear(); //Clear the whole unordered_map<>
+}
diff --git a/mwe/ecs-memory-efficient/src/Components.cpp b/mwe/ecs-memory-efficient/src/Components.cpp
new file mode 100644
index 0000000..69b5eaa
--- /dev/null
+++ b/mwe/ecs-memory-efficient/src/Components.cpp
@@ -0,0 +1,10 @@
+#include "Components.h"
+#include <iostream>
+
+Component::Component() : mActive(true) {}
+
+Sprite::Sprite(std::string path) : mPath(path) {}
+
+Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) : mMass(mass), mGravityScale(gravityScale), mBodyType(bodyType) {}
+
+Colider::Colider(int size) : mSize(size) {}
diff --git a/mwe/ecs-memory-efficient/src/GameObjectMax.cpp b/mwe/ecs-memory-efficient/src/GameObjectMax.cpp
new file mode 100644
index 0000000..62c41de
--- /dev/null
+++ b/mwe/ecs-memory-efficient/src/GameObjectMax.cpp
@@ -0,0 +1,5 @@
+#include "GameObjectMax.h"
+
+#include "ComponentManager.h"
+
+GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, int layer) : mId(id), mName(name), mTag(tag), mActive(true), mLayer(layer) {}
diff --git a/mwe/ecs-memory-efficient/src/main.cpp b/mwe/ecs-memory-efficient/src/main.cpp
new file mode 100644
index 0000000..9e8fbd5
--- /dev/null
+++ b/mwe/ecs-memory-efficient/src/main.cpp
@@ -0,0 +1,108 @@
+#include <iostream>
+#include <vector>
+#include <cstdint>
+
+#include "ComponentManager.h"
+#include "GameObjectMax.h"
+#include "Components.h"
+
+int main() {
+ GameObject gameObect0(0, "Name: 0", "Tag: 0", 0); //Entity 0
+ GameObject gameObect1(1, "Name: 1", "Tag: 1", 1); //Entity 1
+ GameObject gameObect2(2, "Name: 2", "Tag: 2", 2); //Entity 2
+ GameObject gameObect3(3, "Name: 3", "Tag: 3", 3); //Entity 3
+ GameObject gameObect4(4, "Name: 4", "Tag: 4", 4); //Entity 4
+ GameObject gameObect5(5, "Name: 5", "Tag: 5", 5); //Entity 5
+ GameObject gameObect6(6, "Name: 6", "Tag: 6", 6); //Entity 6
+ GameObject gameObect7(7, "Name: 7", "Tag: 7", 7); //Entity 7
+
+ gameObect0.AddComponent<Sprite>("C:/object0"); //Add a sprite to entity0
+ gameObect0.AddComponent<Rigidbody>(1, 2, 3); //Also add a rigidbody to entity0
+ gameObect0.AddComponent<Rigidbody>(3, 2, 1); //Add a second rigidbody to entity0
+
+ gameObect1.AddComponent<Rigidbody>(4, 5, 6); //Only add a rigidbody to entity1
+
+ gameObect2.AddComponent<Sprite>("C:/object2/1"); //Add four sprites to entity2
+ gameObect2.AddComponent<Sprite>("C:/object2/2");
+ gameObect2.AddComponent<Sprite>("C:/object2/3");
+ gameObect2.AddComponent<Sprite>("C:/object2/4");
+ gameObect2.AddComponent<Rigidbody>(10, 100, 500); //Add four rigidbodies to entity2
+ gameObect2.AddComponent<Rigidbody>(10, 100, 501);
+ gameObect2.AddComponent<Rigidbody>(10, 100, 502);
+ gameObect2.AddComponent<Rigidbody>(10, 100, 500);
+
+ //Add non components to entity3, entity4, entity5 and entity6
+
+ gameObect7.AddComponent<Sprite>("C:/object7"); //Add a sprite to entity 7
+ gameObect7.AddComponent<Colider>(30); //Add a colder to entity 7
+
+ //The entities are now initialized
+ //Now I will demonstrate some ways of retreiving/getting components
+
+ std::cout << "Finding all sprites of entity 0" << std::endl;
+ std::vector<std::reference_wrapper<Sprite>> spriteOfEntity0 = ComponentManager::GetInstance().GetComponentsByID<Sprite>(gameObect0.mId);
+ for(Sprite& spriteEntity0 : spriteOfEntity0) {
+ std::cout << "Sprite of entity 0: " << spriteEntity0.mPath << std::endl;
+ }
+ std::cout << std::endl;
+
+ std::cout << "Finding all rigidbodies of entity 0" << std::endl;
+ std::vector<std::reference_wrapper<Rigidbody>> rigidbodyOfEntity0 = ComponentManager::GetInstance().GetComponentsByID<Rigidbody>(gameObect0.mId);
+ for(Rigidbody& rigidbodyEntity0 : rigidbodyOfEntity0) {
+ std::cout << "Rigidbody of entity 0: " << rigidbodyEntity0.mMass << " " << rigidbodyEntity0.mGravityScale << " " << rigidbodyEntity0.mBodyType << std::endl;
+
+ rigidbodyEntity0.mMass = 15;
+ }
+ std::cout << std::endl;
+
+ std::cout << "Finding all coliders of entity 0" << std::endl;
+ std::vector<std::reference_wrapper<Colider>> coliderOfEntity0 = ComponentManager::GetInstance().GetComponentsByID<Colider>(gameObect0.mId);
+ for(Colider& coliderEntity0 : coliderOfEntity0) {
+ std::cout << "Colider of entity 0: " << coliderEntity0.mSize << std::endl;
+ }
+ std::cout << std::endl;
+
+ std::cout << "Finding all sprites of entity 3" << std::endl;
+ std::vector<std::reference_wrapper<Sprite>> spriteOfEntity3 = ComponentManager::GetInstance().GetComponentsByID<Sprite>(gameObect3.mId);
+ for(Sprite& spriteEntity3 : spriteOfEntity3) {
+ std::cout << "Sprite of entity 3: " << spriteEntity3.mPath << std::endl;
+ }
+ std::cout << std::endl;
+
+ std::cout << "Finding all rigidbodies of entity 3" << std::endl;
+ std::vector<std::reference_wrapper<Rigidbody>> rigidbodyOfEntity3 = ComponentManager::GetInstance().GetComponentsByID<Rigidbody>(gameObect3.mId);
+ for(Rigidbody& rigidbodyEntity3 : rigidbodyOfEntity3) {
+ std::cout << "Rigidbody of entity 3: " << rigidbodyEntity3.mMass << " " << rigidbodyEntity3.mGravityScale << " " << rigidbodyEntity3.mBodyType << std::endl;
+ }
+ std::cout << std::endl;
+
+ std::cout << "Finding all sprites of all entities" << std::endl;
+ std::vector<std::pair<std::reference_wrapper<Sprite>, std::uint32_t>> sprites = ComponentManager::GetInstance().GetComponentsByType<Sprite>();
+ for(auto& [sprite, id] : sprites) {
+ std::cout << "Sprite of id: " << id << ": " << sprite.get().mPath << std::endl;
+ }
+ std::cout << std::endl;
+
+ std::cout << "Finding all coliders of all entities" << std::endl;
+ std::vector<std::pair<std::reference_wrapper<Colider>, std::uint32_t>> coliders = ComponentManager::GetInstance().GetComponentsByType<Colider>();
+ for(auto& [colider, id] : coliders) {
+ std::cout << "Colder of id: " << id << ": " << colider.get().mSize << std::endl;
+ }
+ std::cout << std::endl;
+
+ std::cout << "Finding all rigidbodies of all entities" << std::endl;
+ std::vector<std::pair<std::reference_wrapper<Rigidbody>, std::uint32_t>> rigidBodies = ComponentManager::GetInstance().GetComponentsByType<Rigidbody>();
+ for(auto& [rigidbody, id] : rigidBodies) {
+ std::cout << "Rigidbody of id: " << id << ": " << rigidbody.get().mMass << " " << rigidbody.get().mGravityScale << " " << rigidbody.get().mBodyType << std::endl;
+
+ rigidbody.get().mMass = -1;
+ }
+ std::cout << std::endl;
+
+ std::cout << "Finding all rigidbodies of all entities for the second time (after changing mMass to -1)" << std::endl;
+ std::vector<std::pair<std::reference_wrapper<Rigidbody>, std::uint32_t>> rigidBodies2 = ComponentManager::GetInstance().GetComponentsByType<Rigidbody>();
+ for(auto& [rigidbody2, id2] : rigidBodies2) {
+ std::cout << "Rigidbody of id: " << id2 << ": " << rigidbody2.get().mMass << " " << rigidbody2.get().mGravityScale << " " << rigidbody2.get().mBodyType << std::endl;
+ }
+ std::cout << std::endl;
+}