SDL_SetRenderLogicalPresentation
Lets you draw at a fixed size (e.g. 640×360) and have SDL scale it to whatever the window is.
Signature
bool SDL_SetRenderLogicalPresentation(SDL_Renderer *renderer, int w, int h, SDL_RendererLogicalPresentation mode);
How it works
Pixel-art games are designed for one small resolution. With a logical size, all your drawing coordinates stay in that resolution and SDL scales the result to the window, adding black bars if the proportions differ.
INTEGER_SCALE only scales by whole numbers (2×, 3×…), so every game pixel becomes the same number of screen pixels and nothing looks uneven.
Parameters
The renderer.
Logical width you draw in.
Logical height you draw in.
How to fit the logical size into the window.
Common values
- SDL_LOGICAL_PRESENTATION_LETTERBOX
- Keep proportions, black bars on the sides that do not fit.
- SDL_LOGICAL_PRESENTATION_INTEGER_SCALE
- Whole-number scale only, centered. Best for pixel art.
- SDL_LOGICAL_PRESENTATION_STRETCH
- Fill the window, distorting proportions.
- SDL_LOGICAL_PRESENTATION_OVERSCAN
- Fill the window, cutting off the parts that overflow.
- SDL_LOGICAL_PRESENTATION_DISABLED
- Turn it off: draw in real pixels again.
Return value
true on success, false on failure.
Tips & pitfalls
Mouse positions stay in window coordinates. Convert them with
SDL_RenderCoordinatesFromWindow.Combine with
SDL_SetTextureScaleMode(tex, SDL_SCALEMODE_NEAREST) so the pixels stay sharp.
Example
1SDL_SetRenderLogicalPresentation(renderer, 640, 360,
2 SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
3// from now on, (0,0)-(640,360) covers the game area in any window size