--- a/src/font.c Thu Apr 18 21:53:53 2024 +0200 +++ b/src/font.c Thu Apr 18 22:53:55 2024 +0200 @@ -43,17 +43,19 @@ } } -AscFont const *asc_font(enum AscFontStyle style, int size) { +void asc_font(enum AscFontStyle style, int size) { for (unsigned int i = 0 ; i < asc_context.fonts_loaded ; i++) { AscFont *font = &asc_context.fonts[i]; if (font->size == size && font->style == style) { - return font; + asc_context.active_font = i; + return; } } if (asc_context.fonts_loaded == ASC_MAX_FONTS) { - asc_dprintf("WARNING: Maximum number of fonts reached, wiping cache!"); - asc_font_cache_clear(); + asc_error("Too many fonts. Cannot load more until cache is repaired."); + asc_context.active_font = ASC_MAX_FONTS; + return; } unsigned int slot = asc_context.fonts_loaded++; @@ -64,27 +66,8 @@ if (font->ptr == NULL) { asc_context.fonts_loaded--; asc_error(TTF_GetError()); - return NULL; - } - asc_dprintf("Loaded font size %u, style %u in slot %u", size, style, slot); - return font; -} - -void asc_font_cache_clear(void) { - asc_dprintf("Fonts in cache that are being unloaded: %u", asc_context.fonts_loaded); - while (asc_context.fonts_loaded > 0) { - unsigned int i = --asc_context.fonts_loaded; - AscFont *font = &asc_context.fonts[i]; - TTF_CloseFont(font->ptr); - font->ptr = NULL; + } else { + asc_dprintf("Loaded font size %u, style %u in slot %u", size, style, slot); + asc_context.active_font = slot; } } - -AscFont const *asc_font_cache_validate(AscFont const *font) { - if (font->ptr) { - return font; - } else { - asc_dprintf("Cache miss for font size %u, style %u", font->size, font->style); - return asc_font(font->style, font->size); - } -}