123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- /*
- * 2D Game Engine
- * Pool.h:
- * Based on pikuma.com 2D game engine in C++ and Lua course
- * Copyright (c) 2021 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 11/02/2021.
- */
- #ifndef GAMEENGINE_POOL_H
- #define GAMEENGINE_POOL_H
- #include <vector>
- class IPool
- {
- public:
- virtual ~IPool() = default;
- virtual void removeEntityFromPool(uint32_t entityId) = 0;
- };
- template <typename T> class Pool:public IPool
- {
- private:
- std::vector<T> data;
- public:
- Pool(int size = 100) { this->resize(size); }
- virtual ~Pool() = default;
- bool isEmpty() const { return this->data.empty(); }
- uint32_t getSize() { return this->data.size(); }
- void resize(uint32_t size) { this->data.resize(size); }
- void clear() { this->data.clear(); }
- //void add(T object) { this->data.push_back(object); }
- void set(uint32_t index, T object) { this->data[index] = object; }
- T& get(uint32_t index) { return this->data[index]; }
- void remove(uint32_t index) { /* Just ignore */ }
- T& operator[](uint32_t index) { return this->data[index]; }
- void removeEntityFromPool(uint32_t entityId) { /* nothing to be done */ }
- };
- template <typename T> class PackedPool:public IPool
- {
- private:
- std::vector<T> data;
- uint32_t size;
- /* map of Entity id to Pool index */
- std::unordered_map<uint32_t, uint32_t> entityIdToIndex;
- /* map of Pool index to Entity id */
- std::unordered_map<uint32_t, uint32_t> indexToEntityId;
- public:
- PackedPool(uint32_t capacity = 100)
- {
- this->size = 0;
- this->data.resize(capacity);
- };
- virtual ~PackedPool() = default;
- bool isEmpty() const { return this->size == 0; }
- uint32_t getSize() { return this->size; }
- void resize(uint32_t size) { /* Nothing to be done here */ }
- void clear()
- {
- this->data.clear();
- this->indexToEntityId.clear();
- this->entityIdToIndex.clear();
- this->size = 0;
- }
- void set(uint32_t entityId, T object)
- {
- if (this->entityIdToIndex.find(entityId) != entityIdToIndex.end())
- {
- uint32_t index = this->entityIdToIndex[entityId];
- data[index] = object;
- }
- else
- {
- uint32_t index = size;
- this->entityIdToIndex.emplace(entityId, index);
- this->indexToEntityId.emplace(index, entityId);
- if (this->data.size() <= index)
- {
- this->data.resize(this->size * 2);
- }
- this->data[index] = object;
- this->size ++;
- }
- }
- void remove(uint32_t entityId)
- {
- uint32_t indexOfRemoved = this->entityIdToIndex[entityId];
- uint32_t indexOfLast = this->size - 1;
- uint32_t entityIdOfLastElement = this->indexToEntityId[indexOfLast];
- this->data[indexOfRemoved] = this->data[indexOfLast];
- this->entityIdToIndex[entityIdOfLastElement] = indexOfRemoved;
- this->indexToEntityId[indexOfRemoved] = entityIdOfLastElement;
- this->entityIdToIndex.erase(entityId);
- this->indexToEntityId.erase(indexOfLast);
- this->size --;
- }
- void removeEntityFromPool(uint32_t entityId)
- {
- if (this->entityIdToIndex.find(entityId) != this->entityIdToIndex.end())
- {
- this->remove(entityId);
- }
- }
- T& get(uint32_t entityId)
- {
- uint32_t indexOfEntity = this->entityIdToIndex[entityId];
- return this->data[indexOfEntity];
- }
- T& operator[](uint32_t index)
- {
- return this->get(index);
- }
- };
- template <typename T> class MyPackedPool:public IPool
- {
- private:
- std::vector<T> data;
- uint32_t size;
- /* map of Entity id to Pool index */
- std::vector<uint32_t> entityIdToIndex;
- /* map of Pool index to Entity id */
- std::vector<uint32_t> indexToEntityId;
- public:
- MyPackedPool(uint32_t capacity = 100)
- {
- this->size = 0;
- this->data.resize(capacity);
- this->entityIdToIndex.resize(capacity);
- this->indexToEntityId.resize(capacity);
- };
- virtual ~MyPackedPool() = default;
- bool isEmpty() const { return this->size == 0; }
- uint32_t getSize() { return this->size; }
- void resize(uint32_t size) { /* Nothing to be done here */ }
- void clear()
- {
- this->data.clear();
- this->indexToEntityId.clear();
- this->entityIdToIndex.clear();
- this->size = 0;
- }
- void set(uint32_t entityId, T object)
- {
- uint32_t index = this->size;
- if (this->data.size() <= index)
- {
- this->data.resize(this->size * 2);
- }
- if (this->entityIdToIndex.size() <= entityId)
- {
- this->entityIdToIndex.resize(entityId + 1);
- }
- if (this->indexToEntityId.size() <= this->size)
- {
- this->indexToEntityId.resize(this->size + 1);
- }
- this->entityIdToIndex[entityId] = index;
- this->indexToEntityId[index] = entityId;
- this->data[index] = object;
- this->size ++;
- }
- void remove(uint32_t entityId)
- {
- uint32_t indexOfRemoved = this->entityIdToIndex[entityId];
- uint32_t indexOfLast = this->size - 1;
- uint32_t entityIdOfLastElement = this->indexToEntityId[indexOfLast];
- this->data[indexOfRemoved] = this->data[indexOfLast];
- this->entityIdToIndex[entityIdOfLastElement] = indexOfRemoved;
- this->indexToEntityId[indexOfRemoved] = entityIdOfLastElement;
- this->size --;
- }
- void removeEntityFromPool(uint32_t entityId)
- {
- }
- T& get(uint32_t entityId)
- {
- uint32_t indexOfEntity = this->entityIdToIndex[entityId];
- return this->data[indexOfEntity];
- }
- T& operator[](uint32_t index)
- {
- return this->get(index);
- }
- };
- #define PoolToUse PackedPool
- #endif /* GAMEENGINE_POOL_H */
|