summaryrefslogtreecommitdiffhomepage
path: root/anim
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-09-30 18:48:50 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-09-30 18:50:32 +0200
commit5c9863cf0998b5f1b6107ce09b54cd3e8b484221 (patch)
tree7ca268c80a0ec7ac0c1b815d984cf11309b96840 /anim
parentd3a29055d8b1dce89c77af0988ea840e949d2450 (diff)
.
Diffstat (limited to 'anim')
-rw-r--r--anim/atlas.cpp63
-rw-r--r--anim/atlas.hpp37
-rw-r--r--anim/serialize.cpp83
-rw-r--r--anim/serialize.hpp2
4 files changed, 5 insertions, 180 deletions
diff --git a/anim/atlas.cpp b/anim/atlas.cpp
deleted file mode 100644
index fe4b5f65..00000000
--- a/anim/atlas.cpp
+++ /dev/null
@@ -1,63 +0,0 @@
-#undef NDEBUG
-
-#include "atlas.hpp"
-#include "serialize.hpp"
-
-#include <cassert>
-#include <filesystem>
-#include <opencv2/imgcodecs.hpp>
-
-void anim_atlas_row::add_entry(const anim_atlas_entry& x) noexcept
-{
- auto& frame = *x.frame;
- const auto& mat = x.mat;
- frame.offset = {xpos, ypos};
- frame.size = {mat.cols, mat.rows};
-
- assert(mat.rows > 0 && mat.cols > 0);
- data.push_back(x);
- xpos += mat.cols;
- max_height = std::max(mat.rows, max_height);
-}
-
-void anim_atlas::advance_row() noexcept
-{
- auto& row = rows.back();
- if (row.data.empty())
- return;
- assert(row.xpos); assert(row.max_height);
- ypos += row.max_height;
- maxx = std::max(row.xpos, maxx);
- rows.push_back({{}, 0, 0, ypos});
-}
-
-Magnum::Vector2i anim_atlas::offset() const noexcept
-{
- const auto& row = rows.back();
- return {row.xpos, row.ypos};
-}
-
-Magnum::Vector2i anim_atlas::size() const noexcept
-{
- const anim_atlas_row& row = rows.back();
- // prevent accidentally writing out of bounds by forgetting to call
- // anim_atlas::advance_row() one last time prior to anim_atlas::size()
- return {std::max(maxx, row.xpos), ypos + row.max_height};
-}
-
-bool anim_atlas::dump(const std::filesystem::path& filename) const noexcept
-{
- auto sz = size();
- cv::Mat4b mat(sz[1], sz[0]);
- mat.setTo(0);
-
- for (const anim_atlas_row& row : rows)
- for (const anim_atlas_entry& x : row.data)
- {
- auto offset = x.frame->offset, size = x.frame->size;
- cv::Rect roi = {offset[0], offset[1], size[0], size[1]};
- x.mat.copyTo(mat(roi));
- }
-
- return cv::imwrite(filename.string(), mat);
-}
diff --git a/anim/atlas.hpp b/anim/atlas.hpp
deleted file mode 100644
index 5c5e918f..00000000
--- a/anim/atlas.hpp
+++ /dev/null
@@ -1,37 +0,0 @@
-#pragma once
-
-#include <vector>
-#include <Magnum/Magnum.h>
-#include <Magnum/Math/Vector2.h>
-#include <opencv2/core/mat.hpp>
-
-struct anim_frame;
-
-namespace std::filesystem { class path; }
-
-struct anim_atlas_entry
-{
- anim_frame* frame;
- cv::Mat4b mat;
-};
-
-struct anim_atlas_row
-{
- std::vector<anim_atlas_entry> data;
- int max_height = 0, xpos = 0, ypos = 0;
-
- void add_entry(const anim_atlas_entry& x) noexcept;
-};
-
-class anim_atlas
-{
- std::vector<anim_atlas_row> rows = {{}};
- int ypos = 0, maxx = 0;
-
-public:
- void add_entry(const anim_atlas_entry& x) noexcept { rows.back().add_entry(x); }
- void advance_row() noexcept;
- Magnum::Vector2i offset() const noexcept;
- Magnum::Vector2i size() const noexcept;
- [[nodiscard]] bool dump(const std::filesystem::path& filename) const noexcept;
-};
diff --git a/anim/serialize.cpp b/anim/serialize.cpp
index b0082c3c..83d2b2f9 100644
--- a/anim/serialize.cpp
+++ b/anim/serialize.cpp
@@ -1,50 +1,12 @@
#include "serialize.hpp"
-#include <algorithm>
-#include <utility>
-#include <fstream>
-#include <exception>
-
#include <Corrade/Utility/Debug.h>
#include <Corrade/Utility/DebugStl.h>
-#include "../fake-json.hpp"
+#include "../json-magnum.hpp"
using Corrade::Utility::Error;
-namespace nlohmann {
-
-template<>
-struct adl_serializer<Magnum::Vector2i> final {
- static void to_json(json& j, const Magnum::Vector2i& x);
- static void from_json(const json& j, Magnum::Vector2i& x);
-};
-
-void adl_serializer<Magnum::Vector2i>::to_json(json& j, const Magnum::Vector2i& val)
-{
- char buf[64];
- snprintf(buf, sizeof(buf), "%d x %d", val[0], val[1]);
- j = buf;
-}
-
-void adl_serializer<Magnum::Vector2i>::from_json(const json& j, Magnum::Vector2i& val)
-{
- std::string str = j;
- int x = 0, y = 0, n = 0;
- int ret = std::sscanf(str.c_str(), "%d x %d%n", &x, &y, &n);
- if (ret != 2 || (std::size_t)n != str.size())
- {
- std::string msg; msg.reserve(64 + str.size());
- msg += "failed to parse string '";
- msg += str;
- msg += "' as Magnum::Vector2i";
- throw std::invalid_argument(msg);
- }
- val = { x, y };
-}
-
-} // namespace nlohmann
-
#if defined __clang__ || defined __CLION_IDE__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wweak-vtables"
@@ -61,47 +23,10 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim, name, nframes, actionframe, fps, groups
std::tuple<anim, bool> anim::from_json(const std::filesystem::path& pathname) noexcept
{
- using namespace nlohmann;
- std::ifstream s;
- s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit);
- try {
- s.open(pathname, std::ios_base::in);
- } catch (const std::ios::failure& e) {
- Error{Error::Flag::NoSpace} << "failed to open " << pathname << ": " << e.what();
- return { {}, false };
- }
- anim ret;
- try {
- json j;
- s >> j;
- using nlohmann::from_json;
- from_json(j, ret);
- } catch (const std::exception& e) {
- Error{Error::Flag::NoSpace} << "failed to parse " << pathname << ": " << e.what();
- return { {}, false };
- }
- return { std::move(ret), true };
+ return json_helper<anim>::from_json(pathname);
}
-bool anim::to_json(const std::filesystem::path& pathname) noexcept
+bool anim::to_json(const std::filesystem::path& pathname) const noexcept
{
- try {
- nlohmann::json j = *this;
-
- std::ofstream s;
- s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit);
- try {
- s.open(pathname, std::ios_base::out | std::ios_base::trunc);
- } catch (const std::ios::failure& e) {
- Error{} << "failed to open" << pathname << "for writing:" << e.what();
- return false;
- }
- s << j.dump(4);
- s.flush();
- } catch (const std::exception& e) {
- Error{Error::Flag::NoSpace} << "failed writing to " << pathname << ": " << e.what();
- return false;
- }
-
- return true;
+ return json_helper<anim>::to_json(*this, pathname);
}
diff --git a/anim/serialize.hpp b/anim/serialize.hpp
index a8b13d41..49641fb5 100644
--- a/anim/serialize.hpp
+++ b/anim/serialize.hpp
@@ -32,7 +32,7 @@ struct anim_group final
struct anim final
{
static std::tuple<anim, bool> from_json(const std::filesystem::path& pathname) noexcept;
- [[nodiscard]] bool to_json(const std::filesystem::path& pathname) noexcept;
+ [[nodiscard]] bool to_json(const std::filesystem::path& pathname) const noexcept;
static constexpr int default_fps = 24;
std::string name;