Tuesday, August 5, 2008

SDL_Image - Loading more image formats

The SDL_Image (Advanced version of the basic SDL rendering library) library will allow you to greatly improve games you work on mainly because of the large amount of image formats it will let you load (BMP, PNM (PPM/PGM/PBM), XPM, LBM, PCX, GIF, JPEG, PNG, TGA, and TIFF). Furthermore, SDL_Image will allow you to use transparency layers of images you load instead of requiring a colour key for transparency like SDL does with bitmap images. 
In order to used the SDL_Image library, you will need a few extra packages that aren't shipped with the default SDL bundle. You can get ALL the Devpacks required for DevC++ HERE. Installation has to be done in a specific order (e.g you won't get SDL_Image installed until you have libpng Devpack installed ... etc...). Once the SDL_Image library is installed, it's extremely easy to switch from SDL to SDL_Image code:

- Initialisation : all you need is to include the SDL_Image.h header file in addition to the default SDL headers
#include "SDL/SDL.h" 
#include "SDL/SDL_image.h" 
- Loading an image to a surface : you will now have to replace your SDL_LoadBMP (Loads a surface from a named Windows BMP file) with the IMG_Load. The only parameter you will need to specify for the IMG_Load is the path to the file you wish to load. Finally, the fact of loading an image with or without its transparency layer will depend only on whether you use the SDL_DisplayFormatAlpha (This function takes a surface and copies it to a new surface of the pixel format and colors of the video framebuffer plus an alpha channel, suitable for fast blitting onto the display surface. It calls SDL_ConvertSurface) or SDL_DisplayFormat (This function takes a surface and copies it to a new surface of the pixel format and colors of the video framebuffer ignoring the transparency layer if there is one on the image) function to convert your software surface to a hardware surface.
SDL_Surface *temp = IMG_Load(path);
SDL_Surface *surf = SDL_DisplayFormat(temp); // Load image without alpha channel
SDL_FreeSurface(temp);
SDL_Surface *temp = IMG_Load(path);
SDL_Surface *surf = SDL_DisplayFormatAlpha(temp); // Load image with alpha channel
SDL_FreeSurface(temp);

No comments:

Post a Comment