Why Handle DPI Scaling
Why do we need to handle DPI scaling at all? Can't we just let the operating system handle it?
While the operating system can handle DPI scaling automatically, there are several important reasons why we should handle it ourselves:
Quality Loss with Automatic Scaling
When we let the operating system handle scaling, it's essentially taking our rendered output and stretching it to fit the screen. This is like taking a small image and making it bigger - it gets blurry. Here's a comparison:
#include <SDL.h>
#include <iostream>
void CompareScaling(SDL_Window* Window) {
SDL_Surface* Surface{
SDL_GetWindowSurface(Window)};
// Draw a small detailed pattern
for (int y = 0; y < 10; ++y) {
for (int x = 0; x < 10; ++x) {
SDL_Rect Pixel{x * 2, y * 2, 1, 1};
SDL_FillRect(Surface, &Pixel,
SDL_MapRGB(Surface->format,
(x + y) % 2 ? 255 : 0, // Red
0, // Green
0 // Blue
));
}
}
}
When the OS scales this pattern, the individual pixels become blurry. When we handle scaling ourselves, we can render at the native resolution, keeping everything sharp.
Control and Flexibility
By handling DPI scaling ourselves, we can:
- Choose what content scales and what doesn't
- Apply different scaling strategies to different elements
- Optimize performance by rendering at the correct resolution from the start
- Ensure consistent behavior across different platforms
Performance Considerations
OS-level scaling can be less efficient because:
- The entire window is scaled as a single unit
- Extra memory is needed for the scaling operation
- The GPU must perform additional work to upscale the content
By handling scaling ourselves, we can optimize each element individually and avoid unnecessary scaling operations.
Pixel Density and High-DPI Displays
Learn how to create SDL applications that look great on modern displays across different platforms