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.
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
However, there are cases where you might want to check both dimensions:
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:
Answers to questions are automatically generated and may not have been reviewed.
Learn how to create SDL applications that look great on modern displays across different platforms