From c54dec90f5c5dc9decf6aab64ab2202c51cfa92f Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Mon, 9 May 2011 04:13:11 +0200 Subject: [PATCH] Minor cleanups in SFontPlus and ASFont. Removed unused includes. Avoid importing classes into default namespace in headers. Don't use a type alias if it does not add value. --- src/asfont.cpp | 22 +++++++++------------- src/asfont.h | 20 +++++++++----------- src/sfontplus.cpp | 46 +++++++++++++++++++++------------------------- src/sfontplus.h | 25 ++++++++++--------------- 4 files changed, 49 insertions(+), 64 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index 40b3169..0518a1d 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -2,10 +2,6 @@ #include "surface.h" #include "utilities.h" -#include - -using namespace std; - ASFont::ASFont(SDL_Surface* font) { this->font.initFont(font); halfHeight = getHeight()/2; @@ -18,7 +14,7 @@ ASFont::ASFont(Surface* font) { halfLineHeight = getLineHeight()/2; } -ASFont::ASFont(const string &font) { +ASFont::ASFont(const std::string &font) { this->font.initFont(font); halfHeight = getHeight()/2; halfLineHeight = getLineHeight()/2; @@ -57,7 +53,7 @@ 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, vector *text, int x, int y, const unsigned short halign, const unsigned short valign) { +void ASFont::write(SDL_Surface* surface, std::vector *text, int x, int y, const unsigned short halign, const unsigned short valign) { switch (valign) { case SFontVAlignMiddle: y -= getHalfHeight()*text->size(); @@ -67,7 +63,7 @@ void ASFont::write(SDL_Surface* surface, vector *text, int x, int y, con break; } - for (uint i=0; isize(); i++) { + for (unsigned i=0; isize(); i++) { int ix = x; switch (halign) { case SFontHAlignCenter: @@ -83,8 +79,8 @@ void ASFont::write(SDL_Surface* surface, vector *text, int x, int y, con } void ASFont::write(Surface* surface, const std::string& text, int x, int y, const unsigned short halign, const unsigned short valign) { - if (text.find("\n",0)!=string::npos) { - vector textArr; + if (text.find("\n", 0) != std::string::npos) { + std::vector textArr; split(textArr,text,"\n"); write(surface->raw, &textArr, x, y, halign, valign); } else @@ -109,16 +105,16 @@ int ASFont::getTextWidth(const char* text) { return font.getTextWidth(text); } int ASFont::getTextWidth(const std::string& text) { - if (text.find("\n",0)!=string::npos) { - vector textArr; + if (text.find("\n", 0) != std::string::npos) { + std::vector textArr; split(textArr,text,"\n"); return getTextWidth(&textArr); } else return getTextWidth(text.c_str()); } -int ASFont::getTextWidth(vector *text) { +int ASFont::getTextWidth(std::vector *text) { int w = 0; - for (uint i=0; isize(); i++) + for (unsigned i=0; isize(); i++) w = max( getTextWidth(text->at(i).c_str()), w ); return w; } diff --git a/src/asfont.h b/src/asfont.h index 2ac318f..f70f0c1 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -3,13 +3,13 @@ #ifndef ASFONT_H #define ASFONT_H -#include -#include -#include #include "sfontplus.h" -using std::string; -using std::vector; +#include +#include + +struct SDL_Surface; +class Surface; const unsigned short SFontHAlignLeft = 0; const unsigned short SFontHAlignRight = 1; @@ -18,13 +18,11 @@ const unsigned short SFontVAlignTop = 0; const unsigned short SFontVAlignBottom = 1; const unsigned short SFontVAlignMiddle = 2; -class Surface; - class ASFont { public: ASFont(SDL_Surface* font); ASFont(Surface* font); - ASFont(const string &font); + ASFont(const std::string &font); ~ASFont(); bool utf8Code(unsigned char c); @@ -34,11 +32,11 @@ public: int getLineHeight(); int getHalfLineHeight(); int getTextWidth(const char* text); - int getTextWidth(const string& text); - int getTextWidth(vector *text); + int getTextWidth(const std::string& text); + int getTextWidth(std::vector *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, const unsigned short halign = 0, const unsigned short valign = 0); - void write(SDL_Surface* surface, vector *text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0); + void write(SDL_Surface* surface, std::vector *text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0); void write(Surface* surface, const std::string& text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0); private: diff --git a/src/sfontplus.cpp b/src/sfontplus.cpp index 8ac44bb..4059e6f 100644 --- a/src/sfontplus.cpp +++ b/src/sfontplus.cpp @@ -1,11 +1,7 @@ #include "sfontplus.h" - #include "imageio.h" #include -#include - -using namespace std; Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) { assert(x>=0); @@ -49,7 +45,7 @@ SFontPlus::SFontPlus(SDL_Surface* font) { initFont(font); } -SFontPlus::SFontPlus(const string &font) { +SFontPlus::SFontPlus(const std::string &font) { surface = NULL; initFont(font); } @@ -63,7 +59,7 @@ bool SFontPlus::utf8Code(unsigned char c) { //return c>=194; } -void SFontPlus::initFont(const string &font, const string &characters) { +void SFontPlus::initFont(const std::string &font, const std::string &characters) { SDL_Surface *buf = loadPNG(font); if (buf!=NULL) { initFont( SDL_DisplayFormatAlpha(buf), characters ); @@ -71,7 +67,7 @@ void SFontPlus::initFont(const string &font, const string &characters) { } } -void SFontPlus::initFont(SDL_Surface *font, const string &characters) { +void SFontPlus::initFont(SDL_Surface *font, const std::string &characters) { freeFont(); this->characters = characters; if (font==NULL) return; @@ -79,22 +75,22 @@ void SFontPlus::initFont(SDL_Surface *font, const string &characters) { Uint32 pink = SDL_MapRGB(surface->format, 255,0,255); #ifdef DEBUG bool utf8 = false; - for (uint x=0; x128; if (utf8) DEBUG("%d\n", (unsigned char)characters[x]); } #endif - uint c = 0; + unsigned c = 0; SDL_LockSurface(surface); - for (uint x=0; x<(uint)surface->w && cw && cw && getPixel(x,0) == pink) x++; + while (x<(unsigned)surface->w && getPixel(x,0) == pink) x++; charpos.push_back(x); //utf8 characters @@ -110,12 +106,12 @@ void SFontPlus::initFont(SDL_Surface *font, const string &characters) { SDL_UnlockSurface(surface); Uint32 colKey = getPixel(0,surface->h-1); SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colKey); - string::size_type pos = characters.find("0")*2; + std::string::size_type pos = characters.find("0")*2; SDL_Rect srcrect = {charpos[pos], 1, charpos[pos+2] - charpos[pos], surface->h - 1}; - uint y = srcrect.h; + unsigned y = srcrect.h; bool nonKeyFound = false; while (y-- > 0 && !nonKeyFound) { - uint x = srcrect.w; + unsigned x = srcrect.w; while (x-- > 0 && !nonKeyFound) nonKeyFound = getPixel(x+srcrect.x,y+srcrect.y) != colKey; } @@ -129,10 +125,10 @@ void SFontPlus::freeFont() { } } -void SFontPlus::write(SDL_Surface *s, const string &text, int x, int y) { +void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) { if (text.empty()) return; - string::size_type pos; + std::string::size_type pos; SDL_Rect srcrect, dstrect; // these values won't change in the loop @@ -140,14 +136,14 @@ void SFontPlus::write(SDL_Surface *s, const string &text, int x, int y) { dstrect.y = y; srcrect.h = dstrect.h = surface->h-1; - for(uint i=0; iw; i++) { + for(unsigned i=0; iw; i++) { //Utf8 characters if (utf8Code(text[i]) && i+1h - 1; } -uint SFontPlus::getLineHeight() { +unsigned SFontPlus::getLineHeight() { return lineHeight; } diff --git a/src/sfontplus.h b/src/sfontplus.h index 9950dd6..7f6b0db 100644 --- a/src/sfontplus.h +++ b/src/sfontplus.h @@ -6,38 +6,33 @@ #include #define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" -#ifdef _WIN32 - typedef unsigned int uint; -#endif -using std::vector; -using std::string; class SFontPlus { private: Uint32 getPixel(Sint32 x, Sint32 y); SDL_Surface *surface; - vector charpos; - string characters; - uint height, lineHeight; + std::vector charpos; + std::string characters; + unsigned height, lineHeight; public: SFontPlus(); SFontPlus(SDL_Surface *font); - SFontPlus(const string &font); + SFontPlus(const std::string &font); ~SFontPlus(); bool utf8Code(unsigned char c); - void initFont(SDL_Surface *font, const string &characters = SFONTPLUS_CHARSET); - void initFont(const string &font, const string &characters = SFONTPLUS_CHARSET); + void initFont(SDL_Surface *font, const std::string &characters = SFONTPLUS_CHARSET); + void initFont(const std::string &font, const std::string &characters = SFONTPLUS_CHARSET); void freeFont(); - void write(SDL_Surface *s, const string &text, int x, int y); + void write(SDL_Surface *s, const std::string &text, int x, int y); - uint getTextWidth(const string &text); - uint getHeight(); - uint getLineHeight(); + unsigned getTextWidth(const std::string &text); + unsigned getHeight(); + unsigned getLineHeight(); }; #endif /* SFONTPLUS_H */