From 2b4a4771f90fdbc1e610a90adb701eb7d13ac389 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 19 May 2013 22:36:54 -0700 Subject: [PATCH] Fixed bug 1842 - [patch] SDL_SetWindowPosition sets bad position values when given SDL_WINDOWPOS_CENTERED args Alex Szpakowski When calling SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED), the window moves to the correct position but it seems to internally set its x/y position values to the literal value of the SDL_WINDOWPOS_CENTERED define. This causes all sorts of problems when SDL functions which use the window position (e.g. SDL_SetWindowGrab) are called after the aforementioned SDL_SetWindowPosition call. Looking at the SDL_SetWindowPosition code, it seems that SDL_SendWindowEvent with the SDL_WINDOWEVENT_MOVED event is called at the end of the function using the literal value of the SDL_WINDOWPOS_CENTERED define, instead of the newly set window->x and window->y values. SDL_SendWindowEvent then sets the values of window->windowed.x and window->windowed.y to that value (0x2FFF0000, aka 805240832.) I have attached a patch which changes SDL_SetWindowPosition to make sure SDL_SendWindowEvent is called with the correct coordinate values, if SDL_WINDOWPOS_CENTERED is used (fixes the issue for me.) Tested with Mac OS 10.8.3. --- src/video/SDL_video.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index 57cc934df..f0f3380db 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -1497,12 +1497,6 @@ SDL_SetWindowPosition(SDL_Window * window, int x, int y) { CHECK_WINDOW_MAGIC(window, ); - if (!SDL_WINDOWPOS_ISUNDEFINED(x)) { - window->x = x; - } - if (!SDL_WINDOWPOS_ISUNDEFINED(y)) { - window->y = y; - } if (SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) { SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window); int displayIndex; @@ -1511,12 +1505,20 @@ SDL_SetWindowPosition(SDL_Window * window, int x, int y) displayIndex = SDL_GetIndexOfDisplay(display); SDL_GetDisplayBounds(displayIndex, &bounds); if (SDL_WINDOWPOS_ISCENTERED(x)) { - window->x = bounds.x + (bounds.w - window->w) / 2; + x = bounds.x + (bounds.w - window->w) / 2; } if (SDL_WINDOWPOS_ISCENTERED(y)) { - window->y = bounds.y + (bounds.h - window->h) / 2; + y = bounds.y + (bounds.h - window->h) / 2; } } + + if (!SDL_WINDOWPOS_ISUNDEFINED(x)) { + window->x = x; + } + if (!SDL_WINDOWPOS_ISUNDEFINED(y)) { + window->y = y; + } + if (!(window->flags & SDL_WINDOW_FULLSCREEN)) { if (_this->SetWindowPosition) { _this->SetWindowPosition(_this, window);