Simple DirectMedia Layer 2.0
Newcomer-Friendly
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).
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
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.