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. :)
Email (required, not published):
Website:
Email (required, not published):
Website:
Process returned 3 (0x3) execution time : 0.878 s
Press any key to continue.
Am not sure where the error code 3 is coming from. I have tried reading in forums but non of them seem to give me any solution that works.
Email (required, not published):
Website:
Email (required, not published):
Website:
"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:
Email (required, not published):
Website: