SDL2
Cross-platform access to graphics, audio, input and windowing for games and multimedia.
What it is
SDL2 (Simple DirectMedia Layer 2) is a cross-platform C library for handling multimedia, input devices, 2D graphics, audio, and game controller support. It is widely used for game development, emulators, and interactive applications.
SDL2 allows you to create windows, render graphics, handle events (keyboard, mouse, joystick), play audio, and manage timers. It provides both a low-level interface and integration with OpenGL/Direct3D for hardware-accelerated graphics.
- Licence
- zlib licence
- Powers
- Valve's Linux ports, many indie games, and numerous emulators
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Writing a game or multimedia application from scratch in C or C++
- You need one API for windowing, controllers, audio and events across desktop platforms
Look elsewhere when
- You want a game engine with a scene graph, physics and an editor — SDL is a layer beneath that
- You only need an OpenGL context and keyboard input; GLFW is smaller
Installation
sudo apt install libsdl2-devGetting started
The smallest useful thing you can do with it, and what each part means.
#include <SDL2/SDL.h>
#include <stdio.h>
int main() {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("SDL_Init Error: %s\n", SDL_GetError());
return 1;
}
SDL_Window *win = SDL_CreateWindow("Hello SDL2", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (win == NULL) {
printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_Delay(3000); // Display window for 3 seconds
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}SDL_Event e;
int quit = 0;
while (!quit) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = 1;
}
}
}Advanced usage
Where the library earns its place over a simpler alternative.
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(ren, 255, 0, 0, 255);
SDL_RenderClear(ren);
SDL_RenderPresent(ren);
SDL_Delay(2000);
SDL_DestroyRenderer(ren);#include <SDL2/SDL_image.h>
SDL_Surface *img = IMG_Load("image.png");
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, img);
SDL_FreeSurface(img);
SDL_RenderCopy(ren, tex, NULL, NULL);
SDL_RenderPresent(ren);#include <SDL2/SDL_mixer.h>
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
Mix_Music *music = Mix_LoadMUS("music.mp3");
Mix_PlayMusic(music, -1);#include <SDL2/SDL_ttf.h>
TTF_Font *font = TTF_OpenFont("Arial.ttf", 24);
SDL_Color color = {255, 255, 255};
SDL_Surface *text_surface = TTF_RenderText_Solid(font, "Hello SDL2", color);
SDL_Texture *text_texture = SDL_CreateTextureFromSurface(ren, text_surface);
SDL_FreeSurface(text_surface);
SDL_RenderCopy(ren, text_texture, NULL, NULL);
SDL_RenderPresent(ren);Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- SDL_Init failed
- Check that SDL2 is installed correctly and the development libraries are properly configured.
- SDL_CreateWindow failed
- Ensure the window dimensions are valid and the video subsystem is initialized.
- IMG_Load failed
- Verify the image file path and format; ensure SDL2_image is initialized.
Best practices
- Always call SDL_Quit() before exiting to release resources.
- Check return values for all SDL functions to handle initialization and runtime errors.
- Use SDL_Renderer and textures for efficient 2D graphics rendering.
- Poll events frequently to keep applications responsive.
- Use SDL subsystems (SDL_image, SDL_mixer, SDL_ttf) for extended functionality.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
SDL2 was developed by Sam Lantinga in 1998 to provide a simple and portable interface to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. SDL2 improved upon SDL1.2 with enhanced support for modern hardware, multiple windows, and better input handling. It is the foundation for many games, engines, and emulators.
