From 0de0eb1fe18949a328b9e76a2dbf6319692cc3cb Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 11 Feb 2024 03:53:12 +0100 Subject: wip loader --- loader/anim-traits.cpp | 40 +++++++++++++++++++++++++++++++++++++--- loader/atlas.cpp | 15 +++++++++------ loader/loader.hpp | 4 ++-- 3 files changed, 48 insertions(+), 11 deletions(-) (limited to 'loader') diff --git a/loader/anim-traits.cpp b/loader/anim-traits.cpp index 925fcfad..e15956e9 100644 --- a/loader/anim-traits.cpp +++ b/loader/anim-traits.cpp @@ -4,11 +4,15 @@ #include "src/anim-atlas.hpp" #include "src/tile-defs.hpp" #include "loader.hpp" +#include "serialize/json-helper.hpp" +#include "serialize/anim.hpp" #include "compat/exception.hpp" +#include #include +#include +#include #include #include -#include #include #include @@ -62,9 +66,39 @@ auto anim_traits::make_invalid_atlas(Storage& s) -> Pointer return Pointer{ InPlace, Utility::move(info) }; } -auto anim_traits::make_atlas(StringView name, const Cell& c) -> std::shared_ptr +auto anim_traits::make_atlas(StringView name, const Cell&) -> std::shared_ptr { - return {}; // todo + char buf[fm_FILENAME_MAX]; + auto json_path = loader.make_atlas_path(buf, {}, name, ".json"_s); + auto anim_info = json_helper::from_json(json_path); + + for (anim_group& group : anim_info.groups) + { + if (!group.mirror_from.isEmpty()) + { + const auto *begin = anim_info.groups.data(), *end = begin + anim_info.groups.size(); + const auto* it = std::find_if(begin, end, [&](const anim_group& x) { return x.name == group.mirror_from; }); + if (it == end) + fm_throw("can't find group '{}' to mirror from '{}'"_cf, group.mirror_from, group.name); + group.frames = array(ArrayView{it->frames.data(), it->frames.size()}); + for (anim_frame& f : group.frames) + f.ground = Vector2i((Int)f.size[0] - f.ground[0], f.ground[1]); + } + } + + auto tex = loader.texture(""_s, name); + + 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(name, tex, std::move(anim_info)); + return atlas; } auto anim_traits::make_cell(StringView name) -> Optional diff --git a/loader/atlas.cpp b/loader/atlas.cpp index b64efde2..b283fa87 100644 --- a/loader/atlas.cpp +++ b/loader/atlas.cpp @@ -14,14 +14,17 @@ namespace floormat { -StringView loader_::make_atlas_path(char(&buf)[fm_FILENAME_MAX], StringView dir, StringView name) +StringView loader_::make_atlas_path(char(&buf)[fm_FILENAME_MAX], StringView dir, StringView name, StringView ext) { fm_soft_assert(!dir || dir[dir.size()-1] == '/'); - const auto dirsiz = dir.size(), namesiz = name.size(); - fm_soft_assert(dirsiz + namesiz + 1 < fm_FILENAME_MAX); - std::memcpy(buf, dir.data(), dirsiz); - std::memcpy(&buf[dirsiz], name.data(), namesiz); - auto len = dirsiz + namesiz; + fm_soft_assert(name); + fm_soft_assert(!ext || ext[0] == '.'); + const auto dirsiz = dir.size(), namesiz = name.size(), extsiz = ext.size(), + len = dirsiz + namesiz + extsiz; + fm_soft_assert(len < fm_FILENAME_MAX); + std::memcpy(&buf[0], dir.data(), dirsiz ); + std::memcpy(&buf[dirsiz], name.data(), namesiz); + std::memcpy(&buf[dirsiz + namesiz], ext.data(), extsiz ); buf[len] = '\0'; return StringView{buf, len, StringViewFlag::NullTerminated}; } diff --git a/loader/loader.hpp b/loader/loader.hpp index 60fda8b6..701f38d7 100644 --- a/loader/loader.hpp +++ b/loader/loader.hpp @@ -3,7 +3,7 @@ #include "src/pass-mode.hpp" #include "loader/policy.hpp" #include -#include +#include //namespace Magnum { using Vector2ub = Math::Vector2; } namespace Magnum::Trade { template class ImageData; using ImageData2D = ImageData<2>; } @@ -44,7 +44,7 @@ struct loader_ static StringView strip_prefix(StringView name); virtual const vobj_cell& vobj(StringView name) = 0; virtual ArrayView vobj_list() = 0; - static StringView make_atlas_path(char(&buf)[fm_FILENAME_MAX], StringView dir, StringView name); + static StringView make_atlas_path(char(&buf)[fm_FILENAME_MAX], StringView dir, StringView name, StringView extension = {}); [[nodiscard]] static bool check_atlas_name(StringView name) noexcept; virtual const wall_cell& make_invalid_wall_atlas() = 0; -- cgit v1.2.3