SDL Introduction
Simple DirectMedia Layer
is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. It is used by video playback software, emulators, and popular games.
SDL officially supports Windows, Mac OS X, Linux, iOS, and Android. Support for other platforms may be found in the source code.
SDL is written in C, works natively with C++, and there are bindings available for several other languages, including C# and Python.
Here are officail documents:
- Installing SDL
- SDL 2.0 API by Name
- SDL 2.0 API by Category
- SDL Tutorials list a few tutorials, includes TwinklebearDev SDL 2.0 Tutorial
- Articles
- Books
SDL Main Features
- Video
- Audio
- Input Event
- Threads and Timers
- OS and Platform Support
- Add-On Library
- SDL_Image
- SDL_TTF
- SDL_Net
- SDL_Mixer
Video System
Basic Conception
- SDL_Rect
- SDL_Color
- SDL_Palette
- SDL_PixelFormat
- SDL_Surface
- SDL_VideoInfo
- SDL_Surface: a rectangular block of pixels
- SDL_Overlay: used for streaming video data
Basic Operations
- Initialization
- Open a Window
- Create a rendering context
- draw an image.
Initialization
To use SDL we first need to initialize the various SDL subsystems through SDL_Init()
which takes a set of flags or together specifying the subsystems we’d like to initialize.
if (SDL_Init(SDL_INIT_VIDEO) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
Note that:
- The event handling system is initialized automatically with the video system.
- The file I/O and threading systems are initialized by default.
Opening a Window
We’ll need a window to display our render in, we can create one with SDL_CreateWindow
which takes a title for the window, the x and y position to create it at, the window width and height and some flags to set properties of the window and returns an SDL_Window*.
SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (win == nullptr){
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}