diff options
Diffstat (limited to 'anim')
-rw-r--r-- | anim/atlas.cpp | 18 | ||||
-rw-r--r-- | anim/atlas.hpp | 13 | ||||
-rw-r--r-- | anim/serialize.cpp | 48 | ||||
-rw-r--r-- | anim/serialize.hpp | 10 |
4 files changed, 52 insertions, 37 deletions
diff --git a/anim/atlas.cpp b/anim/atlas.cpp index ca05cd15..fe4b5f65 100644 --- a/anim/atlas.cpp +++ b/anim/atlas.cpp @@ -1,41 +1,43 @@ +#undef NDEBUG + #include "atlas.hpp" #include "serialize.hpp" -#include "../defs.hpp" +#include <cassert> #include <filesystem> #include <opencv2/imgcodecs.hpp> -void anim_atlas_row::add_entry(const anim_atlas_entry& x) +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); + 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() +void anim_atlas::advance_row() noexcept { auto& row = rows.back(); if (row.data.empty()) return; - ASSERT(row.xpos); ASSERT(row.max_height); + 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 +Magnum::Vector2i anim_atlas::offset() const noexcept { const auto& row = rows.back(); return {row.xpos, row.ypos}; } -Magnum::Vector2i anim_atlas::size() const +Magnum::Vector2i anim_atlas::size() const noexcept { const anim_atlas_row& row = rows.back(); // prevent accidentally writing out of bounds by forgetting to call @@ -43,7 +45,7 @@ Magnum::Vector2i anim_atlas::size() const return {std::max(maxx, row.xpos), ypos + row.max_height}; } -bool anim_atlas::dump(const std::filesystem::path& filename) const +bool anim_atlas::dump(const std::filesystem::path& filename) const noexcept { auto sz = size(); cv::Mat4b mat(sz[1], sz[0]); diff --git a/anim/atlas.hpp b/anim/atlas.hpp index dd6efabc..5c5e918f 100644 --- a/anim/atlas.hpp +++ b/anim/atlas.hpp @@ -1,4 +1,5 @@ #pragma once + #include <vector> #include <Magnum/Magnum.h> #include <Magnum/Math/Vector2.h> @@ -19,7 +20,7 @@ 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); + void add_entry(const anim_atlas_entry& x) noexcept; }; class anim_atlas @@ -28,9 +29,9 @@ class anim_atlas int ypos = 0, maxx = 0; public: - void add_entry(const anim_atlas_entry& x) { rows.back().add_entry(x); } - void advance_row(); - Magnum::Vector2i offset() const; - Magnum::Vector2i size() const; - [[nodiscard]] bool dump(const std::filesystem::path& filename) const; + 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 7e6d738a..92ff1481 100644 --- a/anim/serialize.cpp +++ b/anim/serialize.cpp @@ -1,5 +1,5 @@ #include "serialize.hpp" -#include "../json.hpp" +#include "json.hpp" #include <algorithm> #include <utility> @@ -43,11 +43,21 @@ void adl_serializer<Magnum::Vector2i>::from_json(const json& j, Magnum::Vector2i } // namespace nlohmann -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim_frame, ground, offset, size); -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim_group, name, frames, ground); -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim, name, nframes, actionframe, fps, groups); +#if defined __clang__ || defined __CLION_IDE__ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wweak-vtables" +# pragma clang diagnostic ignored "-Wcovered-switch-default" +#endif -std::tuple<anim, bool> anim::from_json(const std::filesystem::path& pathname) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim_frame, ground, offset, size) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim_group, name, frames, ground) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim, name, nframes, actionframe, fps, groups, width, height) + +#if defined __clang__ || defined __CLION_IDE__ +# pragma clang diagnostic pop +#endif + +std::tuple<anim, bool> anim::from_json(const std::filesystem::path& pathname) noexcept { using namespace nlohmann; std::ifstream s; @@ -55,7 +65,7 @@ std::tuple<anim, bool> anim::from_json(const std::filesystem::path& pathname) try { s.open(pathname, std::ios_base::in); } catch (const std::ios::failure& e) { - Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "':" << e.what(); + Error{Error::Flag::NoSpace} << "failed to open " << pathname << ": " << e.what(); return { {}, false }; } anim ret; @@ -65,29 +75,29 @@ std::tuple<anim, bool> anim::from_json(const std::filesystem::path& pathname) using nlohmann::from_json; from_json(j, ret); } catch (const std::exception& e) { - Error{Error::Flag::NoSpace} << "failed to parse '" << pathname << "':" << e.what(); + Error{Error::Flag::NoSpace} << "failed to parse " << pathname << ": " << e.what(); return { {}, false }; } return { std::move(ret), true }; } -bool anim::to_json(const std::filesystem::path& pathname) +bool anim::to_json(const std::filesystem::path& pathname) noexcept { - 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{Error::Flag::NoSpace} << "failed to open '" << pathname << "' for writing: " << e.what(); - return false; - } 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 '" << pathname << "' :" << e.what(); + Error{Error::Flag::NoSpace} << "failed writing to " << pathname << ": " << e.what(); return false; } diff --git a/anim/serialize.hpp b/anim/serialize.hpp index 8c049978..a8b13d41 100644 --- a/anim/serialize.hpp +++ b/anim/serialize.hpp @@ -19,7 +19,7 @@ struct anim_frame final enum class anim_direction : unsigned char { N, NE, E, SE, S, SW, W, NW, - COUNT = NW + 1, + COUNT, }; struct anim_group final @@ -31,11 +31,13 @@ struct anim_group final struct anim final { - static std::tuple<anim, bool> from_json(const std::filesystem::path& pathname); - [[nodiscard]] bool to_json(const std::filesystem::path& pathname); + static std::tuple<anim, bool> from_json(const std::filesystem::path& pathname) noexcept; + [[nodiscard]] bool to_json(const std::filesystem::path& pathname) noexcept; static constexpr int default_fps = 24; std::string name; std::array<anim_group, (std::size_t)anim_direction::COUNT> groups; - int nframes = 0, actionframe = -1, fps = default_fps; + int nframes = 0; + int width = 0, height = 0; + int actionframe = -1, fps = default_fps; }; |