Mercurial > sdl-ios-xcode
changeset 1956:ba0d62354872
Simplified driver window creation code.
Implemented several Cocoa window functions
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 29 Jul 2006 21:51:00 +0000 |
parents | 8c6106e45fc1 |
children | c7803e4c21d3 |
files | include/SDL_syswm.h include/SDL_video.h src/video/SDL_video.c src/video/cocoa/SDL_cocoawindow.h src/video/cocoa/SDL_cocoawindow.m src/video/win32/SDL_win32window.c src/video/x11/SDL_x11opengl.c src/video/x11/SDL_x11window.c |
diffstat | 8 files changed, 127 insertions(+), 175 deletions(-) [+] |
line wrap: on
line diff
--- a/include/SDL_syswm.h Sat Jul 29 03:57:55 2006 +0000 +++ b/include/SDL_syswm.h Sat Jul 29 21:51:00 2006 +0000 @@ -148,7 +148,6 @@ { SDL_version version; HWND window; /* The Win32 display window */ - HGLRC hglrc; /* The OpenGL context, if any */ }; #elif defined(SDL_VIDEO_DRIVER_RISCOS)
--- a/include/SDL_video.h Sat Jul 29 03:57:55 2006 +0000 +++ b/include/SDL_video.h Sat Jul 29 21:51:00 2006 +0000 @@ -574,17 +574,15 @@ * * \brief Create a window with the specified position, dimensions, and flags. * - * \param title The title of the window - * \param x The x position of the window - * \param y The y position of the window + * \param title The title of the window, in UTF-8 encoding + * \param x The x position of the window, SDL_WINDOWPOS_CENTERED, or SDL_WINDOWPOS_UNDEFINED + * \param y The y position of the window, SDL_WINDOWPOS_CENTERED, or SDL_WINDOWPOS_UNDEFINED * \param w The width of the window * \param h The height of the window * \param flags The flags for the window, a mask of any of the following: SDL_WINDOW_FULLSCREEN, SDL_WINDOW_OPENGL, SDL_WINDOW_SHOWN, SDL_WINDOW_BORDERLESS, SDL_WINDOW_RESIZABLE, SDL_WINDOW_MAXIMIZED, SDL_WINDOW_MINIMIZED, SDL_WINDOW_INPUT_GRABBED * * \return The id of the window created, or zero if window creation failed. * - * \note Setting the position to -1, -1, indicates any position is fine. - * * \sa SDL_DestroyWindow() */ extern DECLSPEC SDL_WindowID SDLCALL SDL_CreateWindow(const char *title,
--- a/src/video/SDL_video.c Sat Jul 29 03:57:55 2006 +0000 +++ b/src/video/SDL_video.c Sat Jul 29 21:51:00 2006 +0000 @@ -614,9 +614,8 @@ for (i = 0; i < display->num_windows; ++i) { SDL_Window *window = &display->windows[i]; if (FULLSCREEN_VISIBLE(window)) { - SDL_SetWindowPosition(window->id, - ((display_mode.w - window->w) / 2), - ((display_mode.h - window->h) / 2)); + SDL_SetWindowPosition(window->id, SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED); } } @@ -718,12 +717,8 @@ { const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL | - SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS | - SDL_WINDOW_RESIZABLE | - SDL_WINDOW_MAXIMIZED | - SDL_WINDOW_MINIMIZED | - SDL_WINDOW_INPUT_GRABBED); + SDL_WINDOW_RESIZABLE); SDL_VideoDisplay *display; SDL_Window window; int num_windows; @@ -739,9 +734,14 @@ return 0; } + /* Fullscreen windows don't have any window decorations */ + if (flags & SDL_WINDOW_FULLSCREEN) { + flags |= SDL_WINDOW_BORDERLESS; + flags &= ~SDL_WINDOW_RESIZABLE; + } + SDL_zero(window); window.id = _this->next_object_id++; - window.title = title ? SDL_strdup(title) : NULL; window.x = x; window.y = y; window.w = w; @@ -750,9 +750,6 @@ window.display = _this->current_display; if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) { - if (window.title) { - SDL_free(window.title); - } return 0; } @@ -764,27 +761,27 @@ if (_this->DestroyWindow) { _this->DestroyWindow(_this, &window); } - if (window.title) { - SDL_free(window.title); - } return 0; } windows[num_windows] = window; display->windows = windows; display->num_windows++; - if (FULLSCREEN_VISIBLE(&window)) { - /* Hide any other fullscreen windows */ - int i; - for (i = 0; i < display->num_windows; ++i) { - SDL_Window *other = &display->windows[i]; - if (other->id != window.id && FULLSCREEN_VISIBLE(other)) { - SDL_MinimizeWindow(other->id); - } - } - SDL_SetDisplayMode(display->fullscreen_mode); + if (title) { + SDL_SetWindowTitle(window.id, title); + } + if (flags & SDL_WINDOW_MAXIMIZED) { + SDL_MaximizeWindow(window.id); } - + if (flags & SDL_WINDOW_MINIMIZED) { + SDL_MinimizeWindow(window.id); + } + if (flags & SDL_WINDOW_SHOWN) { + SDL_ShowWindow(window.id); + } + if (flags & SDL_WINDOW_INPUT_GRABBED) { + SDL_SetWindowGrab(window.id, 1); + } return window.id; } @@ -833,15 +830,44 @@ int SDL_RecreateWindow(SDL_Window * window, Uint32 flags) { + char *title = window->title; + if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) { SDL_SetError("No OpenGL support in video driver"); return -1; } + if (_this->DestroyWindow) { _this->DestroyWindow(_this, window); } - window->flags = flags; - return _this->CreateWindow(_this, window); + + window->title = NULL; + window->flags = + (flags & + ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED | SDL_WINDOW_SHOWN | + SDL_WINDOW_INPUT_GRABBED)); + + if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) { + return -1; + } + + if (title) { + SDL_SetWindowTitle(window->id, title); + SDL_free(title); + } + if (flags & SDL_WINDOW_MAXIMIZED) { + SDL_MaximizeWindow(window->id); + } + if (flags & SDL_WINDOW_MINIMIZED) { + SDL_MinimizeWindow(window->id); + } + if (flags & SDL_WINDOW_SHOWN) { + SDL_ShowWindow(window->id); + } + if (flags & SDL_WINDOW_INPUT_GRABBED) { + SDL_SetWindowGrab(window->id, 1); + } + return 0; } SDL_Window * @@ -873,6 +899,9 @@ SDL_UninitializedVideo(); return NULL; } + if (!window) { + return NULL; + } return &_this->displays[window->display]; } @@ -891,14 +920,20 @@ SDL_SetWindowTitle(SDL_WindowID windowID, const char *title) { SDL_Window *window = SDL_GetWindowFromID(windowID); + const char *last_title; - if (!window) { + if (!window || title == window->title) { return; } + if (window->title) { SDL_free(window->title); } - window->title = SDL_strdup(title); + if (title) { + window->title = SDL_strdup(title); + } else { + window->title = NULL; + } if (_this->SetWindowTitle) { _this->SetWindowTitle(_this, window); @@ -942,15 +977,20 @@ SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y) { SDL_Window *window = SDL_GetWindowFromID(windowID); + SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); if (!window) { return; } - if (x != SDL_WINDOWPOS_UNDEFINED) { + if (x == SDL_WINDOWPOS_CENTERED) { + window->x = (display->current_mode.w - window->w) / 2; + } else if (x != SDL_WINDOWPOS_UNDEFINED) { window->x = x; } - if (y != SDL_WINDOWPOS_UNDEFINED) { + if (y == SDL_WINDOWPOS_CENTERED) { + window->y = (display->current_mode.h - window->h) / 2; + } else if (y != SDL_WINDOWPOS_UNDEFINED) { window->y = y; } @@ -1045,7 +1085,7 @@ { SDL_Window *window = SDL_GetWindowFromID(windowID); - if (!window) { + if (!window || !(window->flags & SDL_WINDOW_SHOWN)) { return; }
--- a/src/video/cocoa/SDL_cocoawindow.h Sat Jul 29 03:57:55 2006 +0000 +++ b/src/video/cocoa/SDL_cocoawindow.h Sat Jul 29 21:51:00 2006 +0000 @@ -55,8 +55,6 @@ -(void) otherMouseUp:(NSEvent *) theEvent; -(void) mouseMoved:(NSEvent *) theEvent; -(void) scrollWheel:(NSEvent *) theEvent; --(void) mouseEntered:(NSEvent *) theEvent; --(void) mouseExited:(NSEvent *) theEvent; -(void) keyDown:(NSEvent *) theEvent; -(void) keyUp:(NSEvent *) theEvent; @end
--- a/src/video/cocoa/SDL_cocoawindow.m Sat Jul 29 03:57:55 2006 +0000 +++ b/src/video/cocoa/SDL_cocoawindow.m Sat Jul 29 21:51:00 2006 +0000 @@ -195,6 +195,7 @@ - (void)mouseMoved:(NSEvent *)theEvent { + SDL_Window *window = SDL_GetWindowFromID(_data->windowID); int index; SDL_Mouse *mouse; NSPoint point; @@ -207,6 +208,18 @@ } point = [NSEvent mouseLocation]; + if (point.x < rect.origin.x || + point.x > (rect.origin.x + rect.size.width) || + point.y < rect.origin.y || + point.y > (rect.origin.y + rect.size.height)) { + if (window->flags & SDL_WINDOW_MOUSE_FOCUS) { + SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_LEAVE, 0, 0); + } + } else { + if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) { + SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_ENTER, 0, 0); + } + } point.x = point.x - rect.origin.x; point.y = rect.size.height - (point.y - rect.origin.y); SDL_SendMouseMotion(index, 0, (int)point.x, (int)point.y); @@ -217,18 +230,6 @@ fprintf(stderr, "scrollWheel\n"); } -- (void)mouseEntered:(NSEvent *)theEvent -{ -fprintf(stderr, "mouseEntered\n"); - SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_ENTER, 0, 0); -} - -- (void)mouseExited:(NSEvent *)theEvent -{ -fprintf(stderr, "mouseExited\n"); - SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_LEAVE, 0, 0); -} - - (void)keyDown:(NSEvent *)theEvent { fprintf(stderr, "keyDown\n"); @@ -282,7 +283,7 @@ { unsigned int style = [nswindow styleMask]; - if (style == NSBorderlessWindowMask) { + if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) { window->flags |= SDL_WINDOW_BORDERLESS; } else { window->flags &= ~SDL_WINDOW_BORDERLESS; @@ -331,16 +332,14 @@ pool = [[NSAutoreleasePool alloc] init]; - if ((window->flags & SDL_WINDOW_FULLSCREEN) || - window->x == SDL_WINDOWPOS_CENTERED) { + if (window->x == SDL_WINDOWPOS_CENTERED) { rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2; } else if (window->x == SDL_WINDOWPOS_UNDEFINED) { rect.origin.x = 0; } else { rect.origin.x = window->x; } - if ((window->flags & SDL_WINDOW_FULLSCREEN) || - window->y == SDL_WINDOWPOS_CENTERED) { + if (window->y == SDL_WINDOWPOS_CENTERED) { rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2; } else if (window->y == SDL_WINDOWPOS_UNDEFINED) { rect.origin.y = 0; @@ -362,23 +361,6 @@ nswindow = [[NSWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE]; - if (window->flags & SDL_WINDOW_SHOWN) { - [nswindow makeKeyAndOrderFront:nil]; - } - if (window->flags & SDL_WINDOW_MAXIMIZED) { - [nswindow performZoom:nil]; - } - if (window->flags & SDL_WINDOW_MINIMIZED) { - [nswindow performMiniaturize:nil]; - } - - if (window->title) { - title = [[NSString alloc] initWithUTF8String:window->title]; - [nswindow setTitle:title]; - [nswindow setMiniwindowTitle:title]; - [title release]; - } - [pool release]; if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) { @@ -424,10 +406,14 @@ NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window; NSString *string; - string = [[NSString alloc] initWithUTF8String:window->title]; + if(window->title) { + string = [[NSString alloc] initWithUTF8String:window->title]; + } else { + string = [[NSString alloc] init]; + } [nswindow setTitle:string]; - [nswindow setMiniwindowTitle:string]; [string release]; + [pool release]; } @@ -467,16 +453,20 @@ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window; - [nswindow makeKeyAndOrderFront:nil]; + if (![nswindow isMiniaturized]) { + [nswindow makeKeyAndOrderFront:nil]; + } [pool release]; } void Cocoa_HideWindow(_THIS, SDL_Window * window) { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window; - /* FIXME */ + [nswindow orderOut:nil]; + [pool release]; } void @@ -495,7 +485,7 @@ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window; - [nswindow performZoom:nil]; + [nswindow zoom:nil]; [pool release]; } @@ -505,16 +495,22 @@ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window; - [nswindow performMiniaturize:nil]; + [nswindow miniaturize:nil]; [pool release]; } void Cocoa_RestoreWindow(_THIS, SDL_Window * window) { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window; - /* FIXME */ + if ([nswindow isMiniaturized]) { + [nswindow deminiaturize:nil]; + } else if ([nswindow isZoomed]) { + [nswindow zoom:nil]; + } + [pool release]; } void
--- a/src/video/win32/SDL_win32window.c Sat Jul 29 03:57:55 2006 +0000 +++ b/src/video/win32/SDL_win32window.c Sat Jul 29 21:51:00 2006 +0000 @@ -133,23 +133,13 @@ WIN_CreateWindow(_THIS, SDL_Window * window) { HWND hwnd; - LPTSTR title = NULL; HWND top; RECT rect; DWORD style = (WS_CLIPSIBLINGS | WS_CLIPCHILDREN); int x, y; int w, h; - if (window->title) { - title = WIN_UTF8ToString(window->title); - } else { - title = NULL; - } - - if (window->flags & SDL_WINDOW_SHOWN) { - style |= WS_VISIBLE; - } - if ((window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS))) { + if (window->flags & SDL_WINDOW_BORDERLESS) { style |= WS_POPUP; } else { style |= (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX); @@ -157,12 +147,6 @@ if (window->flags & SDL_WINDOW_RESIZABLE) { style |= (WS_THICKFRAME | WS_MAXIMIZEBOX); } - if (window->flags & SDL_WINDOW_MAXIMIZED) { - style |= WS_MAXIMIZE; - } - if (window->flags & SDL_WINDOW_MINIMIZED) { - style |= WS_MINIMIZE; - } /* Figure out what the window area will be */ if (window->flags & SDL_WINDOW_FULLSCREEN) { @@ -178,16 +162,14 @@ w = (rect.right - rect.left); h = (rect.bottom - rect.top); - if ((window->flags & SDL_WINDOW_FULLSCREEN) || - window->x == SDL_WINDOWPOS_CENTERED) { + if (window->x == SDL_WINDOWPOS_CENTERED) { x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2; } else if (window->x == SDL_WINDOWPOS_UNDEFINED) { x = CW_USEDEFAULT; } else { x = window->x + rect.left; } - if ((window->flags & SDL_WINDOW_FULLSCREEN) || - window->y == SDL_WINDOWPOS_CENTERED) { + if (window->y == SDL_WINDOWPOS_CENTERED) { y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2; } else if (window->y == SDL_WINDOWPOS_UNDEFINED) { y = CW_USEDEFAULT; @@ -195,15 +177,11 @@ y = window->y + rect.top; } - hwnd = CreateWindow(SDL_Appname, - title ? title : TEXT(""), - style, x, y, w, h, NULL, NULL, SDL_Instance, NULL); + hwnd = + CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL, + SDL_Instance, NULL); WIN_PumpEvents(_this); - if (title) { - SDL_free(title); - } - if (!hwnd) { WIN_SetError("Couldn't create window"); return -1; @@ -277,7 +255,6 @@ DWORD style; HWND top; int x, y; - int w, h; /* Figure out what the window area will be */ if (window->flags & SDL_WINDOW_FULLSCREEN) { @@ -293,24 +270,10 @@ AdjustWindowRectEx(&rect, style, (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL), 0); - w = (rect.right - rect.left); - h = (rect.bottom - rect.top); + x = window->x + rect.left; + y = window->y + rect.top; - if ((window->flags & SDL_WINDOW_FULLSCREEN) || - window->x == SDL_WINDOWPOS_CENTERED) { - x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2; - window->x = x - rect.left; - } else { - x = window->x + rect.left; - } - if ((window->flags & SDL_WINDOW_FULLSCREEN) || - window->y == SDL_WINDOWPOS_CENTERED) { - y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2; - window->y = y - rect.top; - } else { - y = window->y + rect.top; - } - SetWindowPos(hwnd, top, x, y, h, w, (SWP_NOCOPYBITS | SWP_NOSIZE)); + SetWindowPos(hwnd, top, x, y, 0, 0, (SWP_NOCOPYBITS | SWP_NOSIZE)); } void @@ -438,8 +401,6 @@ HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd; if (info->version.major <= SDL_MAJOR_VERSION) { info->window = hwnd; - /* FIXME! */ - info->hglrc = NULL; return SDL_TRUE; } else { SDL_SetError("Application not compiled with SDL %d.%d\n",
--- a/src/video/x11/SDL_x11opengl.c Sat Jul 29 03:57:55 2006 +0000 +++ b/src/video/x11/SDL_x11opengl.c Sat Jul 29 21:51:00 2006 +0000 @@ -113,8 +113,7 @@ if (!_this->gl_data->glXChooseVisual || !_this->gl_data->glXCreateContext || !_this->gl_data->glXDestroyContext || - !_this->gl_data->glXMakeCurrent || - !_this->gl_data->glXSwapBuffers) { + !_this->gl_data->glXMakeCurrent || !_this->gl_data->glXSwapBuffers) { SDL_SetError("Could not retrieve OpenGL functions"); return -1; }
--- a/src/video/x11/SDL_x11window.c Sat Jul 29 03:57:55 2006 +0000 +++ b/src/video/x11/SDL_x11window.c Sat Jul 29 21:51:00 2006 +0000 @@ -193,8 +193,7 @@ visual, AllocNone); } - if ((window->flags & SDL_WINDOW_FULLSCREEN) || - window->x == SDL_WINDOWPOS_CENTERED) { + if (window->x == SDL_WINDOWPOS_CENTERED) { x = (DisplayWidth(data->display, displaydata->screen) - window->w) / 2; } else if (window->x == SDL_WINDOWPOS_UNDEFINED) { @@ -202,8 +201,7 @@ } else { x = window->x; } - if ((window->flags & SDL_WINDOW_FULLSCREEN) || - window->y == SDL_WINDOWPOS_CENTERED) { + if (window->y == SDL_WINDOWPOS_CENTERED) { y = (DisplayHeight(data->display, displaydata->screen) - window->h) / 2; } else if (window->y == SDL_WINDOWPOS_UNDEFINED) { @@ -360,14 +358,7 @@ wmhints = XAllocWMHints(); if (wmhints) { wmhints->input = True; - if (window->flags & SDL_WINDOW_MINIMIZED) { - wmhints->initial_state = IconicState; - } else if (window->flags & SDL_WINDOW_SHOWN) { - wmhints->initial_state = NormalState; - } else { - wmhints->initial_state = WithdrawnState; - } - wmhints->flags = InputHint | StateHint; + wmhints->flags = InputHint; XSetWMHints(data->display, w, wmhints); XFree(wmhints); } @@ -391,16 +382,6 @@ /* Allow the window to be deleted by the window manager */ XSetWMProtocols(data->display, w, &data->WM_DELETE_WINDOW, 1); - /* Finally, show the window */ - if (window->flags & SDL_WINDOW_SHOWN) { - XEvent event; - - XMapRaised(data->display, w); - do { - XCheckWindowEvent(data->display, w, StructureNotifyMask, &event); - } while (event.type != MapNotify); - } - if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) { #ifdef SDL_VIDEO_OPENGL_GLX if (window->flags & SDL_WINDOW_OPENGL) { @@ -410,9 +391,6 @@ XDestroyWindow(data->display, w); return -1; } - - X11_SetWindowTitle(_this, window); - return 0; } @@ -510,25 +488,8 @@ SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata; Display *display = data->videodata->display; - int x, y; - if ((window->flags & SDL_WINDOW_FULLSCREEN) || - window->x == SDL_WINDOWPOS_CENTERED) { - x = (DisplayWidth(display, displaydata->screen) - window->w) / 2; - } else if (window->x == SDL_WINDOWPOS_UNDEFINED) { - x = 0; - } else { - x = window->x; - } - if ((window->flags & SDL_WINDOW_FULLSCREEN) || - window->y == SDL_WINDOWPOS_CENTERED) { - y = (DisplayHeight(display, displaydata->screen) - window->h) / 2; - } else if (window->y == SDL_WINDOWPOS_UNDEFINED) { - y = 0; - } else { - y = window->y; - } - XMoveWindow(display, data->window, x, y); + XMoveWindow(display, data->window, window->x, window->y); } void