From e1e8ab80a5ec95f429e5735f6f678eb0447c0421 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 4 Feb 2024 20:37:31 +0100 Subject: loader: add functions for uncached loading --- loader/atlas.cpp | 68 ++++++++++++++++++++++++++++--------------------- loader/ground-atlas.cpp | 10 +++++--- loader/impl.hpp | 2 -- loader/json.cpp | 8 ++++-- loader/loader.hpp | 8 +++++- loader/wall-atlas.cpp | 16 ++++++------ 6 files changed, 66 insertions(+), 46 deletions(-) (limited to 'loader') diff --git a/loader/atlas.cpp b/loader/atlas.cpp index 555a2b02..b39bc8f5 100644 --- a/loader/atlas.cpp +++ b/loader/atlas.cpp @@ -42,8 +42,45 @@ bool loader_::check_atlas_name(StringView str) noexcept return true; } +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); + + 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); + + auto atlas = std::make_shared(path, tex, std::move(anim_info)); + return atlas; +} + } // namespace floormat + + namespace floormat::loader_detail { ArrayView loader_impl::anim_atlas_list() @@ -56,6 +93,7 @@ ArrayView loader_impl::anim_atlas_list() std::shared_ptr loader_impl::anim_atlas(StringView name, StringView dir) noexcept(false) { + fm_soft_assert(check_atlas_name(name)); fm_soft_assert(!dir || dir[dir.size()-1] == '/'); char buf[FILENAME_MAX]; auto path = make_atlas_path(buf, dir, name); @@ -64,35 +102,7 @@ std::shared_ptr loader_impl::anim_atlas(StringView name, StringView return it->second; else { - fm_soft_assert(check_atlas_name(name)); - auto anim_info = deserialize_anim(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); - - 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); - - auto atlas = std::make_shared(path, tex, std::move(anim_info)); + auto atlas = get_anim_atlas(path); return anim_atlas_map[atlas->name()] = atlas; } } diff --git a/loader/ground-atlas.cpp b/loader/ground-atlas.cpp index 1a0d3905..974d6fa8 100644 --- a/loader/ground-atlas.cpp +++ b/loader/ground-atlas.cpp @@ -12,11 +12,9 @@ namespace floormat { using loader_detail::loader_impl; -} // namespace floormat - -namespace floormat::loader_detail { -std::shared_ptr loader_impl::get_ground_atlas(StringView name, Vector2ub size, pass_mode pass) +std::shared_ptr +loader_::get_ground_atlas(StringView name, Vector2ub size, pass_mode pass) noexcept(false) { fm_assert(name != ""_s); @@ -29,6 +27,10 @@ std::shared_ptr loader_impl::get_ground_atlas(StringView name, Vec return atlas; } +} // namespace floormat + +namespace floormat::loader_detail { + // todo copypasta from wall-atlas.cpp std::shared_ptr loader_impl::ground_atlas(StringView name, loader_policy policy) noexcept(false) { diff --git a/loader/impl.hpp b/loader/impl.hpp index 9308baba..b376503b 100644 --- a/loader/impl.hpp +++ b/loader/impl.hpp @@ -54,7 +54,6 @@ struct loader_impl final : loader_ ArrayView wall_atlas_list() override; void get_wall_atlas_list(); const wall_info& make_invalid_wall_atlas(); - std::shared_ptr get_wall_atlas(StringView name, StringView dir); // >-----> tile >-----> tsl::robin_map ground_atlas_map; @@ -65,7 +64,6 @@ struct loader_impl final : loader_ ArrayView ground_atlas_list() noexcept(false) override; void get_ground_atlas_list(); const ground_info& make_invalid_ground_atlas(); - std::shared_ptr get_ground_atlas(StringView name, Vector2ub size, pass_mode pass) noexcept(false) override; // >-----> anim >-----> tsl::robin_map> anim_atlas_map; diff --git a/loader/json.cpp b/loader/json.cpp index 7b12b784..756f8151 100644 --- a/loader/json.cpp +++ b/loader/json.cpp @@ -9,13 +9,17 @@ #include #include -namespace floormat::loader_detail { +namespace floormat { -anim_def loader_impl::deserialize_anim(StringView filename) +anim_def loader_::deserialize_anim_def(StringView filename) noexcept(false) { return json_helper::from_json(filename); } +} // namespace floormat + +namespace floormat::loader_detail { + void loader_impl::get_scenery_list() { sceneries_array.clear(); diff --git a/loader/loader.hpp b/loader/loader.hpp index 63740aa7..5c72a719 100644 --- a/loader/loader.hpp +++ b/loader/loader.hpp @@ -14,6 +14,7 @@ using ImageData2D = ImageData<2>; namespace floormat { +struct anim_def; class anim_atlas; struct scenery_proto; struct vobj_info; @@ -37,7 +38,6 @@ struct loader_ { virtual StringView shader(StringView filename) noexcept = 0; virtual Trade::ImageData2D texture(StringView prefix, StringView filename) noexcept(false) = 0; - virtual std::shared_ptr get_ground_atlas(StringView name, Vector2ub size, pass_mode pass) noexcept(false) = 0; virtual std::shared_ptr ground_atlas(StringView filename, loader_policy policy = loader_policy::DEFAULT) noexcept(false) = 0; virtual ArrayView anim_atlas_list() = 0; virtual std::shared_ptr anim_atlas(StringView name, StringView dir = ANIM_PATH) noexcept(false) = 0; @@ -55,6 +55,10 @@ struct loader_ static StringView make_atlas_path(char(&buf)[FILENAME_MAX], StringView dir, StringView name); [[nodiscard]] static bool check_atlas_name(StringView name) noexcept; + /** \deprecated{internal use only}*/ [[nodiscard]] std::shared_ptr get_ground_atlas(StringView name, Vector2ub size, pass_mode pass) noexcept(false); + /** \deprecated{internal use only}*/ [[nodiscard]] std::shared_ptr get_wall_atlas(StringView name) noexcept(false); + /** \deprecated{internal use only}*/ [[nodiscard]] std::shared_ptr get_anim_atlas(StringView path) noexcept(false); + virtual ~loader_() noexcept; fm_DECLARE_DELETED_COPY_ASSIGNMENT(loader_); fm_DECLARE_DELETED_MOVE_ASSIGNMENT(loader_); @@ -69,6 +73,8 @@ struct loader_ static const StringView WALL_TILESET_PATH; protected: + static anim_def deserialize_anim_def(StringView filename) noexcept(false); + loader_(); }; diff --git a/loader/wall-atlas.cpp b/loader/wall-atlas.cpp index 6e66478b..fa8aabc4 100644 --- a/loader/wall-atlas.cpp +++ b/loader/wall-atlas.cpp @@ -30,24 +30,24 @@ using loader_detail::loader_impl; j["name"] = val.name; } -} // namespace floormat - -namespace floormat::loader_detail { - -std::shared_ptr loader_impl::get_wall_atlas(StringView name, StringView dir) +std::shared_ptr loader_::get_wall_atlas(StringView name) noexcept(false) { fm_assert(name != ""_s); char buf[FILENAME_MAX]; - auto filename = make_atlas_path(buf, dir, name); + auto filename = make_atlas_path(buf, loader.WALL_TILESET_PATH, name); auto def = wall_atlas_def::deserialize(""_s.join({filename, ".json"_s})); auto tex = texture(""_s, filename); fm_soft_assert(name == def.header.name); fm_soft_assert(!def.frames.isEmpty()); - auto atlas = std::make_shared(std::move(def), dir, tex); + auto atlas = std::make_shared(std::move(def), loader.WALL_TILESET_PATH, tex); return atlas; } +} // namespace floormat + +namespace floormat::loader_detail { + const wall_info& loader_impl::make_invalid_wall_atlas() { if (invalid_wall_atlas) [[likely]] @@ -102,7 +102,7 @@ std::shared_ptr loader_impl::wall_atlas(StringView name, loade std::unreachable(); } else if (!it->second->atlas) - return it->second->atlas = get_wall_atlas(name, loader.WALL_TILESET_PATH); + return it->second->atlas = get_wall_atlas(name); else return it->second->atlas; } -- cgit v1.2.3