Merge pull request #4 from teamfisk/ecs

Tests for ECS
This commit is contained in:
Viktor Ljung
2015-12-07 15:25:39 +01:00
4 changed files with 190 additions and 119 deletions
+40
View File
@@ -4,6 +4,7 @@
#include "../Common.h"
#include "EntityWrapper.h"
#include "ComponentInfo.h"
#include "Util/Any.h"
struct ComponentWrapper
{
@@ -62,4 +63,43 @@ struct ComponentWrapper
SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); }
};
// TODO: Move this to Tests once entity importing is finished
class ComponentWrapperFactory
{
public:
ComponentWrapperFactory() = default;
ComponentWrapperFactory(std::string componentTypeName, std::size_t allocation = 0)
{
m_ComponentInfo.Name = componentTypeName;
m_ComponentInfo.Meta.Allocation = allocation;
}
template <typename T>
void AddProperty(std::string fieldName, T defaultValue)
{
m_DefaultValues.push_back(defaultValue);
m_ComponentInfo.FieldTypes[fieldName] = typeid(T).name();
m_ComponentInfo.FieldOffsets[fieldName] = m_ComponentInfo.Meta.Stride;
m_ComponentInfo.Meta.Stride += sizeof(T);
}
ComponentInfo& Finalize()
{
m_ComponentInfo.Defaults = std::shared_ptr<char>(new char[m_ComponentInfo.Meta.Stride]);
std::size_t offset = 0;
for (auto& val : m_DefaultValues) {
memcpy(m_ComponentInfo.Defaults.get() + offset, val.Data.get(), val.Size);
offset += val.Size;
}
return m_ComponentInfo;
}
operator ComponentInfo&() { return Finalize(); }
private:
ComponentInfo m_ComponentInfo;
std::vector<Any> m_DefaultValues;
};
#endif
+42
View File
@@ -0,0 +1,42 @@
#ifndef Util_Any_h__
#define Util_Any_h__
#include <memory>
struct Any
{
Any() { }
template <typename T>
Any(const T& value)
{
Data = std::shared_ptr<char>(new char[sizeof(T)]);
Size = sizeof(T);
memcpy(Data.get(), &value, Size);
}
template <typename T>
Any(T&& value)
{
Data = std::shared_ptr<char>(new char[sizeof(T)]);
Size = sizeof(T);
memcpy(Data.get(), &value, Size);
}
template <typename T>
Any& operator=(const T& value)
{
return Any(value);
}
template <typename T>
Any& operator=(T&& value)
{
return Any(value);
}
std::shared_ptr<char> Data = nullptr;
std::size_t Size = 0;
};
#endif
+54 -95
View File
@@ -3,39 +3,7 @@
#include <boost/any.hpp>
#include "GLM.h"
#include "Core/World.h"
struct any
{
any() { }
template <typename T>
any(const T& value)
{
Buffer = std::shared_ptr<char>(new char[sizeof(T)]);
memcpy(Buffer.get(), &value, sizeof(T));
}
template <typename T>
any(T&& value)
{
Buffer = std::shared_ptr<char>(new char[sizeof(T)]);
memcpy(Buffer.get(), &value, sizeof(T));
}
template <typename T>
any& operator=(const T& value)
{
return any(value);
}
template <typename T>
any& operator=(T&& value)
{
return any(value);
}
std::shared_ptr<char> Buffer = nullptr;
};
#include "Core/Util/Any.h"
class HardcodedTestWorld : public World
{
@@ -50,75 +18,66 @@ public:
private:
void registerTestComponents()
{
std::unordered_map<std::string, std::list<std::tuple<std::size_t, std::string, any>>> components
{
{
"Test",
{
std::make_tuple(sizeof(int), "TestInteger", 1337),
std::make_tuple(sizeof(float), "TestFloat", 13.37f)
}
},
{
"Debug",
{
std::make_tuple(sizeof(std::string), "Name", std::string("Unnamed")),
std::make_tuple(sizeof(glm::vec3), "PickingColor", glm::vec3(0.f))
}
},
{
"Transform",
{
std::make_tuple(sizeof(glm::vec3), "Position", glm::vec3(0.f, 0.f, 0.f)),
std::make_tuple(sizeof(glm::quat), "Orientation", glm::quat()),
std::make_tuple(sizeof(glm::vec3), "Scale", glm::vec3(1.f, 1.f, 1.f))
}
}
};
ComponentWrapperFactory f;
for (auto& c : components) {
ComponentInfo ci;
ci.Name = c.first;
f = ComponentWrapperFactory("Test");
f.AddProperty("TestInteger", 1337);
f.AddProperty("TestFloat", 13.37f);
f.AddProperty("TestString", std::string("Carlito"));
RegisterComponent(f);
// Fields
unsigned int stride = 0;
for (auto& f : c.second) {
stride += std::get<0>(f);
}
ci.Meta.Stride = stride;
ci.Defaults = std::shared_ptr<char>(new char[stride]);
unsigned int offset = 0;
for (auto& f : c.second) {
std::size_t size;
std::string fieldName;
any defaultValue;
std::tie(size, fieldName, defaultValue) = f;
f = ComponentWrapperFactory("Debug");
f.AddProperty("Name", std::string("Unnamed"));
RegisterComponent(f);
ci.FieldOffsets[fieldName] = offset;
ci.FieldTypes[fieldName] = "undefined";
memcpy(ci.Defaults.get() + offset, defaultValue.Buffer.get(), size);
offset += size;
}
f = ComponentWrapperFactory("Transform");
f.AddProperty("Position", glm::vec3(0.f, 0.f, 0.f));
f.AddProperty("Orientation", glm::quat());
f.AddProperty("Scale", glm::vec3(1.f, 1.f, 1.f));
RegisterComponent(f);
RegisterComponent(ci);
}
f = ComponentWrapperFactory("Model");
f.AddProperty("Resource", std::string());
f.AddProperty("Color", glm::vec4(1.f, 1.f, 1.f, 1.f));
f.AddProperty("Visible", true);
RegisterComponent(f);
}
void createTestEntities()
{
EntityID e = CreateEntity();
AttachComponent(e, "Test");
AttachComponent(e, "Debug");
AttachComponent(e, "Transform");
ComponentWrapper testComponent = GetComponent(e, "Test");
int testValue = testComponent["TestInteger"];
float testFloat = testComponent["TestFloat"];
ComponentWrapper debugComponent = GetComponent(e, "Debug");
std::string name = debugComponent["Name"];
glm::vec3 pickingColor = debugComponent["PickingColor"];
ComponentWrapper testTransform = GetComponent(e, "Transform");
glm::vec3 pos = testTransform["Position"];
glm::quat ori = testTransform["Orientation"];
glm::vec3 scale = testTransform["Scale"];
World& world = *this;
// Create an entity
EntityID e = world.CreateEntity();
// Attach a Debug component
ComponentWrapper debug = world.AttachComponent(e, "Debug");
// Set the Name field of the Debug component using subscript operator
debug["Name"] = "Carlito";
// Attach a Transform component
world.AttachComponent(e, "Transform");
// Fetch the component based on EntityID and component type
ComponentWrapper transform = world.GetComponent(e, "Transform");
// Set the fields of the Transform component
transform["Position"] = glm::vec3(0.f, 0.f, 0.f);
transform["Scale"] = glm::vec3(1.f, 1.f, 1.f);
// Move on the X axis by fetching field as reference
((glm::vec3&)transform["Position"]).x += 10.f;
// Shrink by a factor of 100
((glm::vec3&)transform["Scale"]) /= 100.f;
// Loop through all Transform components and print them
for (auto& transform : world.GetComponents("Transform")) {
glm::vec3 pos = transform["Position"];
std::cout << "Position: " << pos.x << " " << pos.y << " " << pos.z << std::endl;
glm::vec3 scale = transform["Scale"];
std::cout << "Scale: " << scale.x << " " << scale.y << " " << scale.z << std::endl;
// Fetch the Debug component also present in this entity
ComponentWrapper debug = world.GetComponent(transform.EntityID, "Debug");
std::cout << "Name: " << (std::string)debug["Name"] << std::endl;
}
}
};
+54 -24
View File
@@ -1,41 +1,71 @@
#include <boost/test/unit_test.hpp>
namespace utf = boost::unit_test;
#include "Common.h"
#include "GLM.h"
#include "Core/World.h"
BOOST_AUTO_TEST_CASE(WorldTest)
BOOST_AUTO_TEST_CASE(WorldTestSingleAllocation, * boost::unit_test::tolerance(0.001))
{
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;
auto f = ComponentWrapperFactory("Test");
f.AddProperty("TestInteger", 1337);
f.AddProperty("TestDouble", 13.37);
f.AddProperty("TestString", std::string("Carlito"));
f.AddProperty("TestVec3", glm::vec3(1.f, 2.f, 3.f));
w.RegisterComponent(f);
EntityID e = w.CreateEntity();
ComponentWrapper c = w.AttachComponent(e, "Test");
// Check default values
BOOST_TEST((int)c["TestInteger"] == c.Property<int>("TestInteger"));
BOOST_TEST((int)c["TestInteger"] == 1337);
BOOST_TEST((double)c["TestDouble"] == 13.37);
BOOST_TEST((std::string)c["TestString"] == "Carlito");
glm::vec3 vec3 = c["TestVec3"];
BOOST_TEST(vec3.x == 1.f);
BOOST_TEST(vec3.y == 2.f);
BOOST_TEST(vec3.z == 3.f);
// Change values
((int&)c["TestInteger"]) += 1;
BOOST_TEST((int)c["TestInteger"] == 1338);
((double&)c["TestDouble"]) += 1.11;
std::cout << (double)c["TestDouble"] << std::endl;
BOOST_TEST((double)c["TestDouble"] == 14.48);
c["TestString"] = "Siesta";
BOOST_TEST((std::string)c["TestString"] == "Siesta");
((glm::vec3&)c["TestVec3"]).y += 1.f;
BOOST_TEST(((glm::vec3)c["TestVec3"]).y == 3.f);
}
BOOST_AUTO_TEST_CASE(WorldTestMultipleAllocations, * utf::tolerance(0.00001))
{
World w;
// Create allocation for 3 entities
auto f = ComponentWrapperFactory("Test", 3);
f.AddProperty("TestInteger", 1337);
f.AddProperty("TestDouble", 13.37);
f.AddProperty("TestString", std::string("Carlito"));
f.AddProperty("TestVec3", glm::vec3(1.f, 2.f, 3.f));
w.RegisterComponent(f);
// Create 6 entities with Test components
// 3 will reside in contiguous memory
// 3 will be allocated dynamically
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);
ComponentWrapper c = w.AttachComponent(e, "Test");
c["TestInteger"] = i;
}
// Loop through them and check data
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);
BOOST_TEST((int)c["TestInteger"] == i);
i++;
}
}