SDL2

Language: C

Multimedia / Graphics

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.

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.

Installation

linux: sudo apt install libsdl2-dev
mac: brew install sdl2
windows: Download development libraries from https://www.libsdl.org/download-2.0.php and configure your compiler

Usage

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.

Initializing SDL and creating a window

#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;
}

Initializes SDL2, creates a 640x480 window, waits 3 seconds, and then cleans up.

Handling basic events

SDL_Event e;
int quit = 0;
while (!quit) {
    while (SDL_PollEvent(&e)) {
        if (e.type == SDL_QUIT) {
            quit = 1;
        }
    }
}

Processes SDL events in a loop and exits when the window close event is triggered.

Rendering with SDL2

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);

Creates a renderer, clears the screen with red color, presents it, waits 2 seconds, and then cleans up.

Loading and displaying an image with SDL2_image

#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);

Loads an image file into a texture and displays it in the SDL2 window.

Playing audio with SDL2_mixer

#include <SDL2/SDL_mixer.h>
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
Mix_Music *music = Mix_LoadMUS("music.mp3");
Mix_PlayMusic(music, -1);

Initializes the audio subsystem, loads music, and plays it in a loop.

Using SDL2_ttf for text rendering

#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);

Renders text to a texture using TTF fonts and displays it in the window.

Error Handling

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.