Programming with the SDL Library on Linux

The cross-platform library SDL (Simple DirectMedia Layer) is born by LokiGames to do the porting Windows games on Linux, but thanks to other programmers has become available for Windows, BeOS, MacOS and many others. The main virtues of this library are the portability and the composition of the various modules, including external made ​​by other programmers, all dedicated to the creation of a multimedia application like a video game. To compile programs written with the SDL library is to download the packages directly from your Linux distribution (Ubuntu libsdlx.x-dev, the recommended method), or you can find them also for other platforms on the official website of SDL behold modules base in which is divided the library:

  • Video
  • Audio
  • Joystick
  • Timer
  • Thread
  • Events

To these it is possible to combine other libraries for other tasks such as for the management of the network or images. As for the 3D graphics, SDL allows you to create a context used in OpenGL.
During initialization you can activate only the necessary modules or other libraries to delegate other tasks, use FMOD for audio functions and SDL for graphics, for example. The function that is used to initialize the library is called SDL_Init(), which accepts as a parameter a set of flags can be joined by OR, then the function returns 0 (zero) if successful, otherwise – 1.

  • SDL_INIT_TIMER for timers
  • SDL_INIT_AUDIO for ‘audio
  • SDL_INIT_VIDEO for video
  • SDL_INIT_CDROM for cdrom
  • SDL_INIT_JOYSTICK for joysyick
  • SDL_INIT_EVERYTHING for all previous subsets

Remember that this function must be invoked before any other call to the library and at the end should be closed with SDL_Quit(), in order to free the resources occupied. The SDL library also allows you to add or delete certain systems using the function SDL_InitSubSystem() and SDL_QuitSubSystem(), a function that accepts the same flags of SDL_Init(). Whenever you call a function is always good to check the return value to detect errors while using the function SDL_GetError() one can know the error through an address to the string containing the error occurred precisely in text format. Let’s see a small example of code to initialize the library and use some modules, all on Linux with GCC compiler.

/* File: test_sdl.c
   compilare con: gcc -o test_sdl test_sdl.c
         $(sdl-config --libs --cflags)
*/

#include <stdio.h>
#include <SDL.h>

int main(int argc, char** argv)
{
   int iRet = SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
   if (iRet == -1)
   {
      fprintf(stderr, "ERRORE: %s", SDL_GetError());
      return -1;
   }

   // Ora dobbiamo utilizzare anche il joystick
   iRet = SDL_InitSubSystem(SDL_INIT_JOYSTICK);
   if (iRet == -1)
   {
      fprintf(stderr, "ERRORE: %s", SDL_GetError());
      return -1;
   }

   // Ora non serve più l'audio ed il joystick
   SDL_QuitSubSystem(SDL_INIT_JOYSTICK|SDL_INIT_AUDIO);

   SDL_Quit();

   return 0;
}

In this article, we introduced programming using the SDL library for creating multimedia applications, for now I finish here, but in the future we will discuss the management of the video, so stay tuned.

This entry was posted in Linux. Bookmark the permalink.