From 5c7ca19f4bbdd32d7c60b725510649805f74f10f Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Fri, 18 Jul 2014 04:26:48 +0200 Subject: [PATCH] Don't issue warnings when rendering empty string --- src/font.cpp | 18 +++++++++++------- src/font.h | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/font.cpp b/src/font.cpp index 885d3ac..95c06b2 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -67,7 +67,7 @@ void Font::write(Surface *surface, const string &text, } if (text.find("\n", 0) == string::npos) { - writeLine(surface, text.c_str(), x, y, halign, valign); + writeLine(surface, text, x, y, halign, valign); return; } @@ -75,17 +75,21 @@ void Font::write(Surface *surface, const string &text, split(v, text, "\n"); for (vector::const_iterator it = v.begin(); it != v.end(); it++) { - writeLine(surface, it->c_str(), x, y, halign, valign); + writeLine(surface, *it, x, y, halign, valign); y += lineSpacing; } } -void Font::writeLine(Surface *surface, const char *text, +void Font::writeLine(Surface *surface, std::string const& text, int x, int y, HAlign halign, VAlign valign) { if (!font) { return; } + if (text.empty()) { + // SDL_ttf will return a nullptr when rendering the empty string. + return; + } switch (valign) { case VAlignTop: @@ -99,9 +103,9 @@ void Font::writeLine(Surface *surface, const char *text, } SDL_Color color = { 0, 0, 0, 0 }; - SDL_Surface *s = TTF_RenderUTF8_Blended(font, text, color); + SDL_Surface *s = TTF_RenderUTF8_Blended(font, text.c_str(), color); if (!s) { - ERROR("Font rendering failed for text \"%s\"\n", text); + ERROR("Font rendering failed for text \"%s\"\n", text.c_str()); return; } @@ -140,9 +144,9 @@ void Font::writeLine(Surface *surface, const char *text, color.g = 0xff; color.b = 0xff; - s = TTF_RenderUTF8_Blended(font, text, color); + s = TTF_RenderUTF8_Blended(font, text.c_str(), color); if (!s) { - ERROR("Font rendering failed for text \"%s\"\n", text); + ERROR("Font rendering failed for text \"%s\"\n", text.c_str()); return; } SDL_BlitSurface(s, NULL, surface->raw, &rect); diff --git a/src/font.h b/src/font.h index dd59941..68dde24 100644 --- a/src/font.h +++ b/src/font.h @@ -41,7 +41,7 @@ public: private: Font(TTF_Font *font); - void writeLine(Surface *surface, const char *text, + void writeLine(Surface *surface, std::string const& text, int x, int y, HAlign halign, VAlign valign); TTF_Font *font;