diff --git a/SDK/include/NDK/BaseSystem.hpp b/SDK/include/NDK/BaseSystem.hpp index a323f2952..99dcbf768 100644 --- a/SDK/include/NDK/BaseSystem.hpp +++ b/SDK/include/NDK/BaseSystem.hpp @@ -9,6 +9,7 @@ #include #include +#include #include namespace Ndk @@ -33,7 +34,7 @@ namespace Ndk bool Filters(const Entity* entity) const; - inline const std::vector& GetEntities() const; + inline const EntityList& GetEntities() const; inline SystemIndex GetIndex() const; inline int GetUpdateOrder() const; inline float GetUpdateRate() const; @@ -84,12 +85,11 @@ namespace Ndk static inline bool Initialize(); static inline void Uninitialize(); - std::vector m_entities; - Nz::Bitset m_entityBits; Nz::Bitset<> m_excludedComponents; mutable Nz::Bitset<> m_filterResult; Nz::Bitset<> m_requiredAnyComponents; Nz::Bitset<> m_requiredComponents; + EntityList m_entities; SystemIndex m_systemIndex; World* m_world; bool m_updateEnabled; diff --git a/SDK/include/NDK/BaseSystem.inl b/SDK/include/NDK/BaseSystem.inl index e949d254b..5969dd8e3 100644 --- a/SDK/include/NDK/BaseSystem.inl +++ b/SDK/include/NDK/BaseSystem.inl @@ -56,7 +56,7 @@ namespace Ndk * \return A constant reference to the list of entities */ - inline const std::vector& BaseSystem::GetEntities() const + inline const EntityList& BaseSystem::GetEntities() const { return m_entities; } @@ -121,10 +121,7 @@ namespace Ndk inline bool BaseSystem::HasEntity(const Entity* entity) const { - if (!entity) - return false; - - return m_entityBits.UnboundedTest(entity->GetId()); + return m_entities.Has(entity); } /*! @@ -288,9 +285,7 @@ namespace Ndk { NazaraAssert(entity, "Invalid entity"); - m_entities.emplace_back(entity); - m_entityBits.UnboundedSet(entity->GetId(), true); - + m_entities.Insert(entity); entity->RegisterSystem(m_systemIndex); OnEntityAdded(entity); @@ -308,14 +303,7 @@ namespace Ndk { NazaraAssert(entity, "Invalid entity"); - auto it = std::find(m_entities.begin(), m_entities.end(), *entity); - NazaraAssert(it != m_entities.end(), "Entity is not part of this system"); - - // To avoid moving a lot of handles, we swap and pop - std::swap(*it, m_entities.back()); - m_entities.pop_back(); // We get it out of the vector - - m_entityBits.Reset(entity->GetId()); + m_entities.Remove(entity); entity->UnregisterSystem(m_systemIndex); OnEntityRemoved(entity); // And we alert our callback @@ -360,7 +348,7 @@ namespace Ndk } /*! - * \brief Uninitializes the BaseSystem + * \brief Uninitialize the BaseSystem */ inline void BaseSystem::Uninitialize() diff --git a/SDK/include/NDK/EntityList.hpp b/SDK/include/NDK/EntityList.hpp index 4047388cc..76097f87b 100644 --- a/SDK/include/NDK/EntityList.hpp +++ b/SDK/include/NDK/EntityList.hpp @@ -16,45 +16,64 @@ namespace Ndk class NDK_API EntityList { public: - using Container = std::vector; + class iterator; + friend iterator; + using size_type = std::size_t; - EntityList() = default; + inline EntityList(); + inline EntityList(const EntityList& entityList); + inline EntityList(EntityList&& entityList) noexcept; ~EntityList() = default; inline void Clear(); - inline bool Has(const Entity* entity); - inline bool Has(EntityId entity); + inline bool Has(const Entity* entity) const; + inline bool Has(EntityId entity) const; inline void Insert(Entity* entity); inline void Remove(Entity* entity); // STL API - inline Container::iterator begin(); - inline Container::const_iterator begin() const; - - inline Container::const_iterator cbegin() const; - inline Container::const_iterator cend() const; - inline Container::const_reverse_iterator crbegin() const; - inline Container::const_reverse_iterator crend() const; - + inline iterator begin() const; inline bool empty() const; + inline iterator end() const; + inline size_type size() const; - inline Container::iterator end(); - inline Container::const_iterator end() const; - - inline Container::reverse_iterator rbegin(); - inline Container::const_reverse_iterator rbegin() const; - - inline Container::reverse_iterator rend(); - inline Container::const_reverse_iterator rend() const; - - inline Container::size_type size() const; + inline EntityList& operator=(const EntityList& entityList); + inline EntityList& operator=(EntityList&& entityList) noexcept; private: - std::vector m_entities; + inline std::size_t FindNext(std::size_t currentId) const; + inline World* GetWorld() const; + Nz::Bitset m_entityBits; + World* m_world; + }; + + class NDK_API EntityList::iterator : public std::iterator + { + friend EntityList; + + public: + inline iterator(const iterator& iterator); + + const EntityHandle& operator*() const; + + inline iterator& operator=(const iterator& iterator); + inline iterator& operator++(); + inline iterator operator++(int); + + friend inline bool operator==(const iterator& lhs, const iterator& rhs); + friend inline bool operator!=(const iterator& lhs, const iterator& rhs); + + friend inline void swap(iterator& lhs, iterator& rhs); + + private: + inline iterator(const EntityList* world, std::size_t nextId); + + std::size_t m_nextEntityId; + const EntityList* m_list; }; } diff --git a/SDK/include/NDK/EntityList.inl b/SDK/include/NDK/EntityList.inl index 126af0a7e..f5a751323 100644 --- a/SDK/include/NDK/EntityList.inl +++ b/SDK/include/NDK/EntityList.inl @@ -2,8 +2,10 @@ // This file is part of the "Nazara Development Kit" // For conditions of distribution and use, see copyright notice in Prerequesites.hpp +#include #include #include +#include "EntityList.hpp" namespace Ndk { @@ -14,24 +16,55 @@ namespace Ndk */ /*! - * \brief Clears the set from every entities + * \brief Construct a new entity list */ - - inline void EntityList::Clear() + inline EntityList::EntityList() : + m_world(nullptr) { - m_entities.clear(); - m_entityBits.Clear(); } /*! - * \brief Checks whether or not the set contains the entity + * \brief Construct a new entity list by copying another one + */ + inline EntityList::EntityList(const EntityList& entityList) : + m_entityBits(entityList.m_entityBits), + m_world(entityList.m_world) + { + } + + /*! + * \brief Construct a new entity list by moving a list into this one + */ + inline EntityList::EntityList(EntityList&& entityList) noexcept : + m_entityBits(std::move(entityList.m_entityBits)), + m_world(entityList.m_world) + { + } + + + /*! + * \brief Clears the set from every entities + * + * \remark This resets the implicit world member, allowing you to insert entities from a different world than previously + */ + inline void EntityList::Clear() + { + m_entityBits.Clear(); + m_world = nullptr; + } + + /*! + * \brief Checks whether or not the EntityList contains the entity * \return true If it is the case * * \param entity Pointer to the entity + * + * \remark If the Insert function was called since the EntityList construction (or last call to Clear), the entity passed by parameter must belong to the same world as the previously inserted entities. */ - - inline bool EntityList::Has(const Entity* entity) + inline bool EntityList::Has(const Entity* entity) const { + NazaraAssert(!m_world || !entity || entity->GetWorld() == m_world, "Incompatible world"); + return entity && entity->IsValid() && Has(entity->GetId()); } @@ -41,8 +74,7 @@ namespace Ndk * * \param id Identifier of the entity */ - - inline bool EntityList::Has(EntityId entity) + inline bool EntityList::Has(EntityId entity) const { return m_entityBits.UnboundedTest(entity); } @@ -50,18 +82,21 @@ namespace Ndk /*! * \brief Inserts the entity into the set * - * \param entity Pointer to the entity + * Marks an entity as present in this entity list, it must belongs to the same world as others entities contained in this list. + * + * \param entity Valid pointer to an entity * * \remark If entity is already contained, no action is performed + * \remark If any entity has been inserted since construction (or last Clear call), the entity must belong to the same world as the previously inserted entities + * \remark It's up to the programmer to remove an entity from this list before its deletion */ - inline void EntityList::Insert(Entity* entity) { - if (!Has(entity)) - { - m_entities.emplace_back(entity); - m_entityBits.UnboundedSet(entity->GetId(), true); - } + NazaraAssert(entity, "Invalid entity"); + NazaraAssert(!m_world || entity->GetWorld() == m_world, "Incompatible world"); + + m_entityBits.UnboundedSet(entity->GetId(), true); + m_world = entity->GetWorld(); } /*! @@ -70,89 +105,117 @@ namespace Ndk * \param entity Pointer to the entity * * \remark If entity is not contained, no action is performed + * \remark This function never resets the implicit world member, even if it empties the list. Use the Clear method if you want to reset it. + * + * \see Clear */ - inline void EntityList::Remove(Entity* entity) { - if (Has(entity)) - { - auto it = std::find(m_entities.begin(), m_entities.end(), *entity); - NazaraAssert(it != m_entities.end(), "Entity should be part of the vector"); - - std::swap(*it, m_entities.back()); - m_entities.pop_back(); // We get it out of the vector - m_entityBits.UnboundedSet(entity->GetId(), false); - } + m_entityBits.UnboundedSet(entity->GetId(), false); } - // Nz::Interface STD - inline EntityList::Container::iterator EntityList::begin() + // STL Interface + inline EntityList::iterator EntityList::begin() const { - return m_entities.begin(); - } - - inline EntityList::Container::const_iterator EntityList::begin() const - { - return m_entities.begin(); - } - - inline EntityList::Container::const_iterator EntityList::cbegin() const - { - return m_entities.cbegin(); - } - - inline EntityList::Container::const_iterator EntityList::cend() const - { - return m_entities.cend(); - } - - inline EntityList::Container::const_reverse_iterator EntityList::crbegin() const - { - return m_entities.crbegin(); - } - - inline EntityList::Container::const_reverse_iterator EntityList::crend() const - { - return m_entities.crend(); + return EntityList::iterator(this, m_entityBits.FindFirst()); } inline bool EntityList::empty() const { - return m_entities.empty(); + return !m_entityBits.TestAny(); } - inline EntityList::Container::iterator EntityList::end() + inline EntityList::iterator EntityList::end() const { - return m_entities.end(); + return EntityList::iterator(this, m_entityBits.npos); } - inline EntityList::Container::const_iterator EntityList::end() const + inline EntityList::size_type EntityList::size() const { - return m_entities.end(); + return m_entityBits.Count(); } - inline EntityList::Container::reverse_iterator EntityList::rbegin() + inline EntityList& EntityList::operator=(const EntityList& entityList) { - return m_entities.rbegin(); + m_entityBits = entityList.m_entityBits; + m_world = entityList.m_world; + + return *this; } - inline EntityList::Container::const_reverse_iterator EntityList::rbegin() const + inline EntityList& EntityList::operator=(EntityList && entityList) noexcept { - return m_entities.rbegin(); + m_entityBits = std::move(entityList.m_entityBits); + m_world = entityList.m_world; + + return *this; } - inline EntityList::Container::reverse_iterator EntityList::rend() + inline std::size_t EntityList::FindNext(std::size_t currentId) const { - return m_entities.rend(); + return m_entityBits.FindNext(currentId); } - inline EntityList::Container::const_reverse_iterator EntityList::rend() const + inline World* EntityList::GetWorld() const { - return m_entities.rend(); + return m_world; } - inline EntityList::Container::size_type EntityList::size() const + + inline EntityList::iterator::iterator(const EntityList* list, std::size_t nextId) : + m_nextEntityId(nextId), + m_list(list) { - return m_entities.size(); + } + + inline EntityList::iterator::iterator(const iterator& iterator) : + m_nextEntityId(iterator.m_nextEntityId), + m_list(iterator.m_list) + { + } + + inline EntityList::iterator& EntityList::iterator::operator=(const iterator& iterator) + { + m_nextEntityId = iterator.m_nextEntityId; + m_list = iterator.m_list; + + return *this; + } + + inline EntityList::iterator& EntityList::iterator::operator++() + { + m_nextEntityId = m_list->FindNext(m_nextEntityId); + + return *this; + } + + inline EntityList::iterator EntityList::iterator::operator++(int) + { + std::size_t previousId = m_nextEntityId; + + m_nextEntityId = m_list->FindNext(m_nextEntityId); + + return iterator(m_list, previousId); + } + + inline bool operator==(const EntityList::iterator& lhs, const EntityList::iterator& rhs) + { + NazaraAssert(lhs.m_list == rhs.m_list, "Cannot compare iterator coming from different lists"); + + return lhs.m_nextEntityId == rhs.m_nextEntityId; + } + + inline bool operator!=(const EntityList::iterator& lhs, const EntityList::iterator& rhs) + { + return !operator==(lhs, rhs); + } + + inline void swap(EntityList::iterator& lhs, EntityList::iterator& rhs) + { + NazaraAssert(lhs.m_list == rhs.m_list, "Cannot compare iterator coming from different lists"); + + using std::swap; + + std::swap(lhs.m_nextEntityId, rhs.m_nextEntityId); } } diff --git a/SDK/include/NDK/Systems/RenderSystem.hpp b/SDK/include/NDK/Systems/RenderSystem.hpp index 61f7bd0a0..a74cb9d0e 100644 --- a/SDK/include/NDK/Systems/RenderSystem.hpp +++ b/SDK/include/NDK/Systems/RenderSystem.hpp @@ -57,7 +57,7 @@ namespace Ndk std::unique_ptr m_renderTechnique; std::vector m_volumeEntries; - EntityList m_cameras; + std::vector m_cameras; EntityList m_drawables; EntityList m_directionalLights; EntityList m_lights; diff --git a/SDK/include/NDK/World.hpp b/SDK/include/NDK/World.hpp index 9b2546eda..29b9a4a9e 100644 --- a/SDK/include/NDK/World.hpp +++ b/SDK/include/NDK/World.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -28,7 +29,7 @@ namespace Ndk friend Entity; public: - using EntityList = std::vector; + using EntityVector = std::vector; inline World(bool addDefaultSystems = true); World(const World&) = delete; @@ -41,21 +42,21 @@ namespace Ndk template SystemType& AddSystem(Args&&... args); const EntityHandle& CreateEntity(); - inline EntityList CreateEntities(unsigned int count); + inline EntityVector CreateEntities(unsigned int count); void Clear() noexcept; const EntityHandle& CloneEntity(EntityId id); - const EntityHandle& GetEntity(EntityId id); - inline const EntityList& GetEntities(); + inline const EntityHandle& GetEntity(EntityId id); + inline const EntityList& GetEntities() const; inline BaseSystem& GetSystem(SystemIndex index); template SystemType& GetSystem(); inline bool HasSystem(SystemIndex index) const; template bool HasSystem() const; - void KillEntity(Entity* entity); - inline void KillEntities(const EntityList& list); + inline void KillEntity(Entity* entity); + inline void KillEntities(const EntityVector& list); inline bool IsEntityValid(const Entity* entity) const; inline bool IsEntityIdValid(EntityId id) const; @@ -79,19 +80,22 @@ namespace Ndk struct EntityBlock { EntityBlock(Entity&& e) : - entity(std::move(e)) + entity(std::move(e)), + handle(&entity) { } EntityBlock(EntityBlock&& block) = default; Entity entity; - std::size_t aliveIndex; + EntityHandle handle; }; std::vector> m_systems; std::vector m_orderedSystems; std::vector m_entities; + std::vector m_entityBlocks; + std::vector> m_waitingEntities; std::vector m_freeIdList; EntityList m_aliveEntities; Nz::Bitset m_dirtyEntities; diff --git a/SDK/include/NDK/World.inl b/SDK/include/NDK/World.inl index dcdb0c057..d45f0ff1d 100644 --- a/SDK/include/NDK/World.inl +++ b/SDK/include/NDK/World.inl @@ -82,9 +82,9 @@ namespace Ndk * \param count Number of entities to create */ - inline World::EntityList World::CreateEntities(unsigned int count) + inline World::EntityVector World::CreateEntities(unsigned int count) { - EntityList list; + EntityVector list; list.reserve(count); for (unsigned int i = 0; i < count; ++i) @@ -98,7 +98,7 @@ namespace Ndk * \return A constant reference to the entities */ - inline const World::EntityList& World::GetEntities() + inline const EntityList& World::GetEntities() const { return m_aliveEntities; } @@ -164,18 +164,53 @@ namespace Ndk return HasSystem(index); } + /*! + * \brief Marks an entity for deletion + * + * \param Pointer to the entity + * + * \remark If the entity pointer is invalid, nothing is done + * \remark For safety, entities are not killed until the next world update + */ + inline void World::KillEntity(Entity* entity) + { + if (IsEntityValid(entity)) + m_killedEntities.UnboundedSet(entity->GetId(), true); + } + /*! * \brief Kills a set of entities * + * This function has the same effect as calling KillEntity for every entity contained in the vector + * * \param list Set of entities to kill */ - - inline void World::KillEntities(const EntityList& list) + inline void World::KillEntities(const EntityVector& list) { for (const EntityHandle& entity : list) KillEntity(entity); } + /*! + * \brief Gets an entity + * \return A constant reference to a handle of the entity + * + * \param id Identifier of the entity + * + * \remark Handle referenced by this function can move in memory when updating the world, do not keep a reference to a handle from a world update to another + * \remark If an invalid identifier is provided, an error got triggered and an invalid handle is returned + */ + inline const EntityHandle& World::GetEntity(EntityId id) + { + if (IsEntityIdValid(id)) + return m_entityBlocks[id]->handle; + else + { + NazaraError("Invalid ID"); + return EntityHandle::InvalidHandle; + } + } + /*! * \brief Checks whether or not an entity is valid * \return true If it is the case @@ -197,7 +232,7 @@ namespace Ndk inline bool World::IsEntityIdValid(EntityId id) const { - return id < m_entities.size() && m_entities[id].entity.IsValid(); + return id < m_entityBlocks.size() && m_entityBlocks[id]->entity.IsValid(); } /*! @@ -266,10 +301,12 @@ namespace Ndk { m_aliveEntities = std::move(world.m_aliveEntities); m_dirtyEntities = std::move(world.m_dirtyEntities); + m_entityBlocks = std::move(world.m_entityBlocks); m_freeIdList = std::move(world.m_freeIdList); m_killedEntities = std::move(world.m_killedEntities); m_orderedSystems = std::move(world.m_orderedSystems); m_orderedSystemsUpdated = world.m_orderedSystemsUpdated; + m_waitingEntities = std::move(world.m_waitingEntities); m_entities = std::move(world.m_entities); for (EntityBlock& block : m_entities) @@ -284,7 +321,7 @@ namespace Ndk inline void World::Invalidate() { - m_dirtyEntities.Resize(m_entities.size(), false); + m_dirtyEntities.Resize(m_entityBlocks.size(), false); m_dirtyEntities.Set(true); // Activation of all bits } diff --git a/SDK/src/NDK/EntityList.cpp b/SDK/src/NDK/EntityList.cpp new file mode 100644 index 000000000..03fd3afc3 --- /dev/null +++ b/SDK/src/NDK/EntityList.cpp @@ -0,0 +1,14 @@ +// Copyright (C) 2017 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#include +#include + +namespace Ndk +{ + const EntityHandle& EntityList::iterator::operator*() const + { + return m_list->GetWorld()->GetEntity(static_cast(m_nextEntityId)); + } +} diff --git a/SDK/src/NDK/Systems/RenderSystem.cpp b/SDK/src/NDK/Systems/RenderSystem.cpp index f586056a2..44b0d1bba 100644 --- a/SDK/src/NDK/Systems/RenderSystem.cpp +++ b/SDK/src/NDK/Systems/RenderSystem.cpp @@ -48,7 +48,15 @@ namespace Ndk { m_forceRenderQueueInvalidation = true; //< Hackfix until lights and particles are handled by culling list - m_cameras.Remove(entity); + for (auto it = m_cameras.begin(); it != m_cameras.end(); ++it) + { + if (it->GetObject() == entity) + { + m_cameras.erase(it); + break; + } + } + m_directionalLights.Remove(entity); m_drawables.Remove(entity); m_lights.Remove(entity); @@ -74,14 +82,23 @@ namespace Ndk if (entity->HasComponent() && entity->HasComponent()) { - m_cameras.Insert(entity); + m_cameras.emplace_back(entity); std::sort(m_cameras.begin(), m_cameras.end(), [](const EntityHandle& handle1, const EntityHandle& handle2) { return handle1->GetComponent().GetLayer() < handle2->GetComponent().GetLayer(); }); } else - m_cameras.Remove(entity); + { + for (auto it = m_cameras.begin(); it != m_cameras.end(); ++it) + { + if (it->GetObject() == entity) + { + m_cameras.erase(it); + break; + } + } + } if (entity->HasComponent() && entity->HasComponent()) { @@ -181,7 +198,7 @@ namespace Ndk GraphicsComponent& graphicsComponent = drawable->GetComponent(); graphicsComponent.EnsureBoundingVolumeUpdate(); } - + bool forceInvalidation = false; std::size_t visibilityHash = m_drawableCulling.Cull(camComponent.GetFrustum(), &forceInvalidation); diff --git a/SDK/src/NDK/World.cpp b/SDK/src/NDK/World.cpp index cad4660d4..81b862df9 100644 --- a/SDK/src/NDK/World.cpp +++ b/SDK/src/NDK/World.cpp @@ -60,29 +60,51 @@ namespace Ndk const EntityHandle& World::CreateEntity() { EntityId id; + EntityBlock* entBlock; if (!m_freeIdList.empty()) { // We get an identifier id = m_freeIdList.back(); m_freeIdList.pop_back(); + + entBlock = &m_entities[id]; + entBlock->handle.Reset(&entBlock->entity); //< Reset handle (as it was reset when entity got destroyed) + + m_entityBlocks[id] = entBlock; } else { // We allocate a new entity - id = static_cast(m_entities.size()); + id = static_cast(m_entityBlocks.size()); - // We can't use emplace_back due to the scope - m_entities.push_back(Entity(this, id)); + if (m_entities.capacity() > m_entities.size()) + { + NazaraAssert(m_waitingEntities.empty(), "There should be no waiting entities if space is available in main container"); + + m_entities.push_back(Entity(this, id)); //< We can't use emplace_back due to the scope + entBlock = &m_entities.back(); + } + else + { + // Pushing to entities would reallocate vector and thus, invalidate EntityHandles (which we don't want until world update) + // To prevent this, allocate them into a separate vector and move them at update + // For now, we are counting on m_entities grow strategy to keep allocation frequency low + m_waitingEntities.emplace_back(std::make_unique(Entity(this, id))); + entBlock = m_waitingEntities.back().get(); + } + + if (id >= m_entityBlocks.size()) + m_entityBlocks.resize(id + 1); + + m_entityBlocks[id] = entBlock; } - // We initialise the entity and we add it to the list of alive entities - Entity& entity = m_entities[id].entity; - entity.Create(); + // We initialize the entity and we add it to the list of alive entities + entBlock->entity.Create(); - m_aliveEntities.emplace_back(&entity); - m_entities[id].aliveIndex = m_aliveEntities.size() - 1; + m_aliveEntities.Insert(&entBlock->entity); - return m_aliveEntities.back(); + return entBlock->handle; } /*! @@ -96,8 +118,9 @@ namespace Ndk // First, destruction of entities, then handles // This is made to avoid that handle warn uselessly entities before their destruction m_entities.clear(); + m_entityBlocks.clear(); - m_aliveEntities.clear(); + m_aliveEntities.Clear(); m_dirtyEntities.Clear(); m_killedEntities.Clear(); } @@ -120,7 +143,7 @@ namespace Ndk return EntityHandle::InvalidHandle; } - EntityHandle clone = CreateEntity(); + const EntityHandle& clone = CreateEntity(); const Nz::Bitset<>& componentBits = original->GetComponentBits(); for (std::size_t i = componentBits.FindFirst(); i != componentBits.npos; i = componentBits.FindNext(i)) @@ -132,40 +155,6 @@ namespace Ndk return GetEntity(clone->GetId()); } - /*! - * \brief Kills an entity - * - * \param Pointer to the entity - * - * \remark No change is done if entity is invalid - */ - - void World::KillEntity(Entity* entity) - { - if (IsEntityValid(entity)) - m_killedEntities.UnboundedSet(entity->GetId(), true); - } - - /*! - * \brief Gets an entity - * \return A constant reference to the modified entity - * - * \param id Identifier of the entity - * - * \remark Produces a NazaraError if entity identifier is not valid - */ - - const EntityHandle& World::GetEntity(EntityId id) - { - if (IsEntityIdValid(id)) - return m_aliveEntities[m_entities[id].aliveIndex]; - else - { - NazaraError("Invalid ID"); - return EntityHandle::InvalidHandle; - } - } - /*! * \brief Updates the world * @@ -177,45 +166,43 @@ namespace Ndk if (!m_orderedSystemsUpdated) ReorderSystems(); + // Move waiting entities to entity list + if (!m_waitingEntities.empty()) + { + constexpr std::size_t MinEntityCapacity = 10; //< We want to be able to grow maximum entity count by at least ten without going to the waiting list + + m_entities.reserve(m_entities.size() + m_waitingEntities.size() + MinEntityCapacity); + for (auto& blockPtr : m_waitingEntities) + m_entities.push_back(std::move(*blockPtr)); + + m_waitingEntities.clear(); + + // Update entity blocks pointers + for (std::size_t i = 0; i < m_entities.size(); ++i) + m_entityBlocks[i] = &m_entities[i]; + } + // Handle killed entities before last call for (std::size_t i = m_killedEntities.FindFirst(); i != m_killedEntities.npos; i = m_killedEntities.FindNext(i)) { - EntityBlock& block = m_entities[i]; - Entity& entity = block.entity; + NazaraAssert(i < m_entityBlocks.size(), "Entity index out of range"); - NazaraAssert(entity.IsValid(), "Entity must be valid"); + Entity* entity = &m_entityBlocks[i]->entity; // Destruction of the entity (invalidation of handle by the same way) - entity.Destroy(); + entity->Destroy(); // Send back the identifier of the entity to the free queue - m_freeIdList.push_back(entity.GetId()); - - // We take out the handle from the list of alive entities - // With the idiom swap and pop - - NazaraAssert(block.aliveIndex < m_aliveEntities.size(), "Alive index out of range"); - - if (block.aliveIndex < m_aliveEntities.size() - 1) // If it's not the last handle - { - EntityHandle& lastHandle = m_aliveEntities.back(); - EntityHandle& myHandle = m_aliveEntities[block.aliveIndex]; - - myHandle = std::move(lastHandle); - - // We don't forget to update the index associated to the entity - m_entities[myHandle->GetId()].aliveIndex = block.aliveIndex; - } - m_aliveEntities.pop_back(); + m_freeIdList.push_back(entity->GetId()); } m_killedEntities.Reset(); // Handle of entities which need an update from the systems for (std::size_t i = m_dirtyEntities.FindFirst(); i != m_dirtyEntities.npos; i = m_dirtyEntities.FindNext(i)) { - NazaraAssert(i < m_entities.size(), "Entity index out of range"); + NazaraAssert(i < m_entityBlocks.size(), "Entity index out of range"); - Entity* entity = &m_entities[i].entity; + Entity* entity = &m_entityBlocks[i]->entity; // Check entity validity (as it could have been reported as dirty and killed during the same iteration) if (!entity->IsValid()) @@ -242,7 +229,7 @@ namespace Ndk } else { - // No, it shouldn't, remove it if it's part of the system + // No it shouldn't, remove it if it's part of the system if (partOfSystem) system->RemoveEntity(entity); } diff --git a/tests/SDK/NDK/BaseSystem.cpp b/tests/SDK/NDK/BaseSystem.cpp index a202c1f70..3bb0fa9e4 100644 --- a/tests/SDK/NDK/BaseSystem.cpp +++ b/tests/SDK/NDK/BaseSystem.cpp @@ -38,7 +38,7 @@ SCENARIO("BaseSystem", "[NDK][BASESYSTEM]") WHEN("We add an entity") { - const Ndk::EntityHandle& entity = world.CreateEntity(); + Ndk::EntityHandle entity = world.CreateEntity(); entity->AddComponent(); THEN("System should have it") @@ -50,7 +50,7 @@ SCENARIO("BaseSystem", "[NDK][BASESYSTEM]") WHEN("We add an entity with excluded component") { - const Ndk::EntityHandle& entity = world.CreateEntity(); + Ndk::EntityHandle entity = world.CreateEntity(); entity->AddComponent(); entity->AddComponent(); diff --git a/tests/SDK/NDK/Entity.cpp b/tests/SDK/NDK/Entity.cpp index cf02370ec..dd3216fb0 100644 --- a/tests/SDK/NDK/Entity.cpp +++ b/tests/SDK/NDK/Entity.cpp @@ -57,18 +57,18 @@ SCENARIO("Entity", "[NDK][ENTITY]") { Ndk::World world; Ndk::BaseSystem& system = world.AddSystem(); - const Ndk::EntityHandle& entity = world.CreateEntity(); + Ndk::EntityHandle entity = world.CreateEntity(); WHEN("We add our UpdateComponent") { UpdatableComponent& updatableComponent = entity->AddComponent(); - REQUIRE(!updatableComponent.IsUpdated()); + CHECK(!updatableComponent.IsUpdated()); THEN("Update the world should update the entity's component") { world.Update(1.f); UpdatableComponent& updatableComponentGet = entity->GetComponent(); - REQUIRE(updatableComponentGet.IsUpdated()); + CHECK(updatableComponentGet.IsUpdated()); } THEN("Update the world should not update the entity's component if it's disabled") @@ -76,14 +76,14 @@ SCENARIO("Entity", "[NDK][ENTITY]") entity->Enable(false); world.Update(1.f); UpdatableComponent& updatableComponentGet = entity->GetComponent(); - REQUIRE(!updatableComponentGet.IsUpdated()); + CHECK(!updatableComponentGet.IsUpdated()); } THEN("We can remove its component") { entity->RemoveComponent(Ndk::GetComponentIndex()); world.Update(1.f); - REQUIRE(!entity->HasComponent()); + CHECK(!entity->HasComponent()); } } @@ -94,7 +94,7 @@ SCENARIO("Entity", "[NDK][ENTITY]") THEN("It's no more valid") { - REQUIRE(!world.IsEntityValid(entity)); + CHECK(!world.IsEntityValid(entity)); } } } diff --git a/tests/SDK/NDK/EntityOwner.cpp b/tests/SDK/NDK/EntityOwner.cpp index 464dce4d6..f2fd76d58 100644 --- a/tests/SDK/NDK/EntityOwner.cpp +++ b/tests/SDK/NDK/EntityOwner.cpp @@ -7,7 +7,7 @@ SCENARIO("EntityOwner", "[NDK][ENTITYOWNER]") GIVEN("A world & an entity") { Ndk::World world; - const Ndk::EntityHandle& entity = world.CreateEntity(); + Ndk::EntityHandle entity = world.CreateEntity(); WHEN("We set the ownership of the entity to our owner") { @@ -15,14 +15,14 @@ SCENARIO("EntityOwner", "[NDK][ENTITYOWNER]") THEN("Entity is still valid") { - REQUIRE(entity.IsValid()); + CHECK(entity.IsValid()); } THEN("Resetting or getting out of scope is no more valid") { entityOwner.Reset(); world.Update(1.f); - REQUIRE(!entity.IsValid()); + CHECK(!entity.IsValid()); } THEN("Moving an entity owner works") diff --git a/tests/SDK/NDK/World.cpp b/tests/SDK/NDK/World.cpp index 85ee241b4..d46b6621b 100644 --- a/tests/SDK/NDK/World.cpp +++ b/tests/SDK/NDK/World.cpp @@ -79,7 +79,7 @@ SCENARIO("World", "[NDK][WORLD]") AND_WHEN("We update our world with our entity") { REQUIRE(&world.GetSystem(UpdateSystem::systemIndex) == &world.GetSystem()); - const Ndk::EntityHandle& entity = world.CreateEntity(); + Ndk::EntityHandle entity = world.CreateEntity(); UpdatableComponent& component = entity->AddComponent(); THEN("Our entity component must be updated")