#include "wall-traits.hpp" #include "compat/exception.hpp" #include "compat/vector-wrapper.hpp" #include "atlas-loader-storage.hpp" #include "wall-cell.hpp" #include "loader.hpp" #include "src/tile-defs.hpp" #include "src/wall-atlas.hpp" #include #include #include #include namespace floormat::loader_detail { namespace { const auto placeholder_cell = wall_cell{}; } using wall_traits = atlas_loader_traits; StringView atlas_loader_traits::loader_name() { return "wall_atlas"_s; } auto atlas_loader_traits::atlas_of(const Cell& x) -> const std::shared_ptr& { return x.atlas; } auto atlas_loader_traits::atlas_of(Cell& x) -> std::shared_ptr& { return x.atlas; } StringView atlas_loader_traits::name_of(const Cell& x) { return x.name; } StringView atlas_loader_traits::name_of(const Atlas& x) { return x.name(); } void wall_traits::ensure_atlases_loaded(Storage& st) { if (!st.cell_array.empty()) [[likely]] return; fm_assert(st.name_map.empty()); st.cell_array = wall_cell::load_atlases_from_json().vec; st.name_map.reserve(st.cell_array.size()); for (auto& c : st.cell_array) { fm_soft_assert(c.name != ""_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 wall_traits::make_invalid_atlas(Storage& st) -> const Cell& { 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( wall_atlas_def { Wall::Info{.name = name, .depth = 8}, array({{ {}, frame_size}, }), array({{ { .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{InPlaceInit, wall_cell{ .name = name, .atlas = std::move(a) } }; return *st.invalid_atlas; } auto wall_traits::make_atlas(StringView name, const Cell&) -> std::shared_ptr { 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(std::move(def), file, tex); return atlas; } } // namespace floormat::loader_detail