diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-09 19:45:46 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-09 19:45:46 +0100 |
commit | 9482f370e56397d50e5d7efef81a33af7390d07a (patch) | |
tree | bc1f4cec23cd4a31ee90e2bbaf592ac5d54f7625 | |
parent | 5907a8902e6f01774cebbb515349a66c86a47fb3 (diff) |
now sorting both wall types together
-rw-r--r-- | draw/wall.cpp | 16 | ||||
-rw-r--r-- | src/chunk.cpp | 69 | ||||
-rw-r--r-- | src/chunk.hpp | 22 | ||||
-rw-r--r-- | src/tile.cpp | 22 |
4 files changed, 55 insertions, 74 deletions
diff --git a/draw/wall.cpp b/draw/wall.cpp index 2008fe05..c65b19fc 100644 --- a/draw/wall.cpp +++ b/draw/wall.cpp @@ -9,7 +9,7 @@ namespace floormat { -#define FM_DEBUG_DRAW_COUNT +//#define FM_DEBUG_DRAW_COUNT constexpr auto quad_index_count = 6; @@ -17,7 +17,7 @@ wall_mesh::wall_mesh() = default; void wall_mesh::draw(tile_shader& shader, chunk& c) { - auto [mesh_, ids_n, ids_w] = c.ensure_wall_mesh(); + auto [mesh_, ids] = c.ensure_wall_mesh(); tile_atlas* last_atlas = nullptr; std::size_t last_pos = 0; @@ -32,7 +32,7 @@ void wall_mesh::draw(tile_shader& shader, chunk& c) { last_atlas->texture().bind(0); mesh.setCount((int)(quad_index_count * len)); - mesh.setIndexRange((int)(last_pos*quad_index_count), 0, quad_index_count*TILE_COUNT - 1); + mesh.setIndexRange((int)(last_pos*quad_index_count), 0, quad_index_count*TILE_COUNT*2 - 1); shader.draw(mesh); draw_count++; } @@ -40,12 +40,12 @@ void wall_mesh::draw(tile_shader& shader, chunk& c) last_pos = i; }; - for (std::size_t k = 0; k < TILE_COUNT; k++) - if (auto* atlas = c.wall_n_atlas_at(ids_n[k])) + for (std::size_t k = 0; k < TILE_COUNT*2; k++) + { + const std::size_t i = ids[k]; + if (auto* atlas = c.wall_atlas_at(i)) do_draw(k, atlas); - for (std::size_t k = 0; k < TILE_COUNT; k++) - if (auto* atlas = c.wall_w_atlas_at(ids_w[k])) - do_draw(k + TILE_COUNT, atlas); + } do_draw(TILE_COUNT*2, nullptr); #ifdef FM_DEBUG_DRAW_COUNT diff --git a/src/chunk.cpp b/src/chunk.cpp index e736fe42..353446f3 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -14,7 +14,7 @@ bool chunk::empty(bool force) const noexcept for (std::size_t i = 0; i < TILE_COUNT; i++) { - if (_ground_atlases[i] || _wall_north_atlases[i] || _wall_west_atlases[i]) + if (_ground_atlases[i] || _wall_atlases[i*2 + 0] || _wall_atlases[i*2 + 1]) { _maybe_empty = false; return false; @@ -25,8 +25,7 @@ bool chunk::empty(bool force) const noexcept } tile_atlas* chunk::ground_atlas_at(std::size_t i) const noexcept { return _ground_atlases[i].get(); } -tile_atlas* chunk::wall_n_atlas_at(std::size_t i) const noexcept { return _wall_north_atlases[i].get(); } -tile_atlas* chunk::wall_w_atlas_at(std::size_t i) const noexcept { return _wall_west_atlases[i].get(); } +tile_atlas* chunk::wall_atlas_at(std::size_t i) const noexcept { return _wall_atlases[i].get(); } static auto make_index_array(std::size_t offset) { @@ -84,53 +83,35 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple { if (!_walls_modified) - return { wall_mesh, wall_n_indexes, wall_w_indexes }; + return { wall_mesh, wall_indexes }; _walls_modified = false; - for (std::size_t i = 0; i < TILE_COUNT; i++) - wall_n_indexes[i] = std::uint8_t(i); - for (std::size_t i = 0; i < TILE_COUNT; i++) - wall_w_indexes[i] = std::uint8_t(i); + for (std::size_t i = 0; i < TILE_COUNT*2; i++) + wall_indexes[i] = std::uint16_t(i); - std::sort(wall_n_indexes.begin(), wall_n_indexes.end(), [this](std::uint8_t a, std::uint8_t b) { - return _wall_north_atlases[a].get() < _wall_north_atlases[b].get(); + std::sort(wall_indexes.begin(), wall_indexes.end(), [this](std::uint16_t a, std::uint16_t b) { + return _wall_atlases[a] < _wall_atlases[b]; }); - std::sort(wall_w_indexes.begin(), wall_w_indexes.end(), [this](std::uint8_t a, std::uint8_t b) { - return _wall_west_atlases[a].get() < _wall_west_atlases[b].get(); - }); - - std::array<std::array<vertex, 4>, TILE_COUNT> vertexes[2] = {}; - using ids_ = std::array<std::uint8_t, TILE_COUNT>; - using a_ = std::array<std::shared_ptr<tile_atlas>, TILE_COUNT>; - using vs_ = std::array<variant_t, TILE_COUNT>; - using verts_ = std::array<std::array<vertex, 4>, TILE_COUNT>; - constexpr auto do_walls = [](const ids_& ids, const a_& as, const vs_& vs, verts_& verts, const auto& fn) { - for (std::size_t k = 0; k < TILE_COUNT; k++) + std::array<std::array<vertex, 4>, TILE_COUNT*2> vertexes; + for (std::size_t k = 0; k < TILE_COUNT*2; k++) + { + const std::uint16_t i = wall_indexes[k]; + if (const auto& atlas = _wall_atlases[i]; !atlas) + vertexes[k] = {}; + else { - const std::uint8_t i = ids[k]; - if (const auto& atlas = as[i]; !atlas) - verts[k] = {}; - else - { - const local_coords pos{i}; - const float depth = tile_shader::depth_value(pos); - const std::array<Vector3, 4> quad = fn(*atlas, pos); - const auto texcoords = atlas->texcoords_for_id(vs[i] % atlas->num_tiles()); - auto& v = verts[k]; - for (std::size_t j = 0; j < 4; j++) - v[j] = { quad[j], texcoords[j], depth, }; - } + const auto& variant = _wall_variants[i]; + const local_coords pos{i / 2u}; + const auto center = Vector3(pos.x, pos.y, 0) * TILE_SIZE; + const auto quad = i & 1 ? atlas->wall_quad_W(center, TILE_SIZE) : atlas->wall_quad_N(center, TILE_SIZE); + const float depth = tile_shader::depth_value(pos); + const auto texcoords = atlas->texcoords_for_id(variant % atlas->num_tiles()); + auto& v = vertexes[k]; + for (std::size_t j = 0; j < 4; j++) + v[j] = { quad[j], texcoords[j], depth, }; } - }; - do_walls(wall_n_indexes, _wall_north_atlases, _wall_north_variants, vertexes[0], - [](const tile_atlas& a, local_coords pos) { - return a.wall_quad_N(Vector3(pos.x, pos.y, 0) * TILE_SIZE, TILE_SIZE); - }); - do_walls(wall_w_indexes, _wall_west_atlases, _wall_west_variants, vertexes[1], - [](const tile_atlas& a, local_coords pos) { - return a.wall_quad_W(Vector3(pos.x, pos.y, 0) * TILE_SIZE, TILE_SIZE); - }); + } using index_t = std::array<std::array<UnsignedShort, 6>, TILE_COUNT>; const index_t indexes[2] = { make_index_array(0), make_index_array(TILE_COUNT) }; @@ -140,7 +121,7 @@ auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple .setIndexBuffer(GL::Buffer{indexes}, 0, GL::MeshIndexType::UnsignedShort) .setCount(6 * TILE_COUNT); wall_mesh = Utility::move(mesh); - return { wall_mesh, wall_n_indexes, wall_w_indexes }; + return { wall_mesh, wall_indexes }; } fm_noinline diff --git a/src/chunk.hpp b/src/chunk.hpp index 1b702d81..c0cb436d 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -44,30 +44,30 @@ struct chunk final void mark_modified() noexcept; struct ground_mesh_tuple final { - GL::Mesh& mesh; - const std::array<std::uint8_t, TILE_COUNT>& ids; + GL::Mesh& mesh; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) + const std::array<std::uint8_t, TILE_COUNT>& ids; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) }; struct wall_mesh_tuple final { - GL::Mesh& mesh; - const std::array<std::uint8_t, TILE_COUNT>& n; - const std::array<std::uint8_t, TILE_COUNT>& w; + GL::Mesh& mesh; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) + const std::array<std::uint16_t, TILE_COUNT*2>& ids; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) }; ground_mesh_tuple ensure_ground_mesh() noexcept; tile_atlas* ground_atlas_at(std::size_t i) const noexcept; wall_mesh_tuple ensure_wall_mesh() noexcept; - tile_atlas* wall_n_atlas_at(std::size_t i) const noexcept; - tile_atlas* wall_w_atlas_at(std::size_t i) const noexcept; + tile_atlas* wall_atlas_at(std::size_t i) const noexcept; private: - std::array<std::shared_ptr<tile_atlas>, TILE_COUNT> _ground_atlases, _wall_north_atlases, _wall_west_atlases; + std::array<std::shared_ptr<tile_atlas>, TILE_COUNT> _ground_atlases; + std::array<std::uint8_t, TILE_COUNT> ground_indexes = {}; + std::array<variant_t, TILE_COUNT> _ground_variants = {}; + std::array<std::shared_ptr<tile_atlas>, TILE_COUNT*2> _wall_atlases; + std::array<std::uint16_t, TILE_COUNT*2> wall_indexes = {}; + std::array<variant_t, TILE_COUNT*2> _wall_variants; std::array<std::shared_ptr<anim_atlas>, TILE_COUNT> _scenery_atlases; std::array<scenery, TILE_COUNT> _scenery_variants = {}; - std::array<variant_t, TILE_COUNT> _ground_variants = {}, _wall_north_variants = {}, _wall_west_variants = {}; std::bitset<TILE_COUNT*2> _passability = {}; - std::array<std::uint8_t, TILE_COUNT> ground_indexes = {}; - std::array<std::uint8_t, TILE_COUNT> wall_n_indexes = {}, wall_w_indexes = {}; GL::Mesh ground_mesh{NoCreate}, wall_mesh{NoCreate}; mutable std::uint8_t _maybe_empty : 1 = true, _ground_modified : 1 = true, diff --git a/src/tile.cpp b/src/tile.cpp index 6f329702..5f003258 100644 --- a/src/tile.cpp +++ b/src/tile.cpp @@ -44,24 +44,24 @@ scenery_proto tile_proto::scenery_image() const noexcept { return { scener tile_ref::tile_ref(struct chunk& c, std::uint8_t i) noexcept : _chunk{&c}, i{i} {} std::shared_ptr<tile_atlas> tile_ref::ground_atlas() noexcept { return _chunk->_ground_atlases[i]; } -std::shared_ptr<tile_atlas> tile_ref::wall_north_atlas() noexcept { return _chunk->_wall_north_atlases[i]; } -std::shared_ptr<tile_atlas> tile_ref::wall_west_atlas() noexcept { return _chunk->_wall_west_atlases[i]; } +std::shared_ptr<tile_atlas> tile_ref::wall_north_atlas() noexcept { return _chunk->_wall_atlases[i*2+0]; } +std::shared_ptr<tile_atlas> tile_ref::wall_west_atlas() noexcept { return _chunk->_wall_atlases[i*2+1]; } std::shared_ptr<anim_atlas> tile_ref::scenery_atlas() noexcept { return _chunk->_scenery_atlases[i]; } std::shared_ptr<const tile_atlas> tile_ref::ground_atlas() const noexcept { return _chunk->_ground_atlases[i]; } -std::shared_ptr<const tile_atlas> tile_ref::wall_north_atlas() const noexcept { return _chunk->_wall_north_atlases[i]; } -std::shared_ptr<const tile_atlas> tile_ref::wall_west_atlas() const noexcept { return _chunk->_wall_west_atlases[i]; } +std::shared_ptr<const tile_atlas> tile_ref::wall_north_atlas() const noexcept { return _chunk->_wall_atlases[i*2+0]; } +std::shared_ptr<const tile_atlas> tile_ref::wall_west_atlas() const noexcept { return _chunk->_wall_atlases[i*2+1]; } std::shared_ptr<const anim_atlas> tile_ref::scenery_atlas() const noexcept { return _chunk->_scenery_atlases[i]; } tile_image_ref tile_ref::ground() noexcept { return {_chunk->_ground_atlases[i], _chunk->_ground_variants[i] }; } -tile_image_ref tile_ref::wall_north() noexcept { return {_chunk->_wall_north_atlases[i], _chunk->_wall_north_variants[i] }; } -tile_image_ref tile_ref::wall_west() noexcept { return {_chunk->_wall_west_atlases[i], _chunk->_wall_west_variants[i] }; } +tile_image_ref tile_ref::wall_north() noexcept { return {_chunk->_wall_atlases[i*2+0], _chunk->_wall_variants[i*2+0] }; } +tile_image_ref tile_ref::wall_west() noexcept { return {_chunk->_wall_atlases[i*2+1], _chunk->_wall_variants[i*2+1] }; } scenery_ref tile_ref::scenery() noexcept { return {_chunk->_scenery_atlases[i], _chunk->_scenery_variants[i] }; } tile_image_proto tile_ref::ground() const noexcept { return { _chunk->_ground_atlases[i], _chunk->_ground_variants[i] }; } -tile_image_proto tile_ref::wall_north() const noexcept { return { _chunk->_wall_north_atlases[i], _chunk->_wall_north_variants[i] }; } -tile_image_proto tile_ref::wall_west() const noexcept { return { _chunk->_wall_west_atlases[i], _chunk->_wall_west_variants[i] }; } -scenery_proto tile_ref::scenery() const noexcept { return {_chunk->_scenery_atlases[i], _chunk->_scenery_variants[i] }; } +tile_image_proto tile_ref::wall_north() const noexcept { return { _chunk->_wall_atlases[i*2+0], _chunk->_wall_variants[i*2+0] }; } +tile_image_proto tile_ref::wall_west() const noexcept { return { _chunk->_wall_atlases[i*2+1], _chunk->_wall_variants[i*2+1] }; } +scenery_proto tile_ref::scenery() const noexcept { return { _chunk->_scenery_atlases[i], _chunk->_scenery_variants[i] }; } pass_mode_ref tile_ref::pass_mode() noexcept { return { *_chunk, i }; } pass_mode tile_ref::pass_mode() const noexcept { return pass_mode_ref { *const_cast<struct chunk*>(_chunk), i }; } @@ -69,8 +69,8 @@ pass_mode tile_ref::pass_mode() const noexcept { return pass_mode_ref { *const_c tile_ref::operator tile_proto() const noexcept { return { - _chunk->_ground_atlases[i], _chunk->_wall_north_atlases[i], _chunk->_wall_west_atlases[i], _chunk->_scenery_atlases[i], - _chunk->_ground_variants[i], _chunk->_wall_north_variants[i], _chunk->_wall_west_variants[i], _chunk->_scenery_variants[i], + _chunk->_ground_atlases[i], _chunk->_wall_atlases[i*2+0], _chunk->_wall_atlases[i*2+1], _chunk->_scenery_atlases[i], + _chunk->_ground_variants[i], _chunk->_wall_variants[i*2+0], _chunk->_wall_variants[i*2+1], _chunk->_scenery_variants[i], pass_mode(), }; } |