Fixed bug 1842 - [patch] SDL_SetWindowPosition sets bad position values when given SDL_WINDOWPOS_CENTERED args
authorSam Lantinga <slouken@libsdl.org>
Sun, 19 May 2013 22:36:54 -0700
changeset 7195d206c3a59b2e
parent 7194 987fb567bba9
child 7196 fe0848afab9f
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
     1.1 --- a/src/video/SDL_video.c	Sun May 19 22:28:10 2013 -0700
     1.2 +++ b/src/video/SDL_video.c	Sun May 19 22:36:54 2013 -0700
     1.3 @@ -1497,26 +1497,28 @@
     1.4  {
     1.5      CHECK_WINDOW_MAGIC(window, );
     1.6  
     1.7 +    if (SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
     1.8 +        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
     1.9 +        int displayIndex;
    1.10 +        SDL_Rect bounds;
    1.11 +
    1.12 +        displayIndex = SDL_GetIndexOfDisplay(display);
    1.13 +        SDL_GetDisplayBounds(displayIndex, &bounds);
    1.14 +        if (SDL_WINDOWPOS_ISCENTERED(x)) {
    1.15 +            x = bounds.x + (bounds.w - window->w) / 2;
    1.16 +        }
    1.17 +        if (SDL_WINDOWPOS_ISCENTERED(y)) {
    1.18 +            y = bounds.y + (bounds.h - window->h) / 2;
    1.19 +        }
    1.20 +    }
    1.21 +
    1.22      if (!SDL_WINDOWPOS_ISUNDEFINED(x)) {
    1.23          window->x = x;
    1.24      }
    1.25      if (!SDL_WINDOWPOS_ISUNDEFINED(y)) {
    1.26          window->y = y;
    1.27      }
    1.28 -    if (SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
    1.29 -        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
    1.30 -        int displayIndex;
    1.31 -        SDL_Rect bounds;
    1.32 -
    1.33 -        displayIndex = SDL_GetIndexOfDisplay(display);
    1.34 -        SDL_GetDisplayBounds(displayIndex, &bounds);
    1.35 -        if (SDL_WINDOWPOS_ISCENTERED(x)) {
    1.36 -            window->x = bounds.x + (bounds.w - window->w) / 2;
    1.37 -        }
    1.38 -        if (SDL_WINDOWPOS_ISCENTERED(y)) {
    1.39 -            window->y = bounds.y + (bounds.h - window->h) / 2;
    1.40 -        }
    1.41 -    }
    1.42 +
    1.43      if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
    1.44          if (_this->SetWindowPosition) {
    1.45              _this->SetWindowPosition(_this, window);