diff --git a/Escape-the-Dawn/ComponentFactory.h b/Escape-the-Dawn/ComponentFactory.h index 7001fd3..4596c0b 100644 --- a/Escape-the-Dawn/ComponentFactory.h +++ b/Escape-the-Dawn/ComponentFactory.h @@ -11,7 +11,7 @@ struct Component; #define REGISTER_COMPONENT(NAME, TYPE) \ static ComponentRegistrar registrar(NAME); -template +template class ComponentRegistrar { public: diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index 25fbc2c..43a3f9d 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -90,6 +90,8 @@ + + @@ -99,6 +101,8 @@ + + diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters index cf68ad5..5b0a008 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters @@ -18,6 +18,8 @@ {7abbf0e9-8bda-475e-bf00-a347f396b892} + + {aca68cba-a16a-4975-8ff3-4d56b88373a1} {091f6f01-79ab-4a49-9639-c7185e89c84e} @@ -32,6 +34,11 @@ Source Files\Renderer + + Source Files + + + Source Files @@ -56,6 +63,12 @@ Header Files + + Header Files + + + Header Files\Systems + Header Files\Renderer diff --git a/Escape-the-Dawn/System.h b/Escape-the-Dawn/System.h new file mode 100644 index 0000000..4ea04a1 --- /dev/null +++ b/Escape-the-Dawn/System.h @@ -0,0 +1,20 @@ +#ifndef System_h__ +#define System_h__ + +#include "Entity.h" + +class World; + +class System +{ +public: + System(World* world) : m_World(world) { } + virtual ~System() { } + + virtual void Update(double dt, EntityID entity, EntityID parent) = 0; + +protected: + World* m_World; +}; + +#endif // System_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Systems/CollisionSystem.cpp b/Escape-the-Dawn/Systems/CollisionSystem.cpp new file mode 100644 index 0000000..5670652 --- /dev/null +++ b/Escape-the-Dawn/Systems/CollisionSystem.cpp @@ -0,0 +1,16 @@ +#include "CollisionSystem.h" +#include "World.h" + +void Systems::Collision::Update(double dt, EntityID entity, EntityID parent) +{ + if (parent != 0) + { + auto transform = m_World->GetComponent(entity, "Transform"); + auto parentTransform = m_World->GetComponent(parent, "Transform"); + + transform->Position[0] += parentTransform->Position[0]; + transform->Position[1] += parentTransform->Position[1]; + transform->Position[2] += parentTransform->Position[2]; + } + LOG_INFO("Updating entity %i with parent %i", entity, parent); +} diff --git a/Escape-the-Dawn/Systems/CollisionSystem.h b/Escape-the-Dawn/Systems/CollisionSystem.h new file mode 100644 index 0000000..c5c1bdd --- /dev/null +++ b/Escape-the-Dawn/Systems/CollisionSystem.h @@ -0,0 +1,23 @@ +#ifndef CollisionSystem_h__ +#define CollisionSystem_h__ + +#include "System.h" +#include "logging.h" + +#include "Components/Transform.h" + + +namespace Systems +{ + +class Collision : public System +{ +public: + Collision(World* world) : System(world) { } + + void Update(double dt, EntityID entity, EntityID parent) override; +}; + +} + +#endif // CollisionSystem_h__ diff --git a/Escape-the-Dawn/World.cpp b/Escape-the-Dawn/World.cpp new file mode 100644 index 0000000..fe130a8 --- /dev/null +++ b/Escape-the-Dawn/World.cpp @@ -0,0 +1,87 @@ +#include "World.h" + +void World::RecycleEntityID(EntityID id) +{ + m_RecycledEntityIDs.push(id); +} + +EntityID World::GenerateEntityID() +{ + if (!m_RecycledEntityIDs.empty()) { + EntityID id = m_RecycledEntityIDs.top(); + m_RecycledEntityIDs.pop(); + return id; + } else { + return ++m_LastEntityID; + } +} + +void World::RecursiveUpdate(std::shared_ptr system, double dt, EntityID parentEntity) +{ + for (auto pair : m_EntityParents) { + EntityID child = pair.first; + EntityID parent = pair.second; + + if (parent == parentEntity) { + system->Update(dt, child, parent); + RecursiveUpdate(system, dt, child); + } + } +} + +void World::Update(double dt) +{ + for (auto system : m_Systems) { + RecursiveUpdate(system, dt, 0); + } +} + +void World::AddSystem(System* system) +{ + +} + +//std::vector GetEntityChildren(EntityID entity); +//{ +// std::vector children; +// auto range = m_SceneGraph.equal_range(entity); +// for (auto it = range.first; it != range.second; ++it) +// children.push_back(it->second); +// return children; +//} + +EntityID World::GetEntityParent(EntityID entity) +{ + auto it = m_EntityParents.find(entity); + return it == m_EntityParents.end() ? 0 : it->second; +} + +bool World::ValidEntity(EntityID entity) +{ + return m_EntityParents.find(entity) != m_EntityParents.end(); +} + +void World::RemoveEntity(EntityID entity) +{ + m_EntityParents.erase(entity); + RecycleEntityID(entity); +} + +EntityID World::CreateEntity(EntityID parent /*= 0*/) +{ + EntityID newEntity = GenerateEntityID(); + m_EntityParents.insert(std::pair(newEntity, parent)); + return newEntity; +} + +World::~World() +{ + +} + +World::World() +{ + m_LastEntityID = 0; + + m_Systems.push_back(std::make_shared(this)); +} \ No newline at end of file diff --git a/Escape-the-Dawn/World.h b/Escape-the-Dawn/World.h index 9f00114..1dec1c5 100644 --- a/Escape-the-Dawn/World.h +++ b/Escape-the-Dawn/World.h @@ -9,79 +9,60 @@ #include "Entity.h" #include "Component.h" #include "ComponentFactory.h" -//#include "Components/Transform.h" +#include "System.h" +#include "Systems/CollisionSystem.h" class World { public: - World() - { - m_LastEntityID = 0; - } + World(); + ~World(); - EntityID CreateEntity(EntityID parent = 0) - { - EntityID newEntity = GenerateEntityID(); - m_SceneGraph.insert(std::pair(newEntity, parent)); - return newEntity; - } + EntityID CreateEntity(EntityID parent = 0); - void RemoveEntity(EntityID entity) - { - m_SceneGraph.erase(entity); - RecycleEntityID(entity); - } + void RemoveEntity(EntityID entity); - bool ValidEntity(EntityID entity) - { - return m_SceneGraph.find(entity) != m_SceneGraph.end(); - } + bool ValidEntity(EntityID entity); - EntityID GetEntityParent(EntityID entity) - { - auto it = m_SceneGraph.find(entity); - return it == m_SceneGraph.end() ? 0 : it->second; - } + EntityID GetEntityParent(EntityID entity); - std::shared_ptr AddComponent(EntityID entity, std::string componentType) + template + std::shared_ptr AddComponent(EntityID entity, std::string componentType) { - std::shared_ptr component = ComponentFactory::Instance()->Create(componentType); + std::shared_ptr component = std::static_pointer_cast(ComponentFactory::Instance()->Create(componentType)); m_ComponentsOfType[componentType].push_back(component); m_EntityComponents[entity][componentType] = component; + return component; } - /*std::vector GetEntityChildren(EntityID entity) + template + std::shared_ptr GetComponent(EntityID entity, std::string componentType) { - std::vector children; - auto range = m_SceneGraph.equal_range(entity); - for (auto it = range.first; it != range.second; ++it) - children.push_back(it->second); - return children; - }*/ + return std::static_pointer_cast(m_EntityComponents[entity][componentType]); + } + + /*std::vector GetEntityChildren(EntityID entity);*/ + + void AddSystem(System* system); + + // Recursively update through the scene graph + void Update(double dt); + + void RecursiveUpdate(std::shared_ptr system, double dt, EntityID parentEntity); private: EntityID m_LastEntityID; std::stack m_RecycledEntityIDs; - // A map of child entities to parent entities - std::unordered_map m_SceneGraph; + // A bottom to top tree. A map of child entities to parent entities. + std::unordered_map m_EntityParents; + std::unordered_map>> m_ComponentsOfType; std::unordered_map>> m_EntityComponents; + std::vector> m_Systems; - EntityID GenerateEntityID() - { - if (!m_RecycledEntityIDs.empty()) { - EntityID id = m_RecycledEntityIDs.top(); - m_RecycledEntityIDs.pop(); - return id; - } else { - return ++m_LastEntityID; - } - } + EntityID GenerateEntityID(); - void RecycleEntityID(EntityID id) - { - m_RecycledEntityIDs.push(id); - } + void RecycleEntityID(EntityID id); }; #endif // World_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/logging.h b/Escape-the-Dawn/logging.h index e2a7b7f..e23be53 100644 --- a/Escape-the-Dawn/logging.h +++ b/Escape-the-Dawn/logging.h @@ -49,7 +49,7 @@ static void _LOG(_LOG_LEVEL logLevel, char* file, char* func, unsigned int line, char message[512]; va_list args; va_start(args, format); - vsnprintf(message, 512, format, args); + vsnprintf_s(message, 512, format, args); va_end(args); if (logLevel == LOG_LEVEL_ERROR) diff --git a/Escape-the-Dawn/main.cpp b/Escape-the-Dawn/main.cpp index e67053c..c9a4f93 100644 --- a/Escape-the-Dawn/main.cpp +++ b/Escape-the-Dawn/main.cpp @@ -9,12 +9,34 @@ #include "logging.h" #include "glerror.h" +#include "World.h" + GLFWwindow* window; GLint glVersion[2]; GLchar* glVendor; int main(int argc, char* argv[]) { + World world; + + EntityID ent1 = world.CreateEntity(); + auto t1 = world.AddComponent(ent1, "Transform"); + t1->Position[0] = 1.0f; + t1->Position[1] = 1.0f; + t1->Position[2] = 1.0f; + EntityID ent2 = world.CreateEntity(ent1); + auto t2 = world.AddComponent(ent2, "Transform"); + t2->Position[0] = 1.0f; + t2->Position[1] = 1.0f; + t2->Position[2] = 1.0f; + + world.Update(0.0); + + LOG_INFO("ent1: %f %f %f", t1->Position[0], t1->Position[1], t1->Position[2]); + LOG_INFO("ent2: %f %f %f", t2->Position[0], t2->Position[1], t2->Position[2]); + + return 0; + // Initialize GLFW if (!glfwInit()) { diff --git a/Tests/Tests.vcxproj b/Tests/Tests.vcxproj index c013dba..b6294b2 100644 --- a/Tests/Tests.vcxproj +++ b/Tests/Tests.vcxproj @@ -80,6 +80,16 @@ + + + {fe405cfa-8fce-4867-92cf-797e73b36482} + true + true + false + true + false + + diff --git a/Tests/main.cpp b/Tests/main.cpp index 6a335d1..bc0a4f0 100644 --- a/Tests/main.cpp +++ b/Tests/main.cpp @@ -20,6 +20,27 @@ BOOST_AUTO_TEST_CASE(Parenting) BOOST_CHECK(world.GetEntityParent(ent2) == ent1); } +BOOST_AUTO_TEST_CASE(SceneGraphTraversal) +{ + World world; + + EntityID ent1 = world.CreateEntity(); + auto t1 = world.AddComponent(ent1, "Transform"); + t1->Position[0] = 1.0f; + t1->Position[1] = 1.0f; + t1->Position[2] = 1.0f; + EntityID ent2 = world.CreateEntity(ent1); + auto t2 = world.AddComponent(ent2, "Transform"); + t2->Position[0] = 1.0f; + t2->Position[1] = 1.0f; + t2->Position[2] = 1.0f; + + world.Update(0.0); + + LOG_INFO("ent1: %f %f %f", t1->Position[0], t1->Position[1], t1->Position[2]); + LOG_INFO("ent2: %f %f %f", t2->Position[0], t2->Position[1], t2->Position[2]); +} + BOOST_AUTO_TEST_CASE(Recycling) { World world;