Allow the application to resize the window without destroying the OpenGL context SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Fri, 16 Oct 2009 07:24:11 +0000
branchSDL-1.2
changeset 4345b40d6e58a5e5
parent 4344 14f95e514408
child 4346 5fe9b267cf55
Allow the application to resize the window without destroying the OpenGL context
src/video/windib/SDL_dibvideo.c
     1.1 --- a/src/video/windib/SDL_dibvideo.c	Fri Oct 16 05:12:20 2009 +0000
     1.2 +++ b/src/video/windib/SDL_dibvideo.c	Fri Oct 16 07:24:11 2009 +0000
     1.3 @@ -508,6 +508,76 @@
     1.4  /* Various screen update functions available */
     1.5  static void DIB_NormalUpdate(_THIS, int numrects, SDL_Rect *rects);
     1.6  
     1.7 +static void DIB_ResizeWindow(_THIS, int width, int height, int prev_width, int prev_height, Uint32 flags)
     1.8 +{
     1.9 +	RECT bounds;
    1.10 +	int x, y;
    1.11 +
    1.12 +#ifndef _WIN32_WCE
    1.13 +	/* Resize the window */
    1.14 +	if ( !SDL_windowid && !IsZoomed(SDL_Window) ) {
    1.15 +#else
    1.16 +	if ( !SDL_windowid ) {
    1.17 +#endif
    1.18 +		HWND top;
    1.19 +		UINT swp_flags;
    1.20 +		const char *window = NULL;
    1.21 +		const char *center = NULL;
    1.22 +
    1.23 +		if ( width != prev_width || height != prev_height ) {
    1.24 +			window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
    1.25 +			center = SDL_getenv("SDL_VIDEO_CENTERED");
    1.26 +			if ( window ) {
    1.27 +				if ( SDL_sscanf(window, "%d,%d", &x, &y) == 2 ) {
    1.28 +					SDL_windowX = x;
    1.29 +					SDL_windowY = y;
    1.30 +				}
    1.31 +				if ( SDL_strcmp(window, "center") == 0 ) {
    1.32 +					center = window;
    1.33 +				}
    1.34 +			}
    1.35 +		}
    1.36 +		swp_flags = (SWP_NOCOPYBITS | SWP_SHOWWINDOW);
    1.37 +
    1.38 +		bounds.left = SDL_windowX;
    1.39 +		bounds.top = SDL_windowY;
    1.40 +		bounds.right = SDL_windowX+width;
    1.41 +		bounds.bottom = SDL_windowY+height;
    1.42 +#ifndef _WIN32_WCE
    1.43 +		AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), (GetMenu(SDL_Window) != NULL), 0);
    1.44 +#else
    1.45 +		// The bMenu parameter must be FALSE; menu bars are not supported
    1.46 +		AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), 0, 0);
    1.47 +#endif
    1.48 +		width = bounds.right-bounds.left;
    1.49 +		height = bounds.bottom-bounds.top;
    1.50 +		if ( (flags & SDL_FULLSCREEN) ) {
    1.51 +			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
    1.52 +			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
    1.53 +		} else if ( center ) {
    1.54 +			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
    1.55 +			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
    1.56 +		} else if ( SDL_windowX || SDL_windowY || window ) {
    1.57 +			x = bounds.left;
    1.58 +			y = bounds.top;
    1.59 +		} else {
    1.60 +			x = y = -1;
    1.61 +			swp_flags |= SWP_NOMOVE;
    1.62 +		}
    1.63 +		if ( flags & SDL_FULLSCREEN ) {
    1.64 +			top = HWND_TOPMOST;
    1.65 +		} else {
    1.66 +			top = HWND_NOTOPMOST;
    1.67 +		}
    1.68 +		SetWindowPos(SDL_Window, top, x, y, width, height, swp_flags);
    1.69 +		if ( !(flags & SDL_FULLSCREEN) ) {
    1.70 +			SDL_windowX = SDL_bounds.left;
    1.71 +			SDL_windowY = SDL_bounds.top;
    1.72 +		}
    1.73 +		SetForegroundWindow(SDL_Window);
    1.74 +	}
    1.75 +}
    1.76 +
    1.77  SDL_Surface *DIB_SetVideoMode(_THIS, SDL_Surface *current,
    1.78  				int width, int height, int bpp, Uint32 flags)
    1.79  {
    1.80 @@ -524,10 +594,12 @@
    1.81  	int binfo_size;
    1.82  	BITMAPINFO *binfo;
    1.83  	HDC hdc;
    1.84 -	RECT bounds;
    1.85 -	int x, y;
    1.86  	Uint32 Rmask, Gmask, Bmask;
    1.87  
    1.88 +	prev_w = current->w;
    1.89 +	prev_h = current->h;
    1.90 +	prev_flags = current->flags;
    1.91 +
    1.92  	/*
    1.93  	 * Special case for OpenGL windows...since the app needs to call
    1.94  	 *  SDL_SetVideoMode() in response to resize events to continue to
    1.95 @@ -535,31 +607,21 @@
    1.96  	 *  there's no sense in tearing the context down just to rebuild it
    1.97  	 *  to what it already was...tearing it down sacrifices your GL state
    1.98  	 *  and uploaded textures. So if we're requesting the same video mode
    1.99 -	 *  attributes and the width/height matches the physical window, just
   1.100 -	 *  return immediately.
   1.101 +	 *  attributes just resize the window and return immediately.
   1.102  	 */
   1.103 -	if ( (SDL_Window != NULL) &&
   1.104 -	     (current != NULL) &&
   1.105 -	     (current->flags == flags) &&
   1.106 +	if ( SDL_Window &&
   1.107 +	     ((current->flags & ~SDL_ANYFORMAT) == (flags & ~SDL_ANYFORMAT)) &&
   1.108  	     (current->format->BitsPerPixel == bpp) &&
   1.109 -	     ((flags & SDL_FULLSCREEN) == 0) ) {  /* probably not safe for fs */
   1.110 -		int curwidth, curheight;
   1.111 -		RECT size;
   1.112 -
   1.113 -		/* Get the current position of our window */
   1.114 -		GetClientRect(SDL_Window, &size);
   1.115 -
   1.116 -		curwidth = (size.right - size.left);
   1.117 -		curheight = (size.bottom - size.top);
   1.118 -		if ((width == curwidth) && (height == curheight)) {
   1.119 -			current->w = width;
   1.120 -			current->h = height;
   1.121 -			return current;  /* we're already good to go. */
   1.122 -		}
   1.123 +	     (flags & SDL_OPENGL) && 
   1.124 +	     !(flags & SDL_FULLSCREEN) ) {  /* probably not safe for fs */
   1.125 +		current->w = width;
   1.126 +		current->h = height;
   1.127 +		SDL_resizing = 1;
   1.128 +		DIB_ResizeWindow(this, width, height, prev_w, prev_h, flags);
   1.129 +		SDL_resizing = 0;
   1.130 +		return current;
   1.131  	}
   1.132  
   1.133 -	prev_flags = current->flags;
   1.134 -
   1.135  	/* Clean up any GL context that may be hanging around */
   1.136  	if ( current->flags & SDL_OPENGL ) {
   1.137  		WIN_GL_ShutDown(this);
   1.138 @@ -607,8 +669,6 @@
   1.139  	}
   1.140  
   1.141  	/* Fill in part of the video surface */
   1.142 -	prev_w = video->w;
   1.143 -	prev_h = video->h;
   1.144  	video->flags = 0;	/* Clear flags */
   1.145  	video->w = width;
   1.146  	video->h = height;
   1.147 @@ -843,69 +903,7 @@
   1.148  			video->flags |= SDL_HWPALETTE;
   1.149  		}
   1.150  	}
   1.151 -#ifndef _WIN32_WCE
   1.152 -	/* Resize the window */
   1.153 -	if ( !SDL_windowid && !IsZoomed(SDL_Window) ) {
   1.154 -#else
   1.155 -	if ( !SDL_windowid ) {
   1.156 -#endif
   1.157 -		HWND top;
   1.158 -		UINT swp_flags;
   1.159 -		const char *window = NULL;
   1.160 -		const char *center = NULL;
   1.161 -
   1.162 -		if ( video->w != prev_w || video->h != prev_h ) {
   1.163 -			window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
   1.164 -			center = SDL_getenv("SDL_VIDEO_CENTERED");
   1.165 -			if ( window ) {
   1.166 -				if ( SDL_sscanf(window, "%d,%d", &x, &y) == 2 ) {
   1.167 -					SDL_windowX = x;
   1.168 -					SDL_windowY = y;
   1.169 -				}
   1.170 -				if ( SDL_strcmp(window, "center") == 0 ) {
   1.171 -					center = window;
   1.172 -				}
   1.173 -			}
   1.174 -		}
   1.175 -		swp_flags = (SWP_NOCOPYBITS | SWP_SHOWWINDOW);
   1.176 -
   1.177 -		bounds.left = SDL_windowX;
   1.178 -		bounds.top = SDL_windowY;
   1.179 -		bounds.right = SDL_windowX+video->w;
   1.180 -		bounds.bottom = SDL_windowY+video->h;
   1.181 -#ifndef _WIN32_WCE
   1.182 -		AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), (GetMenu(SDL_Window) != NULL), 0);
   1.183 -#else
   1.184 -		// The bMenu parameter must be FALSE; menu bars are not supported
   1.185 -		AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), 0, 0);
   1.186 -#endif
   1.187 -		width = bounds.right-bounds.left;
   1.188 -		height = bounds.bottom-bounds.top;
   1.189 -		if ( (flags & SDL_FULLSCREEN) ) {
   1.190 -			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
   1.191 -			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
   1.192 -		} else if ( center ) {
   1.193 -			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
   1.194 -			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
   1.195 -		} else if ( SDL_windowX || SDL_windowY || window ) {
   1.196 -			x = bounds.left;
   1.197 -			y = bounds.top;
   1.198 -		} else {
   1.199 -			x = y = -1;
   1.200 -			swp_flags |= SWP_NOMOVE;
   1.201 -		}
   1.202 -		if ( flags & SDL_FULLSCREEN ) {
   1.203 -			top = HWND_TOPMOST;
   1.204 -		} else {
   1.205 -			top = HWND_NOTOPMOST;
   1.206 -		}
   1.207 -		SetWindowPos(SDL_Window, top, x, y, width, height, swp_flags);
   1.208 -		if ( !(flags & SDL_FULLSCREEN) ) {
   1.209 -			SDL_windowX = SDL_bounds.left;
   1.210 -			SDL_windowY = SDL_bounds.top;
   1.211 -		}
   1.212 -		SetForegroundWindow(SDL_Window);
   1.213 -	}
   1.214 +	DIB_ResizeWindow(this, width, height, prev_w, prev_h, flags);
   1.215  	SDL_resizing = 0;
   1.216  
   1.217  	/* Set up for OpenGL */