Simplified and unified the window creation process a little.

This commit is contained in:
Sam Lantinga 2011-02-21 22:03:39 -08:00
parent 165076c9c3
commit f8481050cd
3 changed files with 69 additions and 59 deletions

View File

@ -97,13 +97,14 @@ typedef struct SDL_Window SDL_Window;
*/
typedef enum
{
SDL_WINDOW_FULLSCREEN = 0x00000001, /**< fullscreen window, implies borderless */
SDL_WINDOW_FULLSCREEN = 0x00000001, /**< fullscreen window */
SDL_WINDOW_OPENGL = 0x00000002, /**< window usable with OpenGL context */
SDL_WINDOW_SHOWN = 0x00000004, /**< window is visible */
SDL_WINDOW_BORDERLESS = 0x00000008, /**< no window decoration */
SDL_WINDOW_RESIZABLE = 0x00000010, /**< window can be resized */
SDL_WINDOW_MINIMIZED = 0x00000020, /**< window is minimized */
SDL_WINDOW_MAXIMIZED = 0x00000040, /**< window is maximized */
SDL_WINDOW_HIDDEN = 0x00000008, /**< window is not visible */
SDL_WINDOW_BORDERLESS = 0x00000010, /**< no window decoration */
SDL_WINDOW_RESIZABLE = 0x00000020, /**< window can be resized */
SDL_WINDOW_MINIMIZED = 0x00000040, /**< window is minimized */
SDL_WINDOW_MAXIMIZED = 0x00000080, /**< window is maximized */
SDL_WINDOW_INPUT_GRABBED = 0x00000100, /**< window has grabbed input focus */
SDL_WINDOW_INPUT_FOCUS = 0x00000200, /**< window has input focus */
SDL_WINDOW_MOUSE_FOCUS = 0x00000400, /**< window has mouse focus */

View File

@ -580,6 +580,21 @@ SDL_GetNumVideoDisplays(void)
return _this->num_displays;
}
int
SDL_GetIndexOfDisplay(SDL_VideoDisplay *display)
{
int displayIndex;
for (displayIndex = 0; displayIndex < _this->num_displays; ++displayIndex) {
if (display == &_this->displays[displayIndex]) {
return displayIndex;
}
}
/* Couldn't find the display, just use index 0 */
return 0;
}
int
SDL_GetDisplayBounds(int displayIndex, SDL_Rect * rect)
{
@ -1066,14 +1081,32 @@ SDL_UpdateFullscreenMode(SDL_Window * window)
SDL_OnWindowResized(window);
}
#define CREATE_FLAGS \
(SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE)
static void
SDL_FinishWindowCreation(SDL_Window *window, Uint32 flags)
{
if (flags & SDL_WINDOW_MAXIMIZED) {
SDL_MaximizeWindow(window);
}
if (flags & SDL_WINDOW_MINIMIZED) {
SDL_MinimizeWindow(window);
}
if (flags & SDL_WINDOW_FULLSCREEN) {
SDL_SetWindowFullscreen(window, SDL_TRUE);
}
if (flags & SDL_WINDOW_INPUT_GRABBED) {
SDL_SetWindowGrab(window, SDL_TRUE);
}
if (!(flags & SDL_WINDOW_HIDDEN)) {
SDL_ShowWindow(window);
}
}
SDL_Window *
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
{
const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
SDL_WINDOW_OPENGL |
SDL_WINDOW_BORDERLESS |
SDL_WINDOW_RESIZABLE |
SDL_WINDOW_INPUT_GRABBED);
SDL_Window *window;
if (!_this) {
@ -1101,7 +1134,22 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
window->y = y;
window->w = w;
window->h = h;
window->flags = (flags & allowed_flags);
if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISUNDEFINED(y) ||
SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
int displayIndex;
SDL_Rect bounds;
displayIndex = SDL_GetIndexOfDisplay(display);
SDL_GetDisplayBounds(displayIndex, &bounds);
if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
window->x = bounds.x + (bounds.w - w) / 2;
}
if (SDL_WINDOWPOS_ISUNDEFINED(y) || SDL_WINDOWPOS_ISCENTERED(y)) {
window->y = bounds.y + (bounds.h - h) / 2;
}
}
window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
window->next = _this->windows;
if (_this->windows) {
_this->windows->prev = window;
@ -1116,16 +1164,7 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
if (title) {
SDL_SetWindowTitle(window, title);
}
if (flags & SDL_WINDOW_MAXIMIZED) {
SDL_MaximizeWindow(window);
}
if (flags & SDL_WINDOW_MINIMIZED) {
SDL_MinimizeWindow(window);
}
if (flags & SDL_WINDOW_SHOWN) {
SDL_ShowWindow(window);
}
SDL_UpdateWindowGrab(window);
SDL_FinishWindowCreation(window, flags);
return window;
}
@ -1160,12 +1199,6 @@ SDL_CreateWindowFrom(const void *data)
int
SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
{
const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
SDL_WINDOW_OPENGL |
SDL_WINDOW_BORDERLESS |
SDL_WINDOW_RESIZABLE |
SDL_WINDOW_INPUT_GRABBED |
SDL_WINDOW_FOREIGN);
char *title = window->title;
if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
@ -1204,7 +1237,7 @@ SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
}
window->title = NULL;
window->flags = (flags & allowed_flags);
window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
if (_this->CreateWindow(_this, window) < 0) {
@ -1219,16 +1252,7 @@ SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
SDL_SetWindowTitle(window, title);
SDL_free(title);
}
if (flags & SDL_WINDOW_MAXIMIZED) {
SDL_MaximizeWindow(window);
}
if (flags & SDL_WINDOW_MINIMIZED) {
SDL_MinimizeWindow(window);
}
if (flags & SDL_WINDOW_SHOWN) {
SDL_ShowWindow(window);
}
SDL_UpdateWindowGrab(window);
SDL_FinishWindowCreation(window, flags);
return 0;
}

