From b2be7d57642197c0f65d2645c767c4f868ababb1 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 11 Feb 2024 06:47:28 +0100 Subject: implement anim_loader --- loader/anim-atlas.cpp | 123 +++++++--------------------------------- loader/atlas-loader-storage.hpp | 3 + loader/atlas-loader.inl | 5 +- loader/impl.cpp | 11 +--- loader/impl.hpp | 9 ++- loader/loader.hpp | 4 +- 6 files changed, 34 insertions(+), 121 deletions(-) (limited to 'loader') diff --git a/loader/anim-atlas.cpp b/loader/anim-atlas.cpp index 9f8de563..be7cc688 100644 --- a/loader/anim-atlas.cpp +++ b/loader/anim-atlas.cpp @@ -1,127 +1,42 @@ #include "impl.hpp" -#include "loader/anim-cell.hpp" +#include "atlas-loader.inl" +#include "anim-cell.hpp" +#include "anim-traits.hpp" #include "compat/exception.hpp" -#include "src/anim-atlas.hpp" -#include -#include -#include -#include +//#include "src/anim-atlas.hpp" -namespace floormat { - -std::shared_ptr -loader_::get_anim_atlas(StringView path) noexcept(false) -{ - auto anim_info = deserialize_anim_def(path + ".json"); - - for (anim_group& group : anim_info.groups) - { - if (!group.mirror_from.isEmpty()) - { - auto it = std::find_if(anim_info.groups.cbegin(), anim_info.groups.cend(), - [&](const anim_group& x) { return x.name == group.mirror_from; }); - if (it == anim_info.groups.cend()) - fm_throw("can't find group '{}' to mirror from '{}'"_cf, group.mirror_from, group.name); - group.frames = array(arrayView(it->frames)); - for (anim_frame& f : group.frames) - f.ground = Vector2i((Int)f.size[0] - f.ground[0], f.ground[1]); - } - } - - auto tex = texture(""_s, path); +namespace floormat::loader_detail { - fm_soft_assert(!anim_info.object_name.isEmpty()); - fm_soft_assert(anim_info.pixel_size.product() > 0); - fm_soft_assert(!anim_info.groups.isEmpty()); - fm_soft_assert(anim_info.nframes > 0); - fm_soft_assert(anim_info.nframes == 1 || anim_info.fps > 0); - const auto size = tex.pixels().size(); - const auto width = size[1], height = size[0]; - fm_soft_assert(anim_info.pixel_size[0] == width && anim_info.pixel_size[1] == height); +template class atlas_loader; - auto atlas = std::make_shared(path, tex, std::move(anim_info)); - return atlas; +std::shared_ptr loader_impl::get_anim_atlas(StringView path) noexcept(false) +{ + return _anim_loader->make_atlas(path, {}); } -} // namespace floormat - -namespace floormat::loader_detail { - -ArrayView loader_impl::anim_atlas_list() +atlas_loader* loader_impl::make_anim_atlas_loader() { - if (anim_atlases.empty()) - get_anim_atlas_list(); - fm_assert(!anim_atlases.empty()); - return { anim_atlases.data(), anim_atlases.size() }; + return new atlas_loader; } -std::shared_ptr loader_impl::anim_atlas(StringView name, StringView dir, loader_policy policy) noexcept(false) +ArrayView loader_impl::anim_atlas_list() { - if (name == INVALID) return make_invalid_anim_atlas().atlas; // todo! hack + return _anim_loader->ensure_atlas_list(); +} +std::shared_ptr loader_impl::anim_atlas(StringView name, StringView dir, loader_policy p) noexcept(false) +{ 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); - if (auto it = anim_atlas_map.find(path); it != anim_atlas_map.end()) - return it->second; - else - { - auto atlas = get_anim_atlas(path); - return anim_atlas_map[atlas->name()] = atlas; - } -} - -void loader_impl::get_anim_atlas_list() -{ - anim_atlases.clear(); - using f = Path::ListFlag; - constexpr auto flags = f::SkipDirectories | f::SkipDotAndDotDot | f::SkipSpecial | f::SortAscending; - if (const auto list = Path::list(ANIM_PATH, flags); list) - { - anim_atlases.reserve(list->size()); - constexpr auto suffix = ".json"_s; - for (StringView str : *list) - if (str.hasSuffix(suffix)) - anim_atlases.emplace_back(str.exceptSuffix(suffix.size())); - } - anim_atlases.shrink_to_fit(); - fm_assert(!anim_atlases.empty()); + return _anim_loader->get_atlas(path, p); } const anim_cell& loader_impl::make_invalid_anim_atlas() { - if (invalid_anim_atlas) [[likely]] - return *invalid_anim_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 = INVALID, - .anim_name = INVALID, - .groups = Utility::move(groups), - .pixel_size = size, - .scale = anim_scale::fixed{size.x(), true}, - .nframes = 1, - }; - auto atlas = std::make_shared(INVALID, make_error_texture(size), std::move(def)); - auto info = anim_cell { - .atlas = atlas, - .name = INVALID, - }; - invalid_anim_atlas = Pointer{ InPlace, std::move(info) }; - return *invalid_anim_atlas; + return _anim_loader->get_invalid_atlas(); } } // namespace floormat::loader_detail diff --git a/loader/atlas-loader-storage.hpp b/loader/atlas-loader-storage.hpp index c5e45cc2..ca3ccbab 100644 --- a/loader/atlas-loader-storage.hpp +++ b/loader/atlas-loader-storage.hpp @@ -10,7 +10,10 @@ namespace floormat::loader_detail { template struct atlas_storage { + static_assert(std::is_same_v); + using Traits = TRAITS; + using Atlas = typename TRAITS::Atlas; using Cell = typename TRAITS::Cell; tsl::robin_map name_map; diff --git a/loader/atlas-loader.inl b/loader/atlas-loader.inl index c2a7cb88..08667d63 100644 --- a/loader/atlas-loader.inl +++ b/loader/atlas-loader.inl @@ -40,7 +40,8 @@ auto atlas_loader::ensure_atlas_list() -> ArrayView } s.name_map.max_load_factor(0.4f); - s.name_map.reserve(s.cell_array.size()*3/2 + 1); + if (!s.cell_array.empty()) + s.name_map.reserve(s.cell_array.size()*5/2 + 1); for (auto i = 0uz; const auto& c : s.cell_array) s.name_map[t.name_of(c)] = i++; @@ -172,7 +173,7 @@ template auto atlas_loader::make_atlas(StringView name, const Cell& c) -> std::shared_ptr { fm_assert(name != ""_s); - fm_soft_assert(!c.name || t.name_of(c) == name); + fm_soft_assert(!t.name_of(c) || t.name_of(c) == name); fm_soft_assert(loader.check_atlas_name(name)); return t.make_atlas(name, c); } diff --git a/loader/impl.cpp b/loader/impl.cpp index 1d758c4e..704d6de2 100644 --- a/loader/impl.cpp +++ b/loader/impl.cpp @@ -1,16 +1,13 @@ #include "impl.hpp" #include "compat/assert.hpp" -#include "scenery-cell.hpp" -#include "wall-cell.hpp" -#include "anim-cell.hpp" #include "ground-traits.hpp" #include "ground-cell.hpp" -// wall #include "wall-traits.hpp" #include "wall-cell.hpp" -// todo scenery_traits +#include "anim-traits.hpp" #include "anim-cell.hpp" #include "scenery-cell.hpp" +// todo scenery_traits #include "vobj-cell.hpp" #include "atlas-loader.hpp" #include "atlas-loader-storage.hpp" @@ -56,9 +53,7 @@ void loader_impl::destroy() { _ground_loader = {InPlace}; _wall_loader = {InPlace}; - anim_atlas_map.clear(); - anim_atlases.clear(); - invalid_anim_atlas = nullptr; + _anim_loader = {InPlace}; sceneries_map.clear(); sceneries_array.clear(); diff --git a/loader/impl.hpp b/loader/impl.hpp index cc24981f..02d28939 100644 --- a/loader/impl.hpp +++ b/loader/impl.hpp @@ -58,13 +58,12 @@ struct loader_impl final : loader_ std::shared_ptr get_ground_atlas(StringView name, Vector2ub size, pass_mode pass) noexcept(false) override; // >-----> anim >-----> - tsl::robin_map> anim_atlas_map; - std::vector anim_atlases; - Pointer invalid_anim_atlas; - ArrayView anim_atlas_list() override; + [[nodiscard]] static atlas_loader* make_anim_atlas_loader(); + safe_ptr> _anim_loader{ make_anim_atlas_loader() }; + ArrayView anim_atlas_list() override; std::shared_ptr anim_atlas(StringView name, StringView dir, loader_policy policy) noexcept(false) override; - void get_anim_atlas_list(); const anim_cell& make_invalid_anim_atlas() override; + std::shared_ptr get_anim_atlas(StringView path) noexcept(false) override; // >-----> scenery >-----> std::vector sceneries_array; diff --git a/loader/loader.hpp b/loader/loader.hpp index 701f38d7..501e25f5 100644 --- a/loader/loader.hpp +++ b/loader/loader.hpp @@ -31,7 +31,7 @@ struct loader_ virtual Trade::ImageData2D make_error_texture(Vector2ui size) = 0; virtual Trade::ImageData2D texture(StringView prefix, StringView filename) noexcept(false) = 0; virtual const std::shared_ptr& ground_atlas(StringView filename, loader_policy policy = loader_policy::DEFAULT) noexcept(false) = 0; - virtual ArrayView anim_atlas_list() = 0; + virtual ArrayView anim_atlas_list() = 0; virtual std::shared_ptr anim_atlas(StringView name, StringView dir, loader_policy policy = loader_policy::DEFAULT) noexcept(false) = 0; virtual const std::shared_ptr& wall_atlas(StringView name, loader_policy policy = loader_policy::DEFAULT) noexcept(false) = 0; virtual ArrayView wall_atlas_list() = 0; @@ -56,7 +56,7 @@ struct loader_ /** \deprecated{internal use only}*/ [[nodiscard]] virtual std::shared_ptr get_wall_atlas(StringView name) noexcept(false) = 0; /** \deprecated{internal use only}*/ [[nodiscard]] - std::shared_ptr get_anim_atlas(StringView path) noexcept(false); + virtual std::shared_ptr get_anim_atlas(StringView path) noexcept(false) = 0; virtual ~loader_() noexcept; fm_DECLARE_DELETED_COPY_ASSIGNMENT(loader_); -- cgit v1.2.3