Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding SDL_GetWindowSizeInPixels for window size in pixels #6112

Merged
merged 15 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions include/SDL_video.h
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,27 @@ extern DECLSPEC int SDLCALL SDL_GetWindowBordersSize(SDL_Window * window,
int *top, int *left,
int *bottom, int *right);

/**
* Get the size of a window in pixels.
*
* This may differ from SDL_GetWindowSize() if we're rendering to a high-DPI
* drawable, i.e. the window was created with `SDL_WINDOW_ALLOW_HIGHDPI` on a
* platform with high-DPI support (Apple calls this "Retina"), and not
* disabled by the `SDL_HINT_VIDEO_HIGHDPI_DISABLED` hint.
*
* \param window the window from which the drawable size should be queried
* \param w a pointer to variable for storing the width in pixels, may be NULL
* \param h a pointer to variable for storing the height in pixels, may be
* NULL
*
* \since This function is available since SDL 2.26.0.
*
* \sa SDL_CreateWindow
* \sa SDL_GetWindowSize
*/
extern DECLSPEC void SDLCALL SDL_GetWindowSizeInPixels(SDL_Window * window,
int *w, int *h);

/**
* Set the minimum size of a window's client area.
*
Expand Down
1 change: 1 addition & 0 deletions src/dynapi/SDL2.exports
Original file line number Diff line number Diff line change
Expand Up @@ -858,3 +858,4 @@
++'_SDL_GetRectDisplayIndex'.'SDL2.dll'.'SDL_GetRectDisplayIndex'
++'_SDL_ResetHint'.'SDL2.dll'.'SDL_ResetHint'
++'_SDL_crc16'.'SDL2.dll'.'SDL_crc16'
++'_SDL_GetWindowSizeInPixels'.'SDL2.dll'.'SDL_GetWindowSizeInPixels'
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -884,3 +884,4 @@
#define SDL_GetRectDisplayIndex SDL_GetRectDisplayIndex_REAL
#define SDL_ResetHint SDL_ResetHint_REAL
#define SDL_crc16 SDL_crc16_REAL
#define SDL_GetWindowSizeInPixels SDL_GetWindowSizeInPixels_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 @@ -967,3 +967,4 @@ SDL_DYNAPI_PROC(int,SDL_GetPointDisplayIndex,(const SDL_Point *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GetRectDisplayIndex,(const SDL_Rect *a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_ResetHint,(const char *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_crc16,(Uint16 a, const void *b, size_t c),(a,b,c),return)
SDL_DYNAPI_PROC(void,SDL_GetWindowSizeInPixels,(SDL_Window *a, int *b, int *c),(a,b,c),)
6 changes: 3 additions & 3 deletions src/render/direct3d/SDL_render_d3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ D3D_ActivateRenderer(SDL_Renderer * renderer)
int w, h;
Uint32 window_flags = SDL_GetWindowFlags(window);

WIN_GetDrawableSize(window, &w, &h);
SDL_GetWindowSizeInPixels(window, &w, &h);
data->pparams.BackBufferWidth = w;
data->pparams.BackBufferHeight = h;
if (window_flags & SDL_WINDOW_FULLSCREEN && (window_flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != SDL_WINDOW_FULLSCREEN_DESKTOP) {
Expand Down Expand Up @@ -357,7 +357,7 @@ D3D_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
static int
D3D_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
{
WIN_GetDrawableSize(renderer->window, w, h);
SDL_GetWindowSizeInPixels(renderer->window, w, h);
return 0;
}

Expand Down Expand Up @@ -1653,7 +1653,7 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
SDL_GetWindowWMInfo(window, &windowinfo);

window_flags = SDL_GetWindowFlags(window);
WIN_GetDrawableSize(window, &w, &h);
SDL_GetWindowSizeInPixels(window, &w, &h);
SDL_GetWindowDisplayMode(window, &fullscreen_mode);

SDL_zero(pparams);
Expand Down
4 changes: 2 additions & 2 deletions src/render/direct3d11/SDL_render_d3d11.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
#if defined(__WINRT__)
SDL_GetWindowSize(renderer->window, &w, &h);
#else
WIN_GetDrawableSize(renderer->window, &w, &h);
SDL_GetWindowSizeInPixels(renderer->window, &w, &h);
#endif
data->rotation = D3D11_GetCurrentRotation();
/* SDL_Log("%s: windowSize={%d,%d}, orientation=%d\n", __FUNCTION__, w, h, (int)data->rotation); */
Expand Down Expand Up @@ -1062,7 +1062,7 @@ D3D11_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
static int
D3D11_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
{
WIN_GetDrawableSize(renderer->window, w, h);
SDL_GetWindowSizeInPixels(renderer->window, w, h);
return 0;
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/render/direct3d12/SDL_render_d3d12.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ D3D12_DestroyRenderer(SDL_Renderer * renderer)
static int
D3D12_GetOutputSize(SDL_Renderer *renderer, int *w, int *h)
{
WIN_GetDrawableSize(renderer->window, w, h);
SDL_GetWindowSizeInPixels(renderer->window, w, h);
return 0;
}

Expand Down Expand Up @@ -1267,7 +1267,7 @@ D3D12_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
/* The width and height of the swap chain must be based on the display's
* non-rotated size.
*/
WIN_GetDrawableSize(renderer->window, &w, &h);
SDL_GetWindowSizeInPixels(renderer->window, &w, &h);
data->rotation = D3D12_GetCurrentRotation();
if (D3D12_IsDisplayRotated90Degrees(data->rotation)) {
int tmp = w;
Expand Down
1 change: 1 addition & 0 deletions src/video/SDL_sysvideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ struct SDL_VideoDevice
void (*SetWindowMinimumSize) (_THIS, SDL_Window * window);
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);
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
28 changes: 25 additions & 3 deletions src/video/SDL_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,28 @@ SDL_GetWindowBordersSize(SDL_Window * window, int *top, int *left, int *bottom,
return _this->GetWindowBordersSize(_this, window, top, left, bottom, right);
}

void
SDL_GetWindowSizeInPixels(SDL_Window *window, int *w, int *h)
{
int filter;

CHECK_WINDOW_MAGIC(window,);

if (w == NULL) {
w = &filter;
}

if (h == NULL) {
h = &filter;
}

if (_this->GetWindowSizeInPixels) {
_this->GetWindowSizeInPixels(_this, window, w, h);
} else {
SDL_GetWindowSize(window, w, h);
}
}

void
SDL_SetWindowMinimumSize(SDL_Window * window, int min_w, int min_h)
{
Expand Down Expand Up @@ -4096,7 +4118,7 @@ void SDL_GL_GetDrawableSize(SDL_Window * window, int *w, int *h)
if (_this->GL_GetDrawableSize) {
_this->GL_GetDrawableSize(_this, window, w, h);
} else {
SDL_GetWindowSize(window, w, h);
SDL_GetWindowSizeInPixels(window, w, h);
}
}

Expand Down Expand Up @@ -4791,7 +4813,7 @@ void SDL_Vulkan_GetDrawableSize(SDL_Window * window, int *w, int *h)
if (_this->Vulkan_GetDrawableSize) {
_this->Vulkan_GetDrawableSize(_this, window, w, h);
} else {
SDL_GetWindowSize(window, w, h);
SDL_GetWindowSizeInPixels(window, w, h);
}
}

