From ed7c2a6a73f24f97a0f04c2e6be6862ffe54b585 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 21 Feb 2023 01:25:34 +0000 Subject: Support for loading objects, uses and model factories from an XML resource --- assetFactory/modelFactory.cpp | 17 +++++++++++++++++ assetFactory/modelFactory.h | 9 ++++++++- assetFactory/object.cpp | 22 ++++++++++++++++++++++ assetFactory/object.h | 13 ++++++++++++- assetFactory/use.cpp | 20 ++++++++++++++++++++ assetFactory/use.h | 12 +++++++++++- res/brush47.xml | 15 +++++++++++++++ test/Jamfile.jam | 2 +- test/test-modelFactory.cpp | 23 +++++++++++++++++++++++ 9 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 res/brush47.xml diff --git a/assetFactory/modelFactory.cpp b/assetFactory/modelFactory.cpp index 4c25e48..2642900 100644 --- a/assetFactory/modelFactory.cpp +++ b/assetFactory/modelFactory.cpp @@ -2,7 +2,10 @@ #include "cuboid.h" #include "cylinder.h" #include "modelFactoryMesh_fwd.h" +#include "object.h" #include "plane.h" +#include "saxParse-persistence.h" +#include ModelFactory::ModelFactory() : shapes { @@ -12,3 +15,17 @@ ModelFactory::ModelFactory() : } { } + +std::shared_ptr +ModelFactory::loadXML(const std::filesystem::path & filename) +{ + filesystem::FileStar file {filename.c_str(), "r"}; + return Persistence::SAXParsePersistence {}.loadState>(file); +} + +bool +ModelFactory::persist(Persistence::PersistenceStore & store) +{ + using MapObjects = Persistence::MapByMember; + return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); +} diff --git a/assetFactory/modelFactory.h b/assetFactory/modelFactory.h index f2b1b48..94db055 100644 --- a/assetFactory/modelFactory.h +++ b/assetFactory/modelFactory.h @@ -1,12 +1,19 @@ #pragma once +#include "persistence.h" #include "shape.h" +#include -class ModelFactory { +class ModelFactory : public Persistence::Persistable { public: using Shapes = std::map>; ModelFactory(); + [[nodiscard]] static std::shared_ptr loadXML(const std::filesystem::path &); Shapes shapes; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; }; diff --git a/assetFactory/object.cpp b/assetFactory/object.cpp index 8b70676..faa9a17 100644 --- a/assetFactory/object.cpp +++ b/assetFactory/object.cpp @@ -15,3 +15,25 @@ Object::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) c } return faces; } + +template struct Appender : public Persistence::SelectionT> { + Appender(Container & c) : Persistence::SelectionT> {s}, container {c} { } + using Persistence::SelectionT>::SelectionT; + void + endObject(Persistence::Stack & stk) override + { + container.emplace_back(s); + stk.pop(); + } + +private: + std::shared_ptr s; + Container & container; +}; + +bool +Object::persist(Persistence::PersistenceStore & store) +{ + using UseAppend = Appender; + return STORE_TYPE && STORE_MEMBER(id) && STORE_NAME_HELPER("use", uses, UseAppend); +} diff --git a/assetFactory/object.h b/assetFactory/object.h index da28c1f..1069f66 100644 --- a/assetFactory/object.h +++ b/assetFactory/object.h @@ -1,15 +1,26 @@ #pragma once +#include "persistence.h" #include "shape.h" #include "stdTypeDefs.hpp" #include "use.h" -class Object : public StdTypeDefs, public Shape { +class Object : public StdTypeDefs, public Shape, public Persistence::Persistable { public: + Object() = default; Object(std::string i); CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const override; Use::Collection uses; std::string id; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; + std::string + getId() const override + { + return id; + }; }; diff --git a/assetFactory/use.cpp b/assetFactory/use.cpp index d191329..21e26f3 100644 --- a/assetFactory/use.cpp +++ b/assetFactory/use.cpp @@ -1,4 +1,5 @@ #include "use.h" +#include "modelFactory.h" Shape::CreatedFaces Use::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const @@ -9,3 +10,22 @@ Use::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) cons } return faces; } + +struct Lookup : public Persistence::SelectionV { + using Persistence::SelectionV::SelectionV; + using Persistence::SelectionV::setValue; + void + setValue(std::string && str) override + { + if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("modelFactory"))) { + v = mf->shapes.at(str); + } + } +}; + +bool +Use::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_HELPER(type, Lookup) && STORE_MEMBER(position) && STORE_MEMBER(scale) + && STORE_MEMBER(rotation); +} diff --git a/assetFactory/use.h b/assetFactory/use.h index 28f5459..96f07f6 100644 --- a/assetFactory/use.h +++ b/assetFactory/use.h @@ -2,10 +2,11 @@ #include "faceController.h" #include "modelFactoryMesh_fwd.h" +#include "persistence.h" #include "shape.h" #include "stdTypeDefs.hpp" -class Use : public StdTypeDefs, public Mutation { +class Use : public StdTypeDefs, public Mutation, public Persistence::Persistable { public: using FaceControllers = std::map; @@ -14,4 +15,13 @@ public: Shape::CPtr type; std::string colour; FaceControllers faceControllers; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; + std::string + getId() const override + { + return {}; + }; }; diff --git a/res/brush47.xml b/res/brush47.xml new file mode 100644 index 0000000..8f1d934 --- /dev/null +++ b/res/brush47.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/test/Jamfile.jam b/test/Jamfile.jam index d802975..027b880 100644 --- a/test/Jamfile.jam +++ b/test/Jamfile.jam @@ -52,6 +52,6 @@ run test-text.cpp ; run test-enumDetails.cpp ; run test-render.cpp : : : test ; run test-glContextBhvr.cpp ; -run test-modelFactory.cpp : : : test ; +run test-modelFactory.cpp : -- : ../res/brush47.xml : test ; compile test-static-enumDetails.cpp ; compile test-static-stream_support.cpp ; diff --git a/test/test-modelFactory.cpp b/test/test-modelFactory.cpp index 39362f5..8f2d56c 100644 --- a/test/test-modelFactory.cpp +++ b/test/test-modelFactory.cpp @@ -147,4 +147,27 @@ BOOST_AUTO_TEST_CASE(brush47) render(20); } +BOOST_AUTO_TEST_CASE(brush47xml) +{ + auto mf = ModelFactory::loadXML(RESDIR "/brush47.xml"); + BOOST_REQUIRE(mf); + BOOST_REQUIRE_EQUAL(6, mf->shapes.size()); + BOOST_CHECK(mf->shapes.at("plane")); + BOOST_CHECK(mf->shapes.at("cylinder")); + BOOST_CHECK(mf->shapes.at("cuboid")); + BOOST_CHECK(mf->shapes.at("wheel")); + BOOST_CHECK(mf->shapes.at("axel")); + auto bogey = mf->shapes.at("bogey"); + BOOST_REQUIRE(bogey); + auto bogeyObj = std::dynamic_pointer_cast(bogey); + BOOST_CHECK_EQUAL(3, bogeyObj->uses.size()); + + FactoryMesh::Collection factoryMeshes; + std::transform(factoryMeshes.begin(), factoryMeshes.end(), std::back_inserter(meshes.objects), + [](const FactoryMesh::CPtr & factoryMesh) -> Mesh::Ptr { + return factoryMesh->createMesh(); + }); + + render(20); +} BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3