From 242e4d0ea14a713e38b99e4206bb0f05c05f92df Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 21 Nov 2021 07:36:16 +0100 Subject: Fix bug where window resize event is updated every frame --- src/graphics/font.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/graphics') diff --git a/src/graphics/font.c b/src/graphics/font.c index b6ceaf5..40fe641 100644 --- a/src/graphics/font.c +++ b/src/graphics/font.c @@ -4,6 +4,8 @@ #define STB_TRUETYPE_IMPLEMENTATION #include "../../external/stb_truetype.h" +/* TODO: Show a rectangle for unsupported glyphs or if |self| is NULL */ + /* Need padding so filtering doesn't touch pixels in another glyphs area */ #define GLYPH_PADDING 2 #define GLYPH_UPSAMPLE 1 @@ -125,6 +127,9 @@ static void mgl_font_handle_new_render_position(mgl_font *self, int glyph_width) } int mgl_font_get_glyph(mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph) { + if(!self) + return -1; + if(self->font_atlas.width == 0) { if(self->texture.id == 0 && mgl_texture_init(&self->texture) != 0) return -1; @@ -136,7 +141,9 @@ int mgl_font_get_glyph(mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph .pixel_coordinates = true }; - if(mgl_texture_load_from_memory(&self->texture, NULL, initial_atlas_size, initial_atlas_size, MGL_IMAGE_FORMAT_ALPHA, &load_options) == 0) { + if(mgl_texture_load_from_memory(&self->texture, NULL, initial_atlas_size, initial_atlas_size, MGL_IMAGE_FORMAT_ALPHA, &load_options) != 0) { + return -1; + } else { /*fprintf(stderr, "Error: failed to create font atlas texture, error: mgl_texture_load_from_memory failed\n");*/ self->font_atlas.width = initial_atlas_size; self->font_atlas.height = initial_atlas_size; @@ -230,5 +237,7 @@ int mgl_font_get_glyph(mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph } float mgl_font_get_kerning(const mgl_font *self, uint32_t prev_codepoint, uint32_t codepoint) { + if(!self || self->texture.id == 0) + return 0.0f; return stbtt_GetCodepointKernAdvance(self->font_info, prev_codepoint, codepoint) * stbtt_ScaleForPixelHeight(self->font_info, self->character_size); } -- cgit v1.2.3