From 9da3e6696abce7f294d0d4f3bfb1db6ce80c6265 Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 6 Jun 2025 15:54:25 +0200 Subject: [PATCH] Backends: SDL2: added ImGui_ImplSDL2_GetDpiScaleForDisplay(), ImGui_ImplSDL2_GetContentScaleForWindow() helpers. --- backends/imgui_impl_sdl2.cpp | 21 +++++++++++++++++++++ backends/imgui_impl_sdl2.h | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/backends/imgui_impl_sdl2.cpp b/backends/imgui_impl_sdl2.cpp index fa2714bb9..3780b322b 100644 --- a/backends/imgui_impl_sdl2.cpp +++ b/backends/imgui_impl_sdl2.cpp @@ -21,6 +21,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2025-06-11: Added ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window) and ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index) helper to facilitate making DPI-aware apps. // 2025-04-09: Don't attempt to call SDL_CaptureMouse() on drivers where we don't call SDL_GetGlobalMouseState(). (#8561) // 2025-03-21: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad regardless of ImGuiConfigFlags_NavEnableGamepad being set. // 2025-03-10: When dealing with OEM keys, use scancodes instead of translated keycodes to choose ImGuiKey values. (#7136, #7201, #7206, #7306, #7670, #7672, #8468) @@ -702,6 +703,26 @@ static void ImGui_ImplSDL2_UpdateMouseCursor() } } +// - On Windows the process needs to be marked DPI-aware!! SDL2 doesn't do it by default. You can call ::SetProcessDPIAware() or call ImGui_ImplWin32_EnableDpiAwareness() from Win32 backend. +// - Apple platforms use FramebufferScale so we always return 1.0f. +// - Some accessibility applications are declaring virtual monitors with a DPI of 0.0f, see #7902. We preserve this value for caller to handle. +float ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window) +{ + return ImGui_ImplSDL2_GetContentScaleForDisplay(SDL_GetWindowDisplayIndex(window)); +} + +float ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index) +{ +#if SDL_HAS_PER_MONITOR_DPI +#ifndef __APPLE__ + float dpi = 0.0f; + if (SDL_GetDisplayDPI(display_index, &dpi, nullptr, nullptr) == 0) + return dpi / 96.0f; +#endif +#endif + return 1.0f; +} + static void ImGui_ImplSDL2_CloseGamepads() { ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); diff --git a/backends/imgui_impl_sdl2.h b/backends/imgui_impl_sdl2.h index 9f7b551fc..3c0a4a7e4 100644 --- a/backends/imgui_impl_sdl2.h +++ b/backends/imgui_impl_sdl2.h @@ -38,6 +38,10 @@ IMGUI_IMPL_API void ImGui_ImplSDL2_Shutdown(); IMGUI_IMPL_API void ImGui_ImplSDL2_NewFrame(); IMGUI_IMPL_API bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event); +// DPI-related helpers (optional) +IMGUI_IMPL_API float ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window); +IMGUI_IMPL_API float ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index); + // Gamepad selection automatically starts in AutoFirst mode, picking first available SDL_Gamepad. You may override this. // When using manual mode, caller is responsible for opening/closing gamepad. enum ImGui_ImplSDL2_GamepadMode { ImGui_ImplSDL2_GamepadMode_AutoFirst, ImGui_ImplSDL2_GamepadMode_AutoAll, ImGui_ImplSDL2_GamepadMode_Manual };