diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-11-05 14:44:21 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-11-05 14:44:21 +0100 |
commit | ccb3e58071b3e807109918184727b305df8b96a0 (patch) | |
tree | d8830f9a3ec84ea17fea064b3ff27b2112c4ca74 /src/graphics/font.c | |
parent | 7dd8b0cc0561c5fa0c9096b79fb7f0647b3470b4 (diff) |
Fix font being corrupt with certain sizes, fix image being corrupt if its rgb, fix window resize incorrect size
Diffstat (limited to 'src/graphics/font.c')
-rw-r--r-- | src/graphics/font.c | 14 |
1 files changed, 12 insertions, 2 deletions
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); |