From c39adf3040c3f2f7cfcab5ce5b7e39b815e8131d Mon Sep 17 00:00:00 2001 From: Max-001 <80035972+Max-001@users.noreply.github.com> Date: Sat, 5 Oct 2024 11:05:59 +0200 Subject: Setup ecs-memory-efficient --- .vscode/launch.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to '.vscode/launch.json') diff --git a/.vscode/launch.json b/.vscode/launch.json index 160d242..69e3590 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "name": "Debug with Ninja", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/mwe/ecs-homemade/build/ecs-homemade.exe", + "program": "${workspaceFolder}/mwe/ecs-memory-efficient/build/ecs-memory-efficient.exe", "args": [], "stopAtEntry": true, "cwd": "${workspaceFolder}", -- cgit v1.2.3 From a50a3153be5214234db24a79ab4b706b37e132a0 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 17 Oct 2024 17:12:27 +0200 Subject: `make format` --- .vscode/launch.json | 28 ----- .vscode/tasks.json | 38 ------- mwe/ecs-memory-efficient/inc/ComponentManager.h | 26 ++--- mwe/ecs-memory-efficient/inc/ComponentManager.hpp | 14 +-- mwe/ecs-memory-efficient/inc/ContiguousContainer.h | 38 +++---- .../inc/ContiguousContainer.hpp | 124 ++++++++++----------- mwe/ecs-memory-efficient/inc/GameObjectMax.h | 12 +- mwe/ecs-memory-efficient/inc/GameObjectMax.hpp | 15 ++- mwe/ecs-memory-efficient/src/ComponentManager.cpp | 13 ++- mwe/ecs-memory-efficient/src/main.cpp | 33 +++--- 10 files changed, 142 insertions(+), 199 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/tasks.json (limited to '.vscode/launch.json') 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 - void addSpriteComponent(Args&&... args); - template - void addRigidbodyComponent(Args&&... args); - template - void addColiderComponent(Args&&... args); + template + void addSpriteComponent(Args &&... args); + template + void addRigidbodyComponent(Args &&... args); + template + void addColiderComponent(Args &&... args); std::vector> getAllSpriteReferences(); std::vector> getAllRigidbodyReferences(); std::vector> getAllColiderReferences(); private: - static ComponentManager mInstance; //Singleton + static ComponentManager mInstance; //Singleton - ComponentManager(); //Singleton + ComponentManager(); //Singleton ContiguousContainer mSpriteContainer; ContiguousContainer 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 -void ComponentManager::addSpriteComponent(Args&&... args) { + +template +void ComponentManager::addSpriteComponent(Args &&... args) { mSpriteContainer.pushBack(std::forward(args)...); } -template -void ComponentManager::addRigidbodyComponent(Args&&... args) { +template +void ComponentManager::addRigidbodyComponent(Args &&... args) { mRigidbodyContainer.pushBack(std::forward(args)...); } -template -void ComponentManager::addColiderComponent(Args&&... args){ +template +void ComponentManager::addColiderComponent(Args &&... args) { mColiderContainer.pushBack(std::forward(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 // For malloc and free -#include // For placement new -#include // For std::move and std::forward +#include // For malloc and free +#include // For placement new #include // For std::bad_alloc -#include // For returning references +#include // For std::move and std::forward +#include // For returning references -template +template class ContiguousContainer { public: - ContiguousContainer(); - ~ContiguousContainer(); + ContiguousContainer(); + ~ContiguousContainer(); - // Use perfect forwarding for pushBack - template - void pushBack(Args&&... args); - - void popBack(); - T& operator[](size_t index); - size_t getSize() const; + // Use perfect forwarding for pushBack + template + 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> getAllReferences(); + std::vector> 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 -ContiguousContainer::ContiguousContainer() - : mSize(0), mCapacity(10) { - // Allocate memory for 10 objects initially - mData = static_cast(malloc(mCapacity * sizeof(T))); - if (!mData) { - throw std::bad_alloc(); - } +template +ContiguousContainer::ContiguousContainer() : mSize(0), mCapacity(10) { + // Allocate memory for 10 objects initially + mData = static_cast(malloc(mCapacity * sizeof(T))); + if (!mData) { + throw std::bad_alloc(); + } } -template +template ContiguousContainer::~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 -template -void ContiguousContainer::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)...); - ++mSize; +template +template +void ContiguousContainer::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)...); + ++mSize; } -template +template void ContiguousContainer::popBack() { - if (mSize > 0) { - --mSize; - // Explicitly call the destructor - mData[mSize].~T(); - } + if (mSize > 0) { + --mSize; + // Explicitly call the destructor + mData[mSize].~T(); + } } -template -T& ContiguousContainer::operator[](size_t index) { - if (index >= mSize) { - throw std::out_of_range("Index out of range"); - } - return mData[index]; +template +T & ContiguousContainer::operator[](size_t index) { + if (index >= mSize) { + throw std::out_of_range("Index out of range"); + } + return mData[index]; } -template +template size_t ContiguousContainer::getSize() const { - return mSize; + return mSize; } // Function that returns a vector of references to all stored objects -template -std::vector> ContiguousContainer::getAllReferences() { - std::vector> 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 +std::vector> +ContiguousContainer::getAllReferences() { + std::vector> 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 +template void ContiguousContainer::resize(size_t new_capacity) { - // Allocate new memory block with the updated capacity - T* new_data = static_cast(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(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 - void addSpriteComponent(Args&&... args); - template - void addRigidbodyComponent(Args&&... args); - template - void addColiderComponent(Args&&... args); + template + void addSpriteComponent(Args &&... args); + template + void addRigidbodyComponent(Args &&... args); + template + 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 -void GameObject::addSpriteComponent(Args&&... args) { - ComponentManager::GetInstance().addSpriteComponent(std::forward(args)...); +void GameObject::addSpriteComponent(Args &&... args) { + ComponentManager::GetInstance().addSpriteComponent( + std::forward(args)...); } template -void GameObject::addRigidbodyComponent(Args&&... args) { - ComponentManager::GetInstance().addRigidbodyComponent(std::forward(args)...); +void GameObject::addRigidbodyComponent(Args &&... args) { + ComponentManager::GetInstance().addRigidbodyComponent( + std::forward(args)...); } template -void GameObject::addColiderComponent(Args&&... args) { - ComponentManager::GetInstance().addColiderComponent(std::forward(args)...); +void GameObject::addColiderComponent(Args &&... args) { + ComponentManager::GetInstance().addColiderComponent( + std::forward(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> ComponentManager::getAllSpriteReferences() { +std::vector> +ComponentManager::getAllSpriteReferences() { return mSpriteContainer.getAllReferences(); } -std::vector> ComponentManager::getAllRigidbodyReferences() { +std::vector> +ComponentManager::getAllRigidbodyReferences() { return mRigidbodyContainer.getAllReferences(); } -std::vector> ComponentManager::getAllColiderReferences() { +std::vector> +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 +#include #include #include -#include -#include #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> allSprites = ComponentManager::GetInstance().getAllSpriteReferences(); - for(Sprite& sprite : allSprites) { + std::vector> allSprites + = ComponentManager::GetInstance().getAllSpriteReferences(); + for (Sprite & sprite : allSprites) { //std::cout << sprite.mPath << std::endl; } //std::cout << std::endl; - std::vector> allRigidbody = ComponentManager::GetInstance().getAllRigidbodyReferences(); - for(Rigidbody& rigidbody : allRigidbody) { + std::vector> allRigidbody + = ComponentManager::GetInstance().getAllRigidbodyReferences(); + for (Rigidbody & rigidbody : allRigidbody) { //std::cout << rigidbody.mMass << " " << rigidbody.mGravityScale << " " << rigidbody.mBodyType << std::endl; } //std::cout << std::endl; - std::vector> allColider = ComponentManager::GetInstance().getAllColiderReferences(); - for(Colider& colider : allColider) { + std::vector> 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(stopAdding - startAdding); - auto LoopTime = std::chrono::duration_cast(stopLooping - stopAdding); + auto Addtime = std::chrono::duration_cast( + stopAdding - startAdding); + auto LoopTime = std::chrono::duration_cast( + stopLooping - stopAdding); std::cout << "AddTime: " << Addtime.count() << " us" << std::endl; std::cout << "LoopTime: " << LoopTime.count() << " us" << std::endl; } -- cgit v1.2.3