diff options
Diffstat (limited to 'loader/anim-atlas.cpp')
-rw-r--r-- | loader/anim-atlas.cpp | 123 |
1 files changed, 19 insertions, 104 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 |