Root/src/imageio.cpp

1#include "imageio.h"
2
3#include <SDL_image.h>
4
5SDL_Surface *loadPNG(const std::string &path) {
6    // Load PNG file into an SDL surface.
7    SDL_Surface *surface = IMG_Load(path.c_str());
8    if (!surface) {
9        return NULL;
10    }
11
12    // Make sure we have a surface that can be blitted using alpha blending.
13    if (surface->format->BytesPerPixel == 4) {
14        SDL_SetAlpha(surface, SDL_SRCALPHA, 255);
15        return surface;
16    } else {
17        SDL_PixelFormat format32;
18        memset(&format32, 0, sizeof(format32));
19        format32.BitsPerPixel = 32;
20        format32.BytesPerPixel = 4;
21        format32.Rmask =
22            SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x00FF0000 : 0x000000FF;
23        format32.Gmask = 0x0000FF00;
24        format32.Bmask =
25            SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x000000FF : 0x00FF0000;
26        format32.Amask = 0xFF000000;
27        format32.Rshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 16 : 0;
28        format32.Gshift = 8;
29        format32.Bshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0 : 16;
30        format32.Ashift = 24;
31        SDL_Surface *surface32 =
32            SDL_ConvertSurface(surface, &format32, SDL_SRCALPHA);
33        SDL_FreeSurface(surface);
34        return surface32;
35    }
36}
37

Archive Download this file



interactive