Open tokyovigilante opened 9 months ago
// Windows DPI scaling
float GetScreenScale()
{
const HDC screen = GetDC(nullptr);
const int dpiX = GetDeviceCaps(screen, LOGPIXELSX);
ReleaseDC(nullptr, screen);
return static_cast<float>(dpiX) / 96.0f;
}
io.Fonts->AddFontFromFileTTF("Add your fonts", 16.0f * GetScreenScale());
Currently under Windows, the display is normal
Linux can use X11 to get the DPI scaling of the system
EDIT: just found https://github.com/ocornut/imgui/issues/6065#issuecomment-1410485983 and it works now correctly on my macOS machine!
Unfortunately the idea here seems to not work on my macOS machine. The result ist the following strangely garbled output:
Code here: https://gist.github.com/bhundt/a142d3dc4368c55ba1e707e6e34120fa
Version/Branch of Dear ImGui:
master
Back-ends:
imgui_impl_sdl3.cpp + imgui_impl_opengl3.cpp
Compiler, OS:
Fedora 40 + Wayland
Full config/build information:
Details:
This is half-bug, half-exploration, but I have been experimenting with SDL3 HIDPI scaling, and note that the approach in SDL3 (compared to SDL2) is that all metrics are computed based on a "logical" (ie apparent or window) resolution, with the backing store (ie pixel) resolution supplied as a drawable, and the difference between representing a scale factor.
https://github.com/libsdl-org/SDL/blob/main/docs/README-highdpi.md
The current SDL3 implementation in Dear IMGUI doesn't seem to respect this, and when an SDL3 window is created using the SDL_WINDOW_HIGH_PIXEL_DENSITY flag, this is the result:
Not beautiful. However, overriding the detected scale and setting a window scale of 1.0 and the drawable size to the window pixel size as follows:
and then scaling fonts and the style scale to the scale factor:
Beautiful. The only other change needed seems to be scaling mouse events.
Acknowledging there are many ways to skin the DPI cat, SDL3's approach has unified the platform differences (i.e represents Mac/Linux and (presumably) Windows HIDPI windows the same way, and is a sensible approach which doesn't need over-rendering for fractional scaling (ie rendering at 2.0x and downscaling to 1.5x to display). It is also non-breaking for LODPI, ie 1.0x scale factor just renders at 1.0.
I would humbly propose that this is a good and pragmatic model for IMGUI in general, ie working in logical display units, then scaling by a factor up to pixel size when rendering.
(Examples using Source Sans / Source Sans Bold @ 16pt and Freetype)