This 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:
#ifndef _CSURFACE_H_
#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* CSurface::OnLoad(char* File) {
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* CSurface::OnLoad(char* File) {
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:
if((Surf_Test = CSurface::OnLoad("myimage.png")) == NULL) {
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. :)
I followed the previous tuts till I reached SDL Map, and I went to change the LoadBMP with IMG_Load, but now my program is stopping after the execution. (I start it and it turns off).
Email (required, not published):
Website:
Email (required, not published):
Website:
I have set up this library without any error, and am able to display .gif format. But I still can't not display .png format.
I found that when I use
Surf_Temp = IMG_Load(File)
it simply returns NULL. What do you think?
Thanks in advance ; )
Email (required, not published):
Website:
Email (required, not published):
Website:
Oscar, be sure to add the libpng12-0.dll and the zlib1.dll.
Email (required, not published):
Website:
Email (required, not published):
Website:
great tutorials, but I'm having problems compiling the SDL Image library.
Could someone please post precompiled libs for the GNU gcc compiler (MinGW), because I've been trying to compile it for several days... it just doesn't work....
Thanks for any help
Email (required, not published):
Website:
Just in case someone needs this too...
Email (required, not published):
Website:
I would really just like to get the precompiled libs.. i really dont understand why they have't put such a file on their site-.-
Email (required, not published):
Website:
http://www.libsdl.org/projects/SDL_image/
then download the Win32 SDL_image-devel-1.2.10-VC.zip (if youre on windows...)
direct download:
http://www.libsdl.org/projects/SDL_image/release/SDL_image-devel-1.2.10-VC.zip
they should be in there...
Greetings!
Email (required, not published):
Website:
Email (required, not published):
Website:
It is there.. I don't understand.
Email (required, not published):
Website:
Email (required, not published):
Website:
SDL_Surface* CSurface::OnLoad(const char* file)
{
SDL_Surface* temp(0);
SDL_Surface* result(0);
if((temp= IMG_Load(file)))
{
if(temp->format->Amask)
{
result= SDL_DisplayFormatAlpha(temp);
}
else
{
result= SDL_DisplayFormat(temp);
}
SDL_FreeSurface(temp);
}
return result;
}
It seems that a SDL_Surface has a Amask after IMG_Load only if the image had an alpha channel. So I use SDL_DisplayFormatAlpha only in this case.
If you set a colorkey to a surface with an alpha channel, the colorkey seems to be ignored.
Email (required, not published):
Website:
Email (required, not published):
Website:
I am having problems setting up the SDL files. I attempted this tutorial a few weeks ago and was getting errors stating some of the .dll files were not found (jpeg, java, jvm, ect..). Once I found all these files I received " The ordinal 50 could not be located in the dynamic link library jpeg.dll in a pop-up window. This window is titled "SDL2.exe - Ordinal not found". SDL2.exe is located in my projects bin/debug folder if that helps. Also what is the purpose of this SDL2.exe and is it placed/created when the project is compiled?
I apologize in advance if this is a horrible question. Throughout the three books i have read about C++ all these file's purposes were not explained.
Email (required, not published):
Website:
Email (required, not published):
Website:
In regards to SDL2.exe, I don't know what that is. Sounds like your project name.
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
i get this error when I follow this tutorial. what should i do?
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
Surf_Return = SDL_DisplayFormat(Surf_Temp);
SDL_FreeSurface(Surf_Temp);
to:
Surf_Return = SDL_DisplayFormat(Surf_Temp);
SDL_SetColorKey(Surf_Return, SDL_RLEACCEL, Surf_Return->format->colorkey);
SDL_FreeSurface(Surf_Temp);
As recommended by the SDL docs.
Email (required, not published):
Website:
First, thanks for the amazing tutorials! Second, i have a problem that i don't know how to solve. I read the animation tutorial and apply the code, everything went fine. Then i read this SDL image tutorial and tried to apply the changes on the code you did on animation tutorial. When i ran the .exe with this change made here i got the yoshi animation perfect but with no transparency, i mean the pink area in yoshi's PNG didn't got transparent. So i changed the "Surf_Return = SDL_DisplayFormatAlpha(Surf_Temp);" back to "SDL_Return = SDL_DisplayFormat(Surf_Temp);" and keeping the yoshi's PNG on the OnInit.cpp and everthing went fine.
Why DisplayFormatAlpha is not doing what it was suposed to do?
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
SDL_DisplayFormatAlpha is used on RGBA images - those with an alpha channel. I believe SDL_SetColorKey has no effect then. In which case, you'd simply make your image have transparency in whatever image program you have (like Photoshop or something).
Email (required, not published):
Website:
I dont have background in graphics, but some of the Tim words was in my head (is used on RGB and not in RGBA).
Great tutorial as usual.
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website: