diff options
-rw-r--r-- | include/mgl/graphics/font.h | 2 | ||||
-rw-r--r-- | include/mgl/graphics/vertex_buffer.h | 4 | ||||
-rw-r--r-- | src/graphics/font.c | 2 | ||||
-rw-r--r-- | src/graphics/vertex_buffer.c | 31 | ||||
-rw-r--r-- | tests/main.c | 5 |
5 files changed, 23 insertions, 21 deletions
diff --git a/include/mgl/graphics/font.h b/include/mgl/graphics/font.h index 6aa9188..c4904ff 100644 --- a/include/mgl/graphics/font.h +++ b/include/mgl/graphics/font.h @@ -32,6 +32,6 @@ struct mgl_font { int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int character_size); void mgl_font_unload(mgl_font *self); -int mgl_font_get_glyph(mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph); +int mgl_font_get_glyph(const mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph); #endif /* MGL_FONT_H */ diff --git a/include/mgl/graphics/vertex_buffer.h b/include/mgl/graphics/vertex_buffer.h index 0dfd0d6..b8722da 100644 --- a/include/mgl/graphics/vertex_buffer.h +++ b/include/mgl/graphics/vertex_buffer.h @@ -21,11 +21,11 @@ typedef struct { mgl_vec2f position; } mgl_vertex_buffer; -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); void mgl_vertex_buffer_deinit(mgl_vertex_buffer *self); void mgl_vertex_buffer_set_position(mgl_vertex_buffer *self, mgl_vec2f position); -int mgl_vertex_buffer_update(mgl_vertex_buffer *self, const mgl_vertex *vertices, size_t 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); /* |texture| can be NULL to not use any texture */ void mgl_vertex_buffer_draw(mgl_context *context, mgl_vertex_buffer *vertex_buffer, const mgl_texture *texture); diff --git a/src/graphics/font.c b/src/graphics/font.c index f55144a..669c2ac 100644 --- a/src/graphics/font.c +++ b/src/graphics/font.c @@ -111,7 +111,7 @@ void mgl_font_unload(mgl_font *self) { self->num_packed_chars = 0; } -int mgl_font_get_glyph(mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph) { +int mgl_font_get_glyph(const mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph) { stbtt_packedchar *packed_chars = self->packed_chars; if(codepoint >= self->num_packed_chars) return -1; 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); diff --git a/tests/main.c b/tests/main.c index bffbf96..53134b9 100644 --- a/tests/main.c +++ b/tests/main.c @@ -86,8 +86,7 @@ int main(int argc, char **argv) { if(mgl_shader_program_finalize(&shader_program) != 0) return 1; - if(mgl_vertex_buffer_init(&vertex_buffer, MGL_PRIMITIVE_QUADS, MGL_USAGE_STATIC) != 0) - return 1; + mgl_vertex_buffer_init(&vertex_buffer); mgl_vertex vertices[4] = { (mgl_vertex){ @@ -112,7 +111,7 @@ int main(int argc, char **argv) { } }; - if(mgl_vertex_buffer_update(&vertex_buffer, vertices, 4) != 0) + if(mgl_vertex_buffer_update(&vertex_buffer, vertices, 4, MGL_PRIMITIVE_QUADS, MGL_USAGE_STATIC) != 0) return 1; fprintf(stderr, "Font texture width: %d, texture height: %d\n", font.texture.width, font.texture.height); |