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, unsigned int> FieldOffsets;
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
+24 -69
View File
@@ -9,51 +9,21 @@ class ComponentPoolForwardIterator
: public std::iterator<std::forward_iterator_tag, ComponentWrapper>
{
public:
ComponentPoolForwardIterator(const ComponentInfo& componentInfo, const MemoryPool<char>::iterator begin, const MemoryPool<char>::iterator end)
: m_ComponentInfo(componentInfo)
ComponentPoolForwardIterator(const ComponentInfo& componentInfo, const MemoryPool<char>::iterator begin, const MemoryPool<char>::iterator end)
: m_ComponentInfo(componentInfo)
, m_MemoryPoolIterator(begin)
, m_MemoryPoolEnd(end)
{ }
{ }
ComponentPoolForwardIterator(const ComponentPoolForwardIterator& other) = default;
ComponentPoolForwardIterator(ComponentPoolForwardIterator&& other) = default;
~ComponentPoolForwardIterator() = default;
ComponentPoolForwardIterator& operator= (const ComponentPoolForwardIterator& other) = default;
ComponentPoolForwardIterator& operator++()
{
++m_MemoryPoolIterator;
return *this;
}
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);
//}
ComponentPoolForwardIterator& operator=(const ComponentPoolForwardIterator& other) = default;
ComponentPoolForwardIterator& operator++();
ComponentPoolForwardIterator& operator++(int);
bool operator!=(const ComponentPoolForwardIterator& other) const;
bool operator==(const ComponentPoolForwardIterator& other) const;
ComponentWrapper operator*() const;
private:
const ComponentInfo& m_ComponentInfo;
@@ -71,55 +41,40 @@ public:
typedef ComponentWrapper* pointer;
typedef ComponentWrapper& reference;
ComponentPool(const ComponentInfo& ci)
ComponentPool(const ::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;
ComponentWrapper New()
{
char* data = m_Pool.Allocate();
return ComponentWrapper(m_ComponentInfo, data);
}
const ::ComponentInfo& ComponentInfo() const { return m_ComponentInfo; }
void Delete(ComponentWrapper& component)
{
m_Pool.Free(component.Data);
}
// Allocate space for a component and store which entity it belongs to in internal structure
ComponentWrapper Allocate(EntityID entity);
// 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
{
return iterator(m_ComponentInfo, m_Pool.begin(), m_Pool.end());
}
iterator end() const
{
return iterator(m_ComponentInfo, m_Pool.end(), m_Pool.end());
}
iterator begin() const;
iterator end() const;
//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<<)
//Interpret the data in the memory as InterpretType.
template <typename InterpretType = char, typename OutStream>
void Dump(OutStream& out) const
{
m_Pool.Dump<InterpretType>(out);
}
void Dump(OutStream& out) const;
//Dumps information about what the pool memory looks like right now
//into std::cout. Interpret the data in the memory as InterpretType.
template <typename InterpretType = char>
void Dump() const
{
m_Pool.Dump<InterpretType>();
}
void Dump() const;
private:
const ComponentInfo m_ComponentInfo;
::ComponentInfo m_ComponentInfo;
MemoryPool<char> m_Pool;
std::unordered_map<EntityID, char*> m_EntityToComponent;
};
#endif
+15 -14
View File
@@ -1,20 +1,20 @@
#ifndef Component_h__
#define Component_h__
#ifndef ComponentWrapper_h__
#define ComponentWrapper_h__
#include "../Common.h"
#include "Entity.h"
#include "EntityWrapper.h"
#include "ComponentInfo.h"
struct ComponentWrapper
{
ComponentWrapper(const ComponentInfo& componentInfo, char* data)
: EntityID(*reinterpret_cast<::EntityID*>(data))
, Info(componentInfo)
, Data(data)
: Info(componentInfo)
, EntityID(*reinterpret_cast<::EntityID*>(data))
, Data(data + sizeof(EntityID))
{ }
::EntityID& EntityID;
const ComponentInfo& Info;
const ::EntityID EntityID;
char* Data;
template <typename T>
@@ -25,13 +25,13 @@ struct ComponentWrapper
}
template <typename T>
void SetProperty(std::string name, T& value) { Property<T>(name)=value; }
template <typename T>
void SetProperty(std::string name, const T value) { Property<T>(name)=value; }
void SetProperty(std::string name, const T value) { Property<T>(name) = value; }
//template <typename T>
//void SetProperty(std::string name, T& value) { Property<T>(name) = value; }
// Specialization for string literals
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
{
@@ -51,8 +51,9 @@ struct ComponentWrapper
template <typename T>
void operator=(const T val) { m_Component->SetProperty<T>(m_PropertyName, val); }
template <typename T>
void operator=(T& val) { m_Component->SetProperty<T>(m_PropertyName, val); }
// TODO: Pass by reference and rvalue (universal reference?)
//template <typename T>
//void operator=(T& val) { m_Component->SetProperty<T>(m_PropertyName, val); }
// Specialization for string literals
template<std::size_t N>
@@ -61,4 +62,4 @@ struct ComponentWrapper
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/XMLValidator.hpp>
#include "Entity.h"
#include "EntityWrapper.h"
#include "ComponentWrapper.h"
class EntityPreprocessorXMLErrorHandler : public xercesc::DOMErrorHandler
@@ -231,7 +231,7 @@ private:
std::map<std::string, ComponentInfo> m_ComponentInfo;
public:
std::map<std::string, ComponentPool> m_ComponentStore;
std::vector<Entity*> m_Entities;
std::vector<EntityWrapper*> m_Entities;
private:
/*
@@ -380,8 +380,8 @@ private:
// Calculate component size
unsigned int stride = 0;
// Reserve space for Entity pointer
stride += sizeof(Entity*);
std::cout << " Entity " << " (" << sizeof(Entity*) << " byte)" << std::endl;
stride += sizeof(EntityWrapper*);
std::cout << " Entity " << " (" << sizeof(EntityWrapper*) << " byte)" << std::endl;
// Add size of fields
for (auto& field : ci.FieldTypes) {
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__
#include "../Common.h"
#include "Entity.h"
#include "EntityWrapper.h"
#include "ObjectPool.h"
#include "ComponentPool.h"
class World
{
public:
World()
{
World() = default;
~World();
}
EntityID CreateEntity(EntityID parent = 0);
~World()
{
// foreach (m_ComponentPools...
}
void AllocateComponentPool(ComponentInfo& ci)
{
auto pool = new ComponentPool(ci);
m_ComponentPools[ci.Name] = pool;
}
void AddComponent(EntityID entity, std::string componentType);
// Register a component type and allocate space for it
void RegisterComponent(ComponentInfo& ci);
// 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
ComponentWrapper GetComponent(EntityID entity, std::string componentType);
// Get all components of the specified type
const ComponentPool& GetComponents(std::string componentType);
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;
EntityID generateEntityID();
};
#endif