diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/chunk-render.cpp | 55 | ||||
-rw-r--r-- | src/chunk.cpp | 6 | ||||
-rw-r--r-- | src/chunk.hpp | 24 | ||||
-rw-r--r-- | src/tile.cpp | 44 |
4 files changed, 87 insertions, 42 deletions
diff --git a/src/chunk-render.cpp b/src/chunk-render.cpp index 38d35030..6dff9a40 100644 --- a/src/chunk-render.cpp +++ b/src/chunk-render.cpp @@ -16,30 +16,45 @@ static auto make_index_array(size_t max) return array; } +void chunk::ensure_alloc_ground() +{ + if (!_ground) [[unlikely]] + _ground = Pointer<ground_stuff>{InPlaceInit}; +} + +void chunk::ensure_alloc_walls() +{ + if (!_walls) [[unlikely]] + _walls = Pointer<wall_stuff>{InPlaceInit}; +} + auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple { + if (!_ground) + return { ground_mesh, {}, 0 }; + if (!_ground_modified) - return { ground_mesh, ground_indexes, size_t(ground_mesh.count()/6) }; + return { ground_mesh, _ground->ground_indexes, size_t(ground_mesh.count()/6) }; _ground_modified = false; size_t count = 0; for (auto i = 0uz; i < TILE_COUNT; i++) - if (_ground_atlases[i]) - ground_indexes[count++] = uint8_t(i); + if (_ground->_ground_atlases[i]) + _ground->ground_indexes[count++] = uint8_t(i); - std::sort(ground_indexes.begin(), ground_indexes.begin() + count, + std::sort(_ground->ground_indexes.begin(), _ground->ground_indexes.begin() + count, [this](uint8_t a, uint8_t b) { - return _ground_atlases[a] < _ground_atlases[b]; + return _ground->_ground_atlases[a] < _ground->_ground_atlases[b]; }); std::array<std::array<vertex, 4>, TILE_COUNT> vertexes; for (auto k = 0uz; k < count; k++) { - const uint8_t i = ground_indexes[k]; - const auto& atlas = _ground_atlases[i]; + const uint8_t i = _ground->ground_indexes[k]; + const auto& atlas = _ground->_ground_atlases[i]; const local_coords pos{i}; const auto quad = atlas->floor_quad(Vector3(pos) * TILE_SIZE, TILE_SIZE2); - const auto texcoords = atlas->texcoords_for_id(_ground_variants[i]); + const auto texcoords = atlas->texcoords_for_id(_ground->_ground_variants[i]); const float depth = tile_shader::depth_value(pos, tile_shader::ground_depth_offset); auto& v = vertexes[k]; for (auto j = 0uz; j < 4; j++) @@ -55,19 +70,20 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple .setIndexBuffer(GL::Buffer{vert_index_view}, 0, GL::MeshIndexType::UnsignedShort) .setCount(int32_t(6 * count)); ground_mesh = Utility::move(mesh); - return { ground_mesh, ground_indexes, count }; + return { ground_mesh, _ground->ground_indexes, count }; } fm_noinline GL::Mesh chunk::make_wall_mesh(size_t count) { + fm_debug_assert(_walls); //std::array<std::array<vertex, 4>, TILE_COUNT*2> vertexes; vertex vertexes[TILE_COUNT*2][4]; for (auto k = 0uz; k < count; k++) { - const uint16_t i = wall_indexes[k]; - const auto& atlas = _wall_atlases[i]; - const auto& variant = _wall_variants[i]; + const uint16_t i = _walls->wall_indexes[k]; + const auto& atlas = _walls->_wall_atlases[i]; + const auto& variant = _walls->_wall_variants[i]; const local_coords pos{i / 2u}; const auto center = Vector3(pos) * TILE_SIZE; const auto quad = i & 1 ? atlas->wall_quad_W(center, TILE_SIZE) : atlas->wall_quad_N(center, TILE_SIZE); @@ -91,22 +107,25 @@ GL::Mesh chunk::make_wall_mesh(size_t count) auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple { + if (!_walls) + return {wall_mesh, {}, 0}; + if (!_walls_modified) - return { wall_mesh, wall_indexes, size_t(wall_mesh.count()/6) }; + return { wall_mesh, _walls->wall_indexes, size_t(wall_mesh.count()/6) }; _walls_modified = false; size_t count = 0; for (auto i = 0uz; i < TILE_COUNT*2; i++) - if (_wall_atlases[i]) - wall_indexes[count++] = uint16_t(i); + if (_walls->_wall_atlases[i]) + _walls->wall_indexes[count++] = uint16_t(i); - std::sort(wall_indexes.data(), wall_indexes.data() + (ptrdiff_t)count, + std::sort(_walls->wall_indexes.data(), _walls->wall_indexes.data() + (ptrdiff_t)count, [this](uint16_t a, uint16_t b) { - return _wall_atlases[a] < _wall_atlases[b]; + return _walls->_wall_atlases[a] < _walls->_wall_atlases[b]; }); wall_mesh = make_wall_mesh(count); - return { wall_mesh, wall_indexes, count }; + return { wall_mesh, _walls->wall_indexes, count }; } } // namespace floormat diff --git a/src/chunk.cpp b/src/chunk.cpp index 5450a02e..ff35f292 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -27,15 +27,15 @@ bool chunk::empty(bool force) const noexcept if (!force && !_maybe_empty) return false; for (auto i = 0uz; i < TILE_COUNT; i++) - if (_ground_atlases[i] || _wall_atlases[i*2 + 0] || _wall_atlases[i*2 + 1] || !_entities.empty()) + if (!_entities.empty() || _ground && _ground->_ground_atlases[i] || _walls && (_walls->_wall_atlases[i*2+0] || _walls->_wall_atlases[i*2+1])) return _maybe_empty = false; if (!_entities.empty()) return false; return true; } -tile_atlas* chunk::ground_atlas_at(size_t i) const noexcept { return _ground_atlases[i].get(); } -tile_atlas* chunk::wall_atlas_at(size_t i) const noexcept { return _wall_atlases[i].get(); } +tile_atlas* chunk::ground_atlas_at(size_t i) const noexcept { return _ground ? _ground->_ground_atlases[i].get() : nullptr; } +tile_atlas* chunk::wall_atlas_at(size_t i) const noexcept { return _walls ? _walls->_wall_atlases[i].get() : nullptr; } tile_ref chunk::operator[](size_t idx) noexcept { return { *this, uint8_t(idx) }; } tile_proto chunk::operator[](size_t idx) const noexcept { return tile_proto(tile_ref { *const_cast<chunk*>(this), uint8_t(idx) }); } diff --git a/src/chunk.hpp b/src/chunk.hpp index 505e335d..a41ed9a0 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -5,7 +5,7 @@ #include "src/RTree.h" #include <type_traits> #include <array> -#include <memory> +#include <Corrade/Containers/Pointer.h> #include <Magnum/GL/Mesh.h> namespace Corrade::Containers { template<typename T, typename D> class Array; } @@ -94,6 +94,8 @@ struct chunk final using RTree = ::RTree<object_id, float, 2, float>; + void ensure_alloc_ground(); + void ensure_alloc_walls(); ground_mesh_tuple ensure_ground_mesh() noexcept; tile_atlas* ground_atlas_at(size_t i) const noexcept; wall_mesh_tuple ensure_wall_mesh() noexcept; @@ -115,12 +117,20 @@ struct chunk final const std::vector<std::shared_ptr<entity>>& entities() const; private: - std::array<std::shared_ptr<tile_atlas>, TILE_COUNT> _ground_atlases; - std::array<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; // todo make into vector - std::array<uint16_t, TILE_COUNT*2> wall_indexes = {}; - std::array<variant_t, TILE_COUNT*2> _wall_variants = {}; + struct ground_stuff { + std::array<std::shared_ptr<tile_atlas>, TILE_COUNT> _ground_atlases; + std::array<uint8_t, TILE_COUNT> ground_indexes = {}; + std::array<variant_t, TILE_COUNT> _ground_variants = {}; + }; + + struct wall_stuff { + std::array<std::shared_ptr<tile_atlas>, TILE_COUNT*2> _wall_atlases; // todo make into vector + std::array<uint16_t, TILE_COUNT*2> wall_indexes = {}; + std::array<variant_t, TILE_COUNT*2> _wall_variants = {}; + }; + + Pointer<ground_stuff> _ground; + Pointer<wall_stuff> _walls; std::vector<std::shared_ptr<entity>> _entities; struct world* _world; diff --git a/src/tile.cpp b/src/tile.cpp index 5a85a09c..2d658ff1 100644 --- a/src/tile.cpp +++ b/src/tile.cpp @@ -15,27 +15,43 @@ tile_image_proto tile_proto::wall_west() const noexcept { return { wall_west_at tile_ref::tile_ref(struct chunk& c, 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_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<tile_atlas> tile_ref::ground_atlas() noexcept { return _chunk->_ground ? _chunk->_ground->_ground_atlases[i] : nullptr; } +std::shared_ptr<tile_atlas> tile_ref::wall_north_atlas() noexcept { return _chunk->_walls ? _chunk->_walls->_wall_atlases[i*2+0] : nullptr; } +std::shared_ptr<tile_atlas> tile_ref::wall_west_atlas() noexcept { return _chunk->_walls ? _chunk->_walls->_wall_atlases[i*2+1] : nullptr; } -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_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 tile_atlas> tile_ref::ground_atlas() const noexcept { return _chunk->_ground ? _chunk->_ground->_ground_atlases[i] : nullptr; } +std::shared_ptr<const tile_atlas> tile_ref::wall_north_atlas() const noexcept { return _chunk->_walls ? _chunk->_walls->_wall_atlases[i*2+0] : nullptr; } +std::shared_ptr<const tile_atlas> tile_ref::wall_west_atlas() const noexcept { return _chunk->_walls ? _chunk->_walls->_wall_atlases[i*2+1] : nullptr; } -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_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] }; } +tile_image_ref tile_ref::ground() noexcept { _chunk->ensure_alloc_ground(); return {_chunk->_ground->_ground_atlases[i], _chunk->_ground->_ground_variants[i] }; } +tile_image_ref tile_ref::wall_north() noexcept { _chunk->ensure_alloc_walls(); return {_chunk->_walls->_wall_atlases[i*2+0], _chunk->_walls->_wall_variants[i*2+0] }; } +tile_image_ref tile_ref::wall_west() noexcept { _chunk->ensure_alloc_walls(); return {_chunk->_walls->_wall_atlases[i*2+1], _chunk->_walls->_wall_variants[i*2+1] }; } -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_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] }; } +tile_image_proto tile_ref::ground() const noexcept +{ + _chunk->ensure_alloc_ground(); + return { _chunk->_ground->_ground_atlases[i], _chunk->_ground->_ground_variants[i] }; +} + +tile_image_proto tile_ref::wall_north() const noexcept +{ + _chunk->ensure_alloc_walls(); + return { _chunk->_walls->_wall_atlases[i*2+0], _chunk->_walls->_wall_variants[i*2+0] }; +} + +tile_image_proto tile_ref::wall_west() const noexcept +{ + _chunk->ensure_alloc_walls(); + return { _chunk->_walls->_wall_atlases[i*2+1], _chunk->_walls->_wall_variants[i*2+1] }; +} tile_ref::operator tile_proto() const noexcept { + _chunk->ensure_alloc_ground(); + _chunk->ensure_alloc_walls(); return { - _chunk->_ground_atlases[i], _chunk->_wall_atlases[i*2+0], _chunk->_wall_atlases[i*2+1], - _chunk->_ground_variants[i], _chunk->_wall_variants[i*2+0], _chunk->_wall_variants[i*2+1], + _chunk->_ground->_ground_atlases[i], _chunk->_walls->_wall_atlases[i*2+0], _chunk->_walls->_wall_atlases[i*2+1], + _chunk->_ground->_ground_variants[i], _chunk->_walls->_wall_variants[i*2+0], _chunk->_walls->_wall_variants[i*2+1], }; } |