X Window Programming/SDL

From Wikibooks, open books for an open world
Jump to navigation Jump to search


[edit | edit source]
Abstraction layers of several SDL platforms

Simple DirectMedia Layer (SDL) is a cross-platform multimedia library written in C that creates an abstraction over various platforms' graphics, sound, and input APIs, allowing a developer to write a computer game or other multimedia application once and run it on many operating systems including GNU/Linux, Microsoft Windows and MacOS X. It manages video, events, digital audio, CD-ROM, sound, threads, shared object loading, networking and timers.


[edit | edit source]

Sam Lantinga created the library, first releasing it in early 1998, while working for Loki Software. He got the idea while porting a Windows application to Macintosh. He then used SDL to port Doom to BeOS. Several other free libraries appeared to work with SDL, such as SMPEG and OpenAL.

The SDL library has bindings with almost every programming language there is, from the popular (C++, Perl, Python (through pygame), Pascal etc.) to the less known (such as Euphoria or Pliant). This and the fact that it is open-source and licensed under the LGPL make SDL a common choice for a lot of multimedia applications.

SDL itself is very simple; it merely acts as a thin, cross-platform wrapper, providing support for 2D pixel operations, sound, file access, event handling, timing, threading, and more. OpenGL is often used with SDL to provide fast 3D rendering. It is often thought of as a cross-platform DirectX, although it lacks some of its more advanced functionality. SDL instead has a huge number of third party extensions that make it easy to do more advanced functions.

The library is divided into several subsystems, namely the Video (handles both surface functions and OpenGL), Audio, CD-ROM, Joystick and Timer subsystems. Besides this basic, low-level support, there also are a few SDL-dependent libraries that provide some additional functionality. These include SDL_image (provides an easy way to load today's most common image formats), SDL_mixer (complex audio functions, mainly for sound mixing), SDL_net (networking support), SDL_ttf (TrueType Font rendering support), SDL_gfx (some additional graphical functions, such as image resizing and rotating) and SDL_rtf (simple Rich Text Format rendering).

Example C Code

[edit | edit source]
// Headers
#include "SDL.h"

// Main function
int main( int argc, char* argv[] )
    // Initialize SDL
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
        return 1;

    // Delay 2 seconds
    SDL_Delay( 2000 );

    // Quit SDL

    // Return
    return 0;

A very basic SDL program. It loads SDL subsystems, pauses for 2 seconds, closes SDL, then exits the program.

Here is more advanced example :

  #include <SDL.h>
  #define DIM 400.0
  int main() {
       SDL_Surface *screen = SDL_SetVideoMode(DIM, DIM, 0, 0);
       SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE, DIM, DIM, 24, 0xFF, 0xFF00, 0xFF0000, 0);
       double fact = 2;
       double cx = -0.74364500005891;
       double cy = 0.13182700000109;
       while (fact > 1e-18) {
            double xa = cx - fact;
            double ya = cy - fact;
            int y;
            for (y = 0; y < DIM; y++) {
                 Uint8 *pixline = surface->pixels + y*surface->pitch;
                 double y0 = ya + y/DIM*2*fact;
                 int x;
                 for (x = 0; x < DIM; x++) {
                      double x0 = xa + x/DIM*2*fact;
                      double xn = 0, yn = 0, tmpxn;
                      int i;
                      for (i = 0; i<512; i++) {
                           tmpxn = xn*xn - yn*yn + x0;
                           yn = 2*xn*yn + y0;
                           xn = tmpxn;
                           if (xn*xn + yn*yn > 4)
                                break;  // approximate infinity
                      if (i == 512) {
                           // in Mandelbrot set
                           pixline[x*3] = pixline[x*3+1] = pixline[x*3+2] = 0;
                      } else {
                           // not in Mandelbrot set; use escape iteration value to set color (grades of blue then white)
                           pixline[x*3] = pixline[x*3+1] = i < 256 ? 0 : i - 256;
                           pixline[x*3+2] = i < 256 ? i : 255;
            SDL_BlitSurface(surface, NULL, screen, NULL);
            fact /= 2;

That is easily compiled and ran under Linux with "gcc `sdl-config --cflags --libs` -O3 mandelbrot.c && ./a.out", on Windows it's the same, but you have to use MinGW to compile it.


[edit | edit source]
  • SMPEG - SDL MPEG Player Library
  • Guichan and ParaGUI - Widget Sets
  • GGI - a free cross-platform graphics interface