diff --git a/SDK/include/NDK/Algorithm.hpp b/SDK/include/NDK/Algorithm.hpp new file mode 100644 index 000000000..68d4711d4 --- /dev/null +++ b/SDK/include/NDK/Algorithm.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2015 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 + +#pragma once + +#ifndef NDK_ALGORITHM_HPP +#define NDK_ALGORITHM_HPP + +#include + +namespace Ndk +{ + template ComponentId BuildComponentId(const char (&id)[N]); + template constexpr ComponentId GetComponentId(); +} + +#include + +#endif // NDK_ALGORITHM_HPP diff --git a/SDK/include/NDK/Algorithm.inl b/SDK/include/NDK/Algorithm.inl new file mode 100644 index 000000000..dbe2b292d --- /dev/null +++ b/SDK/include/NDK/Algorithm.inl @@ -0,0 +1,27 @@ +// Copyright (C) 2015 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 + +namespace Ndk +{ + ///TODO: constexpr avec le C++14 + template + ComponentId BuildComponentId(const char (&id)[N]) + { + static_assert(N-1 <= sizeof(ComponentId), "ID too long for this size of component id"); + + ComponentId componentId = 0; + for (int i = 0; i < N; ++i) + componentId |= static_cast(id[i]) << i*8; + + return componentId; + } + + template + constexpr ComponentId GetComponentId() + { + return ComponentType::ComponentId; + } +} diff --git a/SDK/include/NDK/BaseComponent.hpp b/SDK/include/NDK/BaseComponent.hpp index a3a4a08fb..a77514d64 100644 --- a/SDK/include/NDK/BaseComponent.hpp +++ b/SDK/include/NDK/BaseComponent.hpp @@ -14,20 +14,15 @@ namespace Ndk class NDK_API BaseComponent { public: - BaseComponent(nzUInt32 componentId); + BaseComponent(ComponentId componentId); virtual ~BaseComponent(); virtual BaseComponent* Clone() const = 0; - nzUInt32 GetId() const; - - static nzUInt32 GetNextId(); + ComponentId GetId() const; protected: - nzUInt32 m_componentId; - - private: - static nzUInt32 s_nextId; + ComponentId m_componentId; }; } diff --git a/SDK/include/NDK/BaseComponent.inl b/SDK/include/NDK/BaseComponent.inl index cea669f60..f0262f042 100644 --- a/SDK/include/NDK/BaseComponent.inl +++ b/SDK/include/NDK/BaseComponent.inl @@ -4,18 +4,13 @@ namespace Ndk { - inline BaseComponent::BaseComponent(nzUInt32 componentId) : + inline BaseComponent::BaseComponent(ComponentId componentId) : m_componentId(componentId) { } - inline nzUInt32 BaseComponent::GetId() const + inline ComponentId BaseComponent::GetId() const { return m_componentId; } - - inline nzUInt32 BaseComponent::GetNextId() - { - return s_nextId++; - } } diff --git a/SDK/include/NDK/Component.hpp b/SDK/include/NDK/Component.hpp index d1b96d279..09f23a281 100644 --- a/SDK/include/NDK/Component.hpp +++ b/SDK/include/NDK/Component.hpp @@ -20,8 +20,6 @@ namespace Ndk BaseComponent* Clone() const override; }; - - template constexpr nzUInt32 GetComponentId(); } #include diff --git a/SDK/include/NDK/Component.inl b/SDK/include/NDK/Component.inl index 357c7f594..4a860dfd7 100644 --- a/SDK/include/NDK/Component.inl +++ b/SDK/include/NDK/Component.inl @@ -2,6 +2,7 @@ // 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 @@ -23,10 +24,4 @@ namespace Ndk return new ComponentType(static_cast(*this)); } - - template - constexpr nzUInt32 GetComponentId() - { - return ComponentType::ComponentId; - } } diff --git a/SDK/include/NDK/Entity.hpp b/SDK/include/NDK/Entity.hpp index 28e539d93..d15c46625 100644 --- a/SDK/include/NDK/Entity.hpp +++ b/SDK/include/NDK/Entity.hpp @@ -11,6 +11,7 @@ #include #include #include +#include namespace Ndk { @@ -23,8 +24,6 @@ namespace Ndk friend World; public: - using Id = nzUInt32; - Entity(const Entity&) = delete; Entity(Entity&& entity); ~Entity(); @@ -34,13 +33,12 @@ namespace Ndk EntityHandle CreateHandle(); - BaseComponent& GetComponent(nzUInt32 componentId); + BaseComponent& GetComponent(ComponentId componentId); template ComponentType& GetComponent(); - const NzBitset<>& GetComponentBits() const; - Id GetId() const; + EntityId GetId() const; World* GetWorld() const; - bool HasComponent(nzUInt32 componentId) const; + bool HasComponent(ComponentId componentId) const; template bool HasComponent() const; void Kill(); @@ -48,14 +46,14 @@ namespace Ndk bool IsValid() const; void RemoveAllComponents(); - void RemoveComponent(nzUInt32 componentId); + void RemoveComponent(ComponentId componentId); template void RemoveComponent(); Entity& operator=(const Entity&) = delete; Entity& operator=(Entity&&) = delete; private: - Entity(World& world, Id id); + Entity(World& world, EntityId id); void Create(); void Destroy(); @@ -63,10 +61,9 @@ namespace Ndk void RegisterHandle(EntityHandle* handle); void UnregisterHandle(EntityHandle* handle); - std::vector> m_components; std::vector m_handles; - Id m_id; - NzBitset<> m_componentBits; + std::unordered_map> m_components; + EntityId m_id; World* m_world; bool m_valid; }; diff --git a/SDK/include/NDK/Entity.inl b/SDK/include/NDK/Entity.inl index 3dbe0d65f..0f6489b24 100644 --- a/SDK/include/NDK/Entity.inl +++ b/SDK/include/NDK/Entity.inl @@ -9,7 +9,7 @@ namespace Ndk { - inline Entity::Entity(World& world, Id id) : + inline Entity::Entity(World& world, EntityId id) : m_id(id), m_world(&world) { @@ -25,7 +25,7 @@ namespace Ndk return static_cast(AddComponent(std::move(ptr))); } - inline BaseComponent& Entity::GetComponent(nzUInt32 componentId) + inline BaseComponent& Entity::GetComponent(ComponentId componentId) { ///DOC: Le component doit être présent NazaraAssert(HasComponent(componentId), "This component is not part of the entity"); @@ -42,16 +42,11 @@ namespace Ndk ///DOC: Le component doit être présent static_assert(std::is_base_of(), "ComponentType is not a component"); - nzUInt32 componentId = GetComponentId(); + ComponentId componentId = GetComponentId(); return static_cast(GetComponent(componentId)); } - inline const NzBitset<>& Entity::GetComponentBits() const - { - return m_componentBits; - } - - inline Entity::Id Entity::GetId() const + inline EntityId Entity::GetId() const { return m_id; } @@ -61,9 +56,9 @@ namespace Ndk return m_world; } - inline bool Entity::HasComponent(nzUInt32 componentId) const + inline bool Entity::HasComponent(ComponentId componentId) const { - return m_components.size() > componentId && m_components[componentId]; + return m_components.count(componentId) > 0; } template @@ -71,7 +66,7 @@ namespace Ndk { static_assert(std::is_base_of(), "ComponentType is not a component"); - nzUInt32 componentId = GetComponentId(); + ComponentId componentId = GetComponentId(); return HasComponent(componentId); } @@ -80,7 +75,7 @@ namespace Ndk { static_assert(std::is_base_of(), "ComponentType is not a component"); - nzUInt32 componentId = GetComponentId(); + ComponentId componentId = GetComponentId(); RemoveComponent(componentId); } diff --git a/SDK/include/NDK/Prerequesites.hpp b/SDK/include/NDK/Prerequesites.hpp index d42c75dc0..f83ef5506 100644 --- a/SDK/include/NDK/Prerequesites.hpp +++ b/SDK/include/NDK/Prerequesites.hpp @@ -55,4 +55,10 @@ #define NDK_API #endif +namespace Ndk +{ + using ComponentId = nzUInt32; + using EntityId = nzUInt32; +} + #endif // NDK_PREREQUESITES_HPP diff --git a/SDK/include/NDK/World.hpp b/SDK/include/NDK/World.hpp index 41fab8989..6b21469dc 100644 --- a/SDK/include/NDK/World.hpp +++ b/SDK/include/NDK/World.hpp @@ -29,13 +29,13 @@ namespace Ndk void Clear(); - const EntityHandle& GetEntity(Entity::Id id); + const EntityHandle& GetEntity(EntityId id); void KillEntity(const EntityHandle& entity); void KillEntities(const EntityList& list); bool IsEntityValid(const EntityHandle& entity) const; - bool IsEntityIdValid(Entity::Id id) const; + bool IsEntityIdValid(EntityId id) const; void Update(); @@ -51,7 +51,7 @@ namespace Ndk unsigned int aliveIndex; }; - std::vector m_freeIdList; + std::vector m_freeIdList; std::vector m_entities; EntityList m_aliveEntities; NzBitset m_killedEntities; diff --git a/SDK/include/NDK/World.inl b/SDK/include/NDK/World.inl index c252f5e57..7ecc6d0ae 100644 --- a/SDK/include/NDK/World.inl +++ b/SDK/include/NDK/World.inl @@ -28,7 +28,7 @@ namespace Ndk return entity.IsValid() && entity->GetWorld() == this && IsEntityIdValid(entity->GetId()); } - inline bool World::IsEntityIdValid(Entity::Id id) const + inline bool World::IsEntityIdValid(EntityId id) const { return id < m_entities.size() && m_entities[id].entity.IsValid(); } diff --git a/SDK/src/NDK/BaseComponent.cpp b/SDK/src/NDK/BaseComponent.cpp index f3bbaaa19..f227f40a0 100644 --- a/SDK/src/NDK/BaseComponent.cpp +++ b/SDK/src/NDK/BaseComponent.cpp @@ -7,6 +7,4 @@ namespace Ndk { BaseComponent::~BaseComponent() = default; - - nzUInt32 BaseComponent::s_nextId = 0; } diff --git a/SDK/src/NDK/Entity.cpp b/SDK/src/NDK/Entity.cpp index c84ec83c7..e3e499f6a 100644 --- a/SDK/src/NDK/Entity.cpp +++ b/SDK/src/NDK/Entity.cpp @@ -27,18 +27,10 @@ namespace Ndk { NazaraAssert(component, "Component must be valid"); - nzUInt32 componentId = component->GetId(); - - // Nous supprimons l'ancien component, s'il existe - RemoveComponent(componentId); - - // Nous nous assurons que le vecteur de component est suffisamment grand pour contenir le nouveau component - if (m_components.size() <= componentId) - m_components.resize(componentId + 1); + ComponentId componentId = component->GetId(); // Affectation et retour du component m_components[componentId] = std::move(component); - m_componentBits.UnboundedSet(componentId); return *m_components[componentId].get(); } @@ -61,17 +53,13 @@ namespace Ndk void Entity::RemoveAllComponents() { m_components.clear(); - m_componentBits.Clear(); } - void Entity::RemoveComponent(nzUInt32 componentId) + void Entity::RemoveComponent(ComponentId componentId) { ///DOC: N'a aucun effet si le component n'est pas présent if (HasComponent(componentId)) - { m_components[componentId].reset(); - m_componentBits.Reset(componentId); - } } void Entity::Create() diff --git a/SDK/src/NDK/World.cpp b/SDK/src/NDK/World.cpp index 7e2984de5..a04c1f4d2 100644 --- a/SDK/src/NDK/World.cpp +++ b/SDK/src/NDK/World.cpp @@ -15,7 +15,7 @@ namespace Ndk EntityHandle World::CreateEntity() { - Entity::Id id; + EntityId id; if (!m_freeIdList.empty()) { // On récupère un identifiant @@ -62,7 +62,7 @@ namespace Ndk m_killedEntities.UnboundedSet(entity->GetId(), true); } - const EntityHandle& World::GetEntity(Entity::Id id) + const EntityHandle& World::GetEntity(EntityId id) { if (IsEntityIdValid(id)) return m_aliveEntities[m_entities[id].aliveIndex];