diff options
-rw-r--r-- | loader/anim-atlas.cpp | 123 | ||||
-rw-r--r-- | loader/atlas-loader-storage.hpp | 3 | ||||
-rw-r--r-- | loader/atlas-loader.inl | 5 | ||||
-rw-r--r-- | loader/impl.cpp | 11 | ||||
-rw-r--r-- | loader/impl.hpp | 9 | ||||
-rw-r--r-- | loader/loader.hpp | 4 | ||||
-rw-r--r-- | serialize/savegame.cpp | 4 | ||||
-rw-r--r-- | test/loader.cpp | 2 |
8 files changed, 36 insertions, 125 deletions
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 <Corrade/Containers/Array.h> -#include <Corrade/Containers/StridedArrayView.h> -#include <Corrade/Utility/Path.h> -#include <Magnum/Trade/ImageData.h> +//#include "src/anim-atlas.hpp" -namespace floormat { - -std::shared_ptr<class anim_atlas> -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<anim_atlas>; - auto atlas = std::make_shared<class anim_atlas>(path, tex, std::move(anim_info)); - return atlas; +std::shared_ptr<class anim_atlas> loader_impl::get_anim_atlas(StringView path) noexcept(false) +{ + return _anim_loader->make_atlas(path, {}); } -} // namespace floormat - -namespace floormat::loader_detail { - -ArrayView<const String> loader_impl::anim_atlas_list() +atlas_loader<class anim_atlas>* 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<class anim_atlas>; } -std::shared_ptr<anim_atlas> loader_impl::anim_atlas(StringView name, StringView dir, loader_policy policy) noexcept(false) +ArrayView<const anim_cell> 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<anim_atlas> 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<anim_group>{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<class anim_atlas>(INVALID, make_error_texture(size), std::move(def)); - auto info = anim_cell { - .atlas = atlas, - .name = INVALID, - }; - invalid_anim_atlas = Pointer<anim_cell>{ 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<typename ATLAS, typename TRAITS> struct atlas_storage { + static_assert(std::is_same_v<typename TRAITS::Atlas, ATLAS>); + using Traits = TRAITS; + using Atlas = typename TRAITS::Atlas; using Cell = typename TRAITS::Cell; tsl::robin_map<StringView, size_t, hash_string_view> 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<ATLAS, TRAITS>::ensure_atlas_list() -> ArrayView<const Cell> } 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<typename ATLAS, typename TRAITS> auto atlas_loader<ATLAS, TRAITS>::make_atlas(StringView name, const Cell& c) -> std::shared_ptr<Atlas> { fm_assert(name != "<invalid>"_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<class ground_atlas> get_ground_atlas(StringView name, Vector2ub size, pass_mode pass) noexcept(false) override; // >-----> anim >-----> - tsl::robin_map<StringView, std::shared_ptr<class anim_atlas>> anim_atlas_map; - std::vector<String> anim_atlases; - Pointer<anim_cell> invalid_anim_atlas; - ArrayView<const String> anim_atlas_list() override; + [[nodiscard]] static atlas_loader<class anim_atlas>* make_anim_atlas_loader(); + safe_ptr<atlas_loader<class anim_atlas>> _anim_loader{ make_anim_atlas_loader() }; + ArrayView<const anim_cell> anim_atlas_list() override; std::shared_ptr<class anim_atlas> 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<class anim_atlas> get_anim_atlas(StringView path) noexcept(false) override; // >-----> scenery >-----> std::vector<scenery_cell> 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<class ground_atlas>& ground_atlas(StringView filename, loader_policy policy = loader_policy::DEFAULT) noexcept(false) = 0; - virtual ArrayView<const String> anim_atlas_list() = 0; + virtual ArrayView<const anim_cell> anim_atlas_list() = 0; virtual std::shared_ptr<class anim_atlas> anim_atlas(StringView name, StringView dir, loader_policy policy = loader_policy::DEFAULT) noexcept(false) = 0; virtual const std::shared_ptr<class wall_atlas>& wall_atlas(StringView name, loader_policy policy = loader_policy::DEFAULT) noexcept(false) = 0; virtual ArrayView<const wall_cell> wall_atlas_list() = 0; @@ -56,7 +56,7 @@ struct loader_ /** \deprecated{internal use only}*/ [[nodiscard]] virtual std::shared_ptr<class wall_atlas> get_wall_atlas(StringView name) noexcept(false) = 0; /** \deprecated{internal use only}*/ [[nodiscard]] - std::shared_ptr<class anim_atlas> get_anim_atlas(StringView path) noexcept(false); + virtual std::shared_ptr<class anim_atlas> get_anim_atlas(StringView path) noexcept(false) = 0; virtual ~loader_() noexcept; fm_DECLARE_DELETED_COPY_ASSIGNMENT(loader_); diff --git a/serialize/savegame.cpp b/serialize/savegame.cpp index a1aae8bf..620d7457 100644 --- a/serialize/savegame.cpp +++ b/serialize/savegame.cpp @@ -767,7 +767,7 @@ struct reader final : visitor_<reader> f(id); switch (type) { - case atlas_type::anim: a = loader.anim_atlas(get_atlas<atlas_type::anim>(id), {}); return; + case atlas_type::anim: a = loader.anim_atlas(get_atlas<atlas_type::anim>(id), {}, loader_policy::warn); return; case atlas_type::vobj: a = loader.vobj(get_atlas<atlas_type::vobj>(id)).atlas; return; case atlas_type::ground: case atlas_type::wall: @@ -882,7 +882,7 @@ ok: atlas = loader.wall_atlas(str, loader_policy::warn).get(); goto ok; case atlas_type::anim: - atlas = loader.anim_atlas(str, {}).get(); + atlas = loader.anim_atlas(str, {}, loader_policy::warn).get(); goto ok; case atlas_type::vobj: atlas = loader.vobj(str).atlas.get(); diff --git a/test/loader.cpp b/test/loader.cpp index c51ad971..e3e6921d 100644 --- a/test/loader.cpp +++ b/test/loader.cpp @@ -80,8 +80,6 @@ void test_app::test_loader() fm_assert(loader.ground_atlas("texel")->pass_mode() == pass_mode::blocked); fm_assert(loader.ground_atlas("metal1")->pass_mode() == pass_mode::pass); loader.sceneries(); - for (StringView name : loader.anim_atlas_list()) - loader.anim_atlas(name, loader.ANIM_PATH); } } // namespace floormat |