aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/VertexBuffer.cpp
blob: 2eb06a889a26d01ef84eb697de120a0bfc415f27 (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
#include "../../include/mglpp/graphics/VertexBuffer.hpp"
#include "../../include/mglpp/graphics/Texture.hpp"

extern "C" {
#include <mgl/mgl.h>
}

namespace mgl {
    VertexBuffer::VertexBuffer(PrimitiveType primitive_type, Usage usage) : texture(nullptr), primitive_type(primitive_type), usage(usage) {
        vertex_buffer.id = 0;
    }

    VertexBuffer::~VertexBuffer() {
        mgl_vertex_buffer_deinit(&vertex_buffer);
    }

    void VertexBuffer::set_position(vec2f position) {
        mgl_vertex_buffer_set_position(&vertex_buffer, {position.x, position.y});
    }

    void VertexBuffer::set_color(Color color) {
        // No-op, because the vertex buffer has colors for each vertex
        (void)color;
    }

    vec2f VertexBuffer::get_position() const {
        return { vertex_buffer.position.x, vertex_buffer.position.y };
    }

    void VertexBuffer::set_texture(Texture *texture) {
        this->texture = texture;
    }

    const Texture* VertexBuffer::get_texture() const {
        return texture;
    }

    bool VertexBuffer::update(const Vertex *vertices, size_t vertex_count) {
        if(vertex_buffer.id == 0) {
            int res = mgl_vertex_buffer_init(&vertex_buffer, (mgl_primitive_type)primitive_type, (mgl_vertex_buffer_usage)usage);
            if(res != 0)
                return false;
        }
        return mgl_vertex_buffer_update(&vertex_buffer, (const mgl_vertex*)vertices, vertex_count) == 0;
    }

    void VertexBuffer::draw(Window&) {
        mgl_vertex_buffer_draw(mgl_get_context(), &vertex_buffer, texture ? texture->internal_texture() : nullptr);
    }
}