#include "sfontplus.h" #include #include #include using namespace std; Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) { assert(x>=0); assert(xw); Uint32 Bpp = surface->format->BytesPerPixel; // Get the pixel switch(Bpp) { case 1: return *((Uint8 *)surface->pixels + y * surface->pitch + x); break; case 2: return *((Uint16 *)surface->pixels + y * surface->pitch/2 + x); break; case 3: { // Format/endian independent Uint8 *bits = ((Uint8 *)surface->pixels)+y*surface->pitch+x*Bpp; Uint8 r, g, b; r = *((bits)+surface->format->Rshift/8); g = *((bits)+surface->format->Gshift/8); b = *((bits)+surface->format->Bshift/8); return SDL_MapRGB(surface->format, r, g, b); } break; case 4: return *((Uint32 *)surface->pixels + y * surface->pitch/4 + x); break; } return 0; } SFontPlus::SFontPlus() { surface = NULL; } SFontPlus::SFontPlus(SDL_Surface* font) { surface = NULL; initFont(font); } SFontPlus::SFontPlus(const string &font) { surface = NULL; initFont(font); } SFontPlus::~SFontPlus() { freeFont(); } bool SFontPlus::utf8Code(unsigned char c) { return (c>=194 && c<=198) || c==208 || c==209; //return c>=194; } void SFontPlus::initFont(const string &font, const string &characters) { SDL_Surface *buf = IMG_Load(font.c_str()); if (buf!=NULL) { initFont( SDL_DisplayFormatAlpha(buf), characters ); SDL_FreeSurface(buf); } } void SFontPlus::initFont(SDL_Surface *font, const string &characters) { freeFont(); this->characters = characters; if (font==NULL) return; surface = font; Uint32 pink = SDL_MapRGB(surface->format, 255,0,255); #ifdef _DEBUG bool utf8 = false; for (uint x=0; x128; if (utf8) printf("%d\n", (unsigned char)characters[x]); } #endif uint c = 0; SDL_LockSurface(surface); for (uint x=0; x<(uint)surface->w && cw && getPixel(x,0) == pink) x++; charpos.push_back(x); //utf8 characters if (c>0 && utf8Code(characters[c-1])) { charpos.push_back(startx); charpos.push_back(x); c++; } c++; } } SDL_UnlockSurface(surface); Uint32 colKey = getPixel(0,surface->h-1); SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colKey); 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+1; bool nonKeyFound = false; while (y-->0 && !nonKeyFound) { uint x = srcrect.w+1; while (x-->0 && !nonKeyFound) nonKeyFound = getPixel(x+srcrect.x,y+srcrect.y) != colKey; } lineHeight = y+1; } void SFontPlus::freeFont() { if (surface!=NULL) { SDL_FreeSurface(surface); surface = NULL; } } void SFontPlus::write(SDL_Surface *s, const string &text, int x, int y) { if (text.empty()) return; string::size_type pos; SDL_Rect srcrect, dstrect; // these values won't change in the loop srcrect.y = 1; dstrect.y = y; srcrect.h = dstrect.h = surface->h-1; for(uint i=0; iw; i++) { //Utf8 characters if (utf8Code(text[i]) && i+1h - 1; } uint SFontPlus::getLineHeight() { return lineHeight; }