
In this article I will try to give a brief overview of the very useful open-source library SDL (Simple DirectMedia Library) for writing cross-platform multimedia applications. On Habré about it almost did not mention, so I want to fill this "gap".
Intro
SDL is positioned as a tool that provides a platform-independent, low-level API for accessing hardware capabilities such as 2D and 3D rendering, audio playback and processing of input devices (mouse, keyboard, joystick). Liba can be useful mainly for those who develop cross-platform games, but it can be used with the same success, for example, in programs like video and audio players, emulators, etc. In addition to the above features, SDL has tools for working with multithreading, files, timers, and a CD / DVD drive. This allows using libu also in those projects that are not related to graphics and / or sound reproduction.
SDL is ported to many platforms. In addition to Windows, Linux and Mac OS (X), there are also ports on Solaris, IRIX, * BSD, etc. The complete list is on the
main page of the project , there are also listed bindings to many programming languages, incl. scripting.
Distributed under the
GNU LGPL license. The current version 1.2.14, according to the developers, has been released to fix a number of bugs and is the latest in branch 1.2, the next one will be 1.3.
A little bit about installation and API
For all platforms, the necessary files are available for download at the office. site, and for Linux, you can also search in turnips (at least under Ubuntu, I found it in the libsdl-dev package). Source codes are available in archives, or on svn.
SDL does not require installation per se. It is usually connected in the form of a dynamic library. To distribute along with the program, it is enough to attach a single .so file (.dll).
')
SDL is written in C. The interface consists of functions and macros, the list of which is
not so long and not difficult to learn. On the API project
wiki , functions and data types are grouped both alphabetically and by category, so you can easily find something you need.
Small example
Below is a simple example of a program using SDL:
#include <SDL/SDL.h>
int main()
{
// SDL (, )
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
return 1;
//
SDL_putenv( "SDL_VIDEO_CENTERED=true" );
// . , 800x600@16 OpenGL
SDL_Surface *surface = SDL_SetVideoMode(800, 600, 16, SDL_DOUBLEBUF | SDL_OPENGL);
if (surface == NULL)
return 1;
//
SDL_Event evt;
bool stop = false ;
while (!stop)
{
//
while (SDL_PollEvent(&evt))
{
switch (evt.type)
{
case SDL_KEYDOWN:
// Esc...
if (evt.key.keysym.sym == SDLK_ESCAPE)
stop = true ;
case SDL_QUIT:
// ...
stop = true ;
default :;
}
}
//
// ...
// GL
SDL_GL_SwapBuffers();
}
// SDL
SDL_Quit();
return 0;
}
* This source code was highlighted with Source Code Highlighter .
(
it is unusual for me to write comments in Russian, but since probably not everyone is good with English, I will leave it just in case )
Run through the code. First of all, we initialize the subsystems we need. In this case, this video, audio and timing. After that, separately set the video mode with the function SDL_SetVideoMode (), which returns the created video context. She also concurrently also creates a window in which rendering will take place. There are a lot of initialization flags, the full list is on the wiki.
After preliminary settings, go to the main working cycle of the program. At each iteration, he retrieves information from the queue about the events that have occurred, after which he swaps the framebuffers (double buffering is enabled). Between these two actions, you can create something more meaningful, for example, draw the next frame of the animation. The loop is executed until the window is closed or the Esc key is pressed. Before shutting down, we call the function SDL_Quit (), which closes all SDL subsystems.
As you can see from the example, the SDL API is very simple, preparing the system for work takes only a couple of lines, and the event processing cycle is similar to the standard message processing loop of Windows programs.
Outro
Finally, I want to note a few additional points:
- For development on OpenGL SDL is a worthy alternative to GLUT. You can integrate with various engines (I managed to make it work together with OGRE).
- SDL is not intended to create any interface elements other than ordinary windows, and cannot compete in this regard with projects like Qt or wxWidgets.
- The project has quite convenient documentation in several languages, including in offline version. In addition to the wiki, there is a detailed description of the library's capabilities and many lessons compiled by the community.
- The library has earned considerable popularity among igrodelov (mainly under Linux), and is used in projects such as Nexuiz and Civilization: Call To Power. I myself learned about Libe by digging into Nexuiz files and stumbled upon the SDL abbreviation that I still did not understand at that time :).
- A list of APIs that the library itself uses on different platforms for displaying graphics and sound is available.
- Finally, the main developer of the library is Sam Lantinga, software engineer from the notorious Blizzard Entertainment :)
That's all. The review turned out to be very superficial, although I hope it will be useful to those whose interests include the development of multimedia applications, and who have not yet heard about this library.