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
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
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.