Every SDL program must include SDL.h. Including SDL.h gives you access to all
the functions needed for SDL programming.
The first SDL function that any program calls must be SDL_Init. SDL_Init allows
you to tell the library which parts of SDL you will be using. Currently, you may
initialize and use timer, audio, video functions, cdrom functions, and joystick
functions. If you wish to use all of these, SDL_INIT_EVERYTHING initializes all
subsystems. There are also two special purpose SDL_Init paramaters that are not
covered in this particular tutorial. These are SDL_INIT_NOPARACHUTE and
SDL_INIT_EVENTTHREAD.
if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Could not initialize SDL: %s\\n",
SDL_GetError());
return -1;
}
atexit(SDL_Quit);
SDL_Init will return a value less than zero if an error occurs. It is important
to check that the function worked properly before making any SDL calls.
|
When exiting your C++ program, you must execute the function SDL_Quit(). That
will clear up everything. If you wouldn't execute it before exiting your
program, starge things may occur. To tell the compiler that you want to run
SDL_Quit on exit, tell it like this:
atexit(SDL_Quit);
That way you don't need to put SDL_Quit() before every return [nr]; in main(). |