diff options
Diffstat (limited to 'serialize')
-rw-r--r-- | serialize/tile.cpp | 1 | ||||
-rw-r--r-- | serialize/world-reader.cpp | 6 | ||||
-rw-r--r-- | serialize/world-writer.cpp | 27 |
3 files changed, 26 insertions, 8 deletions
diff --git a/serialize/tile.cpp b/serialize/tile.cpp index 8da51af9..68def2d8 100644 --- a/serialize/tile.cpp +++ b/serialize/tile.cpp @@ -7,7 +7,6 @@ namespace floormat { 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) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(chunk_coords, x, y) diff --git a/serialize/world-reader.cpp b/serialize/world-reader.cpp index 4c67031e..b999b5d4 100644 --- a/serialize/world-reader.cpp +++ b/serialize/world-reader.cpp @@ -35,8 +35,8 @@ void reader_state::read_atlases(reader_t& s) Vector2ub size;
s >> size[0];
s >> size[1];
- auto str = s.read_asciiz_string<atlas_name_max>();
- atlases[i] = loader.tile_atlas({str.buf, str.len}, size);
+ const auto& [buf, len] = s.read_asciiz_string<atlas_name_max>();
+ atlases[i] = loader.tile_atlas({buf, len}, size);
}
}
@@ -73,7 +73,7 @@ void reader_state::read_chunks(reader_t& s) };
tile& t = chunk[i];
if (flags & meta_ground)
- t.ground_image = make_atlas();
+ t.ground = make_atlas();
if (flags & meta_wall_n)
t.wall_north = make_atlas();
if (flags & meta_wall_w)
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 <vector>
+#include <algorithm>
#include <Corrade/Containers/StringView.h>
#include <Corrade/Utility/Path.h>
@@ -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<interned_atlas> 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);
|