From 2083c7ce05683c864f29b7c46de0cd874b103982 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 10 Feb 2024 00:57:16 +0100 Subject: loader: rename serialized_scenery -> scenery_cell --- loader/anim-atlas.cpp | 127 ++++++++++++++++++++++++++++++++++++++++++++++++ loader/anim.cpp | 127 ------------------------------------------------ loader/impl.cpp | 4 +- loader/impl.hpp | 6 +-- loader/loader.cpp | 2 +- loader/loader.hpp | 4 +- loader/scenery-cell.hpp | 13 +++++ loader/scenery.cpp | 8 +-- loader/scenery.hpp | 13 ----- 9 files changed, 152 insertions(+), 152 deletions(-) create mode 100644 loader/anim-atlas.cpp delete mode 100644 loader/anim.cpp create mode 100644 loader/scenery-cell.hpp delete mode 100644 loader/scenery.hpp (limited to 'loader') diff --git a/loader/anim-atlas.cpp b/loader/anim-atlas.cpp new file mode 100644 index 00000000..6fca5a49 --- /dev/null +++ b/loader/anim-atlas.cpp @@ -0,0 +1,127 @@ +#include "impl.hpp" +#include "loader/anim-cell.hpp" +#include "compat/exception.hpp" +#include "src/anim-atlas.hpp" +#include +#include +#include +#include + +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); + + 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() +{ + if (anim_atlases.empty()) + get_anim_atlas_list(); + fm_assert(!anim_atlases.empty()); + return { anim_atlases.data(), anim_atlases.size() }; +} + +std::shared_ptr loader_impl::anim_atlas(StringView name, StringView dir, loader_policy policy) noexcept(false) +{ + 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); + + 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()); +} + +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{ + .name = INVALID, + .atlas = atlas, + }; + invalid_anim_atlas = Pointer{ InPlace, std::move(info) }; + return *invalid_anim_atlas; +} + +} // namespace floormat::loader_detail diff --git a/loader/anim.cpp b/loader/anim.cpp deleted file mode 100644 index 6fca5a49..00000000 --- a/loader/anim.cpp +++ /dev/null @@ -1,127 +0,0 @@ -#include "impl.hpp" -#include "loader/anim-cell.hpp" -#include "compat/exception.hpp" -#include "src/anim-atlas.hpp" -#include -#include -#include -#include - -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); - - 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() -{ - if (anim_atlases.empty()) - get_anim_atlas_list(); - fm_assert(!anim_atlases.empty()); - return { anim_atlases.data(), anim_atlases.size() }; -} - -std::shared_ptr loader_impl::anim_atlas(StringView name, StringView dir, loader_policy policy) noexcept(false) -{ - 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); - - 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()); -} - -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{ - .name = INVALID, - .atlas = atlas, - }; - invalid_anim_atlas = Pointer{ InPlace, std::move(info) }; - return *invalid_anim_atlas; -} - -} // namespace floormat::loader_detail diff --git a/loader/impl.cpp b/loader/impl.cpp index 86e79109..12b71a9b 100644 --- a/loader/impl.cpp +++ b/loader/impl.cpp @@ -1,6 +1,6 @@ #include "impl.hpp" #include "compat/assert.hpp" -#include "scenery.hpp" +#include "scenery-cell.hpp" #include "wall-cell.hpp" #include "anim-cell.hpp" #include "ground-traits.hpp" @@ -10,7 +10,7 @@ #include "wall-cell.hpp" // todo scenery_traits #include "anim-cell.hpp" -#include "scenery.hpp" +#include "scenery-cell.hpp" #include "vobj-cell.hpp" #include "atlas-loader.hpp" #include "atlas-loader-storage.hpp" diff --git a/loader/impl.hpp b/loader/impl.hpp index 3520d91b..cc24981f 100644 --- a/loader/impl.hpp +++ b/loader/impl.hpp @@ -67,9 +67,9 @@ struct loader_impl final : loader_ const anim_cell& make_invalid_anim_atlas() override; // >-----> scenery >-----> - std::vector sceneries_array; - tsl::robin_map sceneries_map; - ArrayView sceneries() override; + std::vector sceneries_array; + tsl::robin_map sceneries_map; + ArrayView sceneries() override; const scenery_proto& scenery(StringView name) noexcept(false) override; void get_scenery_list(); diff --git a/loader/loader.cpp b/loader/loader.cpp index 8b711206..1820b3e7 100644 --- a/loader/loader.cpp +++ b/loader/loader.cpp @@ -2,7 +2,7 @@ #include "ground-cell.hpp" #include "loader/wall-cell.hpp" #include "anim-cell.hpp" -#include "scenery.hpp" +#include "scenery-cell.hpp" namespace floormat { diff --git a/loader/loader.hpp b/loader/loader.hpp index 8056fa8c..2da6b361 100644 --- a/loader/loader.hpp +++ b/loader/loader.hpp @@ -10,7 +10,7 @@ namespace Magnum::Trade { template class ImageData; using ImageData2D namespace floormat { -struct serialized_scenery; +struct scenery_cell; namespace loader_detail {} namespace Serialize {} @@ -38,7 +38,7 @@ struct loader_ virtual void destroy() = 0; static loader_& default_loader() noexcept; virtual ArrayView ground_atlas_list() noexcept(false) = 0; // todo maybe try returning - virtual ArrayView sceneries() = 0; + virtual ArrayView sceneries() = 0; virtual const scenery_proto& scenery(StringView name) noexcept(false) = 0; virtual StringView startup_directory() noexcept = 0; static StringView strip_prefix(StringView name); diff --git a/loader/scenery-cell.hpp b/loader/scenery-cell.hpp new file mode 100644 index 00000000..8c954b5a --- /dev/null +++ b/loader/scenery-cell.hpp @@ -0,0 +1,13 @@ +#pragma once +#include "src/scenery.hpp" +#include + +namespace floormat { + +struct scenery_cell final +{ + String name; + scenery_proto proto; +}; + +} // namespace floormat diff --git a/loader/scenery.cpp b/loader/scenery.cpp index d374571c..d83cb346 100644 --- a/loader/scenery.cpp +++ b/loader/scenery.cpp @@ -5,7 +5,7 @@ #include "serialize/json-helper.hpp" #include "serialize/anim.hpp" #include "serialize/scenery.hpp" -#include "loader/scenery.hpp" +#include "scenery-cell.hpp" #include "loader/anim-cell.hpp" #include #include @@ -24,7 +24,7 @@ namespace floormat::loader_detail { void loader_impl::get_scenery_list() { sceneries_array.clear(); - sceneries_array = json_helper::from_json>(Path::join(SCENERY_PATH, "scenery.json")); + sceneries_array = json_helper::from_json>(Path::join(SCENERY_PATH, "scenery.json")); if constexpr(true) // todo! { @@ -38,7 +38,7 @@ void loader_impl::get_scenery_list() sceneries_map.clear(); sceneries_map.reserve(sceneries_array.size() * 2); - for (const serialized_scenery& s : sceneries_array) + for (const scenery_cell& s : sceneries_array) { if (sceneries_map.contains(s.name)) fm_abort("duplicate scenery name '%s'", s.name.data()); @@ -48,7 +48,7 @@ void loader_impl::get_scenery_list() fm_assert(!sceneries_map.empty()); } -ArrayView loader_impl::sceneries() +ArrayView loader_impl::sceneries() { if (sceneries_array.empty()) [[likely]] get_scenery_list(); diff --git a/loader/scenery.hpp b/loader/scenery.hpp deleted file mode 100644 index eea31ad3..00000000 --- a/loader/scenery.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include "src/scenery.hpp" -#include - -namespace floormat { - -struct serialized_scenery final -{ - String name; - scenery_proto proto; -}; - -} // namespace floormat -- cgit v1.2.3