summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--loader/anim-traits.cpp40
-rw-r--r--loader/atlas.cpp15
-rw-r--r--loader/loader.hpp4
-rw-r--r--test/anim-atlas.cpp16
-rw-r--r--test/app.cpp1
-rw-r--r--test/app.hpp26
6 files changed, 79 insertions, 23 deletions
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 <cr/Move.h>
#include <cr/StringView.h>
+#include <cr/Array.h>
+#include <cr/StridedArrayView.h>
#include <cr/Pointer.h>
#include <cr/Optional.h>
-#include <cr/Move.h>
#include <mg/ImageData.h>
#include <mg/ImageView.h>
@@ -62,9 +66,39 @@ auto anim_traits::make_invalid_atlas(Storage& s) -> Pointer<Cell>
return Pointer<anim_cell>{ InPlace, Utility::move(info) };
}
-auto anim_traits::make_atlas(StringView name, const Cell& c) -> std::shared_ptr<Atlas>
+auto anim_traits::make_atlas(StringView name, const Cell&) -> std::shared_ptr<Atlas>
{
- 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<struct anim_def>(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<const anim_frame>{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<class anim_atlas>(name, tex, std::move(anim_info));
+ return atlas;
}
auto anim_traits::make_cell(StringView name) -> Optional<Cell>
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 <memory>
-#include <Corrade/Containers/String.h>
+#include <cr/StringView.h>
//namespace Magnum { using Vector2ub = Math::Vector2<unsigned char>; }
namespace Magnum::Trade { template<uint32_t> 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<const struct vobj_cell> 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;
diff --git a/test/anim-atlas.cpp b/test/anim-atlas.cpp
new file mode 100644
index 00000000..9f84a6ff
--- /dev/null
+++ b/test/anim-atlas.cpp
@@ -0,0 +1,16 @@
+#include "app.hpp"
+
+namespace {
+
+
+
+} // namespace
+
+namespace floormat {
+
+void test_app::test_anim_atlas()
+{
+
+}
+
+} // namespace floormat
diff --git a/test/app.cpp b/test/app.cpp
index 06049609..99326c29 100644
--- a/test/app.cpp
+++ b/test/app.cpp
@@ -60,6 +60,7 @@ int test_app::exec()
FM_TEST(test_bitmask),
FM_TEST(test_loader),
FM_TEST(test_serializer1),
+ FM_TEST(test_anim_atlas),
FM_TEST(test_scenery),
FM_TEST(test_astar_pool),
FM_TEST(test_astar),
diff --git a/test/app.hpp b/test/app.hpp
index d7e66b17..41f01507 100644
--- a/test/app.hpp
+++ b/test/app.hpp
@@ -21,27 +21,29 @@ struct test_app final : private FM_APPLICATION
int exec() override;
+ static void test_anim_atlas();
+ static void test_astar();
+ static void test_astar_pool();
+ static void test_bitmask();
static void test_coords();
- static void test_tile_iter();
- static void test_magnum_math();
- static void test_math();
- static void test_iptr();
+ static void test_dijkstra();
static void test_entity();
- static void test_serializer1();
- static void test_bitmask();
static void test_hash();
+ static void test_iptr();
static void test_json();
- static void test_wall_atlas();
- static void test_dijkstra();
- static void test_loader();
static void test_json2();
static void test_json3();
static void test_load_all();
- static void test_astar();
+ static void test_loader();
+ static void test_magnum_math();
+ static void test_math();
+ static void test_raycast();
static void test_scenery();
- static void test_astar_pool();
+ static void test_serializer1();
+ static void test_tile_iter();
+ static void test_wall_atlas();
static void test_wall_atlas2();
- static void test_raycast();
+
static void test_zzz_misc();
};
} // namespace floormat