Skip to content

Commit

Permalink
Add function to query window content scale
Browse files Browse the repository at this point in the history
Analogous to glfwGetWindowContentScale().

See comments on libsdl-org#5778
  • Loading branch information
PJB3005 committed Nov 10, 2022
1 parent 7c05ea0 commit 9638800
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 0 deletions.
30 changes: 30 additions & 0 deletions include/SDL_video.h
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,36 @@ extern DECLSPEC void SDLCALL SDL_SetWindowMaximumSize(SDL_Window * window,
extern DECLSPEC void SDLCALL SDL_GetWindowMaximumSize(SDL_Window * window,
int *w, int *h);

/**
* Get the OS-provided scale factor to apply to the contents of the window.
*
* The content scale is how much larger the OS wants us to draw elements
* like text to match the user's preferences
* (usually based on DPI of the monitor).
*
* SDL_GetWindowSizeInPixels() divided by SDL_GetWindowSize() is approximately
* equivalent to these values, differing slightly due to rounding behaviors.
*
* This value can change if the user changes the scaling settings on the OS,
* or if they have multiple monitors with different scaling settings and drag
* the window between them.
* The `SDL_WINDOWEVENT_SIZE_CHANGED` window event is raised when this happens.
*
* \param window the window to query
* \param h a pointer filled in with the horizontal content scale of the
* window, may be NULL
* \param v a pointer filled in with the vertical content scale of the
* window, may be NULL
*
* \since This function is available since SDL 2.25.0.
*
* \sa SDL_GetWindowSizeInPixels
* \sa SDL_GetWindowSize
*/
extern DECLSPEC void SDLCALL SDL_GetWindowContentScale(SDL_Window * window,
float * h, float * v);


/**
* Set the border state of a window.
*
Expand Down
1 change: 1 addition & 0 deletions src/dynapi/SDL2.exports
Original file line number Diff line number Diff line change
Expand Up @@ -867,3 +867,4 @@
++'_SDL_SensorGetDataWithTimestamp'.'SDL2.dll'.'SDL_SensorGetDataWithTimestamp'
++'_SDL_ResetHints'.'SDL2.dll'.'SDL_ResetHints'
++'_SDL_strcasestr'.'SDL2.dll'.'SDL_strcasestr'
++'_SDL_GetWindowContentScale'.'SDL2.dll'.'SDL_GetWindowContentScale'
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -893,3 +893,4 @@
#define SDL_SensorGetDataWithTimestamp SDL_SensorGetDataWithTimestamp_REAL
#define SDL_ResetHints SDL_ResetHints_REAL
#define SDL_strcasestr SDL_strcasestr_REAL
#define SDL_GetWindowContentScale SDL_GetWindowContentScale_REAL
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -976,3 +976,4 @@ SDL_DYNAPI_PROC(int,SDL_GameControllerGetSensorDataWithTimestamp,(SDL_GameContro
SDL_DYNAPI_PROC(int,SDL_SensorGetDataWithTimestamp,(SDL_Sensor *a, Uint64 *b, float *c, int d),(a,b,c,d),return)
SDL_DYNAPI_PROC(void,SDL_ResetHints,(void),(),)
SDL_DYNAPI_PROC(char*,SDL_strcasestr,(const char *a, const char *b),(a,b),return)
SDL_DYNAPI_PROC(void,SDL_GetWindowContentScale,(SDL_Window *a, float *b, float *c),(a,b,c),)
1 change: 1 addition & 0 deletions src/video/SDL_sysvideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ struct SDL_VideoDevice
void (*SetWindowMaximumSize) (_THIS, SDL_Window * window);
int (*GetWindowBordersSize) (_THIS, SDL_Window * window, int *top, int *left, int *bottom, int *right);
void (*GetWindowSizeInPixels)(_THIS, SDL_Window *window, int *w, int *h);
void (*GetWindowContentScale)(_THIS, SDL_Window *window, float *h, float *v);
int (*SetWindowOpacity) (_THIS, SDL_Window * window, float opacity);
int (*SetWindowModalFor) (_THIS, SDL_Window * modal_window, SDL_Window * parent_window);
int (*SetWindowInputFocus) (_THIS, SDL_Window * window);
Expand Down
21 changes: 21 additions & 0 deletions src/video/SDL_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,27 @@ SDL_GetWindowMaximumSize(SDL_Window * window, int *max_w, int *max_h)
}
}

void
SDL_GetWindowContentScale(SDL_Window *window, float *h, float *v)
{
CHECK_WINDOW_MAGIC(window,);

if (_this->GetWindowContentScale) {
_this->GetWindowContentScale(_this, window, h, v);
} else {
int width, height, pixWidth, pixHeight;

SDL_GetWindowSize(window, &width, &height);
SDL_GetWindowSizeInPixels(window, &pixWidth, &pixHeight);

if (h)
*h = pixWidth / (float) width;

if (v)
*v = pixHeight / (float) height;
}
}

void
SDL_ShowWindow(SDL_Window * window)
{
Expand Down
1 change: 1 addition & 0 deletions src/video/windows/SDL_windowsvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ WIN_CreateDevice(void)
device->SetWindowHitTest = WIN_SetWindowHitTest;
device->AcceptDragAndDrop = WIN_AcceptDragAndDrop;
device->FlashWindow = WIN_FlashWindow;
device->GetWindowContentScale = WIN_GetWindowContentScale;

device->shape_driver.CreateShaper = Win32_CreateShaper;
device->shape_driver.SetWindowShape = Win32_SetWindowShape;
Expand Down
13 changes: 13 additions & 0 deletions src/video/windows/SDL_windowswindow.c
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,19 @@ WIN_FlashWindow(_THIS, SDL_Window * window, SDL_FlashOperation operation)
}
#endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/

void
WIN_GetWindowContentScale(_THIS, SDL_Window *window, float *h, float *v)
{
const SDL_WindowData *data = ((SDL_WindowData *) window->driverdata);

float scale = data->scaling_dpi / 96.0f;

if (h)
*h = scale;
if (v)
*v = scale;
}

#endif /* SDL_VIDEO_DRIVER_WINDOWS */

/* vi: set ts=4 sw=4 expandtab: */
1 change: 1 addition & 0 deletions src/video/windows/SDL_windowswindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ extern void WIN_ClientPointToSDL(const SDL_Window *window, int *w, int *h);
extern void WIN_ClientPointFromSDL(const SDL_Window *window, int *w, int *h);
extern void WIN_AcceptDragAndDrop(SDL_Window * window, SDL_bool accept);
extern int WIN_FlashWindow(_THIS, SDL_Window * window, SDL_FlashOperation operation);
extern void WIN_GetWindowContentScale(_THIS, SDL_Window *window, float *h, float *v);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
Expand Down

0 comments on commit 9638800

Please sign in to comment.