SDL : Introduction
SDL = Simple DirectMedia Layer is a free cross-platform multi-media development API. Its used for
- games
- game SDKs
- emulators
- demos
- multimedia applications
What can it do?
Video
- Set a video mode at any depth (8-bpp or greater) with optional conversion, if the video mode is not supported by the hardware.
- Write directly to a linear graphics framebuffer.
- Create surfaces with colorkey or alpha blending attributes.
- Surface blits are automatically converted to the target format using optimized blitters and are hardware accelerated, when possible. MMX optimized blits are available for the x86.
- Hardware accelerated blit and fill operations are used if supported by the hardware.
Events
- Events provided for:
- Application visibility changes
- Keyboard input
- Mouse input
- User-requested quit
- Each event can be enabled or disabled with SDL_EventState().
- Events are passed through a user-specified filter function before being posted to the internal event queue.
- Thread-safe event queue.
Audio
- Set audio playback of 8-bit and 16-bit audio, mono or stereo, with optional conversion if the format is not supported by the hardware.
- Audio runs independently in a separate thread, filled via a user callback mechanism.
- Designed for custom software audio mixers, but the example archive contains a complete audio/music output library.
CD-ROM audio
- Complete CD audio control API
Threads
- Simple thread creation API
- Simple binary semaphores for synchronization
Timers
- Get the number of milliseconds elapsed
- Wait a specified number of milliseconds
- Set a single periodic timer with 10ms resolution
Endian independence
- Detect the endianness of the current system
- Routines for fast swapping of data values
- Read and write data of a specified endianness
What platforms does it run on?
Linux
- Uses X11 for video display, taking advantage of XFree86 DGA extensions and new MTRR acceleration for fullscreen display.
- Uses the OSS API for sound.
- Threads are implemented using either the clone() system call and SysV IPC, or glibc-2.1 pthreads.
Win32
- Two versions, one safe for all systems based on Win32 APIs, and one with higher performance, based on DirectX APIs.
- Safe version uses GDI for video display. High performance version uses DirectDraw for video display, taking advantage of hardware acceleration if available.
- Safe version uses waveOut APIs for sound. High performace version uses DirectSound for audio playback.
BeOS
- BWindow is used for video display.
- BSoundPlayer API is used for sound.
Unofficial ports, ports in progress
- Solaris, IRIX, FreeBSD
- MacOS
Using the Simple DirectMedia Layer API
Initializing the library
SDL is composed of eight subsystems – Audio, CDROM, Event Handling, File I/O, Joystick Handling, Threading, Timers and Video. Use SDL_Init() to dynamically load and initialize the library. This function takes a set of flags corresponding to the portions you want to activate:
- SDL_INIT_AUDIO
- SDL_INIT_VIDEO
- SDL_INIT_CDROM
- SDL_INIT_TIMER
Use SDL_Quit() to clean up the library when you are done with it.
Example:-
#include “SDL/SDL.h” //Include SDL functions and datatypes
int main( int argc, char* args[] ) {
SDL_Init( SDL_INIT_EVERYTHING ); //Start SDL
SDL_Quit(); //Quit SDL
return 0;
}