Expand Down Expand Up @@ -4844,7 +4866,7 @@ void SDL_Metal_GetDrawableSize(SDL_Window * window, int *w, int *h)
if (_this->Metal_GetDrawableSize) {
_this->Metal_GetDrawableSize(_this, window, w, h);
} else {
SDL_GetWindowSize(window, w, h);
SDL_GetWindowSizeInPixels(window, w, h);
}
}

Expand Down
12 changes: 1 addition & 11 deletions src/video/cocoa/SDL_cocoametalview.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,7 @@ - (NSView *)hitTest:(NSPoint)point {
}
} else {
/* Fall back to the viewport size. */
NSRect viewport = [contentView bounds];
if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
/* This gives us the correct viewport for a Retina-enabled view. */
viewport = [contentView convertRectToBacking:viewport];
}
if (w) {
*w = viewport.size.width;
}
if (h) {
*h = viewport.size.height;
}
SDL_GetWindowSizeInPixels(window, w, h);
}
}}

Expand Down
2 changes: 0 additions & 2 deletions src/video/cocoa/SDL_cocoaopengl.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ extern void Cocoa_GL_UnloadLibrary(_THIS);
extern SDL_GLContext Cocoa_GL_CreateContext(_THIS, SDL_Window * window);
extern int Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window,
SDL_GLContext context);
extern void Cocoa_GL_GetDrawableSize(_THIS, SDL_Window * window,
int * w, int * h);
extern int Cocoa_GL_SetSwapInterval(_THIS, int interval);
extern int Cocoa_GL_GetSwapInterval(_THIS);
extern int Cocoa_GL_SwapWindow(_THIS, SDL_Window * window);
Expand Down
22 changes: 0 additions & 22 deletions src/video/cocoa/SDL_cocoaopengl.m
Original file line number Diff line number Diff line change
Expand Up @@ -385,28 +385,6 @@ - (void)dealloc
return 0;
}}

