Simple DirectMedia Layer 2.0

Newcomer-Friendly

© Lead Image © tomwang, 123RF.com

© Lead Image © tomwang, 123RF.com

Article from Issue 159/2014
Author(s):

After several years of development work, version 2.0 of the SDL library was released in August 2013. Despite its many innovations, migrating to and getting started with SDL 2.0 is amazingly easy.

Since 1998, Simple DirectMedia Layer (SDL) has formed the basis for countless games, multimedia applications, and even virtualization. The small cross-platform library makes it easy for applications to access the graphics card, the audio hardware, the keyboard, and other input devices. Because it is available for many different platforms and operating systems, you can easily port any programs that have been developed with it (see the "Supported Operating Systems" box). Without SDL, Linux users would probably still be waiting for conversions of blockbuster games like Portal (Figures  1 and 2).

Figure 1: Almost all games offered via Steam, including blockbusters like the puzzle game Portal, are based on SDL.
Figure 2: Even serious applications, such as the virtualization software Qemu and VirtualBox, use SDL for their screen output.

Supported Operating Systems

Officially, SDL 2.0.0 supported the following operating systems when this issue went to press:

  • Linux as of kernel 2.6
  • Android as of version 2.3.3
  • Windows XP, Vista, 7, and 8
  • Mac OS X as of version 10.5
  • iOS as of version 5.1.1

Although SDL 2.0 will probably compile and support use on NetBSD, FreeBSD, OpenBSD, and Solaris, the developers provide no guarantees. Unofficial ports to Haiku, PlayStation Portable, and Pandora also exist.

The list of supported systems is significantly shorter than that of SDL 1.2. Because of a lack of maintainers, the team was forced in particular to remove ports for older operating systems, such as OS/2 and BeOS.

For an amazing 12 years, SDL version 1.2 provided the underpinnings of numerous programs. Changes have only come in small doses, with the last revision (1.2.15) appearing in January 2012. SDL 1.2 is thus considered extremely robust, but hardware development has mercilessly left it behind. In the background, SDL developers have already been working for several years in parallel on a revamped, modern variant.

The developer versions initially had a version number of 1.3, but – because of the many internal changes – the developers made the leap to 2.0 in February 2012. Although this new branch had already been used in many software projects, the developers were reluctant to put a "Stable" stamp on it. In August 2013, however, SDL inventor and project manager Sam Lantinga drew the line and officially released the current state of development as version 2.0.0.

2.0 Picks Up Speed

The new version finally can open multiple windows at the same time, detect multiple connected screens, and control multiple audio devices simultaneously. The SDL now uses state-of-the-art 3D graphics hardware acceleration for 2D graphic output. Under Linux, image data finds its way to the screen via the OpenGL interface. In an emergency, or on request, the lean library can also compute the graphics itself with an internal software-based renderer.

SDL still only offers drawing functions for 2D graphics. Programmers can create 3D graphics with OpenGL as of version 3.0 – or OpenGL ES on mobile devices. SDL then handles all other tasks, such as the audio output or querying the keyboard. Under Windows, Mac OS X, and Linux, the new version of SDL can finally deal with force-feedback devices; it also supports input devices with a touch interface.

Improved power management aims to ensure longer battery life on mobile devices. Additionally, numerous small improvements, such as Unicode support, have been implemented. Existing SDL programs can be ported easily to SDL 2.0, but because of the many changes, a recompile is not enough.

A licensing change also has been made: The LGPL license of the legacy SDL 1.2 made it difficult to use in commercial projects. Lead developer Lantinga even founded a company from which developers could acquire a license for commercial programs in the meantime. The new version 2.0, however, is released under the more liberal zlib license, which permits unrestricted commercial use of the library.

Getting Started

