Pixel Density and High-DPI Displays

Why Handle DPI Scaling

Why do we need to handle DPI scaling at all? Can't we just let the operating system handle it?

Abstract art representing computer programming

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.

Answers to questions are automatically generated and may not have been reviewed.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 71 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved