aboutsummaryrefslogtreecommitdiff
path: root/mwe/ecs-memory-efficient/inc
diff options
context:
space:
mode:
Diffstat (limited to 'mwe/ecs-memory-efficient/inc')
-rw-r--r--mwe/ecs-memory-efficient/inc/ComponentManager.h36
-rw-r--r--mwe/ecs-memory-efficient/inc/ComponentManager.hpp15
-rw-r--r--mwe/ecs-memory-efficient/inc/Components.h33
-rw-r--r--mwe/ecs-memory-efficient/inc/ContiguousContainer.h34
-rw-r--r--mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp84
-rw-r--r--mwe/ecs-memory-efficient/inc/GameObjectMax.h24
-rw-r--r--mwe/ecs-memory-efficient/inc/GameObjectMax.hpp16
7 files changed, 242 insertions, 0 deletions
diff --git a/mwe/ecs-memory-efficient/inc/ComponentManager.h b/mwe/ecs-memory-efficient/inc/ComponentManager.h
new file mode 100644
index 0000000..066795a
--- /dev/null
+++ b/mwe/ecs-memory-efficient/inc/ComponentManager.h
@@ -0,0 +1,36 @@
+#pragma once
+
+#include "Components.h"
+#include "ContiguousContainer.h"
+
+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... Args>
+ void addSpriteComponent(Args&&... args);
+ template<typename... Args>
+ void addRigidbodyComponent(Args&&... args);
+ template<typename... Args>
+ void addColiderComponent(Args&&... args);
+
+ std::vector<std::reference_wrapper<Sprite>> getAllSpriteReferences();
+ std::vector<std::reference_wrapper<Rigidbody>> getAllRigidbodyReferences();
+ std::vector<std::reference_wrapper<Colider>> getAllColiderReferences();
+
+private:
+ static ComponentManager mInstance; //Singleton
+
+ ComponentManager(); //Singleton
+
+ ContiguousContainer<Sprite> mSpriteContainer;
+ ContiguousContainer<Rigidbody> mRigidbodyContainer;
+ ContiguousContainer<Colider> mColiderContainer;
+};
+
+#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..1607c0c
--- /dev/null
+++ b/mwe/ecs-memory-efficient/inc/ComponentManager.hpp
@@ -0,0 +1,15 @@
+
+template<typename... Args>
+void ComponentManager::addSpriteComponent(Args&&... args) {
+ mSpriteContainer.pushBack(std::forward<Args>(args)...);
+}
+
+template<typename... Args>
+void ComponentManager::addRigidbodyComponent(Args&&... args) {
+ mRigidbodyContainer.pushBack(std::forward<Args>(args)...);
+}
+
+template<typename... Args>
+void ComponentManager::addColiderComponent(Args&&... args){
+ mColiderContainer.pushBack(std::forward<Args>(args)...);
+}
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/ContiguousContainer.h b/mwe/ecs-memory-efficient/inc/ContiguousContainer.h
new file mode 100644
index 0000000..dd0321e
--- /dev/null
+++ b/mwe/ecs-memory-efficient/inc/ContiguousContainer.h
@@ -0,0 +1,34 @@
+#pragma once
+
+#include <cstdlib> // For malloc and free
+#include <new> // For placement new
+#include <utility> // For std::move and std::forward
+#include <stdexcept> // For std::bad_alloc
+#include <vector> // For returning references
+
+template<typename T>
+class ContiguousContainer {
+public:
+ ContiguousContainer();
+ ~ContiguousContainer();
+
+ // Use perfect forwarding for pushBack
+ template<typename... Args>
+ void pushBack(Args&&... args);
+
+ void popBack();
+ T& operator[](size_t index);
+ size_t getSize() const;
+
+ // Function to return references to all stored objects
+ std::vector<std::reference_wrapper<T>> getAllReferences();
+
+private:
+ T* mData;
+ size_t mSize;
+ size_t mCapacity;
+
+ void resize(size_t new_capacity); // Resize function to allocate more space
+};
+
+#include "ContiguousContainer.hpp"
diff --git a/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp b/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp
new file mode 100644
index 0000000..878a85f
--- /dev/null
+++ b/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp
@@ -0,0 +1,84 @@
+template<typename T>
+ContiguousContainer<T>::ContiguousContainer()
+ : mSize(0), mCapacity(10) {
+ // Allocate memory for 10 objects initially
+ mData = static_cast<T*>(malloc(mCapacity * sizeof(T)));
+ if (!mData) {
+ throw std::bad_alloc();
+ }
+}
+
+template<typename T>
+ContiguousContainer<T>::~ContiguousContainer() {
+ // Destroy all constructed objects
+ for (size_t i = 0; i < mSize; ++i) {
+ mData[i].~T();
+ }
+ // Free the allocated memory
+ free(mData);
+}
+
+template<typename T>
+template<typename... Args>
+void ContiguousContainer<T>::pushBack(Args&&... args) {
+ if (mSize == mCapacity) {
+ // Double the capacity if the container is full
+ resize(mCapacity * 2);
+ }
+ // Use placement new with perfect forwarding to construct the object in place
+ new (mData + mSize) T(std::forward<Args>(args)...);
+ ++mSize;
+}
+
+template<typename T>
+void ContiguousContainer<T>::popBack() {
+ if (mSize > 0) {
+ --mSize;
+ // Explicitly call the destructor
+ mData[mSize].~T();
+ }
+}
+
+template<typename T>
+T& ContiguousContainer<T>::operator[](size_t index) {
+ if (index >= mSize) {
+ throw std::out_of_range("Index out of range");
+ }
+ return mData[index];
+}
+
+template<typename T>
+size_t ContiguousContainer<T>::getSize() const {
+ return mSize;
+}
+
+// Function that returns a vector of references to all stored objects
+template<typename T>
+std::vector<std::reference_wrapper<T>> ContiguousContainer<T>::getAllReferences() {
+ std::vector<std::reference_wrapper<T>> references;
+ references.reserve(mSize); // Reserve space to avoid reallocation
+ for (size_t i = 0; i < mSize; ++i) {
+ references.push_back(std::ref(mData[i]));
+ }
+ return references;
+}
+
+template<typename T>
+void ContiguousContainer<T>::resize(size_t new_capacity) {
+ // Allocate new memory block with the updated capacity
+ T* new_data = static_cast<T*>(malloc(new_capacity * sizeof(T)));
+ if (!new_data) {
+ throw std::bad_alloc();
+ }
+
+ // Move or copy existing objects to the new memory block
+ for (size_t i = 0; i < mSize; ++i) {
+ new (new_data + i) T(std::move(mData[i])); // Move the objects
+ mData[i].~T(); // Call the destructor for the old object
+ }
+
+ // Free the old memory block
+ free(mData);
+ mData = new_data;
+ mCapacity = new_capacity;
+}
diff --git a/mwe/ecs-memory-efficient/inc/GameObjectMax.h b/mwe/ecs-memory-efficient/inc/GameObjectMax.h
new file mode 100644
index 0000000..62cd3e6
--- /dev/null
+++ b/mwe/ecs-memory-efficient/inc/GameObjectMax.h
@@ -0,0 +1,24 @@
+#pragma once
+
+#include <cstdint>
+#include <string>
+
+class GameObject {
+public:
+ GameObject(std::uint32_t id, std::string name, std::string tag, int layer);
+
+ template<typename... Args>
+ void addSpriteComponent(Args&&... args);
+ template<typename... Args>
+ void addRigidbodyComponent(Args&&... args);
+ template<typename... Args>
+ void addColiderComponent(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..aac9811
--- /dev/null
+++ b/mwe/ecs-memory-efficient/inc/GameObjectMax.hpp
@@ -0,0 +1,16 @@
+#include "ComponentManager.h"
+
+template <typename... Args>
+void GameObject::addSpriteComponent(Args&&... args) {
+ ComponentManager::GetInstance().addSpriteComponent(std::forward<Args>(args)...);
+}
+
+template <typename... Args>
+void GameObject::addRigidbodyComponent(Args&&... args) {
+ ComponentManager::GetInstance().addRigidbodyComponent(std::forward<Args>(args)...);
+}
+
+template <typename... Args>
+void GameObject::addColiderComponent(Args&&... args) {
+ ComponentManager::GetInstance().addColiderComponent(std::forward<Args>(args)...);
+}