DPI Scale Calculation

In GetDPIScale(), why do we only use the width to calculate the scale? What about height?

The decision to use only width for DPI scale calculation is based on how modern displays and operating systems handle DPI scaling. Let's explore why this works and when you might need to consider both dimensions.

Why Width is Usually Sufficient

Most modern displays maintain consistent DPI scaling across both dimensions. Here's how we can verify this:

#include <SDL.h>
#include <iostream>

void CheckScaling(SDL_Window* Window) {
  int w1, h1, w2, h2;
  SDL_GetWindowSize(Window, &w1, &h1);
  SDL_GetWindowSizeInPixels(Window, &w2, &h2);

  float ScaleX{float(w2) / w1};
  float ScaleY{float(h2) / h1};

  std::cout << "Scale X: " << ScaleX << "\n"
            << "Scale Y: " << ScaleY << "\n";
}

int main() {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Window* Window{SDL_CreateWindow(
    "Scale Test",
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    800, 600, 0)};

  CheckScaling(Window);

  SDL_DestroyWindow(Window);
  SDL_Quit();
  return 0;
}

On a typical 125% DPI scale, you'll see:

Scale X: 1.25
Scale Y: 1.25

When to Consider Both Dimensions

However, there are cases where you might want to check both dimensions:

  • Non-square pixel displays (rare in modern systems)
  • Custom scaling settings
  • Platform-specific quirks

Here's an improved version of GetDPIScale():

struct Scale2D {
  float X, Y;
};

Scale2D GetDPIScale2D(SDL_Window* Window) {
  int w1, h1, w2, h2;
  SDL_GetWindowSize(Window, &w1, &h1);
  SDL_GetWindowSizeInPixels(Window, &w2, &h2);

  return Scale2D{
    float(w2) / w1,
    float(h2) / h1
  };
}

void HandleDifferentScales(SDL_Window* Window) {
  Scale2D Scale{GetDPIScale2D(Window)};
  if (std::abs(Scale.X - Scale.Y) > 0.01f) {
    std::cout << "Warning: Non-uniform "
      "scaling detected\n";
  }
}

In practice, using width alone is usually sufficient because:

  • Operating systems maintain uniform scaling
  • Different X/Y scales can cause visual problems
  • Most UI frameworks assume uniform scaling

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?
Why Handle DPI Scaling
Why do we need to handle DPI scaling at all? Can't we just let the operating system handle it?
Image Scaling and DPI
If I'm loading images for my game, do I need different versions for different DPI settings?
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