diff options
| -rw-r--r-- | anim/serialize.cpp | 67 | ||||
| -rw-r--r-- | anim/serialize.hpp | 18 | ||||
| -rw-r--r-- | crop-tool/crop-tool.cpp | 25 | ||||
| -rw-r--r-- | old/main.cpp | 115 | ||||
| -rw-r--r-- | old/matrix-test.cxx | 14 |
5 files changed, 29 insertions, 210 deletions
diff --git a/anim/serialize.cpp b/anim/serialize.cpp index 2a0b5a45..3aca6201 100644 --- a/anim/serialize.cpp +++ b/anim/serialize.cpp @@ -10,47 +10,9 @@ using Corrade::Utility::Debug; using Corrade::Utility::Error; -static constexpr -std::pair<anim_direction, const char*> anim_direction_map[] = { - { anim_direction::N, "n" }, - { anim_direction::NE, "ne" }, - { anim_direction::E, "e" }, - { anim_direction::SE, "se" }, - { anim_direction::S, "s" }, - { anim_direction::SW, "sw" }, - { anim_direction::W, "w" }, - { anim_direction::NW, "nw" }, -}; - -const char* anim_group::direction_to_string(anim_direction group) -{ - auto it = std::find_if(std::cbegin(anim_direction_map), std::cend(anim_direction_map), - [=](const auto& pair) { return group == pair.first; }); - if (it != std::cend(anim_direction_map)) - return it->second; - else - return "(unknown)"; -} - -anim_direction anim_group::string_to_direction(const std::string& str) -{ - auto it = std::find_if(std::cbegin(anim_direction_map), std::cend(anim_direction_map), - [&](const auto& pair) { return str == pair.second; }); - if (it != std::cend(anim_direction_map)) - return it->first; - else - return (anim_direction)0; -} - namespace nlohmann { template<> -struct adl_serializer<anim_direction> final { - static void to_json(json& j, anim_direction x); - static void from_json(const json& j, anim_direction& x); -}; - -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); @@ -68,25 +30,13 @@ void adl_serializer<Magnum::Vector2i>::from_json(const json& j, Magnum::Vector2i j.at("y").get_to(x[1]); } -#if 0 -void adl_serializer<anim_direction>::to_json(json& j, anim_direction x) -{ - j = anim_group::direction_to_string(x); -} - -void adl_serializer<anim_direction>::from_json(const json& j, anim_direction& x) -{ - x = anim_group::string_to_direction(j); -} -#endif - } // 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); -std::optional<anim> anim::from_json(const std::filesystem::path& pathname) +std::tuple<anim, bool> anim::from_json(const std::filesystem::path& pathname) { using namespace nlohmann; std::ifstream s; @@ -95,7 +45,7 @@ std::optional<anim> anim::from_json(const std::filesystem::path& pathname) s.open(pathname, std::ios_base::in); } catch (const std::ios::failure& e) { Error{} << "failed to open" << pathname << ':' << e.what(); - return std::nullopt; + return { {}, false }; } anim ret; try { @@ -105,9 +55,9 @@ std::optional<anim> anim::from_json(const std::filesystem::path& pathname) from_json(j, ret); } catch (const std::exception& e) { Error{} << "failed to parse" << pathname << ':' << e.what(); - return std::nullopt; + return { {}, false }; } - return std::make_optional(std::move(ret)); + return { std::move(ret), true }; } bool anim::to_json(const std::filesystem::path& pathname) @@ -122,8 +72,13 @@ bool anim::to_json(const std::filesystem::path& pathname) Error{} << "failed to open" << pathname << "for writing:" << e.what(); return false; } - s << j.dump(4); - s.flush(); + try { + s << j.dump(4); + s.flush(); + } catch (const std::exception& e) { + Error{} << "failed writing" << pathname << ':' << e.what(); + return false; + } return true; } diff --git a/anim/serialize.hpp b/anim/serialize.hpp index 10fd9e18..8c049978 100644 --- a/anim/serialize.hpp +++ b/anim/serialize.hpp @@ -1,12 +1,15 @@ #pragma once -#include "../defs.hpp" +#include "defs.hpp" + #include <string> #include <array> #include <vector> -#include <optional> -#include <filesystem> -#include <Magnum/Trade/ImageData.h> + +#include <Magnum/Magnum.h> +#include <Magnum/Math/Vector2.h> + +namespace std::filesystem { class path; } struct anim_frame final { @@ -24,15 +27,12 @@ struct anim_group final std::string name; std::vector<anim_frame> frames; Magnum::Vector2i ground; - - static const char* direction_to_string(anim_direction group); - static anim_direction string_to_direction(const std::string& str); }; struct anim final { - static std::optional<anim> from_json(const std::filesystem::path& pathname); - bool to_json(const std::filesystem::path& pathname); + static std::tuple<anim, bool> from_json(const std::filesystem::path& pathname); + [[nodiscard]] bool to_json(const std::filesystem::path& pathname); static constexpr int default_fps = 24; std::string name; diff --git a/crop-tool/crop-tool.cpp b/crop-tool/crop-tool.cpp index 895ee0f7..b76a472a 100644 --- a/crop-tool/crop-tool.cpp +++ b/crop-tool/crop-tool.cpp @@ -103,13 +103,6 @@ static bool load_file(anim_group& group, options& opts, anim_atlas& atlas, const cv::Mat4b resized{size}; cv::resize(mat({start, size}), resized, dest_size, 0, 0, cv::INTER_LANCZOS4); -#if 0 - if (!cv::imwrite(output_filename.string(), resized)) - { - Error{} << "failed writing image" << output_filename; - return false; - } -#endif Magnum::Vector2i ground = { (int)std::round((group.ground[0] - start[0]) * opts.scale), (int)std::round((group.ground[1] - start[1]) * opts.scale), @@ -195,22 +188,22 @@ static std::tuple<options, bool> parse_cmdline(int argc, const char* const* argv if (opts.output_dir.empty()) opts.output_dir = opts.input_dir; - return { opts, false }; + return { std::move(opts), true }; usage: Error{Error::Flag::NoNewlineAtTheEnd} << args.usage(); - return { {}, true }; + return { {}, false }; } int main(int argc, char** argv) { argv[0] = fix_argv0(argv[0]); - auto [opts, error_code] = parse_cmdline(argc, argv); - if (error_code) - return error_code; + auto [opts, opts_ok] = parse_cmdline(argc, argv); + if (!opts_ok) + return EX_USAGE; - auto anim_info = anim::from_json(opts.input_dir/"atlas.json"); + auto [anim_info, anim_ok] = anim::from_json(opts.input_dir/"atlas.json"); - if (!anim_info) + if (!anim_ok) return EX_DATAERR; if (std::error_code error; @@ -223,14 +216,14 @@ int main(int argc, char** argv) anim_atlas atlas; - for (anim_group& group : anim_info->groups) + for (anim_group& group : anim_info.groups) { group.frames.clear(); group.frames.reserve(64); if (!load_directory(group, opts, atlas, opts.input_dir/group.name)) return EX_DATAERR; if (!atlas.dump(opts.output_dir/"atlas.png")) return EX_CANTCREAT; - if (!anim_info->to_json(opts.output_dir/"atlas.json.new")) + if (!anim_info.to_json(opts.output_dir/"atlas.json.new")) return EX_CANTCREAT; } diff --git a/old/main.cpp b/old/main.cpp deleted file mode 100644 index 2da2a2a1..00000000 --- a/old/main.cpp +++ /dev/null @@ -1,115 +0,0 @@ -#include "logging.hpp" -#include <glm/ext/matrix_transform.hpp> -#include <glm/ext/matrix_clip_space.hpp> -#include <SFML/Graphics.hpp> - -#if 0 -static const float isometric_matrix [16] = { -#if 1 - 0.766044, -0.633022, 0.111619, 0.000000, - 0.642788, 0.754407, -0.133022, 0.000000, - 0.000000, 0.173648, 0.984808, 0.000000, - 0.000000, 0.000000, 0.000000, 1.000000, -#else - 0.61237f, -0.50000f, 0.61237f, 0.0f, - 0.35355f, 0.86603f, 0.35355f, 0.0f, - 0.61237f, -0.50000f, 0.61237f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f -#endif -}; -#endif - -static constexpr glm::mat4 transform4_yz { - 1, 0, 0, 0, - 0, 0, 1, 0, - 0, 1, 0, 0, - 0, 0, 0, 1, -}; - -static constexpr glm::mat4 identity_transform4{1}; - -static auto make_projection(sf::Vector2f offset, const glm::mat4& transform = identity_transform4) -{ - auto [x, y] = offset; - auto m = glm::mat4{1}; - m = glm::translate(m, { x, -y, 0 }); - m = glm::scale(m, { 1.f, 0.6f, 1.f }); - m = glm::rotate(m, glm::radians(-45.f), glm::vec3(1.0f, 0.0f, 0.0f)); - m = glm::rotate(m, glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); - m = glm::rotate(m, glm::radians(-45.0f), glm::vec3(0.0f, 0.0f, 1.0f)); - m *= transform; - return m; -} - -static const std::string transform_str{"transform"}; - -int main() -{ - // create the window - sf::RenderWindow window(sf::VideoMode(800, 800), "My window"); - sf::Texture tex; - if (!tex.loadFromFile("images/wildtextures_cracked-asphalt-seamless-texture.jpg")) - return 1; - - sf::Sprite sprite{tex, { { 0, 0 }, { 100, 100 } }}; - sprite.setPosition({0, 0}); - - sf::Shader shader; - if (!shader.loadFromFile("shaders/tile.vert", "shaders/tile.frag")) - return 1; - - int offx = 0, offy = 0; - - // run the program as long as the window is open - while (window.isOpen()) - { - // check all the window's events that were triggered since the last iteration of the loop - sf::Event event = {}; - - while (window.pollEvent(event)) - { - // "close requested" event: we close the window - if (event.type == sf::Event::Closed) - window.close(); - else if (event.type == sf::Event::KeyPressed) - { - constexpr int off = 10; - switch (event.key.code) - { - default: break; - case sf::Keyboard::Escape: window.close(); break; - case sf::Keyboard::Left: offx += -off; break; - case sf::Keyboard::Right: offx += +off; break; - case sf::Keyboard::Up: offy += -off; break; - case sf::Keyboard::Down: offy += +off; break; - } - } - else if (event.type == sf::Event::Resized) - { - auto view = window.getView(); - view.setSize(window.getSize().x, window.getSize().y); - } - } - - // clear the window with black color - window.clear(sf::Color::Black); - - { - auto [w, h] = window.getView().getSize(); - sf::Vector2f camera_offset { offx / (float)w, offy / (float)h }; - //shader.setUniform(transform_str, make_projection(camera_offset)); - //window.draw(sprite, &shader); - auto rect = sprite.getLocalBounds(); - sf::Vector2f pos{-(rect.width - rect.left - offx) / w, - (rect.height - rect.top + offy)*.5f / h}; - auto mv = make_projection(pos/*, transform4_yz*/); - shader.setUniform(transform_str, sf::Glsl::Mat4{&mv[0][0]}); - window.draw(sprite, &shader); - } - - // end the current frame - window.display(); - } - - return 0; -} diff --git a/old/matrix-test.cxx b/old/matrix-test.cxx deleted file mode 100644 index ee3ed43c..00000000 --- a/old/matrix-test.cxx +++ /dev/null @@ -1,14 +0,0 @@ -#include <glm/glm.hpp> -#include <glm/ext/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale -#include <cstdio> -int main(void) -{ - glm::mat4 m(1); - m = glm::rotate(m, glm::radians(-45.f), glm::vec3(1.0f, 0.0f, 0.0f)); - m = glm::rotate(m, glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); - m = glm::rotate(m, glm::radians(-45.0f), glm::vec3(0.0f, 0.0f, 1.0f)); - m = glm::scale(m, { 0.68, 1, 1 }); - for (int j = 0; j < 4; j++) - printf("%f, %f, %f, %f,\n", m[j][0], m[j][1], m[j][2], m[j][3]); - return 0; -} |
