aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/VertexBuffer.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-10-22 07:05:55 +0200
committerdec05eba <dec05eba@protonmail.com>2021-10-22 07:05:55 +0200
commitc9ee5e1c1feccb073863ba17cbfdcf094f235886 (patch)
treef6b9f3fdc21cecbaca1fd58425c0f93c3372a9d4 /src/graphics/VertexBuffer.cpp
parentfe1588ef18163c7557d3d0a62c085f42f2abfab2 (diff)
Add vertex buffer
Diffstat (limited to 'src/graphics/VertexBuffer.cpp')
-rw-r--r--src/graphics/VertexBuffer.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/graphics/VertexBuffer.cpp b/src/graphics/VertexBuffer.cpp
new file mode 100644
index 0000000..2eb06a8
--- /dev/null
+++ b/src/graphics/VertexBuffer.cpp
@@ -0,0 +1,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);
+ }
+} \ No newline at end of file