diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-12 17:48:01 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-12 18:24:55 +0100 |
commit | b84cfa301e2fb131275711c67a2e91e3cda65c4e (patch) | |
tree | d060791ca783f6b9c4da5959535ec0eb85dd73cf /loader/atlas.cpp | |
parent | eea6fad65d5c9fecfb47c4a1c516c253cee85fd2 (diff) |
loader: fix static initializer mess
Diffstat (limited to 'loader/atlas.cpp')
-rw-r--r-- | loader/atlas.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/loader/atlas.cpp b/loader/atlas.cpp new file mode 100644 index 00000000..4a5c6cfa --- /dev/null +++ b/loader/atlas.cpp @@ -0,0 +1,61 @@ +#include "impl.hpp" +#include "compat/assert.hpp" +#include "src/emplacer.hpp" +#include "src/tile-atlas.hpp" +#include "src/anim-atlas.hpp" +#include <Corrade/Containers/ArrayViewStl.h> +#include <Corrade/Containers/Pair.h> +#include <Corrade/Containers/StringView.h> +#include <Corrade/Utility/Path.h> +#include <Magnum/Trade/ImageData.h> + +namespace floormat::loader_detail { + +std::shared_ptr<tile_atlas> loader_impl::tile_atlas(StringView name, Vector2ub size) +{ + const emplacer e{[&] { return std::make_shared<struct tile_atlas>(name, texture(IMAGE_PATH, name), size); }}; + auto atlas = tile_atlas_map.try_emplace(name, e).first->second; + return atlas; +} + +ArrayView<String> loader_impl::anim_atlas_list() +{ + if (anim_atlases.empty()) + get_anim_atlas_list(); + return anim_atlases; +} + +std::shared_ptr<anim_atlas> loader_impl::anim_atlas(StringView name) +{ + if (auto it = anim_atlas_map.find(name); it != anim_atlas_map.end()) + return it->second; + else + { + const auto path = Path::join(ANIM_PATH, Path::splitExtension(name).first()); + auto anim_info = deserialize_anim(path + ".json"); + auto tex = texture("", path); + + fm_assert(!anim_info.anim_name.isEmpty() && !anim_info.object_name.isEmpty()); + fm_assert(anim_info.pixel_size.product() > 0); + fm_assert(!anim_info.groups.empty()); + fm_assert(anim_info.nframes > 0); + fm_assert(anim_info.nframes == 1 || anim_info.fps > 0); + + auto atlas = std::make_shared<struct anim_atlas>(path, tex, std::move(anim_info)); + return anim_atlas_map[atlas->name()] = atlas; + } +} + +void loader_impl::get_anim_atlas_list() +{ + anim_atlases.clear(); + anim_atlases.reserve(64); + 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) + for (StringView str : *list) + if (str.hasSuffix(".json")) + anim_atlases.emplace_back(str.exceptSuffix(std::size(".json")-1)); +} + +} // namespace floormat::loader_detail |