summaryrefslogtreecommitdiffhomepage
path: root/draw/wall.cpp
blob: d1370d45b2b5ef9d536a330c0927fd5339f3c9ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "wall.hpp"
#include "tile-atlas.hpp"
#include "shaders/tile.hpp"
#include "chunk.hpp"
#include "tile-image.hpp"
#include "anim-atlas.hpp"
#include <Magnum/GL/Texture.h>
#include <Magnum/GL/MeshView.h>

namespace floormat {

constexpr auto quad_index_count = 6;

wall_mesh::wall_mesh() = default;

void wall_mesh::draw(tile_shader& shader, chunk& c)
{
    const auto [mesh_, ids, size] = c.ensure_wall_mesh();
    struct { tile_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, tile_atlas* atlas, uint32_t max_index) {
        if (atlas == last.atlas)
            return;
        if (auto len = i - last.pos; last.atlas && len > 0)
        {
            last.atlas->texture().bind(0);
            mesh.setCount((int)(quad_index_count * len));
            mesh.setIndexRange((int)(last.pos*quad_index_count), 0, max_index);
            shader.draw(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.wall_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("wall draws: %zu", draw_count);
#endif
}

} // namespace floormat