Basic and working entity system with a really ugly test case

This commit is contained in:
sippeangelo
2015-12-03 15:07:35 +01:00
parent c7430a9338
commit d3d2e594fe
11 changed files with 286 additions and 135 deletions
+10 -1
View File
@@ -16,7 +16,16 @@ struct ComponentInfo
std::unordered_map<std::string, std::string> FieldTypes; std::unordered_map<std::string, std::string> FieldTypes;
std::unordered_map<std::string, unsigned int> FieldOffsets; std::unordered_map<std::string, unsigned int> FieldOffsets;
Meta_t Meta; Meta_t Meta;
std::shared_ptr<char*> Defaults = nullptr; std::shared_ptr<char> Defaults = nullptr;
};
template<>
struct std::hash<ComponentInfo>
{
inline std::size_t operator()(const ComponentInfo& v) const
{
return std::hash<std::string>()(v.Name);
}
}; };
#endif #endif
+24 -69
View File
@@ -9,51 +9,21 @@ class ComponentPoolForwardIterator
: public std::iterator<std::forward_iterator_tag, ComponentWrapper> : public std::iterator<std::forward_iterator_tag, ComponentWrapper>
{ {
public: public:
ComponentPoolForwardIterator(const ComponentInfo& componentInfo, const MemoryPool<char>::iterator begin, const MemoryPool<char>::iterator end) ComponentPoolForwardIterator(const ComponentInfo& componentInfo, const MemoryPool<char>::iterator begin, const MemoryPool<char>::iterator end)
: m_ComponentInfo(componentInfo) : m_ComponentInfo(componentInfo)
, m_MemoryPoolIterator(begin) , m_MemoryPoolIterator(begin)
, m_MemoryPoolEnd(end) , m_MemoryPoolEnd(end)
{ } { }
ComponentPoolForwardIterator(const ComponentPoolForwardIterator& other) = default; ComponentPoolForwardIterator(const ComponentPoolForwardIterator& other) = default;
ComponentPoolForwardIterator(ComponentPoolForwardIterator&& other) = default; ComponentPoolForwardIterator(ComponentPoolForwardIterator&& other) = default;
~ComponentPoolForwardIterator() = default; ~ComponentPoolForwardIterator() = default;
ComponentPoolForwardIterator& operator= (const ComponentPoolForwardIterator& other) = default; ComponentPoolForwardIterator& operator=(const ComponentPoolForwardIterator& other) = default;
ComponentPoolForwardIterator& operator++();
ComponentPoolForwardIterator& operator++() ComponentPoolForwardIterator& operator++(int);
{ bool operator!=(const ComponentPoolForwardIterator& other) const;
++m_MemoryPoolIterator; bool operator==(const ComponentPoolForwardIterator& other) const;
return *this; ComponentWrapper operator*() const;
}
ComponentPoolForwardIterator& operator++(int)
{
ComponentPoolForwardIterator copyIter(*this);
operator++();
return copyIter;
}
bool operator!= (const ComponentPoolForwardIterator& other) const
{
return m_MemoryPoolIterator != other.m_MemoryPoolIterator;
}
bool operator== (const ComponentPoolForwardIterator& other) const
{
return m_MemoryPoolIterator == other.m_MemoryPoolIterator;
}
ComponentWrapper operator* () const
{
char* data = &(*m_MemoryPoolIterator);
ComponentWrapper wrapper(m_ComponentInfo, data);
return wrapper;
}
//ComponentWrapper* operator-> () const
//{
// return &(*m_MemoryPoolIterator);
//}
private: private:
const ComponentInfo& m_ComponentInfo; const ComponentInfo& m_ComponentInfo;
@@ -71,55 +41,40 @@ public:
typedef ComponentWrapper* pointer; typedef ComponentWrapper* pointer;
typedef ComponentWrapper& reference; typedef ComponentWrapper& reference;
ComponentPool(const ComponentInfo& ci) ComponentPool(const ::ComponentInfo& ci)
: m_ComponentInfo(ci) : m_ComponentInfo(ci)
, m_Pool(ci.Meta.Allocation, ci.Meta.Stride) , m_Pool(ci.Meta.Allocation, sizeof(EntityID) + ci.Meta.Stride)
{ } { }
ComponentPool(const ComponentPool& other) = delete; ComponentPool(const ComponentPool& other) = delete;
ComponentPool(const ComponentPool&& other) = delete; ComponentPool(const ComponentPool&& other) = delete;
ComponentWrapper New() const ::ComponentInfo& ComponentInfo() const { return m_ComponentInfo; }
{
char* data = m_Pool.Allocate();
return ComponentWrapper(m_ComponentInfo, data);
}
void Delete(ComponentWrapper& component) // Allocate space for a component and store which entity it belongs to in internal structure
{ ComponentWrapper Allocate(EntityID entity);
m_Pool.Free(component.Data); // Get the component belonging to a specific entity
} ComponentWrapper GetByEntity(EntityID ent);
// Delete a component and free its memory
void Delete(ComponentWrapper& wrapper);
iterator begin() const iterator begin() const;
{ iterator end() const;
return iterator(m_ComponentInfo, m_Pool.begin(), m_Pool.end());
}
iterator end() const
{
return iterator(m_ComponentInfo, m_Pool.end(), m_Pool.end());
}
//Dumps information about what the pool memory looks like right now //Dumps information about what the pool memory looks like right now
//into an output stream (e.g. file/std::cout, anything that has an operator<<) //into an output stream (e.g. file/std::cout, anything that has an operator<<)
//Interpret the data in the memory as InterpretType. //Interpret the data in the memory as InterpretType.
template <typename InterpretType = char, typename OutStream> template <typename InterpretType = char, typename OutStream>
void Dump(OutStream& out) const void Dump(OutStream& out) const;
{
m_Pool.Dump<InterpretType>(out);
}
//Dumps information about what the pool memory looks like right now //Dumps information about what the pool memory looks like right now
//into std::cout. Interpret the data in the memory as InterpretType. //into std::cout. Interpret the data in the memory as InterpretType.
template <typename InterpretType = char> template <typename InterpretType = char>
void Dump() const void Dump() const;
{
m_Pool.Dump<InterpretType>();
}
private: private:
const ComponentInfo m_ComponentInfo; ::ComponentInfo m_ComponentInfo;
MemoryPool<char> m_Pool; MemoryPool<char> m_Pool;
std::unordered_map<EntityID, char*> m_EntityToComponent;
}; };
#endif #endif
+15 -14
View File
@@ -1,20 +1,20 @@
#ifndef Component_h__ #ifndef ComponentWrapper_h__
#define Component_h__ #define ComponentWrapper_h__
#include "../Common.h" #include "../Common.h"
#include "Entity.h" #include "EntityWrapper.h"
#include "ComponentInfo.h" #include "ComponentInfo.h"
struct ComponentWrapper struct ComponentWrapper
{ {
ComponentWrapper(const ComponentInfo& componentInfo, char* data) ComponentWrapper(const ComponentInfo& componentInfo, char* data)
: EntityID(*reinterpret_cast<::EntityID*>(data)) : Info(componentInfo)
, Info(componentInfo) , EntityID(*reinterpret_cast<::EntityID*>(data))
, Data(data) , Data(data + sizeof(EntityID))
{ } { }
::EntityID& EntityID;
const ComponentInfo& Info; const ComponentInfo& Info;
const ::EntityID EntityID;
char* Data; char* Data;
template <typename T> template <typename T>
@@ -25,13 +25,13 @@ struct ComponentWrapper
} }
template <typename T> template <typename T>
void SetProperty(std::string name, T& value) { Property<T>(name)=value; } void SetProperty(std::string name, const T value) { Property<T>(name) = value; }
template <typename T> //template <typename T>
void SetProperty(std::string name, const T value) { Property<T>(name)=value; } //void SetProperty(std::string name, T& value) { Property<T>(name) = value; }
// Specialization for string literals // Specialization for string literals
template <std::size_t N> template <std::size_t N>
void SetProperty(std::string name, const char(&value)[N]) { Property<std::string>(name)=std::string(value); } void SetProperty(std::string name, const char(&value)[N]) { Property<std::string>(name) = std::string(value); }
struct SubscriptProxy struct SubscriptProxy
{ {
@@ -51,8 +51,9 @@ struct ComponentWrapper
template <typename T> template <typename T>
void operator=(const T val) { m_Component->SetProperty<T>(m_PropertyName, val); } void operator=(const T val) { m_Component->SetProperty<T>(m_PropertyName, val); }
template <typename T> // TODO: Pass by reference and rvalue (universal reference?)
void operator=(T& val) { m_Component->SetProperty<T>(m_PropertyName, val); } //template <typename T>
//void operator=(T& val) { m_Component->SetProperty<T>(m_PropertyName, val); }
// Specialization for string literals // Specialization for string literals
template<std::size_t N> template<std::size_t N>
@@ -61,4 +62,4 @@ struct ComponentWrapper
SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); } SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); }
}; };
#endif #endif
-12
View File
@@ -1,12 +0,0 @@
#ifndef Entity_h__
#define Entity_h__
typedef unsigned int EntityID;
struct Entity
{
unsigned int EntityID = 0;
};
#endif
+4 -4
View File
@@ -27,7 +27,7 @@
#include <xercesc/framework/psvi/XSValue.hpp> #include <xercesc/framework/psvi/XSValue.hpp>
#include <xercesc/framework/XMLValidator.hpp> #include <xercesc/framework/XMLValidator.hpp>
#include "Entity.h" #include "EntityWrapper.h"
#include "ComponentWrapper.h" #include "ComponentWrapper.h"
class EntityPreprocessorXMLErrorHandler : public xercesc::DOMErrorHandler class EntityPreprocessorXMLErrorHandler : public xercesc::DOMErrorHandler
@@ -231,7 +231,7 @@ private:
std::map<std::string, ComponentInfo> m_ComponentInfo; std::map<std::string, ComponentInfo> m_ComponentInfo;
public: public:
std::map<std::string, ComponentPool> m_ComponentStore; std::map<std::string, ComponentPool> m_ComponentStore;
std::vector<Entity*> m_Entities; std::vector<EntityWrapper*> m_Entities;
private: private:
/* /*
@@ -380,8 +380,8 @@ private:
// Calculate component size // Calculate component size
unsigned int stride = 0; unsigned int stride = 0;
// Reserve space for Entity pointer // Reserve space for Entity pointer
stride += sizeof(Entity*); stride += sizeof(EntityWrapper*);
std::cout << " Entity " << " (" << sizeof(Entity*) << " byte)" << std::endl; std::cout << " Entity " << " (" << sizeof(EntityWrapper*) << " byte)" << std::endl;
// Add size of fields // Add size of fields
for (auto& field : ci.FieldTypes) { for (auto& field : ci.FieldTypes) {
std::cout << " " << field.second << " " << field.first << " (" << getTypeStride(field.second) << " byte)" << std::endl; std::cout << " " << field.second << " " << field.first << " (" << getTypeStride(field.second) << " byte)" << std::endl;
+15
View File
@@ -0,0 +1,15 @@
#ifndef Entity_h__
#define Entity_h__
typedef unsigned int EntityID;
struct EntityWrapper
{
EntityWrapper(EntityID entityID)
: ID(entityID)
{ }
EntityID ID;
};
#endif
+19 -16
View File
@@ -2,32 +2,35 @@
#define World_h__ #define World_h__
#include "../Common.h" #include "../Common.h"
#include "Entity.h" #include "EntityWrapper.h"
#include "ObjectPool.h"
#include "ComponentPool.h" #include "ComponentPool.h"
class World class World
{ {
public: public:
World() World() = default;
{ ~World();
} EntityID CreateEntity(EntityID parent = 0);
~World() // Register a component type and allocate space for it
{ void RegisterComponent(ComponentInfo& ci);
// foreach (m_ComponentPools... // Attach a component to an entity and fill it with default values
} ComponentWrapper AttachComponent(EntityID entity, std::string componentType);
// Get a component of an entity
void AllocateComponentPool(ComponentInfo& ci) ComponentWrapper GetComponent(EntityID entity, std::string componentType);
{ // Get all components of the specified type
auto pool = new ComponentPool(ci); const ComponentPool& GetComponents(std::string componentType);
m_ComponentPools[ci.Name] = pool;
}
void AddComponent(EntityID entity, std::string componentType);
private: private:
EntityID m_CurrentEntityID = 0;
std::unordered_map<EntityID, EntityID> m_EntityParents;
std::unordered_multimap<EntityID, EntityID> m_EntityChildren;
std::unordered_map<std::string, ComponentPool*> m_ComponentPools; std::unordered_map<std::string, ComponentPool*> m_ComponentPools;
EntityID generateEntityID();
}; };
#endif #endif
+79
View File
@@ -0,0 +1,79 @@
#include "Core/ComponentPool.h"
ComponentWrapper ComponentPoolForwardIterator::operator*() const
{
char* data = &(*m_MemoryPoolIterator);
ComponentWrapper wrapper(m_ComponentInfo, data);
return wrapper;
}
bool ComponentPoolForwardIterator::operator==(const ComponentPoolForwardIterator& other) const
{
return m_MemoryPoolIterator == other.m_MemoryPoolIterator;
}
bool ComponentPoolForwardIterator::operator!=(const ComponentPoolForwardIterator& other) const
{
return m_MemoryPoolIterator != other.m_MemoryPoolIterator;
}
ComponentPoolForwardIterator& ComponentPoolForwardIterator::operator++(int)
{
ComponentPoolForwardIterator copyIter(*this);
operator++();
return copyIter;
}
ComponentPoolForwardIterator& ComponentPoolForwardIterator::operator++()
{
++m_MemoryPoolIterator;
return *this;
}
//const ::ComponentInfo& ComponentPool::ComponentInfo() const
//{
// return m_ComponentInfo;
//}
ComponentWrapper ComponentPool::Allocate(EntityID entity)
{
char* data = m_Pool.Allocate();
memcpy(data, &entity, sizeof(EntityID));
m_EntityToComponent[entity] = data;
return ComponentWrapper(m_ComponentInfo, data);
}
ComponentWrapper ComponentPool::GetByEntity(EntityID ent)
{
return ComponentWrapper(m_ComponentInfo, m_EntityToComponent.at(ent));
}
void ComponentPool::Delete(ComponentWrapper& wrapper)
{
m_EntityToComponent.erase(wrapper.EntityID);
m_Pool.Free(wrapper.Data);
}
ComponentPool::iterator ComponentPool::begin() const
{
return iterator(m_ComponentInfo, m_Pool.begin(), m_Pool.end());
}
ComponentPool::iterator ComponentPool::end() const
{
return iterator(m_ComponentInfo, m_Pool.end(), m_Pool.end());
}
template <typename InterpretType /*= char*/>
void ComponentPool::Dump() const
{
m_Pool.Dump<InterpretType>();
}
template <typename InterpretType /*= char*/, typename OutStream>
void ComponentPool::Dump(OutStream& out) const
{
m_Pool.Dump<InterpretType>(out);
}
+54
View File
@@ -0,0 +1,54 @@
#include "Core/World.h"
World::~World()
{
for (auto& pool : m_ComponentPools) {
delete pool.second;
}
}
EntityID World::CreateEntity(EntityID parent /*= 0*/)
{
EntityID newEntity = generateEntityID();
m_EntityParents[newEntity] = parent;
if (parent != 0) {
m_EntityChildren.insert(std::make_pair(parent, newEntity));
}
return newEntity;
}
void World::RegisterComponent(ComponentInfo& ci)
{
m_ComponentPools[ci.Name] = new ComponentPool(ci);
}
ComponentWrapper World::AttachComponent(EntityID entity, std::string componentType)
{
ComponentPool* pool = m_ComponentPools.at(componentType);
const ComponentInfo& ci = pool->ComponentInfo();
// Allocate space for the component
ComponentWrapper c = pool->Allocate(entity);
// Write default values
memcpy(c.Data, ci.Defaults.get(), ci.Meta.Stride);
return c;
}
ComponentWrapper World::GetComponent(EntityID entity, std::string componentType)
{
ComponentPool* pool = m_ComponentPools.at(componentType);
return pool->GetByEntity(entity);
}
const ComponentPool& World::GetComponents(std::string componentType)
{
return *m_ComponentPools.at(componentType);
}
EntityID World::generateEntityID()
{
// TODO: Make EntityID generation smarter
return m_CurrentEntityID++;
}
+25 -19
View File
@@ -4,25 +4,31 @@
BOOST_AUTO_TEST_CASE(ComponentPoolTest) BOOST_AUTO_TEST_CASE(ComponentPoolTest)
{ {
ComponentInfo ci; // TODO: Write an updated test for component pool
ci.Name = "Test"; BOOST_CHECK(false);
ci.FieldTypes["Field"] = "int"; //ComponentInfo ci;
ci.FieldOffsets["Field"] = sizeof(EntityID); //ci.Name = "Test";
ci.Meta.Allocation = 4; //ci.FieldTypes["Field"] = "int";
ci.Meta.Stride = sizeof(EntityID) + sizeof(int); //ci.FieldOffsets["Field"] = 0;
//ci.Meta.Allocation = 3;
//ci.Meta.Stride = sizeof(EntityID) + sizeof(int);
ComponentPool pool(ci); //std::vector<ComponentWrapper> wrappers;
for (int i = 0; i < 3; i++) { //ComponentPool pool(ci);
ComponentWrapper c = pool.New(); //for (int i = 0; i < 4; i++) {
c.EntityID = i; // ComponentWrapper c = pool.New();
unsigned int offset = c.Info.FieldOffsets.at("Field"); // c.EntityID = i;
memcpy(&c.Data[offset], &i, sizeof(int)); // unsigned int offset = c.Info.FieldOffsets.at("Field");
} // memcpy(&c.Data[offset], &i, sizeof(int));
// wrappers.push_back(c);
//}
int i = 0; //int i = 0;
for (auto& c : pool) { //for (auto& c : pool) {
BOOST_CHECK(c.EntityID == i); // BOOST_CHECK(c.EntityID == i);
BOOST_CHECK((int)c["Field"] == i); // BOOST_CHECK((int)c["Field"] == i);
i++; // i++;
} //}
//pool.Delete(wrappers[1]);
} }
+41
View File
@@ -0,0 +1,41 @@
#include <boost/test/unit_test.hpp>
#include "Common.h"
#include "Core/World.h"
BOOST_AUTO_TEST_CASE(WorldTest)
{
ComponentInfo ci;
ci.Name = "Test";
ci.FieldTypes["Field"] = "int";
ci.FieldOffsets["Field"] = 0;
ci.Meta.Stride = sizeof(int);
ci.Meta.Allocation = 3;
ci.Defaults = std::shared_ptr<char>(new char[ci.Meta.Stride]);
int default_Field = 1337;
memcpy(ci.Defaults.get(), &default_Field, ci.Meta.Stride);
World w;
w.RegisterComponent(ci);
std::vector<EntityID> ids;
for (int i = 0; i < 6; i++) {
EntityID e = w.CreateEntity();
ids.push_back(e);
w.AttachComponent(e, "Test");
ComponentWrapper c = w.GetComponent(e, "Test");
BOOST_CHECK(c.EntityID == e);
BOOST_CHECK((int)c["Field"] == 1337);
c.SetProperty("Field", i);
BOOST_CHECK((int)c["Field"] == i);
}
int i = 0;
for (auto& c : w.GetComponents("Test")) {
EntityID e = ids.at(i);
BOOST_CHECK(c.EntityID == e);
BOOST_CHECK((int)c["Field"] == i);
i++;
}
}