SDL Image
SDL Tutorials March 14th, 2008This side tutorial is rather simple, short, and sweet. I am going to show you how to stop using those pesky bitmap (BMP) files that are too big and don’t support alpha transparency, and to start using other file formats for you surfaces (I personally like PNG). If you have not read my SDL Coordinates and Bliting tutorial, I encourage you to do so now. We will be building off of that tutorial, modifying the OnLoad function of the CSurface class.
The first thing you need to do is download SDL_image, the latest version, from the main SDL website. You can also download this library from the “Libraries” section of this website, under SDL. If you don’t want to worry about finding all these libraries, the SDL package I provide is good for most SDL beginners, as it provides SDL_image, and some other useful libraries. Be sure to put the include files in the same directory as your SDL include files, and your lib files in the same directory as your SDL lib files to make things easier.
Start by opening your project, and going under the linker settings. Add SDL_image after SDLmain, something like:
mingw32
SDLmain
SDL_image
SDL
If you don’t remember where to find all these settings, hope on over to the first tutorial SDL Tutorial Basics to get a refresher on linking.
First, open up CSurface.h, so that we can include an extra header file:
#define _CSURFACE_H_
#include <SDL.h>
#include <SDL_image.h>
Once that is all done, open up CSurface.cpp and look for the OnLoad function. Currently, this is what our code looks like:
SDL_Surface* Surf_Temp = NULL;
SDL_Surface* Surf_Return = NULL;
if((Surf_Temp = SDL_LoadBMP(File)) == NULL) {
return NULL;
}
Surf_Return = SDL_DisplayFormat(Surf_Temp);
SDL_FreeSurface(Surf_Temp);
return Surf_Return;
}
There are two changes to make here, the first is changing SDL_LoadBMP to IMG_Load. The second is changing SDL_DisplayFormat to SDL_DisplayFormatAlpha. Remember I said BMPs don’t support alpha layers? Well, PNGs do! And if you are wanting to keep that alpha layer you’ll need to use SDL_DisplayFormatAlpha instead of SDL_DisplayFormat. So, our code should look like this now:
SDL_Surface* Surf_Temp = NULL;
SDL_Surface* Surf_Return = NULL;
if((Surf_Temp = IMG_Load(File)) == NULL) {
return NULL;
}
Surf_Return = SDL_DisplayFormatAlpha(Surf_Temp);
SDL_FreeSurface(Surf_Temp);
return Surf_Return;
}
Finally, don’t forget to change the filename that the Test Surface is trying to load in CApp_OnInit.cpp:
return false;
}
And we are done! I told you it would be short. Try compiling, and trying out different file formats. Some common formats supported by SDL_image are:
BMP (Go figure)
GIF
PNG
JPG
PCX
TIF
and others…
Please note that there are additional DLL files associated with SDL image that are needed now (about 5 of them). So be sure to include those with your project. You can download these DLL files here, http://www.libsdl.org/projects/SDL_image/ under Binary -> Win32 -> and the Win32 zip (not the VC8). Also, the DLL files are also included in the project files below.
This tutorial inspired by Blommis.
SDL Image - Tutorial Files:
Win32: Zip, Rar
Linux: Tar (Thanks Gaten), Binary (Thanks Thomas)














March 14th, 2008 at 11:03 pm
Short and sweet and to the point, very nice. Although might I suggest a screen shot of what it should look like, just as an athesticly pleasing element?
Linux archive here: http://gaten.homelinux.net/sdltuts/sdl_image.tar.gz
Also, do you know about Vector graphics support for SDL? I can’t seem to find any thing about it.
March 15th, 2008 at 6:22 am
There is one specific one made for SDL that I have found. I cannot vouch for any specific one though, as I have not needed to use vector graphics before in SDL:
http://www.linuxmotors.com/SDL_svg/
There are also a few multi-purpose ones out there, that I am sure you can convert the output to an SDL Surface:
http://librsvg.sourceforge.net/
http://www.antigrain.com/
I hope that helps!
Screenshots might be a good idea for all the tutorials, I’ll keep that in the back of my mind. I am trying to finish the next major tutorial though first.
March 15th, 2008 at 7:27 am
2 thumbs up for the brilliant set of tutorials, without a doubt the best set of tutorials on SDL I’ve seen for an SDL beginner.
Also, Could you teach how to install the image library on windows from the package on the project site.
March 16th, 2008 at 8:20 pm
Thanks Gaurav!
Regarding installing the image library, download SDL_image-devel-1.2.6-VC8.zip off of the SDL project site: http://www.libsdl.org/projects/SDL_image/. Inside, you will find the include folder and lib folder. Copy these both to where you have your SDL include folder and lib folder. It will say the directory already exists, just press okay, and it will merge the two folders together. It’s exactly the same process as stated on the first tutorial, I encourage to read that for some additional help.
March 26th, 2008 at 4:49 pm
Hi Tim,
Just to clarify. I’m using Codeblocks 8.02 (mingw) on Windows. So I can use the SDL_image-devel-1.2.6-VC8 libraries? Is this also the case for sdl_mixer, sdl_net and sdl_ttf?
I’m just surprised VC8 libraries are compatible with the mingw linker.
March 27th, 2008 at 6:31 am
Yes, the .lib files which are VC specific do in fact work with mingw. I have successfully used sdl_mixer, sdl_ttf, and sdl_image with the VC libs on Codeblocks with mingw. Regarding sdl_net, I am not entirely sure if that would work or not.