C - SDL (Simple Directmedia Layer) - Hello World!

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! cool

Comments

Comment: 

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

Comment: 

Hello Devon,

Did you try what the website below suggests?

http://guides.macrumors.com/Installing_SDL

I hope it will help you. enlightened

Comment: 

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
http://StackOverflow.com/questions/2330257/sdl-causes-undefined-symbols-main-referenced-from-start-in-crt1-10-5-o

$ 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.

Comment: 

However the window is white, not black. Too bad I can't edit my reply.

Comment: 

Dear Devon,

Glad to see that you never give up.
Thanks for the link and this explanation.

And sorry for the window's color. laugh

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.