← Back to C Course | Chapter 13: Graphics | Lesson 9 of 10

C SDL2 Introduction

What is SDL2?

SDL2 (Simple DirectMedia Layer) is a cross-platform C library providing low-level, hardware-accelerated access to graphics rendering, keyboard and mouse input, and audio, and unlike the DOS-era BGI library, it actually compiles and runs on modern Windows, Linux, and macOS systems.

Example: What is SDL2?

c
#include <SDL2/SDL.h>
int main() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Quit();
	return 0;
}

Creating a Window and Renderer

Drawing anything in SDL2 starts with creating an SDL_Window to represent the actual window on screen, and then a paired SDL_Renderer bound to that window, since the renderer — not the window itself — is what all your subsequent drawing calls actually target.

Example: Creating a Window and Renderer

c
#include <SDL2/SDL.h>
int main() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window *window = SDL_CreateWindow("App", 0, 0, 640, 480, 0);
	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();
	return 0;
}

Drawing Shapes in SDL2

SDL_RenderDrawLine() and SDL_RenderFillRect() are two of SDL2's core drawing functions, letting you draw a line between two points or fill a rectangular region on the renderer respectively; after issuing drawing calls you must call SDL_RenderPresent() to actually display them.

Example: Drawing Shapes in SDL2

c
#include <SDL2/SDL.h>
int main() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window *window = SDL_CreateWindow("App", 0, 0, 640, 480, 0);
	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
	SDL_RenderDrawLine(renderer, 0, 0, 100, 100);
	SDL_RenderPresent(renderer);
	SDL_Quit();
	return 0;
}

Handling Basic Keyboard Events

SDL2 has no automatic event loop of its own, so your program must repeatedly call SDL_PollEvent() inside its own loop to check for pending input like key presses or a window-close request, and respond to each event type your program cares about.

Example: Handling Basic Keyboard Events

c
#include <SDL2/SDL.h>
int main() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Event event;
	while (SDL_PollEvent(&event)) {
		if (event.type == SDL_KEYDOWN) {
			// key was pressed
		}
	}
	SDL_Quit();
	return 0;
}

Drawing Custom Pixel Grids

Because SDL2 gives you direct control over individual pixels via its renderer, drawing nested loops of points or small filled rectangles lets you build custom pixel grids, tile-based maps, or procedurally generated patterns that none of SDL2's built-in shape functions produce directly.

Example: Drawing Custom Pixel Grids

c
#include <SDL2/SDL.h>
int main() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window *window = SDL_CreateWindow("App", 0, 0, 640, 480, 0);
	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
	for (int x = 0; x < 10; x++) {
		for (int y = 0; y < 10; y++) {
			SDL_RenderDrawPoint(renderer, x * 5, y * 5);
		}
	}
	SDL_Quit();
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.