From a917bb7c28b383c3c684bf75732188bfff0060bb Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 22 Nov 2022 15:06:39 +0100 Subject: fit local-coords in a single byte --- serialize/tile.cpp | 11 +++++++---- src/local-coords.hpp | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/serialize/tile.cpp b/serialize/tile.cpp index abce91e1..8b756772 100644 --- a/serialize/tile.cpp +++ b/serialize/tile.cpp @@ -11,7 +11,11 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(tile_image_proto, atlas, variant) inline void to_json(nlohmann::json& j, const tile_image_ref& val) { j = tile_image_proto(val); } inline void from_json(const nlohmann::json& j, tile_image_ref& val) { val = tile_image_proto(j); } -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(local_coords, x, y) +struct local_coords_ final { + std::uint8_t x, y; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(local_coords_, x, y) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(chunk_coords, x, y) struct global_coords_ final { @@ -30,8 +34,8 @@ namespace nlohmann { void adl_serializer::to_json(json& j, const tile_image_ref& val) { using nlohmann::to_json; if (val.atlas) to_json(j, val); else j = nullptr; } void adl_serializer::from_json(const json& j, tile_image_ref& val) { using nlohmann::from_json; if (j.is_null()) val = {}; else from_json(j, val); } -void adl_serializer::to_json(json& j, const local_coords& val) { using nlohmann::to_json; to_json(j, val); } -void adl_serializer::from_json(const json& j, local_coords& val) { using nlohmann::from_json; from_json(j, val); } +void adl_serializer::to_json(json& j, const local_coords& val) { using nlohmann::to_json; to_json(j, local_coords_{val.x, val.y}); } +void adl_serializer::from_json(const json& j, local_coords& val) { using nlohmann::from_json; local_coords_ proxy{}; from_json(j, proxy); val = {proxy.x, proxy.y}; } void adl_serializer::to_json(json& j, const chunk_coords& val) { using nlohmann::to_json; to_json(j, val); } void adl_serializer::from_json(const json& j, chunk_coords& val) { using nlohmann::from_json; from_json(j, val); } @@ -41,4 +45,3 @@ void adl_serializer::from_json(const json& j, global_coords& val) } // namespace nlohmann - diff --git a/src/local-coords.hpp b/src/local-coords.hpp index 949a90e0..30fb3617 100644 --- a/src/local-coords.hpp +++ b/src/local-coords.hpp @@ -6,7 +6,7 @@ namespace floormat { struct local_coords final { - std::uint8_t x = 0, y = 0; + std::uint8_t x : 4 = 0, y : 4 = 0; explicit constexpr local_coords(std::size_t idx) noexcept; constexpr local_coords() noexcept = default; template requires (sizeof(T) <= sizeof(std::size_t)) -- cgit v1.2.3