From 41e6e4693e77bc3af677c0e34f9de4010e7a0e74 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 12 May 2011 01:20:26 +0200 Subject: [PATCH] ASFont: only convert font surface if it is not already in a 32bpp format. --- src/asfont.cpp | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index 0854d15..9f0f429 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -4,6 +4,7 @@ #include "utilities.h" #include +#include #define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" @@ -40,28 +41,41 @@ Uint32 ASFont::getPixel(Sint32 x, Sint32 y) { return 0; } -ASFont::ASFont(const std::string &fontImagePath) { - this->characters = SFONTPLUS_CHARSET; - +ASFont::ASFont(const std::string &fontImagePath) + : characters(SFONTPLUS_CHARSET) +{ + // Load PNG file into an SDL surface. SDL_Surface *buf = loadPNG(fontImagePath); if (!buf) { surface = NULL; return; } - surface = SDL_DisplayFormatAlpha(buf); - SDL_FreeSurface(buf); + + // Make sure we have a surface that can be blitted using alpha blending. + if (buf->format->BytesPerPixel == 4) { + surface = buf; + SDL_SetAlpha(surface, SDL_SRCALPHA, 255); + } else { + SDL_PixelFormat format32; + memset(&format32, 0, sizeof(format32)); + format32.BitsPerPixel = 32; + format32.BytesPerPixel = 4; + format32.Rmask = + SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x00FF0000 : 0x000000FF; + format32.Gmask = 0x0000FF00; + format32.Bmask = + SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x000000FF : 0x00FF0000; + format32.Amask = 0xFF000000; + format32.Rshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 16 : 0; + format32.Gshift = 8; + format32.Bshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0 : 16; + format32.Ashift = 24; + surface = SDL_ConvertSurface(buf, &format32, SDL_SRCALPHA); + SDL_FreeSurface(buf); + } Uint32 pink = SDL_MapRGB(surface->format, 255,0,255); -#ifdef DEBUG - bool utf8 = false; - for (unsigned x=0; x128; - if (utf8) DEBUG("%d\n", (unsigned char)characters[x]); - } -#endif - unsigned c = 0; - SDL_LockSurface(surface); for (unsigned x=0; x<(unsigned)surface->w && ch-1); - SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colKey); std::string::size_type pos = characters.find("0")*2; SDL_Rect srcrect = {charpos[pos], 1, charpos[pos+2] - charpos[pos], surface->h - 1}; unsigned y = srcrect.h;