View File

@ -516,7 +516,7 @@ SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created
{
unsigned int style = [nswindow styleMask];
if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
if (style == NSBorderlessWindowMask) {
window->flags |= SDL_WINDOW_BORDERLESS;
} else {
window->flags &= ~SDL_WINDOW_BORDERLESS;
@ -527,7 +527,8 @@ SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created
window->flags &= ~SDL_WINDOW_RESIZABLE;
}
}
if ([nswindow isZoomed]) {
/* isZoomed always returns true if the window is not resizable */
if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
window->flags |= SDL_WINDOW_MAXIMIZED;
} else {
window->flags &= ~SDL_WINDOW_MAXIMIZED;
@ -540,10 +541,6 @@ SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created
if ([nswindow isKeyWindow]) {
window->flags |= SDL_WINDOW_INPUT_FOCUS;
SDL_SetKeyboardFocus(data->window);
if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
/* FIXME */
}
}
/* All done! */
@ -563,20 +560,8 @@ Cocoa_CreateWindow(_THIS, SDL_Window * window)
unsigned int style;
Cocoa_GetDisplayBounds(_this, display, &bounds);
if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
} else if (SDL_WINDOWPOS_ISUNDEFINED(window->x)) {
rect.origin.x = bounds.x;
} else {
rect.origin.x = window->x;
}
if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
} else if (SDL_WINDOWPOS_ISUNDEFINED(window->y)) {
rect.origin.y = bounds.y;
} else {
rect.origin.y = window->y;
}
rect.origin.x = window->x;
rect.origin.y = window->y;
rect.size.width = window->w;
rect.size.height = window->h;
ConvertNSRect(&rect);
@ -763,7 +748,7 @@ Cocoa_RestoreWindow(_THIS, SDL_Window * window)
if ([nswindow isMiniaturized]) {
[nswindow deminiaturize:nil];
} else if ([nswindow isZoomed]) {
} else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
[nswindow zoom:nil];
}
[pool release];