Minimal Hardware‑Accelerated 2D Window with SDL_CreateWindowAndRenderer
Learn how to initialise SDL2 video, create a resizable hardware‑accelerated window, and handle failures with SDL_GetError() while keeping the setup minimal.
02 Jul 2026, 06:39 UTC

Requirements
Before any SDL drawing can happen the video subsystem must be initialised and a window‑renderer pair created. The goal is to obtain a visible, resizable surface that uses GPU acceleration when available, while keeping the initialisation surface as small as possible.
Smallest Suitable Design
Initialise only the video subsystem, request a window that is shown and resizable, and ask for an accelerated renderer. If the accelerated renderer cannot be created, fall back to the default software renderer.
#include
int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
return 1;
}
SDL_Window *win = NULL;
SDL_Renderer *ren = NULL;
if (SDL_CreateWindowAndRenderer(640, 480,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE,
&win, &ren) != 0) {
SDL_Quit();
return 1;
}
int running = 1;
while (running) {
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) running = 0;
}
SDL_SetRenderDrawColor(ren, 100, 149, 237, 255);
SDL_RenderClear(ren);
SDL_RenderPresent(ren);
}
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
Trust and Data Boundaries
The SDL initialisation call is a trust boundary: it either succeeds and returns zero, or fails and leaves SDL internal state unchanged. The application should not assume any resources have been allocated on failure; checking the return value and calling SDL_GetError() isolates SDL from higher‑level logic.
Operational Checks
After a successful SDL_CreateWindowAndRenderer call, query the renderer info to confirm acceleration:
SDL_RendererInfo info;
SDL_GetRendererInfo(ren, &info);
if (info.flags & SDL_RENDERER_ACCELERATED) {
/* hardware path */
} else {
/* software fallback */
}
Failure Modes
- SDL_Init returns non‑zero – missing video driver or unsupported platform.
- SDL_CreateWindowAndRenderer returns non‑zero – window system cannot provide the requested pixel format or hardware acceleration is unavailable.
- SDL_GetRendererInfo shows lack of SDL_RENDERER_ACCELERATED flag – the driver fell back to software.
Each case can be diagnosed with SDL_GetError() and handled by logging, attempting a software renderer, or exiting gracefully.
When to Redesign
If the application needs multiple windows, separate rendering contexts, or explicit control over the swap interval (vsync), move from the single‑window helper to explicit SDL_CreateWindow and SDL_CreateRenderer calls. Likewise, when rendering complex scenes that require off‑screen textures or asynchronous texture updates, introduce streaming textures (SDL_TEXTUREACCESS_STREAMING) to avoid stalls from per‑frame texture creation.