summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-06 23:37:35 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-06 23:37:35 +0200
commit85d9552871b1bbf2df86f9f20d93d5bac575dd12 (patch)
tree443002c8f0359d825fbe1dda1b2e401249865b82
parent86d09212e84b7d3b41acf4abd420d4e14cbc3592 (diff)
a
-rw-r--r--serialize/tile.cpp17
-rw-r--r--test/json.cpp30
2 files changed, 38 insertions, 9 deletions
diff --git a/serialize/tile.cpp b/serialize/tile.cpp
index df49a465..a945f171 100644
--- a/serialize/tile.cpp
+++ b/serialize/tile.cpp
@@ -2,7 +2,6 @@
#include "src/chunk.hpp"
#include "serialize/tile.hpp"
#include "serialize/tile-atlas.hpp"
-#include <tuple>
#include <nlohmann/json.hpp>
namespace Magnum::Examples {
@@ -17,16 +16,16 @@ namespace nlohmann {
using namespace Magnum::Examples;
-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_image>::to_json(json& j, const tile_image& val) { using nlohmann::to_json; if (val.atlas) to_json(j, val); else j = nullptr; }
+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) { j = val; }
-void adl_serializer<tile>::from_json(const json& j, tile& val) { val = j; }
+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) { j = val.tiles(); }
-void adl_serializer<chunk>::from_json(const json& j, chunk& val) { val.tiles() = j; }
+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; from_json(j, val.tiles()); }
-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; }
+void adl_serializer<local_coords>::to_json(json& j, const local_coords& val) { using nlohmann::to_json; to_json(j, val); }
+void adl_serializer<local_coords>::from_json(const json& j, local_coords& val) { using nlohmann::from_json; from_json(j, val); }
} // namespace nlohmann
diff --git a/test/json.cpp b/test/json.cpp
index 5e60b7ee..b98c9ae2 100644
--- a/test/json.cpp
+++ b/test/json.cpp
@@ -5,9 +5,31 @@
#include "serialize/json-helper.hpp"
#include "compat/assert.hpp"
#include "tile-atlas.hpp"
+#include "tile.hpp"
+#include "chunk.hpp"
#include "loader.hpp"
namespace Magnum::Examples {
+
+static chunk make_test_chunk()
+{
+ auto metal1 = loader.tile_atlas("share/game/images/metal1.tga", {2, 2}),
+ metal2 = loader.tile_atlas("share/game/images/metal2.tga", {2, 2}),
+ metal3 = loader.tile_atlas("share/game/images/metal3.tga", {2, 2});
+ constexpr auto N = TILE_MAX_DIM;
+ chunk c;
+ c.foreach_tile([&](tile& x, std::size_t k, local_coords pt) {
+ const auto& atlas = pt.x > N/2 && pt.y >= N/2 ? metal1 : metal2;
+ x.ground_image = { atlas, (std::uint8_t)(k % atlas->num_tiles().product()) };
+ });
+ constexpr auto K = N/2;
+ c[{K, K }].wall_north = { metal3, 0 };
+ c[{K, K }].wall_west = { metal3, 0 };
+ c[{K, K+1}].wall_north = { metal3, 0 };
+ c[{K+1, K }].wall_west = { metal3, 0 };
+ return c;
+}
+
bool app::test_json() // NOLINT(readability-convert-member-functions-to-static)
{
bool ret = true;
@@ -22,6 +44,14 @@ bool app::test_json() // NOLINT(readability-convert-member-functions-to-static)
ret &= json_helper::to_json(v2i_1, output_dir/"vec2i_1.json");
ret &= json_helper::to_json(v2i_2, output_dir/"vec2i_2.json");
}
+ {
+ Magnum::Math::Vector3 vec{0.f/0, -1.f/0, 123.f};
+ ret &= json_helper::to_json(vec, output_dir/"vec3_inf.json");
+ }
+ {
+ const auto chunk = make_test_chunk();
+ ret &= json_helper::to_json(chunk, output_dir/"chunk-1.json");
+ }
return ret;
}