Hardware-Accelerated 2D Rendering with SDL2: Setup and Rectangle Drawing
Learn how to request an accelerated SDL_Renderer, draw a rectangle, and avoid common pitfalls when using SDL2's hardware-accelerated 2D rendering.
05 Mar 2026, 05:29 UTC

Get hardware-accelerated drawing in SDL2
To draw 2D graphics with GPU acceleration in SDL2, request an accelerated renderer when you create it. If the system cannot provide hardware acceleration, SDL2 will automatically fall back to a software renderer, letting the same code run everywhere.
Minimal working example
#include <SDL2/SDL.h>
#include <stdbool.h>
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return 1;
}
SDL_Window *win = SDL_CreateWindow("SDL2 Accelerated Rect",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_SHOWN);
if (!win) {
SDL_Log("Failed to create window: %s", SDL_GetError());
SDL_Quit();
return 1;
}
/* Request hardware acceleration and V-Sync */
SDL_Renderer *ren = SDL_CreateRenderer(win, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!ren) {
SDL_Log("Failed to create renderer: %s", SDL_GetError());
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
bool quit = false;
SDL_Event e;
while (!quit) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) quit = true;
}
/* Clear screen to dark gray */
SDL_SetRenderDrawColor(ren, 30, 30, 30, 255);
SDL_RenderClear(ren);
/* Draw a green rectangle */
SDL_Rect r = {100, 100, 200, 150};
SDL_SetRenderDrawColor(ren, 0, 200, 0, 255);
SDL_RenderFillRect(ren, &r);
/* Show the frame */
SDL_RenderPresent(ren);
}
/* Clean up in reverse order of creation */
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
How the example works
- Initialize the SDL video subsystem.
- Create a window with
SDL_CreateWindow. - Create a renderer with
SDL_CreateRenderer, asking forSDL_RENDERER_ACCELERATED(GPU path) andSDL_RENDERER_PRESENTVSYNCto cap the frame rate to the monitor's refresh. - In the main loop, clear the back buffer, set a draw colour, fill a rectangle, and present the buffer.
- When the loop ends, destroy the renderer first, then the window, and finally quit SDL.
Limits and common mistakes
- Not checking return values. If
SDL_CreateRendererfails (e.g., no compatible GPU driver), it returnsNULL. Using that pointer leads to crashes. Always test the return value and handle the fallback case. - Assuming acceleration is always present. On headless servers or machines with outdated drivers, SDL2 will silently fall back to
SDL_RENDERER_SOFTWARE. Performance may be lower and V-Sync may not work. Verify acceleration withSDL_GetRendererInfoand inspect theflagsfield forSDL_RENDERER_ACCELERATED. - Mixing renderer types. Creating both an accelerated and a software renderer for the same window and using them interchangeably can cause undefined behaviour. Stick to a single renderer per window.
- Incorrect destruction order. Destroying the window before the renderer or calling
SDL_Quitwhile resources still exist can leak memory or trigger crashes. Destroy in the reverse order of creation. - Forgetting to present. Drawing commands only affect the back buffer; without
SDL_RenderPresentnothing appears on screen.
Practical verification
Compile the example (e.g., gcc example.c -lSDL2 -o sdl_rect) and run it. A window should appear showing a green rectangle on a dark gray background. To confirm hardware acceleration, add after renderer creation:
SDL_RendererInfo info;
SDL_GetRendererInfo(ren, &info);
if (info.flags & SDL_RENDERER_ACCELERATED)
SDL_Log("Using hardware acceleration");
else
SDL_Log("Using software renderer");
On systems without a compatible GPU, the log will indicate the software path, and you may notice lower frame rates or missing V-Sync.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.