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