Mercurial > sdl-ios-xcode
comparison src/video/wincommon/SDL_wingl.c @ 373:c638fde8a824
Fixed setting OpenGL mode multiple times on Windows
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 19 May 2002 22:27:42 +0000 |
parents | 13fc64213765 |
children | 8a43e0cbf02f |
comparison
equal
deleted
inserted
replaced
372:7e9e5a51c5f9 | 373:c638fde8a824 |
---|---|
34 | 34 |
35 #ifdef HAVE_OPENGL | 35 #ifdef HAVE_OPENGL |
36 #define DEFAULT_GL_DRIVER_PATH "OPENGL32.DLL" | 36 #define DEFAULT_GL_DRIVER_PATH "OPENGL32.DLL" |
37 #endif | 37 #endif |
38 | 38 |
39 /* If setting the HDC fails, we may need to recreate the window (MSDN) */ | |
40 static int WIN_GL_ResetWindow(_THIS) | |
41 { | |
42 int status = 0; | |
43 int can_reset = 1; | |
44 | |
45 /* If we were passed a window, then we can't create a new one */ | |
46 if ( SDL_windowid ) { | |
47 can_reset = 0; | |
48 } | |
49 #ifndef _WIN32_WCE /* FIXME WinCE needs the UNICODE version of CreateWindow() */ | |
50 if ( can_reset ) { | |
51 /* Save the existing window attributes */ | |
52 LONG style; | |
53 RECT rect = { 0, 0, 0, 0 }; | |
54 style = GetWindowLong(SDL_Window, GWL_STYLE); | |
55 GetWindowRect(SDL_Window, &rect); | |
56 DestroyWindow(SDL_Window); | |
57 SDL_Window = CreateWindow(SDL_Appname, SDL_Appname, | |
58 style, | |
59 rect.left, rect.top, | |
60 (rect.right-rect.left)+1, | |
61 (rect.top-rect.bottom)+1, | |
62 NULL, NULL, SDL_Instance, NULL); | |
63 if ( SDL_Window ) { | |
64 this->SetCaption(this, this->wm_title, this->wm_icon); | |
65 } else { | |
66 SDL_SetError("Couldn't create window"); | |
67 status = -1; | |
68 } | |
69 } else | |
70 #endif /* !_WIN32_WCE */ | |
71 { | |
72 SDL_SetError("Unable to reset window for OpenGL context"); | |
73 status = -1; | |
74 } | |
75 return(status); | |
76 } | |
39 | 77 |
40 int WIN_GL_SetupWindow(_THIS) | 78 int WIN_GL_SetupWindow(_THIS) |
41 { | 79 { |
42 int retval; | 80 int retval; |
43 #ifdef HAVE_OPENGL | 81 #ifdef HAVE_OPENGL |
82 int i; | |
44 int pixel_format; | 83 int pixel_format; |
45 | 84 |
46 /* load the gl driver from a default path */ | 85 /* load the gl driver from a default path */ |
47 if ( ! this->gl_config.driver_loaded ) { | 86 if ( ! this->gl_config.driver_loaded ) { |
48 /* no driver has been loaded, use default (ourselves) */ | 87 /* no driver has been loaded, use default (ourselves) */ |
49 if ( WIN_GL_LoadLibrary(this, NULL) < 0 ) { | 88 if ( WIN_GL_LoadLibrary(this, NULL) < 0 ) { |
50 return(-1); | 89 return(-1); |
51 } | 90 } |
52 } | 91 } |
53 | 92 |
54 /* Get the window device context for our OpenGL drawing */ | 93 for ( i=0; ; ++i ) { |
55 GL_hdc = GetDC(SDL_Window); | 94 /* Get the window device context for our OpenGL drawing */ |
56 if ( GL_hdc == NULL ) { | 95 GL_hdc = GetDC(SDL_Window); |
57 SDL_SetError("Unable to get DC for SDL_Window"); | 96 if ( GL_hdc == NULL ) { |
58 return(-1); | 97 SDL_SetError("Unable to get DC for SDL_Window"); |
59 } | 98 return(-1); |
60 | 99 } |
61 /* Set up the pixel format descriptor with our needed format */ | 100 |
62 memset(&GL_pfd, 0, sizeof(GL_pfd)); | 101 /* Set up the pixel format descriptor with our needed format */ |
63 GL_pfd.nSize = sizeof(GL_pfd); | 102 memset(&GL_pfd, 0, sizeof(GL_pfd)); |
64 GL_pfd.nVersion = 1; | 103 GL_pfd.nSize = sizeof(GL_pfd); |
65 GL_pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL); | 104 GL_pfd.nVersion = 1; |
66 if ( this->gl_config.double_buffer ) { | 105 GL_pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL); |
67 GL_pfd.dwFlags |= PFD_DOUBLEBUFFER; | 106 if ( this->gl_config.double_buffer ) { |
68 } | 107 GL_pfd.dwFlags |= PFD_DOUBLEBUFFER; |
69 GL_pfd.iPixelType = PFD_TYPE_RGBA; | 108 } |
70 GL_pfd.cColorBits = this->gl_config.buffer_size; | 109 GL_pfd.iPixelType = PFD_TYPE_RGBA; |
71 GL_pfd.cRedBits = this->gl_config.red_size; | 110 GL_pfd.cColorBits = this->gl_config.buffer_size; |
72 GL_pfd.cGreenBits = this->gl_config.green_size; | 111 GL_pfd.cRedBits = this->gl_config.red_size; |
73 GL_pfd.cBlueBits = this->gl_config.blue_size; | 112 GL_pfd.cGreenBits = this->gl_config.green_size; |
74 GL_pfd.cAlphaBits = this->gl_config.alpha_size; | 113 GL_pfd.cBlueBits = this->gl_config.blue_size; |
75 GL_pfd.cAccumRedBits = this->gl_config.accum_red_size; | 114 GL_pfd.cAlphaBits = this->gl_config.alpha_size; |
76 GL_pfd.cAccumGreenBits = this->gl_config.accum_green_size; | 115 GL_pfd.cAccumRedBits = this->gl_config.accum_red_size; |
77 GL_pfd.cAccumBlueBits = this->gl_config.accum_blue_size; | 116 GL_pfd.cAccumGreenBits = this->gl_config.accum_green_size; |
78 GL_pfd.cAccumAlphaBits = this->gl_config.accum_alpha_size; | 117 GL_pfd.cAccumBlueBits = this->gl_config.accum_blue_size; |
79 GL_pfd.cAccumBits = | 118 GL_pfd.cAccumAlphaBits = this->gl_config.accum_alpha_size; |
80 (GL_pfd.cAccumRedBits + GL_pfd.cAccumGreenBits + | 119 GL_pfd.cAccumBits = |
81 GL_pfd.cAccumBlueBits + GL_pfd.cAccumAlphaBits); | 120 (GL_pfd.cAccumRedBits + GL_pfd.cAccumGreenBits + |
82 GL_pfd.cDepthBits = this->gl_config.depth_size; | 121 GL_pfd.cAccumBlueBits + GL_pfd.cAccumAlphaBits); |
83 GL_pfd.cStencilBits = this->gl_config.stencil_size; | 122 GL_pfd.cDepthBits = this->gl_config.depth_size; |
84 | 123 GL_pfd.cStencilBits = this->gl_config.stencil_size; |
85 /* Choose and set the closest available pixel format */ | 124 |
86 pixel_format = ChoosePixelFormat(GL_hdc, &GL_pfd); | 125 /* Choose and set the closest available pixel format */ |
87 if ( !pixel_format ) { | 126 pixel_format = ChoosePixelFormat(GL_hdc, &GL_pfd); |
88 SDL_SetError("No matching GL pixel format available"); | 127 if ( !pixel_format ) { |
89 return(-1); | 128 SDL_SetError("No matching GL pixel format available"); |
90 } | 129 return(-1); |
91 if( !SetPixelFormat(GL_hdc, pixel_format, &GL_pfd) ) { | 130 } |
92 SDL_SetError("Unable to set HDC pixel format"); | 131 if( !SetPixelFormat(GL_hdc, pixel_format, &GL_pfd) ) { |
93 return(-1); | 132 if ( i == 0 ) { |
133 /* First time through, try resetting the window */ | |
134 if ( WIN_GL_ResetWindow(this) < 0 ) { | |
135 return(-1); | |
136 } | |
137 continue; | |
138 } | |
139 SDL_SetError("Unable to set HDC pixel format"); | |
140 return(-1); | |
141 } | |
142 /* We either succeeded or failed by this point */ | |
143 break; | |
94 } | 144 } |
95 DescribePixelFormat(GL_hdc, pixel_format, sizeof(GL_pfd), &GL_pfd); | 145 DescribePixelFormat(GL_hdc, pixel_format, sizeof(GL_pfd), &GL_pfd); |
96 | 146 |
97 GL_hrc = this->gl_data->wglCreateContext(GL_hdc); | 147 GL_hrc = this->gl_data->wglCreateContext(GL_hdc); |
98 if( GL_hrc == NULL ) { | 148 if( GL_hrc == NULL ) { |