From 5e561d390dc82b08c20532de0952f428e7b14283 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 21 Feb 2023 19:34:16 +0000 Subject: Rename ModelFactory to AssetFactory --- assetFactory/assetFactory.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 assetFactory/assetFactory.cpp (limited to 'assetFactory/assetFactory.cpp') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp new file mode 100644 index 0000000..0ee1f94 --- /dev/null +++ b/assetFactory/assetFactory.cpp @@ -0,0 +1,31 @@ +#include "assetFactory.h" +#include "cuboid.h" +#include "cylinder.h" +#include "modelFactoryMesh_fwd.h" +#include "object.h" +#include "plane.h" +#include "saxParse-persistence.h" +#include + +AssetFactory::AssetFactory() : + shapes { + {"plane", std::make_shared()}, + {"cuboid", std::make_shared()}, + {"cylinder", std::make_shared()}, + } +{ +} + +std::shared_ptr +AssetFactory::loadXML(const std::filesystem::path & filename) +{ + filesystem::FileStar file {filename.c_str(), "r"}; + return Persistence::SAXParsePersistence {}.loadState>(file); +} + +bool +AssetFactory::persist(Persistence::PersistenceStore & store) +{ + using MapObjects = Persistence::MapByMember; + return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); +} -- cgit v1.2.3 From 320e5cc574f0c8f83def034e36f6d0c57b1f75ac Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 22 Feb 2023 20:54:39 +0000 Subject: Fixup MapByMember to work with shared or unique ptr --- assetFactory/assetFactory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'assetFactory/assetFactory.cpp') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 0ee1f94..470eacf 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -26,6 +26,6 @@ AssetFactory::loadXML(const std::filesystem::path & filename) bool AssetFactory::persist(Persistence::PersistenceStore & store) { - using MapObjects = Persistence::MapByMember; + using MapObjects = Persistence::MapByMember>; return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); } -- cgit v1.2.3 From df2a078c51cee464905c6fb1d1c7c4aa7873f6a1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 22 Feb 2023 23:40:46 +0000 Subject: Implement loading asset, mesh and face definitions --- assetFactory/assetFactory.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'assetFactory/assetFactory.cpp') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 470eacf..564ea28 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -27,5 +27,14 @@ bool AssetFactory::persist(Persistence::PersistenceStore & store) { using MapObjects = Persistence::MapByMember>; - return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); + using MapAssets = Persistence::MapByMember; + return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects) + && STORE_NAME_HELPER("asset", assets, MapAssets); +} + +bool +Asset::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(name) + && STORE_NAME_HELPER("mesh", meshes, Persistence::Appender); } -- cgit v1.2.3 From b7b9eb7bb9deb6704f276fb3b8fd67ba370caf3e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 27 Feb 2023 19:38:14 +0000 Subject: Load the X11 RGB colour definitions into a map --- assetFactory/assetFactory.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'assetFactory/assetFactory.cpp') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 564ea28..016f29e 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -1,4 +1,5 @@ #include "assetFactory.h" +#include "collections.hpp" #include "cuboid.h" #include "cylinder.h" #include "modelFactoryMesh_fwd.h" @@ -12,7 +13,8 @@ AssetFactory::AssetFactory() : {"plane", std::make_shared()}, {"cuboid", std::make_shared()}, {"cylinder", std::make_shared()}, - } + }, + colours {parseX11RGB("/usr/share/X11/rgb.txt")} { } @@ -23,6 +25,30 @@ AssetFactory::loadXML(const std::filesystem::path & filename) return Persistence::SAXParsePersistence {}.loadState>(file); } +AssetFactory::Colours +AssetFactory::parseX11RGB(const char * path) +{ + filesystem::FileStar rgb {path, "r"}; + Colours out; + glm::u8vec3 colour; + char inname[BUFSIZ]; + while (fscanf(rgb, "%hhu %hhu %hhu %[^\n\r]s", &colour.r, &colour.g, &colour.b, inname) == 4) { + std::string name {inname}; + normalizeColourName(name); + out.emplace(std::move(name), colour); + } + return out; +} + +void +AssetFactory::normalizeColourName(std::string & name) +{ + std::erase_if(name, ::isblank); + name *= [l = std::locale {}](auto & ch) { + ch = std::tolower(ch, l); + }; +} + bool AssetFactory::persist(Persistence::PersistenceStore & store) { -- cgit v1.2.3 From 98df33e0a52e086b68df2a62ce2f41cc6b67db63 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 2 Mar 2023 18:18:38 +0000 Subject: Parse colour values as they're read --- assetFactory/assetFactory.cpp | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'assetFactory/assetFactory.cpp') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 016f29e..70f5337 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -30,12 +30,12 @@ AssetFactory::parseX11RGB(const char * path) { filesystem::FileStar rgb {path, "r"}; Colours out; - glm::u8vec3 colour; + Colour colour; char inname[BUFSIZ]; - while (fscanf(rgb, "%hhu %hhu %hhu %[^\n\r]s", &colour.r, &colour.g, &colour.b, inname) == 4) { + while (fscanf(rgb, "%f %f %f %[^\n\r]s", &colour.r, &colour.g, &colour.b, inname) == 4) { std::string name {inname}; normalizeColourName(name); - out.emplace(std::move(name), colour); + out.emplace(std::move(name), colour / 255.f); } return out; } @@ -49,6 +49,32 @@ AssetFactory::normalizeColourName(std::string & name) }; } +AssetFactory::ColourAlpha +AssetFactory::parseColour(std::string_view in) const +{ + if (in.empty()) { + throw std::runtime_error("Empty colour specification"); + } + if (in[0] == '#') { + if (in.length() > 9 || in.length() % 2 == 0) { + throw std::runtime_error("Invalid hex colour specification"); + } + ColourAlpha out {0, 0, 0, 1}; + std::generate_n(&out.r, (in.length() - 1) / 2, [in = in.data() + 1]() mutable { + uint8_t channel; + std::from_chars(in, in + 2, channel, 16); + in += 2; + return static_cast(channel) / 255.f; + }); + return out; + } + if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { + if (const auto colour = mf->colours.find(in); colour != mf->colours.end()) { + return {colour->second, 1}; + } + } + throw std::runtime_error("No such asset factory colour"); +} bool AssetFactory::persist(Persistence::PersistenceStore & store) { -- cgit v1.2.3 From 17ac090dd1dd245cf1e24b62b7333ba9be571bde Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 2 Mar 2023 18:36:34 +0000 Subject: Add ParseBase Acts as a base class for persistence parser, encompasses the parse stack and manages shared objects --- assetFactory/assetFactory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'assetFactory/assetFactory.cpp') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 70f5337..b9724c9 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -68,7 +68,7 @@ AssetFactory::parseColour(std::string_view in) const }); return out; } - if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { + if (auto mf = Persistence::ParseBase::getShared("assetFactory")) { if (const auto colour = mf->colours.find(in); colour != mf->colours.end()) { return {colour->second, 1}; } -- cgit v1.2.3 From 5b7f5f2de723d30d3461f653e90fa4ae95941786 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 3 Mar 2023 19:52:55 +0000 Subject: Split Asset into its own file --- assetFactory/assetFactory.cpp | 7 ------- 1 file changed, 7 deletions(-) (limited to 'assetFactory/assetFactory.cpp') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index b9724c9..f5fc2b3 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -83,10 +83,3 @@ AssetFactory::persist(Persistence::PersistenceStore & store) return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects) && STORE_NAME_HELPER("asset", assets, MapAssets); } - -bool -Asset::persist(Persistence::PersistenceStore & store) -{ - return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(name) - && STORE_NAME_HELPER("mesh", meshes, Persistence::Appender); -} -- cgit v1.2.3