summaryrefslogtreecommitdiffhomepage
path: root/loader
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-02-08 23:09:20 +0100
committerStanislaw Halik <sthalik@misaki.pl>2024-02-08 23:09:20 +0100
commit4f7b007d3239fd1ce3566b30ab86434ccda1da48 (patch)
tree651c0eea8706e8a2c9fdac5ce4566f1dff5e691a /loader
parent55a76536bbcc0ab662c3b56e4af7370d5932f063 (diff)
loader: wall loader wip
Diffstat (limited to 'loader')
-rw-r--r--loader/wall-atlas.cpp21
-rw-r--r--loader/wall-atlas.hpp7
-rw-r--r--loader/wall-cell.cpp36
-rw-r--r--loader/wall-traits.cpp55
-rw-r--r--loader/wall-traits.hpp26
5 files changed, 128 insertions, 17 deletions
diff --git a/loader/wall-atlas.cpp b/loader/wall-atlas.cpp
index 5baa24f3..3c4e3896 100644
--- a/loader/wall-atlas.cpp
+++ b/loader/wall-atlas.cpp
@@ -3,9 +3,10 @@
#include "loader/wall-cell.hpp"
#include "compat/assert.hpp"
#include "compat/exception.hpp"
+#include "compat/vector-wrapper.hpp"
#include "src/wall-atlas.hpp"
-#include "serialize/json-helper.hpp"
-#include "serialize/corrade-string.hpp"
+//#include "serialize/json-helper.hpp"
+//#include "serialize/corrade-string.hpp"
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/ArrayViewStl.h>
#include <Corrade/Containers/StringIterable.h>
@@ -15,20 +16,6 @@
namespace floormat {
-using nlohmann::json;
-
-[[maybe_unused]] static void from_json(const json& j, wall_cell& val)
-{
- val = {};
- val.name = j["name"];
- fm_soft_assert(loader.check_atlas_name(val.name));
-}
-
-[[maybe_unused]] static void to_json(json& j, const wall_cell& val)
-{
- j["name"] = val.name;
-}
-
std::shared_ptr<wall_atlas> loader_::get_wall_atlas(StringView name) noexcept(false)
{
fm_assert(name != "<invalid>"_s);
@@ -140,7 +127,7 @@ void loader_impl::get_wall_atlas_list()
{
fm_assert(wall_atlas_map.empty());
- wall_atlas_array = json_helper::from_json<std::vector<wall_cell>>(Path::join(WALL_TILESET_PATH, "walls.json"_s));
+ wall_atlas_array = wall_cell::load_atlases_from_json().vec;
wall_atlas_array.shrink_to_fit();
wall_atlas_map.clear();
wall_atlas_map.reserve(wall_atlas_array.size()*2);
diff --git a/loader/wall-atlas.hpp b/loader/wall-atlas.hpp
new file mode 100644
index 00000000..b388f543
--- /dev/null
+++ b/loader/wall-atlas.hpp
@@ -0,0 +1,7 @@
+#pragma once
+
+namespace floormat::loader_detail {
+
+
+
+} // namespace floormat::loader_detail
diff --git a/loader/wall-cell.cpp b/loader/wall-cell.cpp
new file mode 100644
index 00000000..99ba2fcc
--- /dev/null
+++ b/loader/wall-cell.cpp
@@ -0,0 +1,36 @@
+#include "wall-cell.hpp"
+#include "compat/vector-wrapper.hpp"
+#include "compat/exception.hpp"
+#include "serialize/json-helper.hpp"
+#include "serialize/corrade-string.hpp"
+#include "loader/loader.hpp"
+
+namespace floormat {
+
+using nlohmann::json;
+
+[[maybe_unused]] static void from_json(const json& j, wall_cell& val)
+{
+ val = {};
+ val.name = j["name"];
+ fm_soft_assert(loader.check_atlas_name(val.name));
+}
+
+[[maybe_unused]] static void to_json(json& j, const wall_cell& val)
+{
+ j["name"] = val.name;
+}
+
+vector_wrapper<const wall_cell> wall_cell::load_atlases_from_json()
+{
+ char buf[fm_FILENAME_MAX];
+ auto s = loader.make_atlas_path(buf, loader.WALL_TILESET_PATH, "walls.json"_s);
+
+ return { json_helper::from_json<std::vector<wall_cell>>(s) };
+}
+
+} // namespace floormat
+
+namespace floormat::loader_detail {
+
+} // namespace floormat::loader_detail
diff --git a/loader/wall-traits.cpp b/loader/wall-traits.cpp
new file mode 100644
index 00000000..d4217a01
--- /dev/null
+++ b/loader/wall-traits.cpp
@@ -0,0 +1,55 @@
+#include "wall-traits.hpp"
+#include "compat/assert.hpp"
+#include "atlas-loader-storage.hpp"
+#include "wall-cell.hpp"
+#include "src/wall-atlas.hpp"
+#include <Corrade/Containers/StringView.h>
+#include <Corrade/Containers/Pointer.h>
+
+namespace floormat::loader_detail {
+
+StringView atlas_loader_traits<wall_atlas>::loader_name() { return "wall_atlas"_s; }
+auto atlas_loader_traits<wall_atlas>::atlas_of(const Cell& x) -> const std::shared_ptr<Atlas>& { return x.atlas; }
+auto atlas_loader_traits<wall_atlas>::atlas_of(Cell& x) -> std::shared_ptr<Atlas>& { return x.atlas; }
+StringView atlas_loader_traits<wall_atlas>::name_of(const Cell& x) { return x.name; }
+StringView atlas_loader_traits<wall_atlas>::name_of(const Atlas& x) { return x.name(); }
+
+using traits = atlas_loader_traits<wall_atlas>;
+
+void traits::ensure_atlases_loaded(Storage& st)
+{
+ if (!st.cell_array.empty()) [[likely]]
+ return;
+
+#if 0
+ wall_atlas_array = json_helper::from_json<std::vector<wall_cell>>(Path::join(WALL_TILESET_PATH, "walls.json"_s));
+ wall_atlas_array.shrink_to_fit();
+ wall_atlas_map.clear();
+ wall_atlas_map.reserve(wall_atlas_array.size()*2);
+
+ for (auto& x : wall_atlas_array)
+ {
+ fm_soft_assert(x.name != "<invalid>"_s);
+ fm_soft_assert(check_atlas_name(x.name));
+ StringView name = x.name;
+ wall_atlas_map[name] = &x;
+ fm_debug_assert(name.data() == wall_atlas_map[name]->name.data());
+ }
+#endif
+
+ fm_assert("todo" && false);
+
+ fm_assert(!st.cell_array.empty());
+}
+
+auto traits::make_invalid_atlas(Storage& st) -> const Cell&
+{
+ fm_assert("todo" && false);
+}
+
+auto traits::make_atlas(StringView name, const Cell& c) -> std::shared_ptr<Atlas>
+{
+ fm_assert("todo" && false);
+}
+
+} // namespace floormat::loader_detail
diff --git a/loader/wall-traits.hpp b/loader/wall-traits.hpp
new file mode 100644
index 00000000..fb5cc2a0
--- /dev/null
+++ b/loader/wall-traits.hpp
@@ -0,0 +1,26 @@
+#pragma once
+#include "atlas-loader-fwd.hpp"
+#include <memory>
+
+namespace floormat { struct wall_cell; class wall_atlas; }
+
+namespace floormat::loader_detail {
+
+template<> struct atlas_loader_traits<wall_atlas>
+{
+ using Atlas = wall_atlas;
+ using Cell = wall_cell; // convertible to atlas and holding the atlas
+ using Self = atlas_loader_traits<wall_atlas>;
+ using Storage = atlas_storage<wall_atlas, Self>;
+
+ static StringView loader_name();
+ static const std::shared_ptr<Atlas>& atlas_of(const Cell& x);
+ static std::shared_ptr<Atlas>& atlas_of(Cell& x);
+ static StringView name_of(const Cell& x);
+ static StringView name_of(const Atlas& x);
+ static void ensure_atlases_loaded(Storage& st);
+ static const Cell& make_invalid_atlas(Storage& st);
+ static std::shared_ptr<Atlas> make_atlas(StringView name, const Cell& c);
+};
+
+} // namespace floormat::loader_detail