diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-12-01 13:21:32 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-12-01 13:21:32 +0100 |
commit | 81f68a2c83c0c25259cd526c8bb4839caa361e8f (patch) | |
tree | ff2db492dbd3dddfc341370a4cf4b2a95abdae70 /loader | |
parent | 511d823c2dc2b917afed6a9c50ad940e5c58c5d5 (diff) |
serialize, loader, test: add serializing scenery
Diffstat (limited to 'loader')
-rw-r--r-- | loader/impl.cpp | 1 | ||||
-rw-r--r-- | loader/impl.hpp | 5 | ||||
-rw-r--r-- | loader/json.cpp | 33 | ||||
-rw-r--r-- | loader/loader.hpp | 5 | ||||
-rw-r--r-- | loader/scenery.hpp | 13 |
5 files changed, 50 insertions, 7 deletions
diff --git a/loader/impl.cpp b/loader/impl.cpp index 65516e17..45c5b055 100644 --- a/loader/impl.cpp +++ b/loader/impl.cpp @@ -1,5 +1,6 @@ #include "impl.hpp" #include "compat/assert.hpp" +#include "loader/scenery.hpp" #include <cstring> #include <memory> #include <Corrade/Containers/Pair.h> diff --git a/loader/impl.hpp b/loader/impl.hpp index 6a49269f..a6916ceb 100644 --- a/loader/impl.hpp +++ b/loader/impl.hpp @@ -25,11 +25,16 @@ struct loader_impl final : loader_ std::unordered_map<String, std::shared_ptr<struct anim_atlas>> anim_atlas_map; std::vector<String> anim_atlases; + std::vector<serialized_scenery> sceneries_array; + std::unordered_map<StringView, const serialized_scenery*> sceneries_map; + StringView shader(StringView filename) override; Trade::ImageData2D texture(StringView prefix, StringView filename); std::shared_ptr<struct tile_atlas> tile_atlas(StringView filename, Vector2ub size) override; ArrayView<String> anim_atlas_list() override; std::shared_ptr<struct anim_atlas> anim_atlas(StringView name, StringView dir) override; + const std::vector<serialized_scenery>& sceneries() override; + const scenery_proto& scenery(StringView name) override; void get_anim_atlas_list(); diff --git a/loader/json.cpp b/loader/json.cpp index 1a698a38..710792f8 100644 --- a/loader/json.cpp +++ b/loader/json.cpp @@ -1,8 +1,10 @@ #include "impl.hpp" +#include "compat/assert.hpp" #include "serialize/json-helper.hpp" #include "serialize/anim.hpp" #include "serialize/tile-atlas.hpp" #include "serialize/scenery.hpp" +#include "loader/scenery.hpp" #include <Corrade/Utility/Path.h> namespace floormat::loader_detail { @@ -12,6 +14,32 @@ anim_def loader_impl::deserialize_anim(StringView filename) return json_helper::from_json<anim_def>(filename); } +const std::vector<serialized_scenery>& loader_impl::sceneries() +{ + if (!sceneries_array.empty()) + return sceneries_array; + + sceneries_array = json_helper::from_json<std::vector<serialized_scenery>>(Path::join(SCENERY_PATH, "scenery.json")); + sceneries_map.reserve(sceneries_array.size() * 2); + for (const serialized_scenery& s : sceneries_array) + { + if (sceneries_map.contains(s.name)) + fm_abort("duplicate scenery name '%s'", s.name.data()); + sceneries_map[s.name] = &s; + } + return sceneries_array; +} + +const scenery_proto& loader_impl::scenery(StringView name) +{ + if (sceneries_array.empty()) + (void)sceneries(); + auto it = sceneries_map.find(name); + if (it == sceneries_map.end()) + fm_abort("no such scenery: '%s'", name.data()); + return it->second->proto; +} + } // namespace floormat::loader_detail namespace floormat { @@ -21,9 +49,4 @@ std::vector<std::shared_ptr<struct tile_atlas>> loader_::tile_atlases(StringView return json_helper::from_json<std::vector<std::shared_ptr<struct tile_atlas>>>(Path::join(loader_::IMAGE_PATH, filename)); } -std::vector<Serialize::serialized_scenery> loader_::sceneries() -{ - return json_helper::from_json<std::vector<Serialize::serialized_scenery>>(Path::join(SCENERY_PATH, "scenery.json")); -} - } // namespace floormat diff --git a/loader/loader.hpp b/loader/loader.hpp index 93ac5654..458bf34d 100644 --- a/loader/loader.hpp +++ b/loader/loader.hpp @@ -4,7 +4,7 @@ #include <Corrade/Containers/StringView.h> namespace Magnum { using Vector2ub = Math::Vector2<unsigned char>; } -namespace floormat::Serialize { struct serialized_scenery; } +namespace floormat { struct serialized_scenery; } namespace floormat { @@ -21,7 +21,8 @@ struct loader_ static void destroy(); static loader_& default_loader() noexcept; static std::vector<std::shared_ptr<struct tile_atlas>> tile_atlases(StringView filename); - static std::vector<Serialize::serialized_scenery> sceneries(); + virtual const std::vector<serialized_scenery>& sceneries() = 0; + virtual const scenery_proto& scenery(StringView name) = 0; loader_(const loader_&) = delete; loader_& operator=(const loader_&) = delete; diff --git a/loader/scenery.hpp b/loader/scenery.hpp new file mode 100644 index 00000000..55f3f972 --- /dev/null +++ b/loader/scenery.hpp @@ -0,0 +1,13 @@ +#pragma once +#include "src/scenery.hpp" +#include <Corrade/Containers/String.h> + +namespace floormat { + +struct serialized_scenery final +{ + String name, descr; + scenery_proto proto; +}; + +} // namespace floormat |