diff --git a/src/asfont.cpp b/src/asfont.cpp index c74015d..401bd12 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -11,35 +11,10 @@ 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; + surface = loadPNG(fontImagePath); + if (!surface) { return; } - - // 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); - } assert(surface->format->BytesPerPixel == 4); SDL_LockSurface(surface); diff --git a/src/imageio.cpp b/src/imageio.cpp index aeebbcb..b9b8921 100644 --- a/src/imageio.cpp +++ b/src/imageio.cpp @@ -2,7 +2,35 @@ #include -SDL_Surface *loadPNG(const std::string &path) -{ - return IMG_Load(path.c_str()); +SDL_Surface *loadPNG(const std::string &path) { + // Load PNG file into an SDL surface. + SDL_Surface *surface = IMG_Load(path.c_str()); + if (!surface) { + return NULL; + } + + // Make sure we have a surface that can be blitted using alpha blending. + if (surface->format->BytesPerPixel == 4) { + SDL_SetAlpha(surface, SDL_SRCALPHA, 255); + return surface; + } 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; + SDL_Surface *surface32 = + SDL_ConvertSurface(surface, &format32, SDL_SRCALPHA); + SDL_FreeSurface(surface); + return surface32; + } } diff --git a/src/imageio.h b/src/imageio.h index 3e4e48f..54d4640 100644 --- a/src/imageio.h +++ b/src/imageio.h @@ -5,6 +5,8 @@ struct SDL_Surface; +/** Loads an image from a PNG file into a newly allocated 32bpp RGBA surface. + */ SDL_Surface *loadPNG(const std::string &path); #endif diff --git a/src/surface.cpp b/src/surface.cpp index 264ea3c..20808b8 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -90,11 +90,8 @@ void Surface::load(const string &img, const string &skin) { skinpath = img; } - SDL_Surface *buf = loadPNG(skinpath); - if (buf!=NULL) { - raw = SDL_DisplayFormatAlpha(buf); - SDL_FreeSurface(buf); - } else { + raw = loadPNG(skinpath); + if (!raw) { ERROR("Couldn't load surface '%s'\n", img.c_str()); } }