From ccb3e58071b3e807109918184727b305df8b96a0 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 5 Nov 2021 14:44:21 +0100 Subject: Fix font being corrupt with certain sizes, fix image being corrupt if its rgb, fix window resize incorrect size --- src/graphics/font.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/graphics/font.c') diff --git a/src/graphics/font.c b/src/graphics/font.c index 2ed54d2..c017755 100644 --- a/src/graphics/font.c +++ b/src/graphics/font.c @@ -10,6 +10,11 @@ /* TODO: Test and fix .tcc files */ +static unsigned int to_div2_ceil(unsigned int value) { + const uint32_t v = value; + return v + (v & 1); +} + int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int character_size) { self->texture.id = 0; @@ -47,8 +52,13 @@ int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int c /* TODO: Optimize */ /* Find optimal size for atlas, starting from small to large */ for(int i = 0; i < 4; ++i) { - self->font_atlas.width = (14 + (8 * i)) * self->character_size; - self->font_atlas.height = (14 + (8 * i)) * self->character_size; + /* + This to_div2_ceil is needed because otherwise for character sizes such as 33 which are not divisable by 2 + causes the font texture to be skewed. I dont know why this happens. Maybe a bug in stbtt? + TODO: Figure out why it happens. + */ + self->font_atlas.width = (14 + (8 * i)) * to_div2_ceil(self->character_size); + self->font_atlas.height = (14 + (8 * i)) * to_div2_ceil(self->character_size); unsigned char *new_atlas = realloc(self->font_atlas.atlas, self->font_atlas.width * self->font_atlas.height); if(!new_atlas) { fprintf(stderr, "Error: failed to load font %s, error: out of memory\n", filepath); -- cgit v1.2.3