From 032b384f3ff2289490c7c465e52c3d955b02e595 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 3 Dec 2015 16:45:03 +0100 Subject: [PATCH] Created framework for a hardcoded world for TESTING PURPOSES --- include/Game/Game.h | 6 +- include/Game/HardcodedTestWorld.h | 124 ++++++++++++++++++++++++++++++ src/Game/Game.cpp | 4 + 3 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 include/Game/HardcodedTestWorld.h diff --git a/include/Game/Game.h b/include/Game/Game.h index 6a942164..e63e657f 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -3,12 +3,11 @@ #include "Core/ResourceManager.h" #include "Core/ConfigFile.h" - #include "Core/EventBroker.h" #include "Rendering/DummyRenderer.h" #include "Core/InputManager.h" - #include "GUI/Frame.h" +#include "Core/World.h" class Game { @@ -26,6 +25,7 @@ private: IRenderer* m_Renderer; InputManager* m_InputManager; GUI::Frame* m_FrameStack; + World* m_World; }; -#endif \ No newline at end of file +#endif diff --git a/include/Game/HardcodedTestWorld.h b/include/Game/HardcodedTestWorld.h new file mode 100644 index 00000000..47586137 --- /dev/null +++ b/include/Game/HardcodedTestWorld.h @@ -0,0 +1,124 @@ +#include +#include +#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; +}; + +class HardcodedTestWorld : public World +{ +public: + HardcodedTestWorld() + : World() + { + registerTestComponents(); + createTestEntities(); + } + +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)) + } + } + }; + + for (auto& c : components) { + ComponentInfo ci; + ci.Name = c.first; + + // 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; + + ci.FieldOffsets[fieldName] = offset; + ci.FieldTypes[fieldName] = "undefined"; + memcpy(ci.Defaults.get() + offset, defaultValue.Buffer.get(), size); + offset += size; + } + + RegisterComponent(ci); + } + } + + 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"]; + } +}; \ No newline at end of file diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index c710e218..f6421547 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -1,4 +1,5 @@ #include "Game.h" +#include "HardcodedTestWorld.h" Game::Game(int argc, char* argv[]) { @@ -30,6 +31,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(); }