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.
sudo apt install libsdl2-devbrew install sdl2Download development libraries from https://www.libsdl.org/download-2.0.php and configure your compilerSDL2 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.
#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.
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.
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.
#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.
#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.
#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.
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.