2011-03-30 04:21:31 +03:00
|
|
|
#include "imageio.h"
|
|
|
|
|
|
|
|
#include <SDL_image.h>
|
|
|
|
|
2011-06-02 07:19:21 +03:00
|
|
|
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 {
|
2011-06-03 13:47:49 +03:00
|
|
|
bool hasAlphaChannel = surface->format->Amask != 0;
|
2011-06-02 07:19:21 +03:00
|
|
|
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;
|
2011-06-03 13:47:49 +03:00
|
|
|
format32.Amask = hasAlphaChannel ? 0xFF000000 : 0;
|
2011-06-02 07:19:21 +03:00
|
|
|
format32.Rshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 16 : 0;
|
|
|
|
format32.Gshift = 8;
|
|
|
|
format32.Bshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0 : 16;
|
2011-06-03 13:47:49 +03:00
|
|
|
format32.Ashift = hasAlphaChannel ? 24 : 0;
|
|
|
|
SDL_Surface *surface32 = SDL_ConvertSurface(
|
|
|
|
surface, &format32, hasAlphaChannel ? SDL_SRCALPHA : 0);
|
2011-06-02 07:19:21 +03:00
|
|
|
SDL_FreeSurface(surface);
|
|
|
|
return surface32;
|
|
|
|
}
|
2011-03-30 04:21:31 +03:00
|
|
|
}
|