Simplified and unified the window creation process a little.
authorSam Lantinga <slouken@libsdl.org>
Mon, 21 Feb 2011 22:03:39 -0800
changeset 53802de85077eb0b
parent 5379 78c071fe4c37
child 5381 79e60256dbf7
Simplified and unified the window creation process a little.
include/SDL_video.h
src/video/SDL_video.c
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/include/SDL_video.h	Mon Feb 21 21:32:11 2011 -0800
     1.2 +++ b/include/SDL_video.h	Mon Feb 21 22:03:39 2011 -0800
     1.3 @@ -97,13 +97,14 @@
     1.4   */
     1.5  typedef enum
     1.6  {
     1.7 -    SDL_WINDOW_FULLSCREEN = 0x00000001,         /**< fullscreen window, implies borderless */
     1.8 +    SDL_WINDOW_FULLSCREEN = 0x00000001,         /**< fullscreen window */
     1.9      SDL_WINDOW_OPENGL = 0x00000002,             /**< window usable with OpenGL context */
    1.10      SDL_WINDOW_SHOWN = 0x00000004,              /**< window is visible */
    1.11 -    SDL_WINDOW_BORDERLESS = 0x00000008,         /**< no window decoration */
    1.12 -    SDL_WINDOW_RESIZABLE = 0x00000010,          /**< window can be resized */
    1.13 -    SDL_WINDOW_MINIMIZED = 0x00000020,          /**< window is minimized */
    1.14 -    SDL_WINDOW_MAXIMIZED = 0x00000040,          /**< window is maximized */
    1.15 +    SDL_WINDOW_HIDDEN = 0x00000008,             /**< window is not visible */
    1.16 +    SDL_WINDOW_BORDERLESS = 0x00000010,         /**< no window decoration */
    1.17 +    SDL_WINDOW_RESIZABLE = 0x00000020,          /**< window can be resized */
    1.18 +    SDL_WINDOW_MINIMIZED = 0x00000040,          /**< window is minimized */
    1.19 +    SDL_WINDOW_MAXIMIZED = 0x00000080,          /**< window is maximized */
    1.20      SDL_WINDOW_INPUT_GRABBED = 0x00000100,      /**< window has grabbed input focus */
    1.21      SDL_WINDOW_INPUT_FOCUS = 0x00000200,        /**< window has input focus */
    1.22      SDL_WINDOW_MOUSE_FOCUS = 0x00000400,        /**< window has mouse focus */
     2.1 --- a/src/video/SDL_video.c	Mon Feb 21 21:32:11 2011 -0800
     2.2 +++ b/src/video/SDL_video.c	Mon Feb 21 22:03:39 2011 -0800
     2.3 @@ -581,6 +581,21 @@
     2.4  }
     2.5  
     2.6  int
     2.7 +SDL_GetIndexOfDisplay(SDL_VideoDisplay *display)
     2.8 +{
     2.9 +    int displayIndex;
    2.10 +
    2.11 +    for (displayIndex = 0; displayIndex < _this->num_displays; ++displayIndex) {
    2.12 +        if (display == &_this->displays[displayIndex]) {
    2.13 +            return displayIndex;
    2.14 +        }
    2.15 +    }
    2.16 +
    2.17 +    /* Couldn't find the display, just use index 0 */
    2.18 +    return 0;
    2.19 +}
    2.20 +
    2.21 +int
    2.22  SDL_GetDisplayBounds(int displayIndex, SDL_Rect * rect)
    2.23  {
    2.24      CHECK_DISPLAY_INDEX(displayIndex, -1);
    2.25 @@ -1066,14 +1081,32 @@
    2.26      SDL_OnWindowResized(window);
    2.27  }
    2.28  
    2.29 +#define CREATE_FLAGS \
    2.30 +    (SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE)
    2.31 +
    2.32 +static void
    2.33 +SDL_FinishWindowCreation(SDL_Window *window, Uint32 flags)
    2.34 +{
    2.35 +    if (flags & SDL_WINDOW_MAXIMIZED) {
    2.36 +        SDL_MaximizeWindow(window);
    2.37 +    }
    2.38 +    if (flags & SDL_WINDOW_MINIMIZED) {
    2.39 +        SDL_MinimizeWindow(window);
    2.40 +    }
    2.41 +    if (flags & SDL_WINDOW_FULLSCREEN) {
    2.42 +        SDL_SetWindowFullscreen(window, SDL_TRUE);
    2.43 +    }
    2.44 +    if (flags & SDL_WINDOW_INPUT_GRABBED) {
    2.45 +        SDL_SetWindowGrab(window, SDL_TRUE);
    2.46 +    }
    2.47 +    if (!(flags & SDL_WINDOW_HIDDEN)) {
    2.48 +        SDL_ShowWindow(window);
    2.49 +    }
    2.50 +}
    2.51 +
    2.52  SDL_Window *
    2.53  SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
    2.54  {
    2.55 -    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
    2.56 -                                  SDL_WINDOW_OPENGL |
    2.57 -                                  SDL_WINDOW_BORDERLESS |
    2.58 -                                  SDL_WINDOW_RESIZABLE |
    2.59 -                                  SDL_WINDOW_INPUT_GRABBED);
    2.60      SDL_Window *window;
    2.61  
    2.62      if (!_this) {
    2.63 @@ -1101,7 +1134,22 @@
    2.64      window->y = y;
    2.65      window->w = w;
    2.66      window->h = h;
    2.67 -    window->flags = (flags & allowed_flags);
    2.68 +    if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISUNDEFINED(y) ||
    2.69 +        SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
    2.70 +        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
    2.71 +        int displayIndex;
    2.72 +        SDL_Rect bounds;
    2.73 +
    2.74 +        displayIndex = SDL_GetIndexOfDisplay(display);
    2.75 +        SDL_GetDisplayBounds(displayIndex, &bounds);
    2.76 +        if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
    2.77 +            window->x = bounds.x + (bounds.w - w) / 2;
    2.78 +        }
    2.79 +        if (SDL_WINDOWPOS_ISUNDEFINED(y) || SDL_WINDOWPOS_ISCENTERED(y)) {
    2.80 +            window->y = bounds.y + (bounds.h - h) / 2;
    2.81 +        }
    2.82 +    }
    2.83 +    window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
    2.84      window->next = _this->windows;
    2.85      if (_this->windows) {
    2.86          _this->windows->prev = window;
    2.87 @@ -1116,16 +1164,7 @@
    2.88      if (title) {
    2.89          SDL_SetWindowTitle(window, title);
    2.90      }
    2.91 -    if (flags & SDL_WINDOW_MAXIMIZED) {
    2.92 -        SDL_MaximizeWindow(window);
    2.93 -    }
    2.94 -    if (flags & SDL_WINDOW_MINIMIZED) {
    2.95 -        SDL_MinimizeWindow(window);
    2.96 -    }
    2.97 -    if (flags & SDL_WINDOW_SHOWN) {
    2.98 -        SDL_ShowWindow(window);
    2.99 -    }
   2.100 -    SDL_UpdateWindowGrab(window);
   2.101 +    SDL_FinishWindowCreation(window, flags);
   2.102  
   2.103      return window;
   2.104  }
   2.105 @@ -1160,12 +1199,6 @@
   2.106  int
   2.107  SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
   2.108  {
   2.109 -    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   2.110 -                                  SDL_WINDOW_OPENGL |
   2.111 -                                  SDL_WINDOW_BORDERLESS |
   2.112 -                                  SDL_WINDOW_RESIZABLE |
   2.113 -                                  SDL_WINDOW_INPUT_GRABBED |
   2.114 -                                  SDL_WINDOW_FOREIGN);
   2.115      char *title = window->title;
   2.116  
   2.117      if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
   2.118 @@ -1204,7 +1237,7 @@
   2.119      }
   2.120  
   2.121      window->title = NULL;
   2.122 -    window->flags = (flags & allowed_flags);
   2.123 +    window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
   2.124  
   2.125      if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
   2.126          if (_this->CreateWindow(_this, window) < 0) {
   2.127 @@ -1219,16 +1252,7 @@
   2.128          SDL_SetWindowTitle(window, title);
   2.129          SDL_free(title);
   2.130      }
   2.131 -    if (flags & SDL_WINDOW_MAXIMIZED) {
   2.132 -        SDL_MaximizeWindow(window);
   2.133 -    }
   2.134 -    if (flags & SDL_WINDOW_MINIMIZED) {
   2.135 -        SDL_MinimizeWindow(window);
   2.136 -    }
   2.137 -    if (flags & SDL_WINDOW_SHOWN) {
   2.138 -        SDL_ShowWindow(window);
   2.139 -    }
   2.140 -    SDL_UpdateWindowGrab(window);
   2.141 +    SDL_FinishWindowCreation(window, flags);
   2.142  
   2.143      return 0;
   2.144  }
     3.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Mon Feb 21 21:32:11 2011 -0800
     3.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Mon Feb 21 22:03:39 2011 -0800
     3.3 @@ -516,7 +516,7 @@
     3.4      {
     3.5          unsigned int style = [nswindow styleMask];
     3.6  
     3.7 -        if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
     3.8 +        if (style == NSBorderlessWindowMask) {
     3.9              window->flags |= SDL_WINDOW_BORDERLESS;
    3.10          } else {
    3.11              window->flags &= ~SDL_WINDOW_BORDERLESS;
    3.12 @@ -527,7 +527,8 @@
    3.13              window->flags &= ~SDL_WINDOW_RESIZABLE;
    3.14          }
    3.15      }
    3.16 -    if ([nswindow isZoomed]) {
    3.17 +    /* isZoomed always returns true if the window is not resizable */
    3.18 +    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
    3.19          window->flags |= SDL_WINDOW_MAXIMIZED;
    3.20      } else {
    3.21          window->flags &= ~SDL_WINDOW_MAXIMIZED;
    3.22 @@ -540,10 +541,6 @@
    3.23      if ([nswindow isKeyWindow]) {
    3.24          window->flags |= SDL_WINDOW_INPUT_FOCUS;
    3.25          SDL_SetKeyboardFocus(data->window);
    3.26 -
    3.27 -        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
    3.28 -            /* FIXME */
    3.29 -        }
    3.30      }
    3.31  
    3.32      /* All done! */
    3.33 @@ -563,20 +560,8 @@
    3.34      unsigned int style;
    3.35  
    3.36      Cocoa_GetDisplayBounds(_this, display, &bounds);
    3.37 -    if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
    3.38 -        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
    3.39 -    } else if (SDL_WINDOWPOS_ISUNDEFINED(window->x)) {
    3.40 -        rect.origin.x = bounds.x;
    3.41 -    } else {
    3.42 -        rect.origin.x = window->x;
    3.43 -    }
    3.44 -    if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
    3.45 -        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
    3.46 -    } else if (SDL_WINDOWPOS_ISUNDEFINED(window->y)) {
    3.47 -        rect.origin.y = bounds.y;
    3.48 -    } else {
    3.49 -        rect.origin.y = window->y;
    3.50 -    }
    3.51 +    rect.origin.x = window->x;
    3.52 +    rect.origin.y = window->y;
    3.53      rect.size.width = window->w;
    3.54      rect.size.height = window->h;
    3.55      ConvertNSRect(&rect);
    3.56 @@ -763,7 +748,7 @@
    3.57  
    3.58      if ([nswindow isMiniaturized]) {
    3.59          [nswindow deminiaturize:nil];
    3.60 -    } else if ([nswindow isZoomed]) {
    3.61 +    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
    3.62          [nswindow zoom:nil];
    3.63      }
    3.64      [pool release];