summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-11-09 11:45:34 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-11-09 11:45:34 +0100
commitff3a18b1a251a5e85057e52303efa0cdd79e8a66 (patch)
tree44c2f3a6f69bc922d71063debd14ba669c74ca0d /src
parentb4770eb85369e91cbf800e8192dac0d8c0c627cf (diff)
add floor mesh to struct chunk
Diffstat (limited to 'src')
-rw-r--r--src/chunk.cpp75
-rw-r--r--src/chunk.hpp14
-rw-r--r--src/world.cpp25
-rw-r--r--src/world.hpp13
4 files changed, 106 insertions, 21 deletions
diff --git a/src/chunk.cpp b/src/chunk.cpp
index ea3fd8d7..973a1a5d 100644
--- a/src/chunk.cpp
+++ b/src/chunk.cpp
@@ -1,4 +1,9 @@
#include "chunk.hpp"
+#include "tile-atlas.hpp"
+#include "shaders/tile.hpp"
+#include <algorithm>
+#include <Corrade/Containers/ArrayViewStl.h>
+#include <Magnum/GL/Buffer.h>
namespace floormat {
@@ -19,7 +24,65 @@ bool chunk::empty(bool force) const noexcept
return true;
}
-chunk::chunk() noexcept = default;
+tile_atlas* chunk::ground_atlas_at(std::size_t i) const noexcept
+{
+ return _ground_atlases[i].get();
+}
+
+static constexpr auto make_index_array()
+{
+ std::array<std::array<UnsignedShort, 6>, TILE_COUNT> array;
+ for (std::size_t i = 0; i < TILE_COUNT; i++)
+ array[i] = tile_atlas::indices(i);
+ return array;
+}
+
+auto chunk::ensure_ground_mesh() noexcept -> mesh_tuple
+{
+ if (!_ground_modified)
+ return { ground_mesh, ground_indexes };
+ _ground_modified = false;
+
+ for (std::size_t i = 0; i < TILE_COUNT; i++)
+ ground_indexes[i] = std::uint8_t(i);
+ std::sort(ground_indexes.begin(), ground_indexes.end(), [this](std::uint8_t a, std::uint8_t b) {
+ return _ground_atlases[a].get() < _ground_atlases[b].get();
+ });
+
+ struct vertex {
+ Vector3 position;
+ Vector2 texcoords;
+ };
+ std::array<std::array<vertex, 4>, TILE_COUNT> vertexes;
+ for (std::size_t k = 0; k < TILE_COUNT; k++)
+ {
+ const std::uint8_t i = ground_indexes[k];
+ if (auto atlas = _ground_atlases[i]; !atlas)
+ vertexes[k] = {};
+ else
+ {
+ const std::uint8_t x = i % TILE_MAX_DIM, y = i / TILE_MAX_DIM;
+ auto quad = atlas->floor_quad(Vector3(x, y, 0) * TILE_SIZE, TILE_SIZE2);
+ auto texcoords = atlas->texcoords_for_id(_ground_variants[i] % atlas->num_tiles());
+ auto& v = vertexes[k];
+ for (std::size_t j = 0; j < 4; j++)
+ v[j] = { quad[j], texcoords[j] };
+ }
+ }
+ constexpr auto indexes = make_index_array();
+
+ GL::Mesh mesh{GL::MeshPrimitive::Triangles};
+ mesh.addVertexBuffer(GL::Buffer{vertexes}, 0, tile_shader::Position{}, tile_shader::TextureCoordinates{})
+ .setIndexBuffer(GL::Buffer{indexes}, 0, GL::MeshIndexType::UnsignedShort)
+ .setCount(6 * TILE_COUNT);
+ ground_mesh = Utility::move(mesh);
+ return { ground_mesh, ground_indexes };
+}
+
+chunk::chunk() noexcept // NOLINT(modernize-use-equals-default)
+{
+ //fm_debug("chunk ctor");
+}
tile_ref chunk::operator[](std::size_t idx) noexcept { return { *this, std::uint8_t(idx) }; }
tile_proto chunk::operator[](std::size_t idx) const noexcept { return tile_proto(tile_ref { *const_cast<chunk*>(this), std::uint8_t(idx) }); }
@@ -36,4 +99,14 @@ auto chunk::end() const noexcept -> const_iterator { return cend(); }
chunk::chunk(chunk&&) noexcept = default;
chunk& chunk::operator=(chunk&&) noexcept = default;
+void chunk::mark_modified() noexcept
+{
+ _ground_modified = true;
+}
+
+bool chunk::is_modified() const noexcept
+{
+ return _ground_modified;
+}
+
} // namespace floormat
diff --git a/src/chunk.hpp b/src/chunk.hpp
index 70a98dbd..1f16a9c1 100644
--- a/src/chunk.hpp
+++ b/src/chunk.hpp
@@ -5,6 +5,7 @@
#include <type_traits>
#include <array>
#include <bitset>
+#include <Magnum/GL/Mesh.h>
namespace floormat {
@@ -38,13 +39,24 @@ struct chunk final
chunk(chunk&&) noexcept;
chunk& operator=(chunk&&) noexcept;
+ void mark_modified() noexcept;
+ bool is_modified() const noexcept;
+
+ struct mesh_tuple { GL::Mesh& mesh; const std::array<std::uint8_t, TILE_COUNT>& ids; }; // NOLINT
+
+ mesh_tuple ensure_ground_mesh() noexcept;
+ tile_atlas* ground_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<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 = {};
- mutable bool _maybe_empty = true;
+ std::array<std::uint8_t, TILE_COUNT> ground_indexes = {};
+ GL::Mesh ground_mesh{NoCreate};
+ mutable std::uint8_t _maybe_empty : 1 = true,
+ _ground_modified : 1 = true;
};
} // namespace floormat
diff --git a/src/world.cpp b/src/world.cpp
index 454f8e68..49a2fdf9 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -16,18 +16,11 @@ fm_noinline
chunk& world::operator[](chunk_coords coord) noexcept
{
maybe_collect();
-
- if (auto& [c, coord2] = _last_chunk; c && coord == coord2)
- {
- return *c;
- }
-
- auto [it, inserted] = _chunks.try_emplace(coord);
- auto& ret = it->second;
- auto& [_c, _coord] = _last_chunk;
- _c = &ret;
- _coord = coord;
- return ret;
+ auto& [c, coord2] = _last_chunk;
+ if (coord != coord2)
+ c = &_chunks.try_emplace(coord).first->second;
+ coord2 = coord;
+ return *c;
}
auto world::operator[](global_coords pt) noexcept -> pair
@@ -46,13 +39,14 @@ void world::clear()
_last_collection = 0;
_chunks.clear();
_chunks.rehash(initial_capacity);
- auto& [c, _] = _last_chunk;
+ auto& [c, pos] = _last_chunk;
c = nullptr;
+ pos = chunk_tuple::invalid_coords;
}
void world::maybe_collect()
{
- if (_last_collection + collect_every > _chunks.size())
+ if (_chunks.size() > _last_collection + collect_every)
collect();
}
@@ -68,8 +62,9 @@ void world::collect(bool force)
}
_last_collection = _chunks.size();
- auto& [c, _] = _last_chunk;
+ auto& [c, pos] = _last_chunk;
c = nullptr;
+ pos = chunk_tuple::invalid_coords;
}
} // namespace floormat
diff --git a/src/world.hpp b/src/world.hpp
index 195d4903..248f9dc4 100644
--- a/src/world.hpp
+++ b/src/world.hpp
@@ -11,23 +11,28 @@ namespace floormat {
struct world final
{
private:
+ struct chunk_tuple final {
+ static constexpr chunk_coords invalid_coords = { -1 << 15, -1 << 15 };
+ chunk* c = nullptr;
+ chunk_coords pos = invalid_coords;
+ } _last_chunk;
+
void maybe_collect();
- static constexpr std::size_t initial_capacity = 64, collect_every = 32;
+ static constexpr std::size_t initial_capacity = 64, collect_every = 64;
static constexpr float max_load_factor = .5;
static constexpr auto hasher = [](chunk_coords c) constexpr -> std::size_t {
return int_hash((std::size_t)c.y << 16 | (std::size_t)c.x);
};
-
std::unordered_map<chunk_coords, chunk, decltype(hasher)> _chunks;
- mutable std::tuple<chunk*, chunk_coords> _last_chunk;
std::size_t _last_collection = 0;
+
explicit world(std::size_t capacity);
public:
explicit world();
- struct pair final { chunk& c; tile_ref t; };
+ struct pair final { chunk& c; tile_ref t; }; // NOLINT
template<typename Hash, typename Alloc, typename Pred>
explicit world(std::unordered_map<chunk_coords, chunk, Hash, Alloc, Pred>&& chunks);