diff --git a/include/Engine/Core/ComponentInfo.h b/include/Engine/Core/ComponentInfo.h index e7f57faf..e3b77d1d 100644 --- a/include/Engine/Core/ComponentInfo.h +++ b/include/Engine/Core/ComponentInfo.h @@ -5,12 +5,6 @@ #include "Entity.h" #include -enum class DirtySet -{ - Transform, - Network -}; - struct ComponentInfo { typedef int EnumType; diff --git a/include/Engine/Core/ComponentPool.h b/include/Engine/Core/ComponentPool.h index 84b38d29..9bbd742f 100644 --- a/include/Engine/Core/ComponentPool.h +++ b/include/Engine/Core/ComponentPool.h @@ -5,16 +5,15 @@ #include "MemoryPool.h" #include "ComponentInfo.h" #include "ComponentWrapper.h" +#include "DirtySet.h" + +class ComponentPool; class ComponentPoolForwardIterator : public std::iterator { public: - ComponentPoolForwardIterator(const ComponentInfo& componentInfo, const MemoryPool::iterator begin, const MemoryPool::iterator end) - : m_ComponentInfo(componentInfo) - , m_MemoryPoolIterator(begin) - , m_MemoryPoolEnd(end) - { } + ComponentPoolForwardIterator(ComponentPool* pool); ComponentPoolForwardIterator(const ComponentPoolForwardIterator& other) = default; ComponentPoolForwardIterator(ComponentPoolForwardIterator&& other) = default; @@ -27,6 +26,7 @@ public: ComponentWrapper operator*() const; private: + ComponentPool* m_ComponentPool; const ComponentInfo& m_ComponentInfo; MemoryPool::iterator m_MemoryPoolIterator; const MemoryPool::iterator m_MemoryPoolEnd; @@ -34,6 +34,7 @@ private: class ComponentPool { + friend class ComponentPoolForwardIterator; public: typedef ComponentPoolForwardIterator iterator; typedef ptrdiff_t difference_type; @@ -41,6 +42,7 @@ public: typedef ComponentWrapper value_type; typedef ComponentWrapper* pointer; typedef ComponentWrapper& reference; + typedef std::unordered_map> DirtySet_t; ComponentPool(const ::ComponentInfo& ci) : m_ComponentInfo(ci) @@ -61,8 +63,8 @@ public: // Delete a component and free its memory void Delete(ComponentWrapper& wrapper); - iterator begin() const; - iterator end() const; + iterator begin(); + iterator end(); size_t size() const; //Dumps information about what the pool memory looks like right now @@ -80,6 +82,7 @@ private: ::ComponentInfo m_ComponentInfo; MemoryPool m_Pool; std::unordered_map m_EntityToComponent; + DirtySet m_DirtySet; }; #endif \ No newline at end of file diff --git a/include/Engine/Core/ComponentWrapper.h b/include/Engine/Core/ComponentWrapper.h index 0d7c922e..b75e1a00 100644 --- a/include/Engine/Core/ComponentWrapper.h +++ b/include/Engine/Core/ComponentWrapper.h @@ -6,42 +6,52 @@ #include "../Common.h" #include "Entity.h" #include "ComponentInfo.h" +#include "DirtySet.h" #include "Util/Any.h" -template -struct ComponentField { }; - -template -struct ComponentField::value>::type> -{ - static T& Get(const ComponentInfo::Field_t& info, char* data) { return *reinterpret_cast(data); } - static void Set(const ComponentInfo::Field_t& info, char* data, const T& value) { Get(data) = value; } -}; - -template <> -struct ComponentField -{ - static std::string& Get(const ComponentInfo::Field_t& info, char* data) { return **reinterpret_cast(data); } - static void Set(const ComponentInfo::Field_t& info, char* data, const std::string& value) { Get(info, data) = value; } -}; - struct ComponentWrapper { - ComponentWrapper(const ComponentInfo& componentInfo, char* data) + ComponentWrapper(const ComponentInfo& componentInfo, char* data, ::DirtyBitField* dirtyBitField) : Info(componentInfo) , EntityID(*reinterpret_cast<::EntityID*>(data)) , Data(data + componentInfo.GetHeaderSize()) + , DirtyBitField(dirtyBitField) { } const ComponentInfo& Info; const ::EntityID EntityID; char* Data; + ::DirtyBitField* DirtyBitField = nullptr; ComponentInfo::EnumType Enum(const char* fieldName, const char* enumKey) { return Info.Meta->FieldEnumDefinitions.at(fieldName).at(enumKey); } + bool Dirty(DirtySetType type, const std::string& fieldName) + { + if (DirtyBitField == nullptr) { + return true; + } else { + auto& field = Info.Fields.at(fieldName); + return DirtyBitField->operator[](type).count(field.Index) == 1; + } + } + + void SetDirty(DirtySetType type, const std::string& fieldName, bool dirty = true) + { + if (DirtyBitField == nullptr) { + return; + } + + auto& field = Info.Fields.at(fieldName); + if (dirty) { + DirtyBitField->operator[](type).insert(field.Index); + } else { + DirtyBitField->operator[](type).erase(field.Index); + } + } + template T& Field(const std::string& name) { @@ -96,32 +106,32 @@ struct ComponentWrapper { friend struct ComponentWrapper; private: - SubscriptProxy(ComponentWrapper* component, std::string propertyName) + SubscriptProxy(ComponentWrapper* component, std::string fieldName) : m_Component(component) - , m_PropertyName(propertyName) + , m_FieldName(fieldName) { } ComponentWrapper* m_Component; - std::string m_PropertyName; + std::string m_FieldName; public: // Return the integer value of an enum type key for this field - ComponentInfo::EnumType Enum(const char* enumKey) { return m_Component->Enum(m_PropertyName.c_str(), enumKey); } - bool Dirty(DirtySet set) { return true; } - void SetDirty(bool dirty = true) { } + ComponentInfo::EnumType Enum(const char* enumKey) { return m_Component->Enum(m_FieldName.c_str(), enumKey); } + bool Dirty(DirtySetType type) { return m_Component->Dirty(type, m_FieldName); } + void SetDirty(DirtySetType type, bool dirty = true) { m_Component->SetDirty(type, m_FieldName, dirty); } template - operator T&() { return m_Component->Field(m_PropertyName); } + operator T&() { return m_Component->Field(m_FieldName); } template - void operator=(const T val) { m_Component->SetField(m_PropertyName, val); } + void operator=(const T val) { m_Component->SetField(m_FieldName, val); } // TODO: Pass by reference and rvalue (universal reference?) //template //void operator=(T& val) { m_Component->SetField(m_PropertyName, val); } // Specialization for string literals template - void operator=(const char(&val)[N]) { m_Component->SetField(m_PropertyName, val); } + void operator=(const char(&val)[N]) { m_Component->SetField(m_FieldName, val); } }; SubscriptProxy operator[](const std::string& propertyName) { return SubscriptProxy(this, propertyName); } }; @@ -130,7 +140,7 @@ struct ComponentWrapper struct SharedComponentWrapper : ComponentWrapper { SharedComponentWrapper(const ComponentInfo& componentInfo, boost::shared_array data) - : ComponentWrapper(componentInfo, data.get()) + : ComponentWrapper(componentInfo, data.get(), nullptr) , m_DataReference(data) { } diff --git a/include/Engine/Core/DirtySet.h b/include/Engine/Core/DirtySet.h new file mode 100644 index 00000000..53885a7a --- /dev/null +++ b/include/Engine/Core/DirtySet.h @@ -0,0 +1,16 @@ +#ifndef DirtySet_h__ +#define DirtySet_h__ + +#include +#include "ComponentInfo.h" + +enum class DirtySetType +{ + Transform, + Network +}; + +typedef std::unordered_map> DirtyBitField; +typedef std::unordered_map DirtySet; + +#endif \ No newline at end of file diff --git a/include/Engine/Core/SystemPipeline.h b/include/Engine/Core/SystemPipeline.h index c4801fde..df6adc9f 100644 --- a/include/Engine/Core/SystemPipeline.h +++ b/include/Engine/Core/SystemPipeline.h @@ -81,7 +81,7 @@ public: for (auto& pair : group.PureSystems) { const std::string& componentName = pair.first; auto& systems = pair.second; - const ComponentPool* pool = m_World->GetComponents(componentName); + ComponentPool* pool = m_World->GetComponents(componentName); if (pool == nullptr) { continue; } diff --git a/include/Engine/Core/World.h b/include/Engine/Core/World.h index 9ae38021..dd7f2887 100644 --- a/include/Engine/Core/World.h +++ b/include/Engine/Core/World.h @@ -35,7 +35,7 @@ public: // Delete a component off an entity void DeleteComponent(EntityID entity, const std::string& componentType); // Get all components of the specified type - const ComponentPool* GetComponents(const std::string& componentType); + ComponentPool* GetComponents(const std::string& componentType); // Get entity parent EntityID GetParent(EntityID entity); // Change the parent of an entity diff --git a/src/Engine/Core/ComponentPool.cpp b/src/Engine/Core/ComponentPool.cpp index 059b2e38..f3526469 100644 --- a/src/Engine/Core/ComponentPool.cpp +++ b/src/Engine/Core/ComponentPool.cpp @@ -1,9 +1,17 @@ #include "Core/ComponentPool.h" +ComponentPoolForwardIterator::ComponentPoolForwardIterator(ComponentPool* pool) + : m_ComponentPool(pool) + , m_ComponentInfo(pool->m_ComponentInfo) + , m_MemoryPoolIterator(pool->m_Pool.begin()) + , m_MemoryPoolEnd(pool->m_Pool.end()) +{ } + ComponentWrapper ComponentPoolForwardIterator::operator*() const { char* data = &(*m_MemoryPoolIterator); - ComponentWrapper wrapper(m_ComponentInfo, data); + EntityID entity = *reinterpret_cast(data); + ComponentWrapper wrapper(m_ComponentInfo, data, &m_ComponentPool->m_DirtySet[entity]); return wrapper; } @@ -71,7 +79,7 @@ ComponentWrapper ComponentPool::Allocate(EntityID entity) memcpy(data, &entity, sizeof(EntityID)); m_EntityToComponent[entity] = data; - ComponentWrapper component(m_ComponentInfo, data); + ComponentWrapper component(m_ComponentInfo, data, &m_DirtySet[entity]); // Copy defaults memcpy(component.Data, m_ComponentInfo.Defaults.get(), m_ComponentInfo.Stride); @@ -82,7 +90,9 @@ ComponentWrapper ComponentPool::Allocate(EntityID entity) ComponentWrapper ComponentPool::GetByEntity(EntityID ent) { - return ComponentWrapper(m_ComponentInfo, m_EntityToComponent.at(ent)); + auto data = m_EntityToComponent.at(ent); + auto bitField = &m_DirtySet[ent]; + return ComponentWrapper(m_ComponentInfo, data, bitField); } bool ComponentPool::KnowsEntity(EntityID ent) @@ -95,16 +105,17 @@ void ComponentPool::Delete(ComponentWrapper& wrapper) ComponentWrapper::Destroy(wrapper.Info, wrapper.Data); m_EntityToComponent.erase(wrapper.EntityID); m_Pool.Free(wrapper.Data - sizeof(EntityID)); + m_DirtySet.erase(wrapper.EntityID); } -ComponentPool::iterator ComponentPool::begin() const +ComponentPool::iterator ComponentPool::begin() { - return iterator(m_ComponentInfo, m_Pool.begin(), m_Pool.end()); + return iterator(this); } -ComponentPool::iterator ComponentPool::end() const +ComponentPool::iterator ComponentPool::end() { - return iterator(m_ComponentInfo, m_Pool.end(), m_Pool.end()); + return iterator(this); } size_t ComponentPool::size() const diff --git a/src/Engine/Core/World.cpp b/src/Engine/Core/World.cpp index 0952a53b..b8af4b62 100644 --- a/src/Engine/Core/World.cpp +++ b/src/Engine/Core/World.cpp @@ -95,7 +95,7 @@ void World::DeleteComponent(EntityID entity, const std::string& componentType) } } -const ComponentPool* World::GetComponents(const std::string& componentType) +ComponentPool* World::GetComponents(const std::string& componentType) { auto it = m_ComponentPools.find(componentType); return (it != m_ComponentPools.end()) ? it->second : nullptr; diff --git a/src/Game/Systems/PlayerMovementSystem.cpp b/src/Game/Systems/PlayerMovementSystem.cpp index b927856a..77e58918 100644 --- a/src/Game/Systems/PlayerMovementSystem.cpp +++ b/src/Game/Systems/PlayerMovementSystem.cpp @@ -29,7 +29,7 @@ void PlayerMovementSystem::Update(double dt) return; } m_SprintEffectTimer = 0.f; - const ComponentPool* pool = m_World->GetComponents("SprintAbility"); + auto pool = m_World->GetComponents("SprintAbility"); if (pool == nullptr) { return; } diff --git a/src/Game/Systems/SpawnerSystem.cpp b/src/Game/Systems/SpawnerSystem.cpp index 282c958b..6e166947 100644 --- a/src/Game/Systems/SpawnerSystem.cpp +++ b/src/Game/Systems/SpawnerSystem.cpp @@ -86,7 +86,7 @@ bool SpawnerSystem::spawnedEntityIsColliding(EntityWrapper spawnedEntity, Entity transformEntityToSpawnPoint(spawnedEntity, spawnPoint); //Check if the spawned entity collides with anything, and if so, continue to the next spawnpoint. EntityAABB spawnedBox = *Collision::EntityAbsoluteAABB(spawnedEntity); - const ComponentPool* otherSpawnedEntities = spawnPoint.World->GetComponents(dontCollideComponent); + auto otherSpawnedEntities = spawnPoint.World->GetComponents(dontCollideComponent); for (const auto& obj : *otherSpawnedEntities) { if (spawnedEntity.ID == obj.EntityID) { continue;