aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/font.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/graphics/font.c')
-rw-r--r--src/graphics/font.c14
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);