diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-01 16:35:03 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-01 16:35:03 +0100 |
commit | 704e9bd3ac58484a5209e186798076f1cbd432ef (patch) | |
tree | 511479ebd088d5c9524803c6f3fe4641009b58bc /serialize | |
parent | 7ff1f0911e0b0c314d6e639887b705d6fc0d78aa (diff) |
wip
Diffstat (limited to 'serialize')
-rw-r--r-- | serialize/tile.cpp | 10 | ||||
-rw-r--r-- | serialize/tile.hpp | 8 | ||||
-rw-r--r-- | serialize/world-impl.hpp | 8 | ||||
-rw-r--r-- | serialize/world-reader.cpp | 20 | ||||
-rw-r--r-- | serialize/world-writer.cpp | 39 |
5 files changed, 45 insertions, 40 deletions
diff --git a/serialize/tile.cpp b/serialize/tile.cpp index 68def2d8..abce91e1 100644 --- a/serialize/tile.cpp +++ b/serialize/tile.cpp @@ -6,7 +6,11 @@ namespace floormat { -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(tile_image, atlas, variant) +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) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(chunk_coords, x, y) @@ -23,8 +27,8 @@ using namespace floormat; namespace nlohmann { -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; if (j.is_null()) val = {}; else from_json(j, val); } +void adl_serializer<tile_image_ref>::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<tile_image_ref>::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<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); } diff --git a/serialize/tile.hpp b/serialize/tile.hpp index 1d629fe6..66982682 100644 --- a/serialize/tile.hpp +++ b/serialize/tile.hpp @@ -3,7 +3,7 @@ namespace floormat { -struct tile_image; +struct tile_image_ref; struct local_coords; struct chunk_coords; struct global_coords; @@ -13,9 +13,9 @@ struct global_coords; namespace nlohmann { template<> -struct adl_serializer<floormat::tile_image> { - static void to_json(json& j, const floormat::tile_image& val); - static void from_json(const json& j, floormat::tile_image& val); +struct adl_serializer<floormat::tile_image_ref> { + static void to_json(json& j, const floormat::tile_image_ref& val); + static void from_json(const json& j, floormat::tile_image_ref& val); }; template<> diff --git a/serialize/world-impl.hpp b/serialize/world-impl.hpp index a0414606..caf5e8a7 100644 --- a/serialize/world-impl.hpp +++ b/serialize/world-impl.hpp @@ -12,10 +12,10 @@ namespace floormat::Serialize { namespace { using tilemeta = std::uint8_t; -using varid = decltype(tile_image::variant); +using varid = decltype(tile_image_proto::variant); using atlasid = std::uint16_t; using chunksiz = std::uint16_t; -using enum tile::pass_mode; +using proto_t = std::uint16_t; template<typename T> constexpr inline T int_max = std::numeric_limits<T>::max(); @@ -24,10 +24,10 @@ template<typename T> constexpr inline T int_max = std::numeric_limits<T>::max(); constexpr inline std::size_t atlas_name_max = 128; constexpr inline auto null_atlas = (atlasid)-1LL; -constexpr inline std::uint16_t proto_version = 1; +constexpr inline proto_t proto_version = 1; constexpr inline auto chunk_magic = (std::uint16_t)~0xc0d3; -constexpr inline std::underlying_type_t<tile::pass_mode> pass_mask = pass_blocked | pass_shoot_through | pass_ok; +constexpr inline std::underlying_type_t<pass_mode> pass_mask = pass_blocked | pass_shoot_through | pass_ok; constexpr inline auto pass_bits = std::bit_width(pass_mask); enum : tilemeta { diff --git a/serialize/world-reader.cpp b/serialize/world-reader.cpp index 385d6e39..aa156609 100644 --- a/serialize/world-reader.cpp +++ b/serialize/world-reader.cpp @@ -65,9 +65,9 @@ void reader_state::read_chunks(reader_t& s) for (std::size_t i = 0; i < TILE_COUNT; i++) { const tilemeta flags = s.read<tilemeta>(); - tile& t = chunk[i]; + tile_ref t = chunk[i]; using uchar = std::uint8_t; - const auto make_atlas = [&]() -> tile_image { + const auto make_atlas = [&]() -> tile_image_proto { auto id = flags & meta_short_atlasid ? (atlasid)(s.read<uchar>()) : s.read<atlasid>(); auto v = flags & meta_short_variant ? (varid) (s.read<uchar>()) : s.read<varid>(); auto atlas = lookup_atlas(id); @@ -76,18 +76,18 @@ void reader_state::read_chunks(reader_t& s) }; if (flags & meta_ground) - t.ground = make_atlas(); + t.ground() = make_atlas(); if (flags & meta_wall_n) - t.wall_north = make_atlas(); + t.wall_north() = make_atlas(); if (flags & meta_wall_w) - t.wall_west = make_atlas(); + t.wall_west() = make_atlas(); - switch (auto x = flags & pass_mask) + switch (auto x = pass_mode(flags & pass_mask)) { - case tile::pass_shoot_through: - case tile::pass_blocked: - case tile::pass_ok: - t.passability = (tile::pass_mode)x; + case pass_shoot_through: + case pass_blocked: + case pass_ok: + t.pass_mode() = x; break; default: fm_abort("bad pass mode '%zu' for tile %zu", i, (std::size_t)x); diff --git a/serialize/world-writer.cpp b/serialize/world-writer.cpp index a55e6eb1..c16dc32a 100644 --- a/serialize/world-writer.cpp +++ b/serialize/world-writer.cpp @@ -29,8 +29,8 @@ struct writer_state final { fm_DECLARE_DEPRECATED_COPY_ASSIGNMENT(writer_state); private: - atlasid intern_atlas(const tile_image& img); - atlasid maybe_intern_atlas(const tile_image& img); + atlasid intern_atlas(const tile_image_proto& img); + atlasid maybe_intern_atlas(const tile_image_proto& img); void serialize_chunk(const chunk& c, chunk_coords coord); void serialize_atlases(); @@ -66,7 +66,7 @@ writer_state::writer_state(const struct world& world) : world{&world} #pragma warning(pop) #endif -atlasid writer_state::intern_atlas(const tile_image& img) +atlasid writer_state::intern_atlas(const tile_image_proto& img) { const void* const ptr = img.atlas.get(); fm_debug_assert(ptr != nullptr); @@ -76,7 +76,7 @@ atlasid writer_state::intern_atlas(const tile_image& img) return (tile_images[ptr] = { &*img.atlas, (atlasid)tile_images.size() }).index; } -atlasid writer_state::maybe_intern_atlas(const tile_image& img) +atlasid writer_state::maybe_intern_atlas(const tile_image_proto& img) { return img ? intern_atlas(img) : null_atlas; } @@ -92,13 +92,14 @@ void writer_state::serialize_chunk(const chunk& c, chunk_coords coord) for (std::size_t i = 0; i < TILE_COUNT; i++) { - const tile& x = c[i]; + const tile_proto x = c[i]; + const auto ground = x.ground_image(), wall_north = x.wall_north_image(), wall_west = x.wall_west_image(); fm_debug_assert(s.bytes_written() + tile_size <= chunkbuf_size); - auto img_g = maybe_intern_atlas(x.ground); - auto img_n = maybe_intern_atlas(x.wall_north); - auto img_w = maybe_intern_atlas(x.wall_west); + auto img_g = maybe_intern_atlas(ground); + auto img_n = maybe_intern_atlas(wall_north); + auto img_w = maybe_intern_atlas(wall_west); tilemeta flags = {}; flags |= meta_ground * (img_g != null_atlas); @@ -110,28 +111,28 @@ void writer_state::serialize_chunk(const chunk& c, chunk_coords coord) constexpr auto ashortp = [](atlasid id) { return id == null_atlas || id == (uchar)id; }; - constexpr auto vshortp = [](const tile_image& img) { + constexpr auto vshortp = [](const tile_image_proto& img) { return !img.atlas || img.variant == (uchar)img.variant; }; if (flags != 0 && ashortp(img_g) && ashortp(img_n) && ashortp(img_w)) flags |= meta_short_atlasid; - if (flags != 0 && vshortp(x.ground) && vshortp(x.wall_north) && vshortp(x.wall_west)) + if (flags != 0 && vshortp(ground) && vshortp(wall_north) && vshortp(wall_west)) flags |= meta_short_variant; - fm_debug_assert((x.passability & pass_mask) == x.passability); - flags |= x.passability; + fm_debug_assert((x.pass_mode & pass_mask) == x.pass_mode); + flags |= x.pass_mode; s << flags; #ifndef FM_NO_DEBUG - constexpr auto check_atlas = [](const tile_image& x) { + constexpr auto check_atlas = [](const tile_image_proto& x) { if (x.atlas) fm_assert(x.variant < x.atlas->num_tiles()); }; - check_atlas(x.ground); - check_atlas(x.wall_north); - check_atlas(x.wall_west); + check_atlas(ground); + check_atlas(wall_north); + check_atlas(wall_west); #endif const auto write = [&](atlasid x, varid v) { @@ -140,11 +141,11 @@ void writer_state::serialize_chunk(const chunk& c, chunk_coords coord) }; if (img_g != null_atlas) - write(img_g, x.ground.variant); + write(img_g, ground.variant); if (img_n != null_atlas) - write(img_n, x.wall_north.variant); + write(img_n, wall_north.variant); if (img_w != null_atlas) - write(img_w, x.wall_west.variant); + write(img_w, wall_west.variant); } const auto nbytes = s.bytes_written(); |