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:
In our case, we have to include the SDL/SDL.h header.
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:
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:
Of course, we need an infinite loop.
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!
Devon Sean McCu... (not verified)
Monday, April 16, 2012 - 11:41pm
Permalink
How to find lib SDL on MacOS?
How to find lib SDL on MacOS?
Compilation started at Tue Apr 17 05:26:19
gcc main.c -c ; gcc main.o -o go -lSDL ; ./go
i686-apple-darwin10-gcc-4.2.1: main.c: No such file or directory
i686-apple-darwin10-gcc-4.2.1: no input files
i686-apple-darwin10-gcc-4.2.1: main.o: No such file or directory
/bin/bash: ./go: No such file or directory
Compilation exited abnormally with code 127 at Tue Apr 17 05:26:19
mv main.cpp main.c
Compilation started at Tue Apr 17 05:28:39
gcc main.c -c ; gcc main.o -o go -lSDL ; ./go
ld: library not found for -lSDL
collect2: ld returned 1 exit status
/bin/bash: ./go: No such file or directory
Compilation exited abnormally with code 127 at Tue Apr 17 05:28:39
Mi-K
Tuesday, April 17, 2012 - 5:23pm
Permalink
Hello Devon,
Hello Devon,
Did you try what the website below suggests?
I hope it will help you.
Devon Sean McCu... (not verified)
Friday, November 15, 2013 - 2:29pm
Permalink
No help there... a year and a
No help there... a year and a half go by... I compile SDL and SDL2 from source, somehow coax Xcode to make SDL.framework, rename your example to hello-sdl.c, try this incantation at
$ sdl-config --cflags --libs -I/usr/local/include/SDL -D_GNU_SOURCE=1 -D_THREAD_SAFE -L/usr/local/lib -lSDLmain -lSDL -Wl -framework Cocoa
$ gcc hello-sdl.c -o hello-sdl `sdl-config --cflags --libs` && ./hello-sdl
presto! A window appears, thank you for the example.
Devon Sean McCu... (not verified)
Friday, November 15, 2013 - 2:33pm
Permalink
However the window is white,
However the window is white, not black. Too bad I can't edit my reply.
Mi-K
Friday, November 15, 2013 - 9:24pm
Permalink
Dear Devon,
Dear Devon,
Glad to see that you never give up.
Thanks for the link and this explanation.
And sorry for the window's color.
Add new comment