void
Cocoa_GL_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
{ @autoreleasepool
{
SDL_WindowData *windata = (__bridge SDL_WindowData *) window->driverdata;
NSView *contentView = windata.sdlContentView;
NSRect viewport = [contentView bounds];

if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
/* This gives us the correct viewport for a Retina-enabled view. */
viewport = [contentView convertRectToBacking:viewport];
}

if (w) {
*w = viewport.size.width;
}

if (h) {
*h = viewport.size.height;
}
}}

int
Cocoa_GL_SetSwapInterval(_THIS, int interval)
{ @autoreleasepool
Expand Down
2 changes: 1 addition & 1 deletion src/video/cocoa/SDL_cocoavideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ @implementation SDL_VideoData
device->SetWindowMinimumSize = Cocoa_SetWindowMinimumSize;
device->SetWindowMaximumSize = Cocoa_SetWindowMaximumSize;
device->SetWindowOpacity = Cocoa_SetWindowOpacity;
device->GetWindowSizeInPixels = Cocoa_GetWindowSizeInPixels;
device->ShowWindow = Cocoa_ShowWindow;
device->HideWindow = Cocoa_HideWindow;
device->RaiseWindow = Cocoa_RaiseWindow;
Expand Down Expand Up @@ -133,7 +134,6 @@ @implementation SDL_VideoData
device->GL_UnloadLibrary = Cocoa_GL_UnloadLibrary;
device->GL_CreateContext = Cocoa_GL_CreateContext;
device->GL_MakeCurrent = Cocoa_GL_MakeCurrent;
device->GL_GetDrawableSize = Cocoa_GL_GetDrawableSize;
device->GL_SetSwapInterval = Cocoa_GL_SetSwapInterval;
device->GL_GetSwapInterval = Cocoa_GL_GetSwapInterval;
device->GL_SwapWindow = Cocoa_GL_SwapWindow;
Expand Down
1 change: 1 addition & 0 deletions src/video/cocoa/SDL_cocoawindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ extern void Cocoa_SetWindowPosition(_THIS, SDL_Window * window);
extern void Cocoa_SetWindowSize(_THIS, SDL_Window * window);
extern void Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window);
extern void Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window);
extern void Cocoa_GetWindowSizeInPixels(_THIS, SDL_Window * window, int *w, int *h);
extern int Cocoa_SetWindowOpacity(_THIS, SDL_Window * window, float opacity);
extern void Cocoa_ShowWindow(_THIS, SDL_Window * window);
extern void Cocoa_HideWindow(_THIS, SDL_Window * window);
Expand Down
18 changes: 18 additions & 0 deletions src/video/cocoa/SDL_cocoawindow.m
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,24 @@ - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
[windata.nswindow setContentMaxSize:maxSize];
}}

void
Cocoa_GetWindowSizeInPixels(_THIS, SDL_Window * window, int *w, int *h)
{ @autoreleasepool
{
SDL_WindowData *windata = (__bridge SDL_WindowData *) window->driverdata;
NSView *contentView = windata.sdlContentView;
NSRect viewport = [contentView bounds];

if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
/* This gives us the correct viewport for a Retina-enabled view. */
viewport = [contentView convertRectToBacking:viewport];
}

*w = viewport.size.width;
*h = viewport.size.height;
}}


void
Cocoa_ShowWindow(_THIS, SDL_Window * window)
{ @autoreleasepool
Expand Down
17 changes: 0 additions & 17 deletions src/video/emscripten/SDL_emscriptenopengles.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,6 @@ Emscripten_GLES_SwapWindow(_THIS, SDL_Window * window)
return ret;
}

