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
+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)
{
ComponentInfo ci;
ci.Name = "Test";
ci.FieldTypes["Field"] = "int";
ci.FieldOffsets["Field"] = sizeof(EntityID);
ci.Meta.Allocation = 4;
ci.Meta.Stride = sizeof(EntityID) + sizeof(int);
// TODO: Write an updated test for component pool
BOOST_CHECK(false);
//ComponentInfo ci;
//ci.Name = "Test";
//ci.FieldTypes["Field"] = "int";
//ci.FieldOffsets["Field"] = 0;
//ci.Meta.Allocation = 3;
//ci.Meta.Stride = sizeof(EntityID) + sizeof(int);
ComponentPool pool(ci);
for (int i = 0; i < 3; i++) {
ComponentWrapper c = pool.New();
c.EntityID = i;
unsigned int offset = c.Info.FieldOffsets.at("Field");
memcpy(&c.Data[offset], &i, sizeof(int));
}
//std::vector<ComponentWrapper> wrappers;
//ComponentPool pool(ci);
//for (int i = 0; i < 4; i++) {
// ComponentWrapper c = pool.New();
// c.EntityID = i;
// unsigned int offset = c.Info.FieldOffsets.at("Field");
// memcpy(&c.Data[offset], &i, sizeof(int));
// wrappers.push_back(c);
//}
int i = 0;
for (auto& c : pool) {
BOOST_CHECK(c.EntityID == i);
BOOST_CHECK((int)c["Field"] == i);
i++;
}
//int i = 0;
//for (auto& c : pool) {
// BOOST_CHECK(c.EntityID == i);
// BOOST_CHECK((int)c["Field"] == 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++;
}
}