Introduces Cocoa_GetWindowDisplayIndex. This enable a proper management for dpi when switching between retina and non-retina displays.

This commit is contained in:
Mirko Galimberti 2022-04-25 10:35:56 +02:00 committed by Sam Lantinga
parent c39df2fb0c
commit 76afb8583b
5 changed files with 84 additions and 48 deletions

View File

@ -237,6 +237,7 @@ struct SDL_VideoDevice
int (*SetWindowGammaRamp) (_THIS, SDL_Window * window, const Uint16 * ramp); int (*SetWindowGammaRamp) (_THIS, SDL_Window * window, const Uint16 * ramp);
int (*GetWindowGammaRamp) (_THIS, SDL_Window * window, Uint16 * ramp); int (*GetWindowGammaRamp) (_THIS, SDL_Window * window, Uint16 * ramp);
void* (*GetWindowICCProfile) (_THIS, SDL_Window * window, size_t* size); void* (*GetWindowICCProfile) (_THIS, SDL_Window * window, size_t* size);
int (*GetWindowDisplayIndex)(_THIS, SDL_Window * window);
void (*SetWindowMouseRect)(_THIS, SDL_Window * window); void (*SetWindowMouseRect)(_THIS, SDL_Window * window);
void (*SetWindowMouseGrab) (_THIS, SDL_Window * window, SDL_bool grabbed); void (*SetWindowMouseGrab) (_THIS, SDL_Window * window, SDL_bool grabbed);
void (*SetWindowKeyboardGrab) (_THIS, SDL_Window * window, SDL_bool grabbed); void (*SetWindowKeyboardGrab) (_THIS, SDL_Window * window, SDL_bool grabbed);

View File

@ -1060,6 +1060,10 @@ SDL_GetDisplay(int displayIndex)
int int
SDL_GetWindowDisplayIndex(SDL_Window * window) SDL_GetWindowDisplayIndex(SDL_Window * window)
{ {
CHECK_WINDOW_MAGIC(window, -1);
if (_this->GetWindowDisplayIndex) {
return _this->GetWindowDisplayIndex(_this, window);
} else {
int displayIndex; int displayIndex;
int i, dist; int i, dist;
int closest = -1; int closest = -1;
@ -1068,8 +1072,6 @@ SDL_GetWindowDisplayIndex(SDL_Window * window)
SDL_Point delta; SDL_Point delta;
SDL_Rect rect; SDL_Rect rect;
CHECK_WINDOW_MAGIC(window, -1);
if (SDL_WINDOWPOS_ISUNDEFINED(window->x) || if (SDL_WINDOWPOS_ISUNDEFINED(window->x) ||
SDL_WINDOWPOS_ISCENTERED(window->x)) { SDL_WINDOWPOS_ISCENTERED(window->x)) {
displayIndex = (window->x & 0xFFFF); displayIndex = (window->x & 0xFFFF);
@ -1115,6 +1117,7 @@ SDL_GetWindowDisplayIndex(SDL_Window * window)
SDL_SetError("Couldn't find any displays"); SDL_SetError("Couldn't find any displays");
} }
return closest; return closest;
}
} }
SDL_VideoDisplay * SDL_VideoDisplay *

View File

@ -103,6 +103,7 @@ Cocoa_CreateDevice(int devindex)
device->SetWindowGammaRamp = Cocoa_SetWindowGammaRamp; device->SetWindowGammaRamp = Cocoa_SetWindowGammaRamp;
device->GetWindowGammaRamp = Cocoa_GetWindowGammaRamp; device->GetWindowGammaRamp = Cocoa_GetWindowGammaRamp;
device->GetWindowICCProfile = Cocoa_GetWindowICCProfile; device->GetWindowICCProfile = Cocoa_GetWindowICCProfile;
device->GetWindowDisplayIndex = Cocoa_GetWindowDisplayIndex;
device->SetWindowMouseRect = Cocoa_SetWindowMouseRect; device->SetWindowMouseRect = Cocoa_SetWindowMouseRect;
device->SetWindowMouseGrab = Cocoa_SetWindowMouseGrab; device->SetWindowMouseGrab = Cocoa_SetWindowMouseGrab;
device->SetWindowKeyboardGrab = Cocoa_SetWindowKeyboardGrab; device->SetWindowKeyboardGrab = Cocoa_SetWindowKeyboardGrab;

View File

@ -153,6 +153,7 @@ extern void Cocoa_SetWindowAlwaysOnTop(_THIS, SDL_Window * window, SDL_bool on_t
extern void Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen); extern void Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
extern int Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp); extern int Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp);
extern void* Cocoa_GetWindowICCProfile(_THIS, SDL_Window * window, size_t * size); extern void* Cocoa_GetWindowICCProfile(_THIS, SDL_Window * window, size_t * size);
extern int Cocoa_GetWindowDisplayIndex(_THIS, SDL_Window * window);
extern int Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp); extern int Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp);
extern void Cocoa_SetWindowMouseRect(_THIS, SDL_Window * window); extern void Cocoa_SetWindowMouseRect(_THIS, SDL_Window * window);
extern void Cocoa_SetWindowMouseGrab(_THIS, SDL_Window * window, SDL_bool grabbed); extern void Cocoa_SetWindowMouseGrab(_THIS, SDL_Window * window, SDL_bool grabbed);

View File

@ -2197,6 +2197,36 @@ Cocoa_GetWindowICCProfile(_THIS, SDL_Window * window, size_t * size)
return retIccProfileData; return retIccProfileData;
} }
int Cocoa_GetWindowDisplayIndex(_THIS, SDL_Window * window){
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
/* Not recognized via CHECK_WINDOW_MAGIC */
if (data == NULL){
return 0;
}
/*
Considering that we already have the display coordinates in which the window is placed (described via displayframe)
instead of checking in which display the window is placed, we should check which SDL display matches the display described
via displayframe.
*/
CGRect displayframe = data->nswindow.screen.frame;
SDL_Point display_center;
SDL_Rect sdl_display_rect;
display_center.x = displayframe.origin.x + displayframe.size.width / 2;
display_center.y = displayframe.origin.y + displayframe.size.height / 2;
for (int i = 0; i < SDL_GetNumVideoDisplays(); i++){
SDL_GetDisplayBounds(i, &sdl_display_rect);
if (SDL_EnclosePoints(&display_center, 1, &sdl_display_rect, NULL)) {
return i;
}
}
SDL_SetError("Couldn't find the display where the window is attached to.");
return -1;
}
int int
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp) Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
{ {