From 8c590ca62849cb65f8b1574ff110592aa8e5332d Mon Sep 17 00:00:00 2001 From: Joseph Walton-Rivers <joseph@walton-rivers.uk> Date: Thu, 23 Jun 2022 10:03:34 +0100 Subject: [PATCH] be less lax with header/implemnetation seperation --- fggl/CMakeLists.txt | 1 + fggl/ecs3/prototype/loader.cpp | 51 +++++++++++++++++ include/fggl/ecs3/prototype/loader.hpp | 77 +++++++++----------------- include/fggl/phys/callbacks.hpp | 1 + 4 files changed, 79 insertions(+), 51 deletions(-) diff --git a/fggl/CMakeLists.txt b/fggl/CMakeLists.txt index d19d33e..4e91e98 100644 --- a/fggl/CMakeLists.txt +++ b/fggl/CMakeLists.txt @@ -42,6 +42,7 @@ target_sources(${PROJECT_NAME} ecs/ecs.cpp ecs3/module/module.cpp ecs3/fast/Container.cpp + ecs3/prototype/loader.cpp ecs3/prototype/world.cpp scenes/menu.cpp diff --git a/fggl/ecs3/prototype/loader.cpp b/fggl/ecs3/prototype/loader.cpp index 5c91504..3a30d26 100644 --- a/fggl/ecs3/prototype/loader.cpp +++ b/fggl/ecs3/prototype/loader.cpp @@ -1,6 +1,57 @@ #include "fggl/ecs3/prototype/loader.hpp" +namespace fggl::ecs3 { + + void load_prototype_node( ecs3::World& world, const YAML::Node& node) { + auto prototype = world.create(true); + auto& typeReg = world.types(); + + // store the prototype's name for future use + auto* nameComp = world.get<ecs3::EntityMeta>( prototype ); + nameComp->typeName = node["name"].as<std::string>(); + + // grab the components and iterate them + if ( node["components"] ) { + world.createFromSpec( prototype, node["components"]); + } + } + + void load_prototype_file( ecs3::World& world, data::Storage& storage, const std::string& name ) { + auto path = storage.resolvePath( data::Data, name ); + + auto root = YAML::LoadFile( path.string().c_str() ); + for (const auto& node : root["prefabs"] ) { + load_prototype_node(world, node); + } + } + +} + namespace fggl::ecs { + + static void process_mesh(data::Mesh& out, const YAML::Node& shape) { + auto transform = data::OFFSET_NONE; + if ( shape["offset"] ) { + auto scaleFactor = shape["offset"].as<math::vec3>(); + transform = glm::translate(transform, scaleFactor); + } + + if ( shape["scale"] ) { + auto scaleFactor = shape["scale"].as<math::vec3>(); + transform = glm::scale(transform, scaleFactor); + } + + // now the shape itself + auto shapeType = shape["type"].as<std::string>(); + if ( shapeType == SHAPE_BOX_VALUE ) { + data::make_cube(out, transform); + } else if ( shapeType == SHAPE_SPHERE_VALUE ) { + int stacks = shape["stacks"].as<int>(DEFAULT_STACKS); + int slices = shape["slices"].as<int>(DEFAULT_SLICES); + data::make_sphere(out, transform, stacks, slices); + } + } + template<> bool restore_config(math::Transform* comp, const YAML::Node& node) { comp->origin(node["origin"].as<math::vec3>( math::VEC3_ZERO )); diff --git a/include/fggl/ecs3/prototype/loader.hpp b/include/fggl/ecs3/prototype/loader.hpp index e3c00c8..d9a72d8 100644 --- a/include/fggl/ecs3/prototype/loader.hpp +++ b/include/fggl/ecs3/prototype/loader.hpp @@ -22,6 +22,9 @@ #include "yaml-cpp/yaml.h" #include "fggl/math/types.hpp" +#include "fggl/phys/types.hpp" +#include "fggl/gfx/phong.hpp" + #include "fggl/data/storage.hpp" #include "fggl/data/model.hpp" #include "fggl/data/procedural.hpp" @@ -30,29 +33,8 @@ namespace fggl::ecs3 { - - void load_prototype_node( ecs3::World& world, const YAML::Node& node) { - auto prototype = world.create(true); - auto& typeReg = world.types(); - - // store the prototype's name for future use - auto* nameComp = world.get<ecs3::EntityMeta>( prototype ); - nameComp->typeName = node["name"].as<std::string>(); - - // grab the components and iterate them - if ( node["components"] ) { - world.createFromSpec( prototype, node["components"]); - } - } - - void load_prototype_file( ecs3::World& world, data::Storage& storage, const std::string& name ) { - auto path = storage.resolvePath( data::Data, name ); - - auto root = YAML::LoadFile( path.string().c_str() ); - for (const auto& node : root["prefabs"] ) { - load_prototype_node(world, node); - } - } + void load_prototype_node( ecs3::World& world, const YAML::Node& node); + void load_prototype_file( ecs3::World& world, data::Storage& storage, const std::string& name ); } // namespace fggl::ecs3 @@ -104,12 +86,12 @@ namespace YAML { } }; + constexpr const char* TYPE_KINEMATIC = "kinematic"; + constexpr const char* TYPE_STATIC = "static"; + constexpr const char* TYPE_DYNAMIC = "dynamic"; + template<> struct convert<fggl::phys::BodyType> { - const static std::string TYPE_KINEMATIC; - const static std::string TYPE_STATIC; - const static std::string TYPE_DYN; - static Node encode(const fggl::phys::BodyType& rhs) { switch (rhs) { case fggl::phys::BodyType::KINEMATIC: @@ -118,9 +100,10 @@ namespace YAML { return Node(TYPE_STATIC); default: case fggl::phys::BodyType::DYNAMIC: - return Node(TYPE_DYN); + return Node(TYPE_DYNAMIC); } } + static bool decode(const Node& node, fggl::phys::BodyType& rhs) { const auto value = node.as<std::string>(); if ( value == TYPE_KINEMATIC ) { @@ -134,9 +117,6 @@ namespace YAML { } }; - const std::string convert<fggl::phys::BodyType>::TYPE_KINEMATIC = "kinematic"; - const std::string convert<fggl::phys::BodyType>::TYPE_STATIC = "static"; - const std::string convert<fggl::phys::BodyType>::TYPE_DYN = "dynamic"; } @@ -147,29 +127,24 @@ namespace fggl::ecs { constexpr const char* SHAPE_SPHERE_VALUE{"sphere"}; constexpr const char* SHAPE_BOX_VALUE{"box"}; - static void process_mesh(data::Mesh& out, const YAML::Node& shape) { - auto transform = data::OFFSET_NONE; - if ( shape["offset"] ) { - auto scaleFactor = shape["offset"].as<math::vec3>(); - transform = glm::translate(transform, scaleFactor); - } + // scene template specialisations + template<> + bool restore_config(math::Transform* comp, const YAML::Node& node); - if ( shape["scale"] ) { - auto scaleFactor = shape["scale"].as<math::vec3>(); - transform = glm::scale(transform, scaleFactor); - } + template<> + bool restore_config(gfx::PhongMaterial* comp, const YAML::Node& node); - // now the shape itself - auto shapeType = shape["type"].as<std::string>(); - if ( shapeType == SHAPE_BOX_VALUE ) { - data::make_cube(out, transform); - } else if ( shapeType == SHAPE_SPHERE_VALUE ) { - int stacks = shape["stacks"].as<int>(DEFAULT_STACKS); - int slices = shape["slices"].as<int>(DEFAULT_SLICES); - data::make_sphere(out, transform, stacks, slices); - } - } + template<> + bool restore_config(data::StaticMesh* meshComp, const YAML::Node& node); + template<> + bool restore_config(phys::RigidBody* body, const YAML::Node& node); + + template<> + bool restore_config(phys::CollisionCallbacks* callbacks, const YAML::Node& node); + + template<> + bool restore_config(phys::CollisionCache* cache, const YAML::Node& node); } #endif //FGGL_ECS3_PROTOTYPE_LOADER_HPP diff --git a/include/fggl/phys/callbacks.hpp b/include/fggl/phys/callbacks.hpp index 7907e53..9ff0eaf 100644 --- a/include/fggl/phys/callbacks.hpp +++ b/include/fggl/phys/callbacks.hpp @@ -21,6 +21,7 @@ #include <functional> #include <unordered_set> +#include "fggl/ecs3/types.hpp" namespace fggl::phys { -- GitLab