summaryrefslogtreecommitdiffhomepage
path: root/draw/wall.cpp
blob: c65b19fc1928849dda46f6e22c7b9f19c9cbe28f (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
52
53
54
55
56
57
#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 {

//#define FM_DEBUG_DRAW_COUNT

constexpr auto quad_index_count = 6;

wall_mesh::wall_mesh() = default;

void wall_mesh::draw(tile_shader& shader, chunk& c)
{
    auto [mesh_, ids] = c.ensure_wall_mesh();

    tile_atlas* last_atlas = nullptr;
    std::size_t last_pos = 0;
    GL::MeshView mesh{mesh_};

    [[maybe_unused]] std::size_t draw_count = 0;

    const auto do_draw = [&](std::size_t i, tile_atlas* atlas) {
        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, quad_index_count*TILE_COUNT*2 - 1);
            shader.draw(mesh);
            draw_count++;
        }
        last_atlas = atlas;
        last_pos = i;
    };

    for (std::size_t k = 0; k < TILE_COUNT*2; k++)
    {
        const std::size_t i = ids[k];
        if (auto* atlas = c.wall_atlas_at(i))
            do_draw(k, atlas);
    }
    do_draw(TILE_COUNT*2, nullptr);

#ifdef FM_DEBUG_DRAW_COUNT
    if (draw_count)
        fm_debug("wall draws: %zu", draw_count);
#endif
}

} // namespace floormat