aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.vscode/launch.json28
-rw-r--r--.vscode/tasks.json38
-rw-r--r--mwe/ecs-memory-efficient/inc/ComponentManager.h26
-rw-r--r--mwe/ecs-memory-efficient/inc/ComponentManager.hpp14
-rw-r--r--mwe/ecs-memory-efficient/inc/ContiguousContainer.h38
-rw-r--r--mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp124
-rw-r--r--mwe/ecs-memory-efficient/inc/GameObjectMax.h12
-rw-r--r--mwe/ecs-memory-efficient/inc/GameObjectMax.hpp15
-rw-r--r--mwe/ecs-memory-efficient/src/ComponentManager.cpp13
-rw-r--r--mwe/ecs-memory-efficient/src/main.cpp33
10 files changed, 142 insertions, 199 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json
deleted file mode 100644
index 69e3590..0000000
--- a/.vscode/launch.json
+++ /dev/null
@@ -1,28 +0,0 @@
-{
- "version": "0.2.0",
- "configurations": [
- {
- "name": "Debug with Ninja",
- "type": "cppdbg",
- "request": "launch",
- "program": "${workspaceFolder}/mwe/ecs-memory-efficient/build/ecs-memory-efficient.exe",
- "args": [],
- "stopAtEntry": true,
- "cwd": "${workspaceFolder}",
- "environment": [],
- "externalConsole": false,
- "MIMode": "gdb",
- "preLaunchTask": "build",
- "setupCommands": [
- {
- "description": "Enable pretty-printing for gdb",
- "text": "-enable-pretty-printing",
- "ignoreFailures": true
- }
- ],
- "logging": {
- "engineLogging": true
- }
- }
- ]
-}
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
deleted file mode 100644
index b00cb77..0000000
--- a/.vscode/tasks.json
+++ /dev/null
@@ -1,38 +0,0 @@
-{
- "version": "2.0.0",
- "tasks": [
- {
- "label": "configure",
- "type": "shell",
- "command": "cmake",
- "args": [
- "-G",
- "Ninja",
- "-B",
- "${workspaceFolder}/mwe/ecs-memory-efficient/build", // Create build folder here
- "${workspaceFolder}/mwe/ecs-memory-efficient" // Path to your source directory
- ],
- "group": {
- "kind": "build",
- "isDefault": false
- },
- "problemMatcher": []
- },
- {
- "label": "build",
- "type": "shell",
- "command": "cmake",
- "args": [
- "--build",
- "${workspaceFolder}/mwe/ecs-memory-efficient/build" // Build directory
- ],
- "dependsOn": "configure", // Ensure the configure task runs first
- "group": {
- "kind": "build",
- "isDefault": true
- },
- "problemMatcher": ["$gcc"],
- "detail": "Generated task for building the project."
- }
- ]
-}
diff --git a/mwe/ecs-memory-efficient/inc/ComponentManager.h b/mwe/ecs-memory-efficient/inc/ComponentManager.h
index 066795a..8279a9a 100644
--- a/mwe/ecs-memory-efficient/inc/ComponentManager.h
+++ b/mwe/ecs-memory-efficient/inc/ComponentManager.h
@@ -5,28 +5,28 @@
class ComponentManager {
public:
- static ComponentManager& GetInstance(); //Singleton
+ static ComponentManager & GetInstance(); //Singleton
- ComponentManager(const ComponentManager&) = delete; //Singleton
- ComponentManager(ComponentManager&&) = delete; //Singleton
- ComponentManager& operator=(const ComponentManager&) = delete; //Singleton
- ComponentManager& operator=(ComponentManager&&) = delete; //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);
+ 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
+ static ComponentManager mInstance; //Singleton
- ComponentManager(); //Singleton
+ ComponentManager(); //Singleton
ContiguousContainer<Sprite> mSpriteContainer;
ContiguousContainer<Rigidbody> mRigidbodyContainer;
diff --git a/mwe/ecs-memory-efficient/inc/ComponentManager.hpp b/mwe/ecs-memory-efficient/inc/ComponentManager.hpp
index 1607c0c..a914a6b 100644
--- a/mwe/ecs-memory-efficient/inc/ComponentManager.hpp
+++ b/mwe/ecs-memory-efficient/inc/ComponentManager.hpp
@@ -1,15 +1,15 @@
-
-template<typename... Args>
-void ComponentManager::addSpriteComponent(Args&&... args) {
+
+template <typename... Args>
+void ComponentManager::addSpriteComponent(Args &&... args) {
mSpriteContainer.pushBack(std::forward<Args>(args)...);
}
-template<typename... Args>
-void ComponentManager::addRigidbodyComponent(Args&&... args) {
+template <typename... Args>
+void ComponentManager::addRigidbodyComponent(Args &&... args) {
mRigidbodyContainer.pushBack(std::forward<Args>(args)...);
}
-template<typename... Args>
-void ComponentManager::addColiderComponent(Args&&... args){
+template <typename... Args>
+void ComponentManager::addColiderComponent(Args &&... args) {
mColiderContainer.pushBack(std::forward<Args>(args)...);
}
diff --git a/mwe/ecs-memory-efficient/inc/ContiguousContainer.h b/mwe/ecs-memory-efficient/inc/ContiguousContainer.h
index dd0321e..e3b57ba 100644
--- a/mwe/ecs-memory-efficient/inc/ContiguousContainer.h
+++ b/mwe/ecs-memory-efficient/inc/ContiguousContainer.h
@@ -1,34 +1,34 @@
#pragma once
-#include <cstdlib> // For malloc and free
-#include <new> // For placement new
-#include <utility> // For std::move and std::forward
+#include <cstdlib> // For malloc and free
+#include <new> // For placement new
#include <stdexcept> // For std::bad_alloc
-#include <vector> // For returning references
+#include <utility> // For std::move and std::forward
+#include <vector> // For returning references
-template<typename T>
+template <typename T>
class ContiguousContainer {
public:
- ContiguousContainer();
- ~ContiguousContainer();
+ 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;
+ // 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();
+ std::vector<std::reference_wrapper<T>> getAllReferences();
private:
- T* mData;
- size_t mSize;
- size_t mCapacity;
+ T * mData;
+ size_t mSize;
+ size_t mCapacity;
- void resize(size_t new_capacity); // Resize function to allocate more space
+ 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
index 878a85f..408d5aa 100644
--- a/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp
+++ b/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp
@@ -1,84 +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() : 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>
+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);
+ // 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>
+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>
+template <typename T>
void ContiguousContainer<T>::popBack() {
- if (mSize > 0) {
- --mSize;
- // Explicitly call the destructor
- mData[mSize].~T();
- }
+ 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>
+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>
+template <typename T>
size_t ContiguousContainer<T>::getSize() const {
- return mSize;
+ 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>
+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>
+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();
- }
+ // 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
- }
+ // 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;
+ // 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
index 62cd3e6..760e330 100644
--- a/mwe/ecs-memory-efficient/inc/GameObjectMax.h
+++ b/mwe/ecs-memory-efficient/inc/GameObjectMax.h
@@ -7,12 +7,12 @@ 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);
+ 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;
diff --git a/mwe/ecs-memory-efficient/inc/GameObjectMax.hpp b/mwe/ecs-memory-efficient/inc/GameObjectMax.hpp
index aac9811..be3ffa2 100644
--- a/mwe/ecs-memory-efficient/inc/GameObjectMax.hpp
+++ b/mwe/ecs-memory-efficient/inc/GameObjectMax.hpp
@@ -1,16 +1,19 @@
#include "ComponentManager.h"
template <typename... Args>
-void GameObject::addSpriteComponent(Args&&... args) {
- ComponentManager::GetInstance().addSpriteComponent(std::forward<Args>(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)...);
+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)...);
+void GameObject::addColiderComponent(Args &&... args) {
+ ComponentManager::GetInstance().addColiderComponent(
+ std::forward<Args>(args)...);
}
diff --git a/mwe/ecs-memory-efficient/src/ComponentManager.cpp b/mwe/ecs-memory-efficient/src/ComponentManager.cpp
index 20d0ce0..8c1fd23 100644
--- a/mwe/ecs-memory-efficient/src/ComponentManager.cpp
+++ b/mwe/ecs-memory-efficient/src/ComponentManager.cpp
@@ -2,20 +2,21 @@
ComponentManager ComponentManager::mInstance;
-ComponentManager& ComponentManager::GetInstance() {
- return mInstance;
-}
+ComponentManager & ComponentManager::GetInstance() { return mInstance; }
ComponentManager::ComponentManager() {}
-std::vector<std::reference_wrapper<Sprite>> ComponentManager::getAllSpriteReferences() {
+std::vector<std::reference_wrapper<Sprite>>
+ComponentManager::getAllSpriteReferences() {
return mSpriteContainer.getAllReferences();
}
-std::vector<std::reference_wrapper<Rigidbody>> ComponentManager::getAllRigidbodyReferences() {
+std::vector<std::reference_wrapper<Rigidbody>>
+ComponentManager::getAllRigidbodyReferences() {
return mRigidbodyContainer.getAllReferences();
}
-std::vector<std::reference_wrapper<Colider>> ComponentManager::getAllColiderReferences() {
+std::vector<std::reference_wrapper<Colider>>
+ComponentManager::getAllColiderReferences() {
return mColiderContainer.getAllReferences();
}
diff --git a/mwe/ecs-memory-efficient/src/main.cpp b/mwe/ecs-memory-efficient/src/main.cpp
index c25816b..9c6f2aa 100644
--- a/mwe/ecs-memory-efficient/src/main.cpp
+++ b/mwe/ecs-memory-efficient/src/main.cpp
@@ -1,18 +1,18 @@
+#include <chrono>
+#include <cstdint>
#include <iostream>
#include <vector>
-#include <cstdint>
-#include <chrono>
#include "ComponentManager.h"
-#include "GameObjectMax.h"
#include "Components.h"
+#include "GameObjectMax.h"
int main() {
auto startAdding = std::chrono::high_resolution_clock::now();
- GameObject* gameObject[100000];
+ GameObject * gameObject[100000];
- for(int i = 0; i < 100000; ++i) {
+ for (int i = 0; i < 100000; ++i) {
gameObject[i] = new GameObject(i, "Name", "Tag", 0);
gameObject[i]->addSpriteComponent("C:/Test");
@@ -24,31 +24,36 @@ int main() {
//This is what systems would do:
- std::vector<std::reference_wrapper<Sprite>> allSprites = ComponentManager::GetInstance().getAllSpriteReferences();
- for(Sprite& sprite : allSprites) {
+ std::vector<std::reference_wrapper<Sprite>> allSprites
+ = ComponentManager::GetInstance().getAllSpriteReferences();
+ for (Sprite & sprite : allSprites) {
//std::cout << sprite.mPath << std::endl;
}
//std::cout << std::endl;
- std::vector<std::reference_wrapper<Rigidbody>> allRigidbody = ComponentManager::GetInstance().getAllRigidbodyReferences();
- for(Rigidbody& rigidbody : allRigidbody) {
+ std::vector<std::reference_wrapper<Rigidbody>> allRigidbody
+ = ComponentManager::GetInstance().getAllRigidbodyReferences();
+ for (Rigidbody & rigidbody : allRigidbody) {
//std::cout << rigidbody.mMass << " " << rigidbody.mGravityScale << " " << rigidbody.mBodyType << std::endl;
}
//std::cout << std::endl;
- std::vector<std::reference_wrapper<Colider>> allColider = ComponentManager::GetInstance().getAllColiderReferences();
- for(Colider& colider : allColider) {
+ std::vector<std::reference_wrapper<Colider>> allColider
+ = ComponentManager::GetInstance().getAllColiderReferences();
+ for (Colider & colider : allColider) {
//std::cout << colider.mSize << std::endl;
}
auto stopLooping = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 100000; ++i) {
- delete gameObject[i];
+ delete gameObject[i];
}
- auto Addtime = std::chrono::duration_cast<std::chrono::microseconds>(stopAdding - startAdding);
- auto LoopTime = std::chrono::duration_cast<std::chrono::microseconds>(stopLooping - stopAdding);
+ auto Addtime = std::chrono::duration_cast<std::chrono::microseconds>(
+ stopAdding - startAdding);
+ auto LoopTime = std::chrono::duration_cast<std::chrono::microseconds>(
+ stopLooping - stopAdding);
std::cout << "AddTime: " << Addtime.count() << " us" << std::endl;
std::cout << "LoopTime: " << LoopTime.count() << " us" << std::endl;
}