diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-06 22:18:01 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-06 22:18:06 +0200 |
commit | 9107e0ff64fc5788d90c0b44da3f7148cba4f966 (patch) | |
tree | 7f719b7958ae4c420ab02a3540734dee33d660a9 | |
parent | ccc6b7fcb08f5a5713c2ac74a28113814b4c70b4 (diff) |
a
-rw-r--r-- | serialize/json-helper.hpp | 10 | ||||
-rw-r--r-- | serialize/magnum-vector.hpp | 35 | ||||
-rw-r--r-- | serialize/tile-atlas.cpp | 20 | ||||
-rw-r--r-- | serialize/tile.cpp | 50 | ||||
-rw-r--r-- | serialize/tile.hpp | 26 | ||||
-rw-r--r-- | src/chunk.hpp | 13 | ||||
-rw-r--r-- | src/tile.hpp | 2 |
7 files changed, 113 insertions, 43 deletions
diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp index 345efe54..86c6a16f 100644 --- a/serialize/json-helper.hpp +++ b/serialize/json-helper.hpp @@ -22,7 +22,12 @@ std::tuple<t, bool> json_helper::from_json(const std::filesystem::path& pathname using Corrade::Utility::Error; std::ifstream s; s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - s.open(pathname, std::ios_base::in); + try { + s.open(pathname, std::ios_base::in); + } catch (const std::ios::failure& e) { + Error{} << "failed to open" << pathname << "for reading:" << e.what(); + return {}; + } t ret; json j; s >> j; @@ -41,7 +46,7 @@ bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) 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(); + Error{} << "failed to open" << pathname << "for writing:" << e.what(); return false; } s << j.dump(4); @@ -49,4 +54,3 @@ bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) s.flush(); return true; } - diff --git a/serialize/magnum-vector.hpp b/serialize/magnum-vector.hpp index eed37708..8ea5e13a 100644 --- a/serialize/magnum-vector.hpp +++ b/serialize/magnum-vector.hpp @@ -11,25 +11,32 @@ namespace nlohmann { template<std::size_t N, typename T> struct adl_serializer<Magnum::Math::Vector<N, T>> { - static void to_json(json& j, const Magnum::Math::Vector<N, T>& val) - { - std::array<T, N> array{}; - for (std::size_t i = 0; i < N; i++) - array[i] = val[i]; - j = array; - } - static void from_json(const json& j, Magnum::Math::Vector<N, T>& val) - { - std::array<T, N> array = j; - for (std::size_t i = 0; i < N; i++) - val[i] = array[i]; - } + using vec = Magnum::Math::Vector<N, T>; + static void to_json(json& j, const vec& val); + static void from_json(const json& j, vec& val); }; -template<typename T> struct adl_serializer<Magnum::Math::Vector2<T>> : adl_serializer<Magnum::Math::Vector<2, T>> {}; +template <std::size_t N, typename T> +void adl_serializer<Magnum::Math::Vector<N, T>>::to_json(json& j, const vec& val) +{ + std::array<T, N> array{}; + for (std::size_t i = 0; i < N; i++) + array[i] = val[i]; + j = array; +} + +template <std::size_t N, typename T> +void adl_serializer<Magnum::Math::Vector<N, T>>::from_json(const json& j, vec& val) +{ + std::array<T, N> array = j; + for (std::size_t i = 0; i < N; i++) + val[i] = array[i]; +} + template<typename T> struct adl_serializer<Magnum::Math::Vector3<T>> : adl_serializer<Magnum::Math::Vector<3, T>> {}; template<typename T> struct adl_serializer<Magnum::Math::Vector4<T>> : adl_serializer<Magnum::Math::Vector<4, T>> {}; template<typename T> struct adl_serializer<Magnum::Math::Color3<T>> : adl_serializer<Magnum::Math::Vector<3, T>> {}; template<typename T> struct adl_serializer<Magnum::Math::Color4<T>> : adl_serializer<Magnum::Math::Vector<4, T>> {}; +template<typename T> struct adl_serializer<Magnum::Math::Vector2<T>> : adl_serializer<Magnum::Math::Vector<2, T>> {}; } // namespace nlohmann diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp index c7e52057..8021f1d5 100644 --- a/serialize/tile-atlas.cpp +++ b/serialize/tile-atlas.cpp @@ -2,25 +2,16 @@ #include "serialize/tile-atlas.hpp" #include "serialize/magnum-vector2i.hpp" #include "loader.hpp" +#include <tuple> #include <nlohmann/json.hpp> -namespace Magnum::Examples::Serialize { - -struct proxy_atlas final { - std::string name; - Vector2ui num_tiles; -}; - -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(proxy_atlas, name, num_tiles) - -} // namespace Magnum::Examples::Serialize +using namespace Magnum; +using namespace Magnum::Examples; namespace nlohmann { -using namespace Magnum::Examples; -using namespace Magnum::Examples::Serialize; - +using proxy_atlas = std::tuple<std::string, Vector2ui>; using shared_atlas = std::shared_ptr<Magnum::Examples::tile_atlas>; void adl_serializer<shared_atlas>::to_json(json& j, const shared_atlas& x) @@ -36,7 +27,8 @@ void adl_serializer<shared_atlas>::to_json(json& j, const shared_atlas& x) void adl_serializer<shared_atlas>::from_json(const json& j, shared_atlas& x) { proxy_atlas proxy = j; - x = loader.tile_atlas(proxy.name, proxy.num_tiles); + const auto& [name, num_tiles] = proxy; + x = loader.tile_atlas(name, num_tiles); } } // namespace nlohmann diff --git a/serialize/tile.cpp b/serialize/tile.cpp new file mode 100644 index 00000000..2f414e62 --- /dev/null +++ b/serialize/tile.cpp @@ -0,0 +1,50 @@ +#include "src/tile.hpp" +#include "src/chunk.hpp" +#include "serialize/tile.hpp" +#include "serialize/tile-atlas.hpp" +#include <tuple> +#include <nlohmann/json.hpp> + +namespace Magnum::Examples { + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(tile_image, atlas, variant) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(tile, ground_image, wall_north, wall_west, passability) + +} // namespace Magnum::Examples + +namespace nlohmann { + +using namespace Magnum::Examples; + +void adl_serializer<tile_image>::to_json(json& j, const tile_image& val) { + using nlohmann::to_json; + to_json(j, val); +} + +void adl_serializer<tile_image>::from_json(const json& j, tile_image& val) { + using nlohmann::from_json; + from_json(j, val); +} + +void adl_serializer<tile>::to_json(json& j, const tile& val) { + using nlohmann::to_json; + to_json(j, val); +} + +void adl_serializer<tile>::from_json(const json& j, tile& val) { + using nlohmann::from_json; + from_json(j, val); +} + +void adl_serializer<chunk>::to_json(json& j, const chunk& val) { + using nlohmann::to_json; + to_json(j, val.tiles()); +} + +void adl_serializer<chunk>::from_json(const json& j, chunk& val) { + using nlohmann::from_json; + std::remove_cvref_t<decltype(val.tiles())> tiles = {}; + tiles = j; +} + +} // namespace nlohmann diff --git a/serialize/tile.hpp b/serialize/tile.hpp index 4224fa54..d0a2d144 100644 --- a/serialize/tile.hpp +++ b/serialize/tile.hpp @@ -1,14 +1,30 @@ #pragma once -#include <nlohmann/json.hpp> +#include <nlohmann/json_fwd.hpp> namespace Magnum::Examples { - - - +struct tile_image; +struct tile; +struct chunk; } // namespace Magnum::Examples namespace nlohmann { - +template<> +struct adl_serializer<Magnum::Examples::tile_image> { + static void to_json(json& j, const Magnum::Examples::tile_image& val); + static void from_json(const json& j, Magnum::Examples::tile_image& val); +}; + +template<> +struct adl_serializer<Magnum::Examples::tile> { + static void to_json(json& j, const Magnum::Examples::tile& val); + static void from_json(const json& j, Magnum::Examples::tile& val); +}; + +template<> +struct adl_serializer<Magnum::Examples::chunk> { + static void to_json(json& j, const Magnum::Examples::chunk& val); + static void from_json(const json& j, Magnum::Examples::chunk& val); +}; } // namespace nlohmann diff --git a/src/chunk.hpp b/src/chunk.hpp index 829ee0d3..bf5833a3 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -7,10 +7,11 @@ namespace Magnum::Examples { struct chunk final { - constexpr tile& operator[](local_coords xy) { return tiles[xy.to_index()]; } - constexpr const tile& operator[](local_coords xy) const { return tiles[xy.to_index()]; } - constexpr tile& operator[](std::size_t i) { return tiles[i]; } - constexpr const tile& operator[](std::size_t i) const { return tiles[i]; } + constexpr tile& operator[](local_coords xy) { return _tiles[xy.to_index()]; } + constexpr const tile& operator[](local_coords xy) const { return _tiles[xy.to_index()]; } + constexpr tile& operator[](std::size_t i) { return _tiles[i]; } + constexpr const tile& operator[](std::size_t i) const { return _tiles[i]; } + auto& tiles() const { return _tiles; } template<typename F> requires std::invocable<F, tile&, std::size_t, local_coords> @@ -24,7 +25,7 @@ private: template<typename F, typename Self> constexpr void foreach_tile_(F&& fun); - std::array<tile, TILE_COUNT> tiles = {}; + std::array<tile, TILE_COUNT> _tiles = {}; }; template<typename F, typename Self> @@ -34,7 +35,7 @@ constexpr void chunk::foreach_tile_(F&& fun) std::size_t k = 0; for (std::size_t j = 0; j < N; j++) for (std::size_t i = 0; i < N; i++, k++) - fun(const_cast<Self>(*this).tiles[k], k, + fun(const_cast<Self>(*this)._tiles[k], k, local_coords{(std::uint8_t)i, (std::uint8_t)j}); } diff --git a/src/tile.hpp b/src/tile.hpp index 261c50ee..eadfa745 100644 --- a/src/tile.hpp +++ b/src/tile.hpp @@ -25,7 +25,7 @@ struct tile final enum class pass_mode : std::uint8_t { pass_blocked, pass_ok, pass_shoot_through, }; using enum pass_mode; - tile_image ground_image, wall_west, wall_north; + tile_image ground_image, wall_north, wall_west; pass_mode passability = pass_shoot_through; }; |