Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Simplified and unified the window creation process a little.
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Feb 22, 2011
1 parent 1079be6 commit f9d5e1e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 59 deletions.
11 changes: 6 additions & 5 deletions include/SDL_video.h
Expand Up @@ -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 */
Expand Down
90 changes: 57 additions & 33 deletions src/video/SDL_video.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
27 changes: 6 additions & 21 deletions src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -516,7 +516,7 @@ - (void)rightMouseDown:(NSEvent *)theEvent
{
unsigned int style = [nswindow styleMask];

if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
if (style == NSBorderlessWindowMask) {
window->flags |= SDL_WINDOW_BORDERLESS;
} else {
window->flags &= ~SDL_WINDOW_BORDERLESS;
Expand All @@ -527,7 +527,8 @@ - (void)rightMouseDown:(NSEvent *)theEvent
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;
Expand All @@ -540,10 +541,6 @@ - (void)rightMouseDown:(NSEvent *)theEvent
if ([nswindow isKeyWindow]) {
window->flags |= SDL_WINDOW_INPUT_FOCUS;
SDL_SetKeyboardFocus(data->window);

if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
/* FIXME */
}
}

/* All done! */
Expand All @@ -563,20 +560,8 @@ - (void)rightMouseDown:(NSEvent *)theEvent
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);
Expand Down Expand Up @@ -763,7 +748,7 @@ - (void)rightMouseDown:(NSEvent *)theEvent

if ([nswindow isMiniaturized]) {
[nswindow deminiaturize:nil];
} else if ([nswindow isZoomed]) {
} else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
[nswindow zoom:nil];
}
[pool release];
Expand Down

0 comments on commit f9d5e1e

Please sign in to comment.