BaseCodeByte
SDL

SDL2 Lessons

Learn SDL2 from setup to shipping: windows, rendering, input, audio, sprites and real-time 2D apps.

Course outline

Tracks and lessons

Track 01beginner

Module 1: SDL Foundations

Learn SDL from setup to shipping as you build real-time 2D applications with windows.

01SDL Overview, Setup, and Project StructureWhat SDL Is SDL, the Simple DirectMedia Layer, is a low-level multimedia library used to create windows, capture input. This course takes you from opening a window to structuring. SDL core for initialization, windows, rendering, timing, and input SDLbeginner

What SDL Is SDL, the Simple DirectMedia Layer, is a low-level multimedia library used to create windows, capture input. This course takes you from opening a window to structuring. SDL core for initialization, windows, rendering, timing, and input SDL

Example
game/
  src/
    main.cpp
    app.cpp
    player.cpp
  include/
  assets/
    textures/
    audio/
    fonts/
  CMakeLists.txt
Quiz

SDL is best described as:

SDL_ttf is used for:

A good SDL project usually separates:

02Windows, Renderers, and the Main LoopCreate the Window A stable loop is the backbone of every SDL application. The design goal is not just. Poll all queued events Measure frame delta time SDL_Window* window = SDL_CreateWindow( "CodeByte SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERbeginner

Create the Window A stable loop is the backbone of every SDL application. The design goal is not just. Poll all queued events Measure frame delta time SDL_Window* window = SDL_CreateWindow( "CodeByte SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTER

Example
SDL_Window* window = SDL_CreateWindow(
    "CodeByte SDL",
    SDL_WINDOWPOS_CENTERED,
    SDL_WINDOWPOS_CENTERED,
    1280, 720,
    SDL_WINDOW_SHOWN
);
Quiz

VSync in the renderer mainly helps with:

The render loop should usually do which order?

SDL_RenderPresent does what?

03Events, Keyboard, Mouse, and Game ActionsEvent Polling Instead of hard-coding gameplay directly to keys, map raw input to named actions such as move_left. Event-based input for one-time actions like pause, jump, menu. State-based input via SDL_GetKeyboardState for continuous movement SDL_Evbeginner

Event Polling Instead of hard-coding gameplay directly to keys, map raw input to named actions such as move_left. Event-based input for one-time actions like pause, jump, menu. State-based input via SDL_GetKeyboardState for continuous movement SDL_Ev

Example
SDL_Event event;
while (SDL_PollEvent(&event)) {
    if (event.type == SDL_QUIT) running = false;
    if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)
        running = false;
}
Quiz

Event-based input is best for:

SDL_QUIT is commonly triggered when:

Action mapping helps by:

Track 02beginner

Module 2: Rendering and Game Systems

Learn SDL from setup to shipping as you build real-time 2D applications with windows.

01Textures, Sprites, and Asset LoadingSurfaces vs Textures SDL often loads image data into a surface first, then converts it into a GPU-friendly texture. Load once, reuse many times Store textures in a resource manager SDL_Surface* surface = IMG_Load("assets/textures/player.png"); SDL_Tebeginner

Surfaces vs Textures SDL often loads image data into a surface first, then converts it into a GPU-friendly texture. Load once, reuse many times Store textures in a resource manager SDL_Surface* surface = IMG_Load("assets/textures/player.png"); SDL_Te

Example
SDL_Surface* surface = IMG_Load("assets/textures/player.png");
SDL_Texture* playerTexture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
Quiz

A texture is usually preferred over a surface for:

A resource manager mainly prevents:

SDL_RenderCopy draws:

02Delta Time, Animation, and Frame-Rate IndependenceWhy Delta Time Matters If one computer renders 144 frames per second and another renders 30, movement must still feel. Animation often advances through frames based on a timer, not. Movement and camera follow Cooldowns and invulnerability windows flointermediate

Why Delta Time Matters If one computer renders 144 frames per second and another renders 30, movement must still feel. Animation often advances through frames based on a timer, not. Movement and camera follow Cooldowns and invulnerability windows flo

Example
float delta = currentTime - previousTime;
player.x += player.velocityX * delta;
player.y += player.velocityY * delta;
Quiz

Delta time is used so that movement is:

Animation timing should usually be based on:

A cooldown timer is a good use of:

03Collision, Cameras, and World CoordinatesAxis-Aligned Bounding Boxes In 2D games, the camera is often just an offset. You keep world positions in game. World space for gameplay logic Screen space for final drawing and UI SDL_Rect player{px, py, 32, 48}; SDL_Rect wall{wx, wy, 64, 64}; if (SDintermediate

Axis-Aligned Bounding Boxes In 2D games, the camera is often just an offset. You keep world positions in game. World space for gameplay logic Screen space for final drawing and UI SDL_Rect player{px, py, 32, 48}; SDL_Rect wall{wx, wy, 64, 64}; if (SD

Example
SDL_Rect player{px, py, 32, 48};
SDL_Rect wall{wx, wy, 64, 64};
if (SDL_HasIntersection(&player, &wall)) {
    // resolve collision
}
Quiz

World coordinates should usually store:

SDL_HasIntersection helps with:

A camera in a 2D game is often represented as:

Track 03intermediate

Module 3: Production SDL

Learn SDL from setup to shipping as you build real-time 2D applications with windows.

01Audio, Fonts, HUDs, and MenusAudio Layers Treat menus like a separate state with its own input rules, focus handling, and rendering flow. Short sound effects for feedback Looping music for mood and pacing TTF_Font* font = TTF_OpenFont("assets/fonts/jetbrains.ttf", 24); SDL_Surfaintermediate

Audio Layers Treat menus like a separate state with its own input rules, focus handling, and rendering flow. Short sound effects for feedback Looping music for mood and pacing TTF_Font* font = TTF_OpenFont("assets/fonts/jetbrains.ttf", 24); SDL_Surfa

Example
TTF_Font* font = TTF_OpenFont("assets/fonts/jetbrains.ttf", 24);
SDL_Surface* textSurface = TTF_RenderText_Blended(font, "Score: 1250", color);
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
Quiz

SDL_ttf is commonly used to render:

Keeping menus in a separate state helps:

Sound effects are best described as:

02Game States, Resource Managers, and Clean ArchitectureCore Systems Small prototypes tolerate messy code. Complete games do not. Clean boundaries let you change levels, UI. Push platform details such as SDL window calls to the. App: owns initialization and shutdown State manager: menu, gameplay, pause, gadvanced

Core Systems Small prototypes tolerate messy code. Complete games do not. Clean boundaries let you change levels, UI. Push platform details such as SDL window calls to the. App: owns initialization and shutdown State manager: menu, gameplay, pause, g

Example
// State manager simulation
const states = ["boot", "menu", "gameplay", "pause", "gameover"];
let current = states[1];
console.log("current state:", current);
current = "gameplay";
console.log("switch to:", current);
const resources = { textures:3, fonts:1, sounds:5 };
console.log("resource summary:", resources);
Quiz

A state manager usually handles:

A resource manager owns things like:

Good architecture helps because it:

03Debugging, Optimization, and Shipping an SDL GameDebugging Checklist Finish the course by building a complete 2D action prototype with menus, sprites, sound, collisions, camera. Validate every SDL call that can fail Log file paths and renderer capabilitiesadvanced

Debugging Checklist Finish the course by building a complete 2D action prototype with menus, sprites, sound, collisions, camera. Validate every SDL call that can fail Log file paths and renderer capabilities

Example
// Release checklist
const checklist = [
  "error handling",
  "asset manifest",
  "fixed controls",
  "volume settings",
  "performance pass",
  "export package"
];
checklist.forEach(item => console.log("[ ] " + item));
console.log("Capstone: top-down dungeon prototype");
Quiz

The first optimization target should usually be:

A strong shipping checklist includes:

Profiling update and render separately helps: