Initializing SDL

SDL is composed of eight subsystems - Audio, CDROM, Event Handling, File I/O, Joystick Handling, Threading, Timers and Video. Before you can use any of these subsystems they must be initialized by calling SDL_Init (or SDL_InitSubSystem. SDL_Init must be called before any other SDL function. It automatically initializes the Event Handling, File I/O and Threading subsystems and it takes a parameter specifying which other subsystems to initialize. So, to initialize the default subsystems and the Video subsystems you would call:

    SDL_Init ( SDL_INIT_VIDEO );
To initialize the default subsystems, the Video subsystem and the Timers subsystem you would call:
    SDL_Init ( SDL_INIT_VIDEO or SDL_INIT_TIMER );

SDL_Init is complemented by SDL_Quit (and SDL_QuitSubSystem). SDL_Quit shuts down all subsystems, including the default ones. It should always be called before a SDL application exits.

With SDL_Init and SDL_Quit firmly embedded in your programmers toolkit you can write your first and most basic SDL application. However, we must be prepare to handle errors. Many SDL functions return a value and indicates whether the function has succeeded or failed, SDL_Init, for instance, returns -1 if it could not initialize a subsystem. SDL provides a useful facility that allows you to determine exactly what the problem was, every time an error occurs within SDL an error message is stored which can be retrieved using SDL_GetError. Use this often, you can never know too much about an error.

Example 1-1. Initializing SDL

uses SDL;   // All SDL App's need this

begin
    MessageBox(0, 'Initializing SDL.', 'Error', MB_OK or MB_ICONHAND); 

// Initialize defaults, Video and Audio
if( ( SDL_Init(SDL_INIT_VIDEO or SDL_INIT_AUDIO)= -1 ) ) then
begin
MessageBox(0, PChar(Format('Couldn''t initialize SDL : %s',
[SDL_GetError])), 'Error', MB_OK or MB_ICONHAND);
exit;
end;
    MessageBox(0, 'SDL Initialized.', 'Error', MB_OK or MB_ICONHAND);
    MessageBox(0, 'Quiting SDL.', 'Error', MB_OK or MB_ICONHAND);
// Shutdown all subsystems
SDL_Quit;
    MessageBox(0, 'Quiting.', 'Error', MB_OK or MB_ICONHAND);
exit;
end;