Merge remote-tracking branch 'origin/master' into Rendering

This commit is contained in:
Tleety
2015-12-03 18:15:56 +01:00
19 changed files with 1087 additions and 4 deletions
+3
View File
@@ -7,6 +7,7 @@ find_package(Boost REQUIRED COMPONENTS system filesystem thread chrono)
find_package(assimp REQUIRED)
find_package(ZLIB REQUIRED)
find_package(PNG REQUIRED)
find_package(Xerces REQUIRED)
# Because FindOpenAL is retarded
#set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${CMAKE_SOURCE_DIR}/deps/include/AL")
#find_package(OpenAL REQUIRED)
@@ -23,6 +24,7 @@ include_directories(
${Boost_INCLUDE_DIRS}
${assimp_INCLUDE_DIRS}
${PNG_INCLUDE_DIRS}
${Xerces_INCLUDE_DIRS}
${OPENAL_INCLUDE_DIR}
${X11_INCLUDE_DIRS}
)
@@ -78,6 +80,7 @@ set(LIBRARIES
${Boost_LIBRARIES}
${assimp_LIBRARIES}
${PNG_LIBRARIES}
${Xerces_LIBRARIES}
${OPENAL_LIBRARY}
${X11_LIBRARIES}
)
+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++;
}
+4
View File
@@ -1,4 +1,5 @@
#include "Game.h"
#include "HardcodedTestWorld.h"
Game::Game(int argc, char* argv[])
{
@@ -32,6 +33,9 @@ Game::Game(int argc, char* argv[])
m_FrameStack->Width = m_Renderer->Resolution().Width;
m_FrameStack->Height = m_Renderer->Resolution().Height;
// Create a TEST WORLD
m_World = new HardcodedTestWorld();
m_LastTime = glfwGetTime();
}
+34
View File
@@ -0,0 +1,34 @@
#include <boost/test/unit_test.hpp>
#include "Core/ComponentPool.h"
BOOST_AUTO_TEST_CASE(ComponentPoolTest)
{
// 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);
//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++;
//}
//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++;
}
}