void
Emscripten_GLES_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
{
SDL_WindowData *data;
if (window->driverdata) {
data = (SDL_WindowData *) window->driverdata;

if (w) {
*w = window->w * data->pixel_ratio;
}

if (h) {
*h = window->h * data->pixel_ratio;
}
}
}

#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN && SDL_VIDEO_OPENGL_EGL */

/* vi: set ts=4 sw=4 expandtab: */
Expand Down
1 change: 0 additions & 1 deletion src/video/emscripten/SDL_emscriptenopengles.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ extern int Emscripten_GLES_LoadLibrary(_THIS, const char *path);
extern SDL_GLContext Emscripten_GLES_CreateContext(_THIS, SDL_Window * window);
extern int Emscripten_GLES_SwapWindow(_THIS, SDL_Window * window);
extern int Emscripten_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context);
extern void Emscripten_GLES_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h);

#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN && SDL_VIDEO_OPENGL_EGL */

Expand Down
15 changes: 14 additions & 1 deletion src/video/emscripten/SDL_emscriptenvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ static int Emscripten_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * d

static int Emscripten_CreateWindow(_THIS, SDL_Window * window);
static void Emscripten_SetWindowSize(_THIS, SDL_Window * window);
static void Emscripten_GetWindowSizeInPixels(_THIS, SDL_Window * window, int *w, int *h);
static void Emscripten_DestroyWindow(_THIS, SDL_Window * window);
static void Emscripten_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
static void Emscripten_PumpEvents(_THIS);
Expand Down Expand Up @@ -101,6 +102,7 @@ Emscripten_CreateDevice(void)
device->MinimizeWindow = Emscripten_MinimizeWindow;
device->RestoreWindow = Emscripten_RestoreWindow;
device->SetWindowMouseGrab = Emscripten_SetWindowMouseGrab;*/
device->GetWindowSizeInPixels = Emscripten_GetWindowSizeInPixels;
device->DestroyWindow = Emscripten_DestroyWindow;
device->SetWindowFullscreen = Emscripten_SetWindowFullscreen;

Expand All @@ -118,7 +120,6 @@ Emscripten_CreateDevice(void)
device->GL_GetSwapInterval = Emscripten_GLES_GetSwapInterval;
device->GL_SwapWindow = Emscripten_GLES_SwapWindow;
device->GL_DeleteContext = Emscripten_GLES_DeleteContext;
device->GL_GetDrawableSize = Emscripten_GLES_GetDrawableSize;
#endif

device->free = Emscripten_DeleteDevice;
Expand Down Expand Up @@ -307,6 +308,18 @@ static void Emscripten_SetWindowSize(_THIS, SDL_Window * window)
}
}


static void
Emscripten_GetWindowSizeInPixels(_THIS, SDL_Window * window, int *w, int *h)
{
SDL_WindowData *data;
if (window->driverdata) {
data = (SDL_WindowData *) window->driverdata;
*w = window->w * data->pixel_ratio;
*h = window->h * data->pixel_ratio;
}
}

static void
Emscripten_DestroyWindow(_THIS, SDL_Window * window)
{
Expand Down
17 changes: 0 additions & 17 deletions src/video/wayland/SDL_waylandopengles.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,23 +197,6 @@ Wayland_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
return ret;
}

void
Wayland_GLES_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
{
SDL_WindowData *data;
if (window->driverdata) {
data = (SDL_WindowData *) window->driverdata;

if (w) {
*w = data->drawable_width;
}

if (h) {
*h = data->drawable_height;
}
}
}

void
Wayland_GLES_DeleteContext(_THIS, SDL_GLContext context)
{
Expand Down
1 change: 0 additions & 1 deletion src/video/wayland/SDL_waylandopengles.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ extern int Wayland_GLES_SetSwapInterval(_THIS, int interval);
extern int Wayland_GLES_GetSwapInterval(_THIS);
extern int Wayland_GLES_SwapWindow(_THIS, SDL_Window * window);
extern int Wayland_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context);
extern void Wayland_GLES_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h);
extern void Wayland_GLES_DeleteContext(_THIS, SDL_GLContext context);

#endif /* SDL_waylandopengles_h_ */
Expand Down
Loading