Installing SDL_image
So far we have only been loading BMP image files. This is all that SDL supports without any extensions. We can use SDL_image
to enable us to load many different image file types such as BMP, GIF, JPEG, LBM, PCX, PNG, PNM, TGA, TIFF, WEBP, XCF, XPM, and XV. First we will need to clone the latest build of SDL_image
to ensure it will work with SDL 2.0:
- Open up the
TortoiseHg
workbench and use Ctrl + Shift + N to clone a new repository. - The repository for SDL_image is listed on http://www.libsdl.org/projects/SDL_image/ and http://hg.libsdl.org/SDL_image/. So let's go ahead and type that into the Source box.
- Our destination will be a new directory,
C:\SDL2_image
. After typing this into the Destination box, hit clone and wait for it to complete. - Once you have created this folder, navigate to our
C:\SDL2_image
cloned repository. Open up theVisualC
folder and then open theSDL_image_VS2010
VC++ project with Visual Studio 2010 express. - Right-click on the
SDL2_image
project and then click on Properties. Here we have to include theSDL.h
header file. Change the configuration to All Configurations, navigate to VC++ Directories, click on the Include Directories drop-down, and then on <Edit…>. Here we can put in ourC:\SDL2\include\
directory. - Next move to Library Directories and add our
C:\SDL2\lib\
folder. Now navigate to Linker | Input | Additional Dependencies and addSDL2.lib
. - Click on OK and we are almost ready to build. We are now using
SDL2.lib
, so we can remove theSDL.lib
and theSDLmain.lib
files from theSDL_image
project. Locate the files in the solution explorer, right-click and then remove the files. Change the build configuration to release and then build. - An error about being unable to start the program may appear. Just click on OK and we can close the project and continue.
- There will now be a
Release
folder inside ourC:\SDL2_image\VisualC\
folder. Open it and copy theSDL_image.dll
to our game's executable folder. - Next copy the
SDL2_image.lib
file into our originalC:\SDL2\lib\
directory. Also copy theSDL_image
header fromC:\SDL2_image\
to theC:\SDL2\include\
directory. - We just have a few more libraries to get and we are done. Download the
SDL_image-1.2.12-win32.zip
file (or the x64 if you are targeting a 64 bit platform) from http://www.libsdl.org/projects/SDL_image/. Extract all and then copy all of the.dll
files apart fromSDL_image.dll
into our game's executable folder. - Open up our game project and go into its properties. Navigate to Linker | Input | Additional Dependencies and add
SDL2_image.lib
. - We have now installed
SDL_image
and can start to load all kinds of different image files. Copy theanimate.png
andanimate-alpha.png
images from the source downloads to our games assets folder and we can start loading PNG files.
Using SDL_image
So we have the library installed, now how do we use it? It is simple to use SDL_image in place of the regular SDL image loading. In our case we only need to replace one function and also add #include <SDL_image.h>
.
SDL_Surface* pTempSurface = SDL_LoadBMP("assets/animate.bmp");
The preceding code will be changed as follows:
SDL_Surface* pTempSurface = IMG_Load("assets/animate.png");
We are now loading a .png
image. PNG files are great to work with, they have a small file size and support an alpha channel. Let's perform a test. Change our renderer clear color to red.
SDL_SetRenderDrawColor(m_pRenderer, 255,0,0,255);
You will see that we still have our black background from the image we are using; this is definitely not ideal for our purposes.
When using PNG files, we can resolve this by using an alpha channel. We remove the background from the image and then when we load it, SDL will not draw anything from the alpha channel.
Let's load this image and see how it looks:
SDL_Surface* pTempSurface = IMG_Load("assets/animate-alpha.png");
This is exactly what we want: