summaryrefslogtreecommitdiffhomepage
path: root/draw/ground.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-01-11 14:29:53 +0100
committerStanislaw Halik <sthalik@misaki.pl>2024-01-11 14:45:29 +0100
commitd74df1b763979d281f76f3f30543aee8d8a638b7 (patch)
tree08939a34160b261c37644595aa8702dd0689d74f /draw/ground.cpp
parent7867213a01fcabb1f05b1836c2ca59dc3bb2132f (diff)
rename floor -> ground
Diffstat (limited to 'draw/ground.cpp')
-rw-r--r--draw/ground.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/draw/ground.cpp b/draw/ground.cpp
new file mode 100644
index 00000000..515729b1
--- /dev/null
+++ b/draw/ground.cpp
@@ -0,0 +1,49 @@
+#include "ground.hpp"
+#include "shaders/shader.hpp"
+#include "src/chunk.hpp"
+#include "src/ground-atlas.hpp"
+#include "compat/assert.hpp"
+#include <Magnum/GL/MeshView.h>
+
+namespace floormat {
+
+ground_mesh::ground_mesh() = default;
+
+void ground_mesh::draw(tile_shader& shader, chunk& c)
+{
+ constexpr int quad_index_count = 6;
+ const auto [mesh_, ids, size] = c.ensure_ground_mesh();
+ struct {
+ ground_atlas* atlas = nullptr; size_t pos = 0; } last;
+ GL::MeshView mesh{mesh_};
+
+ [[maybe_unused]] size_t draw_count = 0;
+ fm_debug_assert(size_t(mesh_.count()) == size*quad_index_count);
+
+ const auto do_draw = [&](size_t i, ground_atlas* atlas, uint32_t max_index) {
+ if (atlas == last.atlas)
+ return;
+ if (auto len = i - last.pos; last.atlas && len > 0)
+ {
+ mesh.setCount((int)(quad_index_count * len));
+ mesh.setIndexOffset((int)(last.pos*quad_index_count), 0, max_index);
+ shader.draw(last.atlas->texture(), mesh);
+ draw_count++;
+ }
+ last = { atlas, i };
+ };
+
+ const auto max_index = uint32_t(size*quad_index_count - 1);
+ size_t k;
+ for (k = 0; k < size; k++)
+ do_draw(k, c.ground_atlas_at(ids[k]), max_index);
+ do_draw(size, nullptr, max_index);
+
+//#define FM_DEBUG_DRAW_COUNT
+#ifdef FM_DEBUG_DRAW_COUNT
+ if (draw_count)
+ fm_debug("ground draws: %zu", draw_count);
+#endif
+}
+
+} // namespace floormat