Godzil 3 роки тому
батько
коміт
4c001d41b7
3 змінених файлів з 15 додано та 13 видалено
  1. 3 1
      source/Game.cpp
  2. 9 10
      source/include/ECS.h
  3. 3 2
      source/include/Game.h

+ 3 - 1
source/Game.cpp

@@ -10,6 +10,8 @@
 #include <SDL_image.h>
 #include <glm/glm.hpp>
 
+#include <memory>
+
 #include <Game.h>
 #include <Logger.h>
 #include <ECS.h>
@@ -20,7 +22,7 @@ Game::Game()
     this->windowsHeight = 0;
     this->windowsWidth = 0;
 
-    this->registry = new Registry();
+    this->registry = std::make_unique<Registry>();
 }
 
 Game::~Game()

+ 9 - 10
source/include/ECS.h

@@ -87,10 +87,10 @@ class Registry
 private:
     uint32_t numEntities = 0;
 
-    std::vector<IPool*> componentPools;
+    std::vector<std::shared_ptr<IPool>> componentPools;
     std::vector<Signature> entityComponentSignatures;
 
-    std::unordered_map<std::type_index, System*> systems;
+    std::unordered_map<std::type_index, std::shared_ptr<System>> systems;
 
     std::set<Entity> entitiesToBeAdded;
     std::set<Entity> entitiesToBeKilled;
@@ -122,7 +122,7 @@ template<typename T, typename ...TArgs> void Registry::addComponent(Entity entit
 {
     const auto componentId = Component<T>::getId();
     const auto entityId = entity.getId();
-    Pool<T> *componentPool;
+    std::shared_ptr<Pool<T>> componentPool;
     /* Check the pool is big enough */
     if (componentId >= this->componentPools.size())
     {
@@ -132,10 +132,10 @@ template<typename T, typename ...TArgs> void Registry::addComponent(Entity entit
     /* Is this component exist in the pool? */
     if (!this->componentPools[componentId])
     {
-        this->componentPools[componentId] = new Pool<T>();
+        this->componentPools[componentId] = std::make_shared<Pool<T>>();
     }
 
-    componentPool = this->componentPools[componentId];
+    componentPool = std::static_pointer_cast<Pool<T>>(this->componentPools[componentId]);
 
     if (entityId >= componentPool->getSize())
     {
@@ -145,7 +145,7 @@ template<typename T, typename ...TArgs> void Registry::addComponent(Entity entit
     T newComponent(std::forward<TArgs>(args)...);
 
     componentPool->set(entityId, newComponent);
-    entityComponentSignatures[entityId].set(componentId);
+    this->entityComponentSignatures[entityId].set(componentId);
 }
 
 template <typename T> void Registry::removeComponent(Entity entity)
@@ -153,7 +153,7 @@ template <typename T> void Registry::removeComponent(Entity entity)
     const auto componentId = Component<T>::getId();
     const auto entityId = entity.getId();
 
-    entityComponentSignatures[entityId].set(componentId, false);
+    this->entityComponentSignatures[entityId].set(componentId, false);
 }
 
 template <typename T> bool Registry::hasComponent(Entity entity)
@@ -161,13 +161,12 @@ template <typename T> bool Registry::hasComponent(Entity entity)
     const auto componentId = Component<T>::getId();
     const auto entityId = entity.getId();
 
-    return entityComponentSignatures[entityId].test(componentId);
+    return this->entityComponentSignatures[entityId].test(componentId);
 }
 
 template<typename T, typename ...TArgs> void Registry::addSystem(TArgs&& ...args)
 {
-    T *newSystem(new T(std::forward<TArgs>(args)...));
-
+    std::shared_ptr<T> newSystem = std::make_shared<T>(std::forward<TArgs>(args)...);
     this->systems.insert(std::make_pair(std::type_index(typeid(T)), newSystem));
 }
 

+ 3 - 2
source/include/Game.h

@@ -9,8 +9,9 @@
 #ifndef GAMEENGINE_GAME_H
 #define GAMEENGINE_GAME_H
 
-#include <SDL.h>
 #include <stdint.h>
+#include <memory>
+#include <SDL.h>
 
 #include <ECS.h>
 
@@ -25,7 +26,7 @@ private:
     SDL_Window *window;
     SDL_Renderer *renderer;
 
-    Registry *registry;
+    std::unique_ptr<Registry> registry;
 
 public:
     Game();