From f4833afa1afa56def5a6d54fa8ce22d3a9b4eb55 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 18 Nov 2021 12:17:14 +0100 Subject: Move glyph creation for text to render function instead of in set string, clear font glyph background before rendering the text --- src/graphics/text.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'src/graphics/text.c') diff --git a/src/graphics/text.c b/src/graphics/text.c index 3620659..66ef7e3 100644 --- a/src/graphics/text.c +++ b/src/graphics/text.c @@ -90,6 +90,7 @@ void mgl_text_init(mgl_text *self, mgl_font *font, const char *str, size_t str_s self->position = (mgl_vec2f){ 0.0f, 0.0f }; self->text = NULL; self->text_size = 0; + self->bounds_dirty = true; mgl_text_set_string(self, str, str_size); } @@ -105,19 +106,13 @@ void mgl_text_set_string(mgl_text *self, const char *str, size_t str_size) { self->text = str; self->text_size = str_size; if(self->text && self->text_size > 0 && self->font) - self->bounds = mgl_text_calculate_bounds(self); - else - self->bounds = (mgl_vec2f){ 0.0f, 0.0f }; + self->bounds_dirty = true; } void mgl_text_set_font(mgl_text *self, mgl_font *font) { self->font = font; - if(self->font) { - if(self->bounds.x < 0.001f && self->bounds.y < 0.001f && self->text && self->text_size > 0) - self->bounds = mgl_text_calculate_bounds(self); - } else { - self->bounds = (mgl_vec2f){ 0.0f, 0.0f }; - } + if(self->text && self->text_size > 0 && self->font) + self->bounds_dirty = true; } void mgl_text_set_position(mgl_text *self, mgl_vec2f position) { @@ -128,7 +123,14 @@ void mgl_text_set_color(mgl_text *self, mgl_color color) { self->color = color; } -mgl_vec2f mgl_text_get_bounds(const mgl_text *self) { +mgl_vec2f mgl_text_get_bounds(mgl_text *self) { + if(self->bounds_dirty) { + self->bounds_dirty = false; + if(self->text && self->text_size > 0 && self->font) + self->bounds = mgl_text_calculate_bounds(self); + else + self->bounds = (mgl_vec2f){ 0.0f, 0.0f }; + } return self->bounds; } @@ -210,6 +212,9 @@ void mgl_text_draw(mgl_context *context, mgl_text *text) { if(!text->text || text->text_size == 0 || !text->font) return; + /* Calculate bounds beforehand if needed, which also generates glyphs because opengl outputs an error if that is done inside glBegin/glEnd */ + mgl_text_get_bounds(text); + TextDrawUserdata text_draw_userdata; text_draw_userdata.position = (mgl_vec2f){ text->position.x, text->position.y + text->font->character_size }; text_draw_userdata.text = text; -- cgit v1.2.3