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 "../Common.h"
#include "EntityWrapper.h" #include "EntityWrapper.h"
#include "ComponentInfo.h" #include "ComponentInfo.h"
#include "Util/Any.h"
struct ComponentWrapper struct ComponentWrapper
{ {
@@ -62,4 +63,43 @@ struct ComponentWrapper
SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); } 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 #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 <boost/any.hpp>
#include "GLM.h" #include "GLM.h"
#include "Core/World.h" #include "Core/World.h"
#include "Core/Util/Any.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;
};
class HardcodedTestWorld : public World class HardcodedTestWorld : public World
{ {
@@ -50,75 +18,66 @@ public:
private: private:
void registerTestComponents() void registerTestComponents()
{ {
std::unordered_map<std::string, std::list<std::tuple<std::size_t, std::string, any>>> components ComponentWrapperFactory f;
{
{
"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))
}
}
};
for (auto& c : components) { f = ComponentWrapperFactory("Test");
ComponentInfo ci; f.AddProperty("TestInteger", 1337);
ci.Name = c.first; f.AddProperty("TestFloat", 13.37f);
f.AddProperty("TestString", std::string("Carlito"));
RegisterComponent(f);
// Fields f = ComponentWrapperFactory("Debug");
unsigned int stride = 0; f.AddProperty("Name", std::string("Unnamed"));
for (auto& f : c.second) { RegisterComponent(f);
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;
ci.FieldOffsets[fieldName] = offset; f = ComponentWrapperFactory("Transform");
ci.FieldTypes[fieldName] = "undefined"; f.AddProperty("Position", glm::vec3(0.f, 0.f, 0.f));
memcpy(ci.Defaults.get() + offset, defaultValue.Buffer.get(), size); f.AddProperty("Orientation", glm::quat());
offset += size; 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() void createTestEntities()
{ {
EntityID e = CreateEntity(); World& world = *this;
AttachComponent(e, "Test");
AttachComponent(e, "Debug"); // Create an entity
AttachComponent(e, "Transform"); EntityID e = world.CreateEntity();
ComponentWrapper testComponent = GetComponent(e, "Test");
int testValue = testComponent["TestInteger"]; // Attach a Debug component
float testFloat = testComponent["TestFloat"]; ComponentWrapper debug = world.AttachComponent(e, "Debug");
ComponentWrapper debugComponent = GetComponent(e, "Debug"); // Set the Name field of the Debug component using subscript operator
std::string name = debugComponent["Name"]; debug["Name"] = "Carlito";
glm::vec3 pickingColor = debugComponent["PickingColor"];
ComponentWrapper testTransform = GetComponent(e, "Transform"); // Attach a Transform component
glm::vec3 pos = testTransform["Position"]; world.AttachComponent(e, "Transform");
glm::quat ori = testTransform["Orientation"]; // Fetch the component based on EntityID and component type
glm::vec3 scale = testTransform["Scale"]; 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> #include <boost/test/unit_test.hpp>
namespace utf = boost::unit_test;
#include "Common.h" #include "Common.h"
#include "GLM.h"
#include "Core/World.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; 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++) { for (int i = 0; i < 6; i++) {
EntityID e = w.CreateEntity(); EntityID e = w.CreateEntity();
ids.push_back(e); ComponentWrapper c = w.AttachComponent(e, "Test");
w.AttachComponent(e, "Test"); c["TestInteger"] = i;
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);
} }
// Loop through them and check data
int i = 0; int i = 0;
for (auto& c : w.GetComponents("Test")) { for (auto& c : w.GetComponents("Test")) {
EntityID e = ids.at(i); BOOST_TEST((int)c["TestInteger"] == i);
BOOST_CHECK(c.EntityID == e);
BOOST_CHECK((int)c["Field"] == i);
i++; i++;
} }
} }