diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-06 22:47:20 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-06 22:47:20 +0200 |
commit | ed90a4cec86f4e8beb38c69fc2aaa0c7bfa84b7b (patch) | |
tree | 5a224daf40d3d6585580bb639dc23ce14f2430bf | |
parent | 9107e0ff64fc5788d90c0b44da3f7148cba4f966 (diff) |
a
-rw-r--r-- | serialize/anim.hpp | 7 | ||||
-rw-r--r-- | serialize/json-helper.hpp | 6 | ||||
-rw-r--r-- | serialize/tile.cpp | 42 | ||||
-rw-r--r-- | serialize/tile.hpp | 7 | ||||
-rw-r--r-- | src/chunk.hpp | 7 | ||||
-rw-r--r-- | test/json.cpp | 2 |
6 files changed, 29 insertions, 42 deletions
diff --git a/serialize/anim.hpp b/serialize/anim.hpp index 726efa44..3b5504f8 100644 --- a/serialize/anim.hpp +++ b/serialize/anim.hpp @@ -4,9 +4,9 @@ #include <array> #include <vector> #include <string> - #include <Magnum/Magnum.h> #include <Magnum/Math/Vector2.h> +#include <nlohmann/json_fwd.hpp> namespace std::filesystem { class path; } @@ -45,3 +45,8 @@ struct anim final } // namespace Magnum::Examples::Serialize +namespace nlohmann { + + + +} // namespace nlohmann diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp index 86c6a16f..6be7f74c 100644 --- a/serialize/json-helper.hpp +++ b/serialize/json-helper.hpp @@ -18,7 +18,6 @@ struct json_helper final { template<typename t> std::tuple<t, bool> json_helper::from_json(const std::filesystem::path& pathname) { - using namespace nlohmann; using Corrade::Utility::Error; std::ifstream s; s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); @@ -29,10 +28,9 @@ std::tuple<t, bool> json_helper::from_json(const std::filesystem::path& pathname return {}; } t ret; - json j; + nlohmann::json j; s >> j; - using nlohmann::from_json; - from_json(j, ret); + ret = j; return { std::move(ret), true }; } diff --git a/serialize/tile.cpp b/serialize/tile.cpp index 2f414e62..df49a465 100644 --- a/serialize/tile.cpp +++ b/serialize/tile.cpp @@ -9,6 +9,7 @@ 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) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(local_coords, x, y) } // namespace Magnum::Examples @@ -16,35 +17,16 @@ 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; -} +void adl_serializer<tile_image>::to_json(json& j, const tile_image& val) { j = val; } +void adl_serializer<tile_image>::from_json(const json& j, tile_image& val) { val = j; } + +void adl_serializer<tile>::to_json(json& j, const tile& val) { j = val; } +void adl_serializer<tile>::from_json(const json& j, tile& val) { val = j; } + +void adl_serializer<chunk>::to_json(json& j, const chunk& val) { j = val.tiles(); } +void adl_serializer<chunk>::from_json(const json& j, chunk& val) { val.tiles() = j; } + +void adl_serializer<local_coords>::to_json(json& j, const local_coords& val) { j = val; } +void adl_serializer<local_coords>::from_json(const json& j, local_coords& val) { val = j; } } // namespace nlohmann diff --git a/serialize/tile.hpp b/serialize/tile.hpp index d0a2d144..f84ebd8d 100644 --- a/serialize/tile.hpp +++ b/serialize/tile.hpp @@ -5,6 +5,7 @@ namespace Magnum::Examples { struct tile_image; struct tile; struct chunk; +struct local_coords; } // namespace Magnum::Examples namespace nlohmann { @@ -27,4 +28,10 @@ struct adl_serializer<Magnum::Examples::chunk> { static void from_json(const json& j, Magnum::Examples::chunk& val); }; +template<> +struct adl_serializer<Magnum::Examples::local_coords> { + static void to_json(json& j, const Magnum::Examples::local_coords& val); + static void from_json(const json& j, Magnum::Examples::local_coords& val); +}; + } // namespace nlohmann diff --git a/src/chunk.hpp b/src/chunk.hpp index bf5833a3..39d64297 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -11,7 +11,8 @@ struct chunk final 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; } + const auto& tiles() const { return _tiles; } + auto& tiles() { return _tiles; } template<typename F> requires std::invocable<F, tile&, std::size_t, local_coords> @@ -39,7 +40,6 @@ constexpr void chunk::foreach_tile_(F&& fun) local_coords{(std::uint8_t)i, (std::uint8_t)j}); } -#if 0 struct chunk_coords final { std::int16_t x = 0, y = 0; constexpr std::size_t to_index() const noexcept; @@ -47,15 +47,12 @@ struct chunk_coords final { static constexpr std::size_t max_bits = sizeof(chunk_coords::x)*8 * 3 / 4; static_assert(max_bits*4/3/8 == sizeof(decltype(chunk_coords::x))); }; -#endif -#if 0 struct global_coords final { std::uint32_t x = 0, y = 0; constexpr global_coords() noexcept = default; constexpr global_coords(decltype(x) x, decltype(y) y) noexcept : x{x}, y{y} {} constexpr global_coords(chunk_coords c, local_coords tile) noexcept; }; -#endif } // namespace Magnum::Examples diff --git a/test/json.cpp b/test/json.cpp index d237c674..5e60b7ee 100644 --- a/test/json.cpp +++ b/test/json.cpp @@ -11,10 +11,8 @@ namespace Magnum::Examples { bool app::test_json() // NOLINT(readability-convert-member-functions-to-static) { bool ret = true; - using nlohmann::to_json; const std::filesystem::path output_dir = "../test/"; { - nlohmann::json j; auto atlas = loader.tile_atlas("share/game/images/metal1.tga", {2, 2}); ret &= json_helper::to_json(atlas, output_dir/"atlas.json"); } |