Skip to content

Commit

Permalink
HiDPI support
Browse files Browse the repository at this point in the history
  • Loading branch information
jorio committed Feb 6, 2023
1 parent fbb518d commit de3d29e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
29 changes: 29 additions & 0 deletions src/SDLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
18 changes: 10 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
{
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit de3d29e

Please sign in to comment.