You probably already installed SDL library on your computer.
If not, you can follow this SDL tutorial setup on Eclipse
.
Let’s test it with a Hello World! tutorial.
For Windows OS, do not forget to add the SDL.dll in the folder where there is your .exe.
To have an easy application than ever beginners can make, we need to follow five steps:
Include the right library
In our case, we have to include the SDL/SDL.h header.
Initialize the right library
We must initialize the library with SDL_init(). The parameter is what we want to initialize.
It can be an initialization of the VIDEO, AUDIO, EVERYTHING, and so on.
Of courses there this list of initializers can be found on the SDL official website
.
I suggest you to download the Introduction documentation direct from the official website.
Here this list of available parameters:
| SDL_INIT_TIMER | Initializes the timer subsystem. |
| SDL_INIT_AUDIO | Initializes the audio subsystem. |
| SDL_INIT_VIDEO | Initializes the video subsystem. |
| SDL_INIT_CDROM | Initializes the cdrom subsystem. |
| SDL_INIT_JOYSTICK | Initializes the joystick subsystem. |
| SDL_INIT_EVERYTHING | Initialize all of the above. |
| SDL_INIT_NOPARACHUTE | Prevents SDL from catching fatal signals. |
| SDL_INIT_EVENTTHREAD |
Setting the video mode
We set a video mode with a width, an height, a number of bit per pixel (BPP) and a flag to tell the SDL which device will make the library working and information about the memory and other useful things.
Here the full list of initializers:
| SDL_SWSURFACE | Surface is stored in system memory |
| SDL_HWSURFACE | Surface is stored in video memory |
| SDL_ASYNCBLIT | Surface uses asynchronous blits if possible |
| SDL_ANYFORMAT | Allows any pixel-format (Display surface) |
| SDL_HWPALETTE | Surface has exclusive palette |
| SDL_DOUBLEBUF | Surface is double buffered (Display surface) |
| SDL_FULLSCREEN | Surface is full screen (Display Surface) |
| SDL_OPENGL | Surface has an OpenGL context (Display Surface) |
| SDL_OPENGLBLIT | Surface supports OpenGL blitting (Display Surface) |
| SDL_RESIZABLE | Surface is resizable (Display Surface) |
| SDL_HWACCEL | Surface blit uses hardware acceleration |
| SDL_SRCCOLORKEY | Surface use colorkey blitting |
| SDL_RLEACCEL | Colorkey blitting is accelerated with RLE |
| SDL_SRCALPHA | Surface blit uses alpha blending |
| SDL_PREALLOC | Surface uses preallocated memory |
An infinite loop
Of course, we need an infinite loop.
Freeing all initializations
We finish with a SDL_Quit() method to free all initializations made before.
Now, let’s see the code.
In your project, create a main.cpp file with the following snippet:
#include <SDL/SDL.h>
int main(int argc, char *argv[]) {
int gogogo = 1;
SDL_Event event;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption("Hello World! :D", NULL);
SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
while (gogogo) {
SDL_WaitEvent(&event);
if (event.type == SDL_QUIT)
gogogo = 0;
}
SDL_Quit();
return 0;
}
Don’t forget to add the -lSDL command during the linkage:
$ gcc main.c -c ; gcc main.o -o go -lSDL ; ./go
The result after compiling will be a black window with the title: “Hello World! :D”.
Well done it works.