Fixed setting OpenGL mode multiple times on Windows
authorSam Lantinga <slouken@libsdl.org>
Sun, 19 May 2002 22:27:42 +0000
changeset 373c638fde8a824
parent 372 7e9e5a51c5f9
child 374 3fc4e71f7714
Fixed setting OpenGL mode multiple times on Windows
docs.html
src/video/wincommon/SDL_wingl.c
     1.1 --- a/docs.html	Sun May 19 21:40:25 2002 +0000
     1.2 +++ b/docs.html	Sun May 19 22:27:42 2002 +0000
     1.3 @@ -16,6 +16,7 @@
     1.4  Major changes since SDL 1.0.0:
     1.5  </H2>
     1.6  <UL>
     1.7 +	<LI> 1.2.5: Fixed setting OpenGL mode multiple times on Windows
     1.8  	<LI> 1.2.5: Added support for Qtopia on embedded systems (thanks David!)
     1.9  	<LI> 1.2.4: Added initial support for Atari (thanks Patrice!)
    1.10  	<LI> 1.2.4: Added support for building SDL for EPOC/SymbianOS 6.0
     2.1 --- a/src/video/wincommon/SDL_wingl.c	Sun May 19 21:40:25 2002 +0000
     2.2 +++ b/src/video/wincommon/SDL_wingl.c	Sun May 19 22:27:42 2002 +0000
     2.3 @@ -36,11 +36,50 @@
     2.4  #define DEFAULT_GL_DRIVER_PATH "OPENGL32.DLL"
     2.5  #endif
     2.6  
     2.7 +/* If setting the HDC fails, we may need to recreate the window (MSDN) */
     2.8 +static int WIN_GL_ResetWindow(_THIS)
     2.9 +{
    2.10 +	int status = 0;
    2.11 +	int can_reset = 1;
    2.12 +
    2.13 +	/* If we were passed a window, then we can't create a new one */
    2.14 +	if ( SDL_windowid ) {
    2.15 +		can_reset = 0;
    2.16 +	}
    2.17 +#ifndef _WIN32_WCE /* FIXME WinCE needs the UNICODE version of CreateWindow() */
    2.18 +	if ( can_reset ) {
    2.19 +		/* Save the existing window attributes */
    2.20 +		LONG style;
    2.21 +		RECT rect = { 0, 0, 0, 0 };
    2.22 +		style = GetWindowLong(SDL_Window, GWL_STYLE);
    2.23 +		GetWindowRect(SDL_Window, &rect);
    2.24 +		DestroyWindow(SDL_Window);
    2.25 +		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
    2.26 +		                          style,
    2.27 +					  rect.left, rect.top,
    2.28 +                                          (rect.right-rect.left)+1,
    2.29 +                                          (rect.top-rect.bottom)+1,
    2.30 +		                          NULL, NULL, SDL_Instance, NULL);
    2.31 +		if ( SDL_Window ) {
    2.32 +			this->SetCaption(this, this->wm_title, this->wm_icon);
    2.33 +		} else {
    2.34 +			SDL_SetError("Couldn't create window");
    2.35 +			status = -1;
    2.36 +		}
    2.37 +	} else
    2.38 +#endif /* !_WIN32_WCE */
    2.39 +	{
    2.40 +		SDL_SetError("Unable to reset window for OpenGL context");
    2.41 +		status = -1;
    2.42 +	}
    2.43 +	return(status);
    2.44 +}
    2.45  
    2.46  int WIN_GL_SetupWindow(_THIS)
    2.47  {
    2.48  	int retval;
    2.49  #ifdef HAVE_OPENGL
    2.50 +	int i;
    2.51  	int pixel_format;
    2.52  
    2.53  	/* load the gl driver from a default path */
    2.54 @@ -51,46 +90,57 @@
    2.55  		}
    2.56  	}
    2.57  
    2.58 -	/* Get the window device context for our OpenGL drawing */
    2.59 -	GL_hdc = GetDC(SDL_Window);
    2.60 -	if ( GL_hdc == NULL ) {
    2.61 -		SDL_SetError("Unable to get DC for SDL_Window");
    2.62 -		return(-1);
    2.63 -	}
    2.64 +	for ( i=0; ; ++i ) {
    2.65 +		/* Get the window device context for our OpenGL drawing */
    2.66 +		GL_hdc = GetDC(SDL_Window);
    2.67 +		if ( GL_hdc == NULL ) {
    2.68 +			SDL_SetError("Unable to get DC for SDL_Window");
    2.69 +			return(-1);
    2.70 +		}
    2.71  
    2.72 -	/* Set up the pixel format descriptor with our needed format */
    2.73 -	memset(&GL_pfd, 0, sizeof(GL_pfd));
    2.74 -	GL_pfd.nSize = sizeof(GL_pfd);
    2.75 -	GL_pfd.nVersion = 1;
    2.76 -	GL_pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL);
    2.77 -	if ( this->gl_config.double_buffer ) {
    2.78 -		GL_pfd.dwFlags |= PFD_DOUBLEBUFFER;
    2.79 -	}
    2.80 -	GL_pfd.iPixelType = PFD_TYPE_RGBA;
    2.81 -	GL_pfd.cColorBits = this->gl_config.buffer_size;
    2.82 -	GL_pfd.cRedBits = this->gl_config.red_size;
    2.83 -	GL_pfd.cGreenBits = this->gl_config.green_size;
    2.84 -	GL_pfd.cBlueBits = this->gl_config.blue_size;
    2.85 -	GL_pfd.cAlphaBits = this->gl_config.alpha_size;
    2.86 -	GL_pfd.cAccumRedBits = this->gl_config.accum_red_size;
    2.87 -	GL_pfd.cAccumGreenBits = this->gl_config.accum_green_size;
    2.88 -	GL_pfd.cAccumBlueBits = this->gl_config.accum_blue_size;
    2.89 -	GL_pfd.cAccumAlphaBits = this->gl_config.accum_alpha_size;
    2.90 -	GL_pfd.cAccumBits =
    2.91 -		(GL_pfd.cAccumRedBits + GL_pfd.cAccumGreenBits +
    2.92 -		 GL_pfd.cAccumBlueBits + GL_pfd.cAccumAlphaBits);
    2.93 -	GL_pfd.cDepthBits = this->gl_config.depth_size;
    2.94 -	GL_pfd.cStencilBits = this->gl_config.stencil_size;
    2.95 +		/* Set up the pixel format descriptor with our needed format */
    2.96 +		memset(&GL_pfd, 0, sizeof(GL_pfd));
    2.97 +		GL_pfd.nSize = sizeof(GL_pfd);
    2.98 +		GL_pfd.nVersion = 1;
    2.99 +		GL_pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL);
   2.100 +		if ( this->gl_config.double_buffer ) {
   2.101 +			GL_pfd.dwFlags |= PFD_DOUBLEBUFFER;
   2.102 +		}
   2.103 +		GL_pfd.iPixelType = PFD_TYPE_RGBA;
   2.104 +		GL_pfd.cColorBits = this->gl_config.buffer_size;
   2.105 +		GL_pfd.cRedBits = this->gl_config.red_size;
   2.106 +		GL_pfd.cGreenBits = this->gl_config.green_size;
   2.107 +		GL_pfd.cBlueBits = this->gl_config.blue_size;
   2.108 +		GL_pfd.cAlphaBits = this->gl_config.alpha_size;
   2.109 +		GL_pfd.cAccumRedBits = this->gl_config.accum_red_size;
   2.110 +		GL_pfd.cAccumGreenBits = this->gl_config.accum_green_size;
   2.111 +		GL_pfd.cAccumBlueBits = this->gl_config.accum_blue_size;
   2.112 +		GL_pfd.cAccumAlphaBits = this->gl_config.accum_alpha_size;
   2.113 +		GL_pfd.cAccumBits =
   2.114 +			(GL_pfd.cAccumRedBits + GL_pfd.cAccumGreenBits +
   2.115 +			 GL_pfd.cAccumBlueBits + GL_pfd.cAccumAlphaBits);
   2.116 +		GL_pfd.cDepthBits = this->gl_config.depth_size;
   2.117 +		GL_pfd.cStencilBits = this->gl_config.stencil_size;
   2.118  
   2.119 -	/* Choose and set the closest available pixel format */
   2.120 -	pixel_format = ChoosePixelFormat(GL_hdc, &GL_pfd);
   2.121 -	if ( !pixel_format ) {
   2.122 -		SDL_SetError("No matching GL pixel format available");
   2.123 -		return(-1);
   2.124 -	}
   2.125 -	if( !SetPixelFormat(GL_hdc, pixel_format, &GL_pfd) ) {
   2.126 -		SDL_SetError("Unable to set HDC pixel format");
   2.127 -		return(-1);
   2.128 +		/* Choose and set the closest available pixel format */
   2.129 +		pixel_format = ChoosePixelFormat(GL_hdc, &GL_pfd);
   2.130 +		if ( !pixel_format ) {
   2.131 +			SDL_SetError("No matching GL pixel format available");
   2.132 +			return(-1);
   2.133 +		}
   2.134 +		if( !SetPixelFormat(GL_hdc, pixel_format, &GL_pfd) ) {
   2.135 +			if ( i == 0 ) {
   2.136 +				/* First time through, try resetting the window */
   2.137 +				if ( WIN_GL_ResetWindow(this) < 0 ) {
   2.138 +					return(-1);
   2.139 +				}
   2.140 +				continue;
   2.141 +			}
   2.142 +			SDL_SetError("Unable to set HDC pixel format");
   2.143 +			return(-1);
   2.144 +		}
   2.145 +		/* We either succeeded or failed by this point */
   2.146 +		break;
   2.147  	}
   2.148  	DescribePixelFormat(GL_hdc, pixel_format, sizeof(GL_pfd), &GL_pfd);
   2.149