From de3d29e478ff57d6c2fd083a7838fe9b286503a6 Mon Sep 17 00:00:00 2001 From: Iliyas Jorio Date: Mon, 6 Feb 2023 19:21:46 +0100 Subject: [PATCH] HiDPI support --- src/SDLU.cpp | 29 +++++++++++++++++++++++++++++ src/main.cpp | 18 ++++++++++-------- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/SDLU.cpp b/src/SDLU.cpp index 51c69cb..e510375 100644 --- a/src/SDLU.cpp +++ b/src/SDLU.cpp @@ -423,6 +423,29 @@ MBoolean SDLU_CheckSDLTyping(SDL_Keycode* sdlKey) static MPoint SDLUi_TranslatePointFromWindowToFrontSurface(MPoint pt) { + // On macOS, the mouse position is relative to the window's "point size" on Retina screens. + int windowPointW = 1; + int windowPointH = 1; + int windowPixelW = 1; + int windowPixelH = 1; + + SDL_GetWindowSize(g_window, &windowPointW, &windowPointH); +#if SDL_VERSION_ATLEAST(2,26,0) + SDL_GetWindowSizeInPixels(g_window, &windowPixelW, &windowPixelH); +#else + // Backwards compat with old versions of SDL + windowPixelW = windowPointW; + windowPixelH = windowPointH; +#endif + + if (windowPointW != windowPixelW || windowPointH != windowPixelH) + { + float dpiScaleX = (float) windowPixelW / (float) windowPointW; // gGameWindowWidth is in actual pixels + float dpiScaleY = (float) windowPixelH / (float) windowPointH; // gGameWindowHeight is in actual pixels + pt.h *= dpiScaleX; + pt.v *= dpiScaleY; + } + SDL_Rect viewport; float scaleX, scaleY; SDL_RenderGetViewport(g_renderer, &viewport); @@ -431,6 +454,12 @@ static MPoint SDLUi_TranslatePointFromWindowToFrontSurface(MPoint pt) pt.h = pt.h / scaleX - viewport.x; pt.v = pt.v / scaleY - viewport.y; + if (widescreen) + { + pt.h += g_widescreenCrop.x; + pt.v += g_widescreenCrop.y; + } + return pt; } diff --git a/src/main.cpp b/src/main.cpp index 348e272..4833725 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -310,7 +310,7 @@ static char candyCrisisResources[512]; MBoolean fullscreen = false; MBoolean widescreen = false; MBoolean useNewTitle = widescreen; -MBoolean crispUpscaling = true; +MBoolean crispUpscaling = false; int main(int argc, char *argv[]) { @@ -564,14 +564,16 @@ void ReserveMonitor( void ) int resW = 640; int resH = widescreen? 360: 480; - SDL_Rect displayBounds = { .x = 0, .y = 0, .w = 640, .h = 480 }; - SDL_GetDisplayUsableBounds(0, &displayBounds); - int scaleX = (displayBounds.w * 95/100) / resW; // allow covering at most 95% of the screen - int scaleY = (displayBounds.h * 95/100) / resH; - int scale = scaleX < scaleY ? scaleX : scaleY; - scale = scale < 1 ? 1 : scale; + SDL_Rect displayBounds = { .x = 0, .y = 0, .w = 640, .h = 480 }; + SDL_GetDisplayUsableBounds(0, &displayBounds); + float scaleX = (displayBounds.w * 75/100) / (float)resW; // allow covering at most 95% of the screen + float scaleY = (displayBounds.h * 75/100) / (float)resH; + float scale = scaleX < scaleY ? scaleX : scaleY; + if (crispUpscaling) + scale = (int) scale; + scale = scale < 1 ? 1 : scale; - SDL_CreateWindowAndRenderer(resW* scale, resH* scale, SDL_WINDOW_RESIZABLE, &g_window, &g_renderer); + SDL_CreateWindowAndRenderer(resW*scale, resH*scale, SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI, &g_window, &g_renderer); SDL_RenderSetLogicalSize(g_renderer, resW, resH); SDL_SetWindowTitle(g_window, "Candy Crisis");