From 1e3df56a5c91bce2ef7b03e31156721e2f76a063 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 27 Oct 2021 18:31:23 +0200 Subject: Move vertex buffer generation from init to first update, making init void --- src/graphics/vertex_buffer.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'src/graphics/vertex_buffer.c') diff --git a/src/graphics/vertex_buffer.c b/src/graphics/vertex_buffer.c index 5a8e768..1f122c3 100644 --- a/src/graphics/vertex_buffer.c +++ b/src/graphics/vertex_buffer.c @@ -17,19 +17,12 @@ static unsigned int mgl_vertex_buffer_usage_to_gl_usage(mgl_vertex_buffer_usage return 0; } -int mgl_vertex_buffer_init(mgl_vertex_buffer *self, mgl_primitive_type primitive_type, mgl_vertex_buffer_usage usage) { +void mgl_vertex_buffer_init(mgl_vertex_buffer *self) { self->id = 0; self->vertex_count = 0; - self->primitive_type = primitive_type; - self->usage = usage; + self->primitive_type = -1; + self->usage = -1; self->position = (mgl_vec2f){ 0.0f, 0.0f }; - - mgl_context *context = mgl_get_context(); - context->gl.glGenBuffers(1, &self->id); - if(self->id == 0) - return -1; - - return 0; } void mgl_vertex_buffer_deinit(mgl_vertex_buffer *self) { @@ -50,8 +43,18 @@ static void mgl_vertex_buffer_set_gl_buffer_pointers(mgl_context *context) { } /* TODO: Check for glBufferData error */ -static int mgl_vertex_buffer_resize(mgl_vertex_buffer *self, const mgl_vertex *vertices, size_t vertex_count) { +static int mgl_vertex_buffer_resize(mgl_vertex_buffer *self, const mgl_vertex *vertices, size_t vertex_count, mgl_primitive_type primitive_type, mgl_vertex_buffer_usage usage) { mgl_context *context = mgl_get_context(); + + if(self->id == 0) { + context->gl.glGenBuffers(1, &self->id); + if(self->id == 0) + return -1; + } + + self->primitive_type = primitive_type; + self->usage = usage; + context->gl.glBindBuffer(GL_ARRAY_BUFFER, self->id); context->gl.glBufferData(GL_ARRAY_BUFFER, sizeof(mgl_vertex) * vertex_count, vertices, mgl_vertex_buffer_usage_to_gl_usage(self->usage)); mgl_vertex_buffer_set_gl_buffer_pointers(context); @@ -65,9 +68,9 @@ void mgl_vertex_buffer_set_position(mgl_vertex_buffer *self, mgl_vec2f position) } /* TODO: Check for glBufferSubData error */ -int mgl_vertex_buffer_update(mgl_vertex_buffer *self, const mgl_vertex *vertices, size_t vertex_count) { - if(vertex_count != self->vertex_count) - return mgl_vertex_buffer_resize(self, vertices, vertex_count); +int mgl_vertex_buffer_update(mgl_vertex_buffer *self, const mgl_vertex *vertices, size_t vertex_count, mgl_primitive_type primitive_type, mgl_vertex_buffer_usage usage) { + if(vertex_count != self->vertex_count || primitive_type != self->primitive_type || usage != self->usage) + return mgl_vertex_buffer_resize(self, vertices, vertex_count, primitive_type, usage); mgl_context *context = mgl_get_context(); context->gl.glBindBuffer(GL_ARRAY_BUFFER, self->id); -- cgit v1.2.3