From c6abddc2349685a9476b6838a59893a17e443d2f Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 11 Feb 2024 01:58:39 +0100 Subject: wip anim atlas loader --- loader/anim-atlas.cpp | 1 + loader/anim-traits.cpp | 62 ++++++++++++++++++++++++++++++++++++++++- loader/anim-traits.hpp | 2 +- loader/atlas-loader-storage.hpp | 1 + loader/atlas-loader.inl | 6 ++-- loader/ground-traits.cpp | 1 + 6 files changed, 68 insertions(+), 5 deletions(-) diff --git a/loader/anim-atlas.cpp b/loader/anim-atlas.cpp index 3ca61256..6fca5a49 100644 --- a/loader/anim-atlas.cpp +++ b/loader/anim-atlas.cpp @@ -60,6 +60,7 @@ std::shared_ptr loader_impl::anim_atlas(StringView name, StringView if (name == INVALID) return make_invalid_anim_atlas().atlas; // todo! hack fm_soft_assert(check_atlas_name(name)); + fm_soft_assert(!dir || dir[dir.size()-1] == '/'); char buf[fm_FILENAME_MAX]; auto path = make_atlas_path(buf, dir, name); diff --git a/loader/anim-traits.cpp b/loader/anim-traits.cpp index 9741f62e..9ff7e6b5 100644 --- a/loader/anim-traits.cpp +++ b/loader/anim-traits.cpp @@ -1,13 +1,73 @@ #include "anim-traits.hpp" -#include "compat/exception.hpp" +#include "atlas-loader-storage.hpp" +#include "anim-cell.hpp" +#include "src/anim-atlas.hpp" +#include "src/tile-defs.hpp" #include "loader.hpp" +#include "compat/exception.hpp" #include +#include #include #include #include namespace floormat::loader_detail { +using anim_traits = atlas_loader_traits; +StringView anim_traits::loader_name() { return "anim_atlas"_s; } +auto anim_traits::atlas_of(const Cell& x) -> const std::shared_ptr& { return x.atlas; } +auto anim_traits::atlas_of(Cell& x) -> std::shared_ptr& { return x.atlas; } +StringView anim_traits::name_of(const Cell& x) { return x.name; } +StringView anim_traits::name_of(const Atlas& x) { return x.name(); } +String& anim_traits::name_of(Cell& x) { return x.name; } + +void anim_traits::ensure_atlases_loaded(Storage& s) +{ + fm_assert(s.name_map.empty()); + s.cell_array = {}; + s.cell_array.reserve(16); + s.name_map[loader.INVALID] = -1uz; +} + +auto anim_traits::make_invalid_atlas(Storage& s) -> Pointer +{ + fm_debug_assert(!s.invalid_atlas); + constexpr auto size = Vector2ui{16}; + + auto frame = anim_frame { + .ground = Vector2i(size/2), + .offset = {}, + .size = size, + }; + auto groups = Array{ValueInit, 1}; + groups[0] = anim_group { + .name = "n"_s, + .frames = array({ frame }), + }; + auto def = anim_def { + .object_name = loader.INVALID, + .anim_name = loader.INVALID, + .groups = Utility::move(groups), + .pixel_size = size, + .scale = anim_scale::fixed{size.x(), true}, + .nframes = 1, + }; + auto atlas = std::make_shared(loader.INVALID, loader.make_error_texture(size), std::move(def)); + auto info = anim_cell{ + .name = loader.INVALID, + .atlas = atlas, + }; + return Pointer{ InPlace, std::move(info) }; +} + +auto anim_traits::make_atlas(StringView name, const Cell&) -> std::shared_ptr +{ + return {}; // todo +} +auto anim_traits::make_cell(StringView) -> Optional +{ + return {}; +} } // namespace floormat::loader_detail diff --git a/loader/anim-traits.hpp b/loader/anim-traits.hpp index 3c4a43af..8eef8b89 100644 --- a/loader/anim-traits.hpp +++ b/loader/anim-traits.hpp @@ -20,7 +20,7 @@ template<> struct atlas_loader_traits static StringView name_of(const Atlas& x); static String& name_of(Cell& x); static void ensure_atlases_loaded(Storage& st); - static const Cell& make_invalid_atlas(Storage& st); + static Pointer make_invalid_atlas(Storage& st); static std::shared_ptr make_atlas(StringView name, const Cell& c); static Optional make_cell(StringView name); }; diff --git a/loader/atlas-loader-storage.hpp b/loader/atlas-loader-storage.hpp index c8bd3261..c5e45cc2 100644 --- a/loader/atlas-loader-storage.hpp +++ b/loader/atlas-loader-storage.hpp @@ -2,6 +2,7 @@ #include "compat/int-hash.hpp" #include "atlas-loader-fwd.hpp" #include +#include #include namespace floormat::loader_detail { diff --git a/loader/atlas-loader.inl b/loader/atlas-loader.inl index f5cd39ea..c1093f82 100644 --- a/loader/atlas-loader.inl +++ b/loader/atlas-loader.inl @@ -50,12 +50,12 @@ auto atlas_loader::ensure_atlas_list() -> ArrayView s.name_map[loader.INVALID] = sz; } - fm_assert(!s.name_map.empty()); - fm_assert(!s.cell_array.empty()); - for (const auto& [name, index] : s.name_map) fm_assert(index < s.cell_array.size() || index == -1uz); + fm_debug_assert(!s.name_map.empty()); + fm_debug_assert(!s.cell_array.empty()); + return { s.cell_array.data(), s.cell_array.size() }; } diff --git a/loader/ground-traits.cpp b/loader/ground-traits.cpp index e56f0692..f1795f4e 100644 --- a/loader/ground-traits.cpp +++ b/loader/ground-traits.cpp @@ -32,6 +32,7 @@ void ground_traits::ensure_atlases_loaded(Storage& s) auto ground_traits::make_invalid_atlas(Storage& s) -> Pointer { + fm_debug_assert(!s.invalid_atlas); fm_assert(!s.invalid_atlas); auto atlas = std::make_shared( ground_def{loader.INVALID, Vector2ub{1,1}, pass_mode::pass}, -- cgit v1.2.3