From 48dfbfb3048811e1e3fda62b6efa346c423069af Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Thu, 11 Apr 2024 10:55:33 +0200 Subject: [PATCH] dpi_aware: add roDisplayFramebufferScale --- src/hello_imgui/dpi_aware.h | 19 +++++++++++++++++-- .../backend_impls/abstract_runner.cpp | 15 +++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/hello_imgui/dpi_aware.h b/src/hello_imgui/dpi_aware.h index b517e3be..22e9be7e 100644 --- a/src/hello_imgui/dpi_aware.h +++ b/src/hello_imgui/dpi_aware.h @@ -38,8 +38,9 @@ namespace HelloImGui struct DpiAwareParams { // `dpiWindowSizeFactor` - // factor by which window size should be multiplied to get a similar - // visible size on different OSes. + // factor by which window size should be multiplied to get a similar + // visible size on different OSes. This affects the size of the window, + // but *not* the size of widgets and text. // In a standard environment (i.e. outside of Hello ImGui), an application with a size of 960x480 pixels, // may have a physical size (in mm or inches) that varies depending on the screen DPI, and the OS. // @@ -71,6 +72,20 @@ struct DpiAwareParams float r = dpiWindowSizeFactor / fontRenderingScale; return r; }; + + // `roDisplayFramebufferScale` + // When rendering, the internal frame buffer size might be bigger than the + // size (in "virtual screen coordinate" pixels) of the window. + // For example: + // - on macOS retina screens, the frame buffer size will be twice the window size + // (and roDisplayFramebufferScale will be 2, 2) + // - on Windows, the frame buffer size will be the same as the window size. + // (since virtual screen coordinate pixels are the same as physical screen pixels on Windows) + // + // This is an output-only value: it will be set by the platform backend + // after it was initialized (any change you make to it at startup will not be used) + // It mirrors the value inside ImGui::GetIO().DisplayFramebufferScale. + ImVec2 roDisplayFramebufferScale = ImVec2(0.f, 0.f); }; // ---------------------------------------------------------------------------- diff --git a/src/hello_imgui/internal/backend_impls/abstract_runner.cpp b/src/hello_imgui/internal/backend_impls/abstract_runner.cpp index 196541c0..168fc0b9 100644 --- a/src/hello_imgui/internal/backend_impls/abstract_runner.cpp +++ b/src/hello_imgui/internal/backend_impls/abstract_runner.cpp @@ -476,9 +476,19 @@ void AbstractRunner::SetupDpiAwareParams() params.dpiAwareParams.fontRenderingScale = 1.0f / fontSizeIncreaseFactor; } - ImGui::GetIO().FontGlobalScale = params.dpiAwareParams.fontRenderingScale; - ImGui::GetIO().DisplayFramebufferScale = mBackendWindowHelper->GetDisplayFramebufferScale(mWindow); + + // + // DisplayFramebufferScale + // + ImVec2 displayFramebufferScale = mBackendWindowHelper->GetDisplayFramebufferScale(mWindow); + // Note: ImGui_ImplGlfw_NewFrame, ImGui_ImplSDL2_NewFrame, ... will also set ImGui::GetIO().DisplayFramebufferScale + // (using their own backend abstraction). + // There is a slight chance of discrepancy here, However, GetDisplayFramebufferScale() and DearImGui + // do get the same results for SDL and GLFW under Windows Linux macOS, Android, iOS. + ImGui::GetIO().DisplayFramebufferScale = params.dpiAwareParams.roDisplayFramebufferScale; + // dpiAwareParams.roDisplayFramebufferScale is an output-only value (for information only) + params.dpiAwareParams.roDisplayFramebufferScale = displayFramebufferScale; } @@ -1101,6 +1111,7 @@ void AbstractRunner::CreateFramesAndRender() mRenderingBackendCallbacks->Impl_NewFrame_3D(); Impl_NewFrame_PlatformBackend(); + { // Workaround against SDL clock that sometimes leads to io.DeltaTime=0.f on emscripten // (which fails to an `IM_ASSERT(io.DeltaTime) > 0` in ImGui::NewFrame())