From b0069421278d6fc3fac0153e2f19c549c7bfce32 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 29 Oct 2022 01:28:12 +0200 Subject: a --- serialize/world-writer.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'serialize/world-writer.cpp') diff --git a/serialize/world-writer.cpp b/serialize/world-writer.cpp index e1750fb2..f47e96b1 100644 --- a/serialize/world-writer.cpp +++ b/serialize/world-writer.cpp @@ -7,6 +7,7 @@ #include "src/chunk.hpp" #include "src/world.hpp" #include +#include #include #include @@ -90,7 +91,7 @@ void writer_state::serialize_chunk(const chunk& c, chunk_coords coord) fm_debug_assert(s.bytes_written() + tile_size <= chunkbuf_size); - auto img_g = maybe_intern_atlas(x.ground_image); + 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_north); @@ -104,8 +105,18 @@ void writer_state::serialize_chunk(const chunk& c, chunk_coords coord) s << flags; +#ifndef FM_NO_DEBUG + constexpr auto check_atlas = [](const tile_image& 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); +#endif + if (img_g != null_atlas) - s << img_g << x.ground_image.variant; + s << img_g << x.ground.variant; if (img_n != null_atlas) s << img_n << x.wall_north.variant; if (img_w != null_atlas) @@ -131,9 +142,17 @@ void writer_state::serialize_atlases() s << sz; - for (const auto& [p, t] : tile_images) + std::vector atlases; + atlases.reserve(tile_images.size()); + + for (const auto& [_, t] : tile_images) + atlases.push_back(t); + std::sort(atlases.begin(), atlases.end(), [](const auto& a, const auto& b) { + return a.index < b.index; + }); + + for (const auto& [atlas, _] : atlases) { - const auto& [atlas, index] = t; const auto name = atlas->name(); const auto namesiz = name.size(); fm_debug_assert(s.bytes_written() + namesiz + 1 <= atlasbuf_size); -- cgit v1.2.3