summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--draw/floor.cpp51
-rw-r--r--draw/floor.hpp13
-rw-r--r--draw/wall.cpp2
-rw-r--r--draw/wireframe.hpp2
-rw-r--r--editor/draw.cpp2
-rw-r--r--editor/update.cpp3
-rw-r--r--main/draw.cpp8
-rw-r--r--src/chunk.cpp75
-rw-r--r--src/chunk.hpp14
-rw-r--r--src/world.cpp25
-rw-r--r--src/world.hpp13
11 files changed, 124 insertions, 84 deletions
diff --git a/draw/floor.cpp b/draw/floor.cpp
index 861336c7..ee7b4014 100644
--- a/draw/floor.cpp
+++ b/draw/floor.cpp
@@ -11,42 +11,20 @@ constexpr auto quad_index_count = 6;
floor_mesh::floor_mesh()
{
- _mesh.setCount((int)(quad_index_count * TILE_COUNT))
- .addVertexBuffer(_positions_buffer, 0, tile_shader::Position{})
- .addVertexBuffer(_vertex_buffer, 0, tile_shader::TextureCoordinates{})
- .setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort);
-}
-
-void floor_mesh::set_tile(quad_data& data, tile_ref& x)
-{
- if (auto ground = x.ground(); ground)
- {
- auto texcoords = ground.atlas->texcoords_for_id(ground.variant);
- for (size_t i = 0; i < 4; i++)
- data[i] = { texcoords[i] };
- }
- else
- for (size_t i = 0; i < 4; i++)
- data[i] = {};
}
void floor_mesh::draw(tile_shader& shader, chunk& c)
{
- //_vertex_buffer.setData({nullptr, sizeof(quad_data) * TILE_COUNT}, Magnum::GL::BufferUsage::DynamicDraw); // orphan the buffer
- std::array<quad_data, TILE_COUNT> data;
- for (auto [x, idx, pt] : c) {
- set_tile(data[idx], x);
- }
- _vertex_buffer.setSubData(0, data);
- Magnum::GL::MeshView mesh{_mesh};
+ auto [mesh_, ids] = c.ensure_ground_mesh();
tile_atlas* last_atlas = nullptr;
std::size_t last_pos = 0;
+ GL::MeshView mesh{mesh_};
const auto do_draw = [&](std::size_t i, tile_atlas* atlas) {
if (atlas == last_atlas)
return;
- if (auto len = i - last_pos; last_atlas != nullptr && len > 0)
+ if (auto len = i - last_pos; last_atlas && len > 0)
{
last_atlas->texture().bind(0);
mesh.setCount((int)(quad_index_count * len));
@@ -57,27 +35,10 @@ void floor_mesh::draw(tile_shader& shader, chunk& c)
last_pos = i;
};
- for (auto [x, i, pt] : c)
- do_draw(i, x.ground_atlas().get());
+ for (std::size_t k = 0; k < TILE_COUNT; k++)
+ if (auto* atlas = c.ground_atlas_at(ids[k]))
+ do_draw(k, atlas);
do_draw(TILE_COUNT, nullptr);
}
-std::array<std::array<UnsignedShort, 6>, TILE_COUNT> floor_mesh::make_index_array()
-{
- // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
- std::array<std::array<UnsignedShort, quad_index_count>, TILE_COUNT> array;
- for (std::size_t i = 0; i < std::size(array); i++)
- array[i] = tile_atlas::indices(i);
- return array;
-}
-
-std::array<std::array<Vector3, 4>, TILE_COUNT> floor_mesh::make_position_array()
-{
- std::array<std::array<Vector3, 4>, TILE_COUNT> array;
- for (std::uint8_t j = 0, k = 0; j < TILE_MAX_DIM; j++)
- for (std::uint8_t i = 0; i < TILE_MAX_DIM; i++, k++)
- array[k] = { tile_atlas::floor_quad(Vector3(i, j, 0) * TILE_SIZE, TILE_SIZE2) };
- return array;
-}
-
} // namespace floormat
diff --git a/draw/floor.hpp b/draw/floor.hpp
index 651cee61..07e183d6 100644
--- a/draw/floor.hpp
+++ b/draw/floor.hpp
@@ -21,19 +21,6 @@ struct floor_mesh final
floor_mesh(const floor_mesh&) = delete;
void draw(tile_shader& shader, chunk& c);
-
-private:
- struct vertex_data final { Vector2 texcoords; };
- using quad_data = std::array<vertex_data, 4>;
-
- static std::array<std::array<UnsignedShort, 6>, TILE_COUNT> make_index_array();
- static std::array<std::array<Vector3, 4>, TILE_COUNT> make_position_array();
-
- GL::Mesh _mesh;
- GL::Buffer _vertex_buffer{std::array<quad_data, TILE_COUNT>{}, Magnum::GL::BufferUsage::DynamicDraw},
- _index_buffer{make_index_array()}, _positions_buffer{make_position_array()};
-
- static void set_tile(quad_data& data, tile_ref& x);
};
} // namespace floormat
diff --git a/draw/wall.cpp b/draw/wall.cpp
index 9bcfe9d4..4c25b267 100644
--- a/draw/wall.cpp
+++ b/draw/wall.cpp
@@ -41,7 +41,7 @@ void wall_mesh::maybe_add_tile(vertex_array& data, texture_array& textures, tile
void wall_mesh::draw(tile_shader& shader, chunk& c)
{
- //_vertex_buffer.setData({nullptr, sizeof(vertex_array)}, Magnum::GL::BufferUsage::DynamicDraw); // orphan the buffer
+ //_texcoord_buffer.setData({nullptr, sizeof(vertex_array)}, Magnum::GL::BufferUsage::DynamicDraw); // orphan the buffer
texture_array textures = {};
{
vertex_array data;
diff --git a/draw/wireframe.hpp b/draw/wireframe.hpp
index 9d736419..1d58ff43 100644
--- a/draw/wireframe.hpp
+++ b/draw/wireframe.hpp
@@ -52,7 +52,7 @@ wireframe_mesh<T>::wireframe_mesh() :
template <wireframe::traits T> void wireframe_mesh<T>::draw(tile_shader& shader, T x)
{
- //_vertex_buffer.setData({nullptr, sizeof(Vector3) * T::num_vertices}, GL::BufferUsage::DynamicDraw); // orphan the buffer
+ //_texcoord_buffer.setData({nullptr, sizeof(Vector3) * T::num_vertices}, GL::BufferUsage::DynamicDraw); // orphan the buffer
set_subdata(x.make_vertex_array());
x.on_draw();
mesh_base::draw(shader);
diff --git a/editor/draw.cpp b/editor/draw.cpp
index ad634815..89613d37 100644
--- a/editor/draw.cpp
+++ b/editor/draw.cpp
@@ -9,6 +9,8 @@ namespace floormat {
void app::draw_cursor()
{
+ GL::Renderer::disable(GL::Renderer::Feature::DepthTest);
+
constexpr float LINE_WIDTH = 2;
if (cursor.tile && !cursor.in_imgui)
diff --git a/editor/update.cpp b/editor/update.cpp
index ed9c2561..36a872c5 100644
--- a/editor/update.cpp
+++ b/editor/update.cpp
@@ -29,7 +29,8 @@ void app::maybe_initialize_chunk_(const chunk_coords& pos, chunk& c)
c[{K, K }].wall_west() = { _wall2, 0 };
c[{K, K+1}].wall_north() = { _wall1, 0 };
c[{K+1, K }].wall_west() = { _wall2, 0 };
- c[{K+1, K+1}].scenery() = { _door, {rotation::N, 0} };
+ //c[{K+1, K+1}].scenery() = { _door, {rotation::N, 0} };
+ //c.mark_modified();
}
void app::maybe_initialize_chunk([[maybe_unused]] const chunk_coords& pos, [[maybe_unused]] chunk& c)
diff --git a/main/draw.cpp b/main/draw.cpp
index 90cf27ce..246d15af 100644
--- a/main/draw.cpp
+++ b/main/draw.cpp
@@ -69,6 +69,8 @@ void main_impl::draw_world() noexcept
auto [minx, maxx, miny, maxy] = get_draw_bounds();
const auto sz = windowSize();
+ GL::Renderer::disable(GL::Renderer::Feature::DepthTest);
+
for (std::int16_t y = miny; y <= maxy; y++)
for (std::int16_t x = minx; x <= maxx; x++)
{
@@ -80,6 +82,8 @@ void main_impl::draw_world() noexcept
_floor_mesh.draw(_shader, _world[c]);
}
+ //GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
+
for (std::int16_t y = miny; y <= maxy; y++)
for (std::int16_t x = minx; x <= maxx; x++)
{
@@ -110,8 +114,8 @@ void main_impl::drawEvent()
float dt = timeline.previousFrameDuration();
if (dt > 0)
{
- const float RC1 = dt_expected.do_sleep ? 1.f : 1.f/10,
- RC2 = dt_expected.do_sleep ? 1.f/10 : 1.f/15;
+ const float RC1 = dt_expected.do_sleep ? 1.f : 1.f/5,
+ RC2 = 1.f/10;
const float alpha1 = dt/(dt + RC1);
const float alpha2 = dt/(dt + RC2);
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);