summaryrefslogtreecommitdiffhomepage
path: root/loader/wall-traits.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-02-09 19:39:11 +0100
committerStanislaw Halik <sthalik@misaki.pl>2024-02-09 20:08:55 +0100
commite7ea49a091deff0abe86c5be5f66933213d62e1e (patch)
treeff2af119093e95d3c1c8b21b1635eaebc1780be3 /loader/wall-traits.cpp
parentd0395f8468b33817a79a0fd625ae52cdff06ae19 (diff)
loader: implement wall_traits
Diffstat (limited to 'loader/wall-traits.cpp')
-rw-r--r--loader/wall-traits.cpp58
1 files changed, 44 insertions, 14 deletions
diff --git a/loader/wall-traits.cpp b/loader/wall-traits.cpp
index 4edc3900..9fbe4286 100644
--- a/loader/wall-traits.cpp
+++ b/loader/wall-traits.cpp
@@ -4,21 +4,24 @@
#include "atlas-loader-storage.hpp"
#include "wall-cell.hpp"
#include "loader.hpp"
+#include "src/tile-defs.hpp"
#include "src/wall-atlas.hpp"
-#include <Corrade/Containers/StringView.h>
-#include <Corrade/Containers/Pointer.h>
+#include <cr/StringView.h>
+#include <cr/Pointer.h>
+#include <mg/ImageData.h>
+#include <mg/ImageView.h>
namespace floormat::loader_detail {
+namespace { const auto placeholder_cell = wall_cell{}; }
+using wall_traits = atlas_loader_traits<wall_atlas>;
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)
+void wall_traits::ensure_atlases_loaded(Storage& st)
{
if (!st.cell_array.empty()) [[likely]]
return;
@@ -27,25 +30,52 @@ void traits::ensure_atlases_loaded(Storage& st)
st.cell_array = wall_cell::load_atlases_from_json().vec;
st.name_map.reserve(st.cell_array.size());
- for (auto& x : st.cell_array)
+ for (auto& c : st.cell_array)
{
- fm_soft_assert(x.name != "<invalid>"_s);
- fm_soft_assert(loader.check_atlas_name(x.name));
- StringView name = x.name;
- st.name_map[name] = &x;
+ fm_soft_assert(c.name != "<invalid>"_s);
+ fm_soft_assert(loader.check_atlas_name(c.name));
+ StringView name = c.name;
+ st.name_map[name] = &c;
}
fm_assert(!st.cell_array.empty());
}
-auto traits::make_invalid_atlas(Storage& st) -> const Cell&
+auto wall_traits::make_invalid_atlas(Storage& st) -> const Cell&
{
- fm_assert("todo" && false);
+ if (st.invalid_atlas) [[likely]]
+ return *st.invalid_atlas;
+
+ constexpr auto name = loader_::INVALID;
+ constexpr auto frame_size = Vector2ui{tile_size_xy, tile_size_z};
+
+ auto a = std::make_shared<class wall_atlas>(
+ wall_atlas_def {
+ Wall::Info{.name = name, .depth = 8},
+ array<Wall::Frame>({{ {}, frame_size}, }),
+ array<Wall::Direction>({{
+ { .index = 0, .count = 1, .pixel_size = frame_size, .is_defined = true, }
+ } }),
+ {{ {.val = 0}, {}, }},
+ {1u},
+ }, name, loader.make_error_texture(frame_size));
+ st.invalid_atlas = Pointer<wall_cell>{InPlaceInit, wall_cell{ .name = name, .atlas = std::move(a) } };
+ return *st.invalid_atlas;
}
-auto traits::make_atlas(StringView name, const Cell& c) -> std::shared_ptr<Atlas>
+auto wall_traits::make_atlas(StringView name, const Cell&) -> std::shared_ptr<Atlas>
{
- fm_assert("todo" && false);
+ char file_buf[fm_FILENAME_MAX], json_buf[fm_FILENAME_MAX];
+ auto file = loader.make_atlas_path(file_buf, loader.WALL_TILESET_PATH, name);
+ int json_size = std::snprintf(json_buf, std::size(json_buf), "%s.json", file_buf);
+ fm_soft_assert(json_size != 0 && (size_t)json_size <= std::size_t(json_buf));
+ auto json_name = StringView{json_buf, (size_t)json_size};
+ auto def = wall_atlas_def::deserialize(json_name);
+ auto tex = loader.texture(""_s, file);
+ fm_soft_assert(name == def.header.name);
+ fm_soft_assert(!def.frames.isEmpty());
+ auto atlas = std::make_shared<class wall_atlas>(std::move(def), file, tex);
+ return atlas;
}
} // namespace floormat::loader_detail