From 01c31f46f4ced39e6c4a192ec87c95b8b644c521 Mon Sep 17 00:00:00 2001 From: benkyd Date: Wed, 8 Jun 2022 20:00:52 +0000 Subject: [PATCH] issues with entity allocation --- Aeon/Entity/ComponentController.hpp | 7 ++++-- Aeon/Entity/Entity.hpp | 5 ++++ Aeon/Entity/EntityController.cpp | 36 +++++++++++++++++++---------- Aeon/Entity/EntityController.hpp | 14 +++++++---- Aeon/Includes.hpp | 5 ++++ Game/ExampleGame.cpp | 25 ++++++++++++++++++++ 6 files changed, 73 insertions(+), 19 deletions(-) diff --git a/Aeon/Entity/ComponentController.hpp b/Aeon/Entity/ComponentController.hpp index cb5c84a..d36214d 100644 --- a/Aeon/Entity/ComponentController.hpp +++ b/Aeon/Entity/ComponentController.hpp @@ -12,7 +12,6 @@ namespace EC // someone fix this ahaha struct IComponentContainer { - virtual ~IComponentContainer() = default; virtual void Create(const Entity) = 0; virtual void Destroy(const Entity) = 0; }; @@ -25,6 +24,10 @@ struct ComponentContainer : public IComponentContainer } + void Create(const Entity) override + { + + } void Destroy(const Entity) override { @@ -57,7 +60,7 @@ public: private: - std::map mComponentContainers; + // std::map mComponentContainers; }; diff --git a/Aeon/Entity/Entity.hpp b/Aeon/Entity/Entity.hpp index 2d6b017..1cafcf3 100644 --- a/Aeon/Entity/Entity.hpp +++ b/Aeon/Entity/Entity.hpp @@ -8,6 +8,11 @@ namespace EC using Entity = uint32_t; +struct EntityDebugMetadata +{ + std::string DebugName; +}; + } #endif diff --git a/Aeon/Entity/EntityController.cpp b/Aeon/Entity/EntityController.cpp index 54bc71f..e7c341e 100644 --- a/Aeon/Entity/EntityController.cpp +++ b/Aeon/Entity/EntityController.cpp @@ -15,25 +15,37 @@ EntityRegistry::~EntityRegistry() Entity EntityRegistry::Create() { - mEntityCeiling++; - uint32_t entityId = mEntityCeiling; - - Entity entity( { entityId } ); - mEntities.push( entity ); - return entity; + uint32_t entityId; + if ( mFreedEntities.empty() ) + { + mEntityCeiling++; + entityId = mEntityCeiling; + } + else + { + mFreedEntities.pop(); + } + + mEntities[entityId] = std::vector(); + return entityId; } -Entity EntityRegistry::Copy( const Entity entity ) +Entity Copy( const Entity& entity ) { // look up everything, create a new entity and populate // with the components in the og entity - return Entity( { 0 } ); + return static_cast( 0 ); } -Entity EntityRegistry::Destroy( Entity entity ) +void EntityRegistry::Destroy( Entity entity ) { - auto id = entity.id; - mEntities = std::move(n.back()); // move last element to removal location - mEntities.pop_back(); + if ( !this->Valid( entity ) ) return; + mFreedEntities.push(entity); + mEntities.erase(entity); +} + +bool EntityRegistry::Valid( const Entity entity ) +{ + return mEntities.find(entity) != mEntities.end(); } diff --git a/Aeon/Entity/EntityController.hpp b/Aeon/Entity/EntityController.hpp index 05b5f35..bfb0328 100644 --- a/Aeon/Entity/EntityController.hpp +++ b/Aeon/Entity/EntityController.hpp @@ -16,10 +16,10 @@ public: EntityRegistry(); ~EntityRegistry(); - Entity Create() + Entity Create(); Entity Copy( const Entity entity ); void Destroy( Entity entity ); - bool Valid(const Entity entity); + bool Valid( const Entity entity ); // add, replace components template @@ -47,12 +47,16 @@ public: // std::vector Sort(std::function<; private: + // Keep track of IDs that were freed for re-allocation + std::queue mFreedEntities; uint32_t mEntityCeiling = 0; - // On destroy, the last entity is moved to the position - // of the old entity for cache coherency - std::map> mEntities + // Currently unused + std::unordered_map mEntityDebug; + // That's not very cache-friendly of you + // optimisations coming soon TM + std::unordered_map> mEntities; }; } diff --git a/Aeon/Includes.hpp b/Aeon/Includes.hpp index a7c746c..e1807dc 100644 --- a/Aeon/Includes.hpp +++ b/Aeon/Includes.hpp @@ -3,7 +3,12 @@ // shut up +#ifdef __WIN32 #include +#else +#include +#endif + extern "C" { #include } diff --git a/Game/ExampleGame.cpp b/Game/ExampleGame.cpp index 2ded34c..ae21db2 100644 --- a/Game/ExampleGame.cpp +++ b/Game/ExampleGame.cpp @@ -91,6 +91,31 @@ public: ExampleGame() : App( { "Example" }, { "Game with AEON!" } ) { + + EC::EntityRegistry registry; + + EC::Entity entity1 = registry.Create(); + + std::cout << entity1 << std::endl; + + std::vector entities; + + for (int i = 0; i < 100000; i++) + { + entities.push_back(registry.Create()); + } + + std::cout << entities[entities.size()] << std::endl; + + for (int i = 0; i < 100000; i++) + { + registry.Destroy(entities[i]); + } + + entity1 = registry.Create(); + + std::cout << entity1 << std::endl; + Level* level = new Level; PushLayer( (Core::GameLayer*)level ); DebugLayer* debug = new DebugLayer;