31 August 2015

SDL Introduction

SDL 2.0 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:

SDL Main Features

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;
}

Creating a Renderer



blog comments powered by Disqus