Skip to content

Commit

Permalink
Fix early font closing during errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightkingale committed May 13, 2024
1 parent f55be74 commit f1157a9
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions source/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,12 @@ draw_text(const char* text, int x, int y, int size, SDL_Color color)
// UTF8 allows us to render all the special system characters.
SDL_Surface* surface = TTF_RenderUTF8_Blended(font, text, color);
if (surface == NULL) {
TTF_CloseFont(font);
return;
}

SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture == NULL) {
SDL_FreeSurface(surface);
TTF_CloseFont(font);
return;
}

Expand All @@ -120,14 +118,12 @@ draw_icon(const char* icon, int x, int y, int size, SDL_Color color)

SDL_Surface* surface = TTF_RenderUTF8_Blended(font, icon, color);
if (surface == NULL) {
TTF_CloseFont(font);
return;
}

SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture == NULL) {
SDL_FreeSurface(surface);
TTF_CloseFont(font);
return;
}

Expand All @@ -151,7 +147,6 @@ get_text_size(const char* text, int size, bool get_height)
int height = 0;
TTF_SizeUTF8(font, text, &width, &height); // This works with unicode characters.

TTF_CloseFont(font);

if (get_height)
return height; // We don't usually need the height.
Expand All @@ -166,10 +161,11 @@ close_fonts()
for (auto& pair : text_font_cache) {
TTF_CloseFont(pair.second);
}
text_font_cache.clear();

for (auto& pair : icon_font_cache) {
TTF_CloseFont(pair.second);
}
text_font_cache.clear();
icon_font_cache.clear();
}

Expand Down

0 comments on commit f1157a9

Please sign in to comment.