Skip to content

Commit

Permalink
Retina display fixes
Browse files Browse the repository at this point in the history
Changes are based on cmaughan's fixes from ocornut#3757 (comment) but messed with a bit to work on Xemu's ImGUI fork
  • Loading branch information
schm1dtmac authored Aug 7, 2024
1 parent fceff32 commit 04ab555
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion backends/imgui_impl_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,27 @@ static void ImGui_ImplSDL2_UpdateMouseData()
int window_x, window_y, mouse_x_global, mouse_y_global;
SDL_GetGlobalMouseState(&mouse_x_global, &mouse_y_global);
SDL_GetWindowPosition(bd->Window, &window_x, &window_y);
io.AddMousePosEvent((float)(mouse_x_global - window_x), (float)(mouse_y_global - window_y));
#ifdef __APPLE__
// Hack to fix Retina mouse coordinates on Mac
int w, h;
int display_w, display_h;
SDL_GetWindowSize(bd->Window, &w, &h);
if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_MINIMIZED)
w = h = 0;
if (bd->Renderer != nullptr)
SDL_GetRendererOutputSize(bd->Renderer, &display_w, &display_h);
else
SDL_GL_GetDrawableSize(bd->Window, &display_w, &display_h);

if (display_h != h)
{
io.AddMousePosEvent((float)2*(mouse_x_global - window_x), (float)2*(mouse_y_global - window_y));
}
else
#endif
{
io.AddMousePosEvent((float)(mouse_x_global - window_x), (float)(mouse_y_global - window_y));
}
}
}
}
Expand Down Expand Up @@ -618,6 +638,15 @@ void ImGui_ImplSDL2_NewFrame()
if (w > 0 && h > 0)
io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);

#if defined(__APPLE__)
// Hack to fix Retina scaling on Mac
if (display_h != h)
{
io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f);
io.DisplaySize = ImVec2((float)display_w, (float)display_h);
}
#endif

// Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
// (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)
static Uint64 frequency = SDL_GetPerformanceFrequency();
Expand Down

0 comments on commit 04ab555

Please sign in to comment.