diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-11-18 12:17:14 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-11-18 12:17:14 +0100 |
commit | f4833afa1afa56def5a6d54fa8ce22d3a9b4eb55 (patch) | |
tree | 04028456c206ec3ad999320dee2a2dbf3c4ab3e4 /src/graphics/text.c | |
parent | ff6d302d7b0ef4c32dca6e2d0ce12927d517875a (diff) |
Move glyph creation for text to render function instead of in set string, clear font glyph background before rendering the text
Diffstat (limited to 'src/graphics/text.c')
-rw-r--r-- | src/graphics/text.c | 25 |
1 files changed, 15 insertions, 10 deletions
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; |