SDL is written in C and provides only a C interface. It can be used directly from C++ programs, and bindings exist for Python, C#, and Pascal [2]. Because SDL 2 is not yet included by any distro, you will need to build and install it yourself. To use the full feature set, you obviously require a C compiler and make, but also the developer packages for X11, OpenGL, ALSA, PulseAudio, libudev, pthreads, D-Bus, and  – depending on your needs  – ESD (the Enlightened Sound Daemon), the aRts audio server, and the libts library for controlling touch screens.

The usual group of three commands completes the installation: ./configure; make; make install. Be sure to check the output from configure for unfulfilled dependencies; otherwise, you may find that required features, such as support for ESD, are missing later on. After installing, run ldconfig once as root. Incidentally, SDL 2 can be installed and run parallel to the legacy SDL 1.2: The library itself is called libSDL2.so, and the header files end up in the SDL2 subdirectory.

Humble Steps

Because SDL 2.0 has a clear and simple API, the first steps are easy. If you have previously worked with the legacy version 1.2, you will also find many things familiar. In your own program, you first include SDL as a single header file:

#include "SDL2/SDL.h"

Before you can take advantage of SDL's features, you must initialize its subsystems. The following line handles this:

SDL_Init(SDL_INIT_EVERYTHING);

The parameter that SDL_Init() expects is a flag specifying which subsystem the function should initialize. The SDL_INIT_EVERYTHING flag enables all available subsystems. If you only want graphics output, you can use SDL_INIT_VIDEO as an alternative.

Developers can link multiple flags with a logical OR to initialize multiple selected subsystems. The following statement enables the video and audio subsystems:

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);

If the call was successful, SDL_Init() returns a 0. If an error has occurred, however, you see a negative value. The corresponding error message is provided by the SDL_GetError() function. Most other SDL functions also use this form of error handling. Thus, to prevent your code from running into a wall, you should always evaluate the return value:

if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
  printf("error: %s \n", SDL_GetError());
  return 1;
}

Although this part is familiar to SDL 1.2 users, unexplored terrain now follows: SDL 2 has ditched the good old SDL_SetVideoMode().

If the initialization was successful, the user can open a window. The function responsible for this, SDL_CreateWindow(), requires a name for the title bar, the position of the upper-left corner of the window on the screen, and the size of the window in pixels as parameters:

SDL_Window *window = SDL_CreateWindow(↩
  "Hello World!",250, 300, 640, 480, SDL_WINDOW_SHOWN);

The window is titled Hello World! At 640x480 pixels, it appears on the desktop 250 pixels from the left and 300 pixels from the top of the screen. The SDL_WINDOW_SHOWN flag ensures that the window is visible onscreen, whereas SDL_WINDOW_HIDDEN would initially hide it.

An alternative would be SDL_WINDOW_FULLSCREEN, which displays the contents of the window in full-screen mode. You can link other flags with an OR; for example, SDL_WINDOW_BORDERLESS removes the window frame, and SDL_WINDOW_MAXIMIZED maximizes the window.

The SDL_CreateWindow() function returns a pointer to an SDL_Window object. You can use the pointer to address the window. If an error has occurred, then the pointer is NULL. The reason is again revealed by SDL_GetError().

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Tcl3D

    Tcl3D brings the world of 3D effects to TCL scripting. We’ll show you how to get started with building your own 3D scripts.

  • AIGLX

    Red Hat’s head of X development describes the evolution of AIGLX.

  • Xgl and Compiz

    A member of Suse’s X11 team delivers an insider’s look at Xgl.

  • Playful Progress: Blender 2.49 Enhances Game Engine and Panoramic View

    Blender 2.49 is still profiting from its experience developing Big Buck Bunny and the Yo Frankie! Blender game. Much of what goes into the new release is on account of the Game Engine, with its video integration and performance boost.

  • Blender 3D Modeling

    Although Walt Disney’s masterpieces are not forgotten, legacy cartoon techniques are a thing of the past. As long ago as 1995, the movie “Toy Story” proved that computers are more than up to the task of animating pictures. We’ll help you get started with building your own animations.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News