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. :)
"The application was unable to start correctly (0xc000007b)."
When I compile, there are no errors. However, when I run the program, the
Build log says:
Process terminated with status -1073741701
I tried re-installing Code Blocks and downloaded all the latest SDL and SDL_image files. Linked them to the appropriate files and added the .dll files into the same folder as the .exe. Still getting the same error.
I Google'd the status error and I could not find this particular number anywhere.
Please help.
Email (required, not published):
Website:
After much research, this error might be due to using a 64-bit version of SDL/SDL_image.
I have a 64-bit version of windows and assumed I needed the 64-bit version of SDL.
Replacing the 64-bit versions of SDL/SDL_image with a 32-bit version fixed the problem (so it seems). So if you are getting an app error like I was, the problem might be you have the wrong version of SDL/SDL_image.
Email (required, not published):
Website:
Email (required, not published):
Website:
if you are using Code::Blocks, add the SDL library images into the Projects->Build Options->Linker Settings link libraries box in this order:
libSDL_image.so
libSDL.so
You may have to search around a bit for them, they should be in /usr/lib or /usr/lib64 but in Ubuntu 12.10 and derivatives they are in /usr/lib/x86_64-linux-gnu.
I did this and it compiled without problems.
Email (required, not published):
Website:
Any idea why?
Thanks,
K
Email (required, not published):
Website:
Draw the (non-transparent) background before drawing the frame.
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
Don't forget to do the same to the .lib files and dlls. (put them in the lib/x86 or lib/x64 folder that holds all your libs and dlls for SDL.
Finally, add SDL_image.lib to the additional libraries (with SDL.lib and SDLmain.lib)
It should work at that point.
Note: If you're using Visual Studio, you may have to redo the entire configuration when you switch from Debug mode to Release mode.
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
Thanks for a great tutorial!
BTW I have a problem with the loaded Image (The same Yoshi PNG Image created from Windows Paint) that the transparency is not set and the pink thing appears with the image.
I am calling the function to set the transparency to the surface.
Am on Visual Studio.
Please help.
Thanks in Advance!
Email (required, not published):
Website:
After reading http://sdl.beuc.net/sdl.wiki/SDL_SetAlpha I found that adding the line
SDL_SetAlpha(Surf_Dest, SDL_RLEACCEL, 255);
to TestSurface::Transparent, after SDL_SetColorKey, that the problem resolves. However, this breaks images with proper alpha channels. As such, to get it to work properly you'd have to have separate code for images with and without true alpha.
Email (required, not published):
Website:
But, what's bothering me about that is that according to the website you indicated, "Up until version 1.1.5, an alpha value of 0 was considered opaque and a value of 255 was considered transparent. This has now been inverted: 0 (SDL_ALPHA_TRANSPARENT) is now considered transparent and 255 (SDL_ALPHA_OPAQUE) is now considered opaque."
What's really cooking my noodle is that the third argument of SDL_SetAlpha(...) seems to have no effect on my program. I tried with both a value of 0 and 255, and both cases result in a transparent background.
Email (required, not published):
Website:
Email (required, not published):
Website:
Didn't know you were supposed to use the x86 libraries instead of the x64 ones. It compiles now.
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
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:
Email (required, not published):
Website:
Clicking the rebuild button forces CodeBlocks to recompile everything and not try to determine what has updated.
Email (required, not published):
Website:
Email (required, not published):
Website: