From b0069421278d6fc3fac0153e2f19c549c7bfce32 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 29 Oct 2022 01:28:12 +0200 Subject: a --- draw/floor.cpp | 6 +++--- editor/editor.cpp | 2 +- editor/update.cpp | 2 +- serialize/tile.cpp | 1 - serialize/world-reader.cpp | 6 +++--- serialize/world-writer.cpp | 27 +++++++++++++++++++++++---- src/chunk.cpp | 2 +- src/tile.hpp | 2 +- test/json.cpp | 2 +- 9 files changed, 34 insertions(+), 16 deletions(-) diff --git a/draw/floor.cpp b/draw/floor.cpp index 43bd8ec0..bfba0c00 100644 --- a/draw/floor.cpp +++ b/draw/floor.cpp @@ -19,9 +19,9 @@ floor_mesh::floor_mesh() void floor_mesh::set_tile(quad_data& data, tile& x) { - if (x.ground_image) + if (x.ground) { - auto texcoords = x.ground_image.atlas->texcoords_for_id(x.ground_image.variant); + auto texcoords = x.ground.atlas->texcoords_for_id(x.ground.variant); for (size_t i = 0; i < 4; i++) data[i] = { texcoords[i] }; } @@ -58,7 +58,7 @@ void floor_mesh::draw(tile_shader& shader, chunk& c) }; for (auto& [x, i, pt] : c) - do_draw(i, x.ground_image.atlas.get()); + do_draw(i, x.ground.atlas.get()); do_draw(TILE_COUNT, nullptr); } diff --git a/editor/editor.cpp b/editor/editor.cpp index 0cd15d17..defc1476 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -161,7 +161,7 @@ void tile_editor::place_tile(world& world, global_coords pos, tile_image& img) case editor_mode::select: break; case editor_mode::floor: { - t.ground_image = { atlas, variant }; + t.ground = {atlas, variant }; break; } case editor_mode::walls: { diff --git a/editor/update.cpp b/editor/update.cpp index f22ad522..52011b73 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -18,7 +18,7 @@ void app::maybe_initialize_chunk_(const chunk_coords& pos, chunk& c) #else const auto& atlas = pt.x == N/2 || pt.y == N/2 ? _floor2 : _floor1; #endif - x.ground_image = { atlas, decltype(tile_image::variant)(k % atlas->num_tiles()) }; + x.ground = { atlas, decltype(tile_image::variant)(k % atlas->num_tiles()) }; } #ifdef FM_NO_BINDINGS const auto& wall1 = floor1, wall2 = floor1; 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(); - atlases[i] = loader.tile_atlas({str.buf, str.len}, size); + const auto& [buf, len] = s.read_asciiz_string(); + 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 +#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); diff --git a/src/chunk.cpp b/src/chunk.cpp index 92817765..9eca0138 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -8,7 +8,7 @@ bool chunk::empty(bool force) const noexcept return false; for (const tile& x : _tiles) - if (x.ground_image || x.wall_north || x.wall_west) + if (x.ground || x.wall_north || x.wall_west) { _maybe_empty = false; return false; diff --git a/src/tile.hpp b/src/tile.hpp index ef0b1ade..0448b426 100644 --- a/src/tile.hpp +++ b/src/tile.hpp @@ -8,7 +8,7 @@ struct tile final { enum pass_mode : std::uint8_t { pass_blocked, pass_ok, pass_shoot_through, }; - tile_image ground_image, wall_north, wall_west; + tile_image ground, wall_north, wall_west; pass_mode passability = pass_shoot_through; constexpr tile() = default; diff --git a/test/json.cpp b/test/json.cpp index 83854c38..927e7355 100644 --- a/test/json.cpp +++ b/test/json.cpp @@ -20,7 +20,7 @@ static chunk make_test_chunk() constexpr auto N = TILE_MAX_DIM; chunk c; for (auto& [x, k, pt] : c) { - x.ground_image = { tiles, decltype(tile_image::variant)(k % tiles->num_tiles()) }; + x.ground = { tiles, decltype(tile_image::variant)(k % tiles->num_tiles()) }; } constexpr auto K = N/2; c[{K, K }].wall_north = { metal1, 0 }; -- cgit v1.2.3