From 1bc55392cdabfda477e9b8a273d0c7779940d7ee Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Tue, 10 May 2011 02:23:13 +0200 Subject: [PATCH] ASFont: cleanups in text write methods. --- src/asfont.cpp | 25 +++++++++++-------------- src/asfont.h | 6 +++--- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index 9c22239..34d6cfe 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -189,16 +189,12 @@ bool ASFont::utf8Code(unsigned char c) { return font.utf8Code(c); } -void ASFont::write(SDL_Surface* surface, const char* text, int x, int y) { - font.write(surface, text, x, y); -} - void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) { switch (halign) { case HAlignLeft: break; case HAlignCenter: - x -= getTextWidth(text)/2; + x -= getTextWidth(text) / 2; break; case HAlignRight: x -= getTextWidth(text); @@ -218,40 +214,41 @@ void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, font.write(surface, text, x, y); } -void ASFont::write(SDL_Surface* surface, std::vector *text, int x, int y, HAlign halign, VAlign valign) { +void ASFont::write(SDL_Surface* surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign) { switch (valign) { case VAlignTop: break; case VAlignMiddle: - y -= (getHeight() / 2) * text->size(); + y -= (getHeight() / 2) * text.size(); break; case VAlignBottom: - y -= getHeight()*text->size(); + y -= getHeight() * text.size(); break; } - for (unsigned i=0; isize(); i++) { + for (std::vector::const_iterator it = text.begin(); it != text.end(); ++it) { int ix = x; switch (halign) { case HAlignLeft: break; case HAlignCenter: - ix -= getTextWidth(text->at(i))/2; + ix -= getTextWidth(*it) / 2; break; case HAlignRight: - ix -= getTextWidth(text->at(i)); + ix -= getTextWidth(*it); break; } - font.write(surface, text->at(i), x, y+getHeight()*i); + font.write(surface, *it, ix, y); + y += getHeight(); } } void ASFont::write(Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) { if (text.find("\n", 0) != std::string::npos) { std::vector textArr; - split(textArr,text,"\n"); - write(surface->raw, &textArr, x, y, halign, valign); + split(textArr, text, "\n"); + write(surface->raw, textArr, x, y, halign, valign); } else write(surface->raw, text, x, y, halign, valign); } diff --git a/src/asfont.h b/src/asfont.h index 6a9dafe..49b994b 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -48,12 +48,12 @@ public: int getLineHeight(); int getTextWidth(const char* text); int getTextWidth(const std::string& text); - void write(SDL_Surface* surface, const char* text, int x, int y); - void write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); - void write(SDL_Surface* surface, std::vector *text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); void write(Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); private: + void write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign); + void write(SDL_Surface* surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign); + SFontPlus font; };