From 08fffbff6aaab9a3e086d9713b64fd6852b361eb Mon Sep 17 00:00:00 2001 From: Nebuleon Fumika Date: Sun, 13 Jul 2014 21:24:20 +0000 Subject: [PATCH] Improve performance in Font::writeLine. Asking FreeType for metrics before asking it for a render, when rendering would compute the metrics anyway, is wasteful. Now the width of text, for horizontal alignment purposes, is simply the width of the render. --- src/font.cpp | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/font.cpp b/src/font.cpp index 5710af7..885d3ac 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -87,17 +87,6 @@ void Font::writeLine(Surface *surface, const char *text, return; } - switch (halign) { - case HAlignLeft: - break; - case HAlignCenter: - x -= getTextWidth(text) / 2; - break; - case HAlignRight: - x -= getTextWidth(text); - break; - } - switch (valign) { case VAlignTop: break; @@ -111,6 +100,21 @@ void Font::writeLine(Surface *surface, const char *text, SDL_Color color = { 0, 0, 0, 0 }; SDL_Surface *s = TTF_RenderUTF8_Blended(font, text, color); + if (!s) { + ERROR("Font rendering failed for text \"%s\"\n", text); + return; + } + + switch (halign) { + case HAlignLeft: + break; + case HAlignCenter: + x -= s->w / 2; + break; + case HAlignRight: + x -= s->w; + break; + } SDL_Rect rect = { (Sint16) x, (Sint16) (y - 1), 0, 0 }; SDL_BlitSurface(s, NULL, surface->raw, &rect); @@ -137,6 +141,10 @@ void Font::writeLine(Surface *surface, const char *text, color.b = 0xff; s = TTF_RenderUTF8_Blended(font, text, color); + if (!s) { + ERROR("Font rendering failed for text \"%s\"\n", text); + return; + } SDL_BlitSurface(s, NULL, surface->raw, &rect); SDL_FreeSurface(s); }