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

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Scaling Text in SDL
How can I make text scale properly with different DPI settings?
Image Scaling and DPI
If I'm loading images for my game, do I need different versions for different DPI settings?
DPI Scale Calculation
In GetDPIScale(), why do we only use the width to calculate the scale? What about height?
DPI with SDL_Renderer
The example uses SDL_Surface. Does DPI scaling work differently with SDL_Renderer or SDL_Texture?
UI Elements and DPI
How do I handle DPI scaling for UI elements like buttons that need to stay a specific physical size?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant