From aeb6c396039830703db71cc86e94c19e656dd964 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 3 Dec 2015 19:07:33 +0100 Subject: [PATCH 1/2] Created ComponentWrapperFactory to make test mocking easier and manual component registration a little less painful --- include/Engine/Core/ComponentWrapper.h | 40 +++++++ include/Engine/Core/Util/Any.h | 42 +++++++ include/Game/HardcodedTestWorld.h | 149 +++++++++---------------- 3 files changed, 136 insertions(+), 95 deletions(-) create mode 100644 include/Engine/Core/Util/Any.h diff --git a/include/Engine/Core/ComponentWrapper.h b/include/Engine/Core/ComponentWrapper.h index 7b388267..95a31240 100644 --- a/include/Engine/Core/ComponentWrapper.h +++ b/include/Engine/Core/ComponentWrapper.h @@ -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 + 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(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 m_DefaultValues; +}; + #endif diff --git a/include/Engine/Core/Util/Any.h b/include/Engine/Core/Util/Any.h new file mode 100644 index 00000000..f0f90737 --- /dev/null +++ b/include/Engine/Core/Util/Any.h @@ -0,0 +1,42 @@ +#ifndef Util_Any_h__ +#define Util_Any_h__ + +#include + +struct Any +{ + Any() { } + + template + Any(const T& value) + { + Data = std::shared_ptr(new char[sizeof(T)]); + Size = sizeof(T); + memcpy(Data.get(), &value, Size); + } + + template + Any(T&& value) + { + Data = std::shared_ptr(new char[sizeof(T)]); + Size = sizeof(T); + memcpy(Data.get(), &value, Size); + } + + template + Any& operator=(const T& value) + { + return Any(value); + } + + template + Any& operator=(T&& value) + { + return Any(value); + } + + std::shared_ptr Data = nullptr; + std::size_t Size = 0; +}; + +#endif \ No newline at end of file diff --git a/include/Game/HardcodedTestWorld.h b/include/Game/HardcodedTestWorld.h index 47586137..1176402d 100644 --- a/include/Game/HardcodedTestWorld.h +++ b/include/Game/HardcodedTestWorld.h @@ -3,39 +3,7 @@ #include #include "GLM.h" #include "Core/World.h" - -struct any -{ - any() { } - - template - any(const T& value) - { - Buffer = std::shared_ptr(new char[sizeof(T)]); - memcpy(Buffer.get(), &value, sizeof(T)); - } - - template - any(T&& value) - { - Buffer = std::shared_ptr(new char[sizeof(T)]); - memcpy(Buffer.get(), &value, sizeof(T)); - } - - template - any& operator=(const T& value) - { - return any(value); - } - - template - any& operator=(T&& value) - { - return any(value); - } - - std::shared_ptr Buffer = nullptr; -}; +#include "Core/Util/Any.h" class HardcodedTestWorld : public World { @@ -50,75 +18,66 @@ public: private: void registerTestComponents() { - std::unordered_map>> 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(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; + } } }; \ No newline at end of file From 25258f7e7892e2759519fd757e821629befca372 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 4 Dec 2015 09:45:50 +0100 Subject: [PATCH 2/2] Better test for World and ECS --- src/Tests/WorldTest.cpp | 78 ++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/src/Tests/WorldTest.cpp b/src/Tests/WorldTest.cpp index 663c243f..4fd4ceed 100644 --- a/src/Tests/WorldTest.cpp +++ b/src/Tests/WorldTest.cpp @@ -1,41 +1,71 @@ #include +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(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 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("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++; } }