Mercurial > sdl-ios-xcode
annotate src/video/wincommon/SDL_wingl.c @ 340:b9f1ce709960
Fixed a minor memory leak in the SDL thread subsystem
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 11 Apr 2002 21:08:44 +0000 |
parents | 13fc64213765 |
children | c638fde8a824 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
297
f6ffac90895c
Updated copyright information for 2002
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 /* WGL implementation of SDL OpenGL support */ | |
29 | |
327
13fc64213765
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
30 #include "SDL_opengl.h" |
0 | 31 #include "SDL_error.h" |
32 #include "SDL_lowvideo.h" | |
33 #include "SDL_wingl_c.h" | |
34 | |
35 #ifdef HAVE_OPENGL | |
36 #define DEFAULT_GL_DRIVER_PATH "OPENGL32.DLL" | |
37 #endif | |
38 | |
39 | |
40 int WIN_GL_SetupWindow(_THIS) | |
41 { | |
42 int retval; | |
43 #ifdef HAVE_OPENGL | |
44 int pixel_format; | |
45 | |
46 /* load the gl driver from a default path */ | |
47 if ( ! this->gl_config.driver_loaded ) { | |
48 /* no driver has been loaded, use default (ourselves) */ | |
49 if ( WIN_GL_LoadLibrary(this, NULL) < 0 ) { | |
50 return(-1); | |
51 } | |
52 } | |
53 | |
54 /* Get the window device context for our OpenGL drawing */ | |
55 GL_hdc = GetDC(SDL_Window); | |
56 if ( GL_hdc == NULL ) { | |
57 SDL_SetError("Unable to get DC for SDL_Window"); | |
58 return(-1); | |
59 } | |
60 | |
61 /* Set up the pixel format descriptor with our needed format */ | |
62 memset(&GL_pfd, 0, sizeof(GL_pfd)); | |
63 GL_pfd.nSize = sizeof(GL_pfd); | |
64 GL_pfd.nVersion = 1; | |
65 GL_pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL); | |
66 if ( this->gl_config.double_buffer ) { | |
67 GL_pfd.dwFlags |= PFD_DOUBLEBUFFER; | |
68 } | |
69 GL_pfd.iPixelType = PFD_TYPE_RGBA; | |
70 GL_pfd.cColorBits = this->gl_config.buffer_size; | |
71 GL_pfd.cRedBits = this->gl_config.red_size; | |
72 GL_pfd.cGreenBits = this->gl_config.green_size; | |
73 GL_pfd.cBlueBits = this->gl_config.blue_size; | |
74 GL_pfd.cAlphaBits = this->gl_config.alpha_size; | |
75 GL_pfd.cAccumRedBits = this->gl_config.accum_red_size; | |
76 GL_pfd.cAccumGreenBits = this->gl_config.accum_green_size; | |
77 GL_pfd.cAccumBlueBits = this->gl_config.accum_blue_size; | |
78 GL_pfd.cAccumAlphaBits = this->gl_config.accum_alpha_size; | |
79 GL_pfd.cAccumBits = | |
80 (GL_pfd.cAccumRedBits + GL_pfd.cAccumGreenBits + | |
81 GL_pfd.cAccumBlueBits + GL_pfd.cAccumAlphaBits); | |
82 GL_pfd.cDepthBits = this->gl_config.depth_size; | |
83 GL_pfd.cStencilBits = this->gl_config.stencil_size; | |
84 | |
85 /* Choose and set the closest available pixel format */ | |
86 pixel_format = ChoosePixelFormat(GL_hdc, &GL_pfd); | |
87 if ( !pixel_format ) { | |
88 SDL_SetError("No matching GL pixel format available"); | |
89 return(-1); | |
90 } | |
91 if( !SetPixelFormat(GL_hdc, pixel_format, &GL_pfd) ) { | |
92 SDL_SetError("Unable to set HDC pixel format"); | |
93 return(-1); | |
94 } | |
95 DescribePixelFormat(GL_hdc, pixel_format, sizeof(GL_pfd), &GL_pfd); | |
96 | |
97 GL_hrc = this->gl_data->wglCreateContext(GL_hdc); | |
98 if( GL_hrc == NULL ) { | |
99 SDL_SetError("Unable to create GL context"); | |
100 return(-1); | |
101 } | |
102 gl_active = 1; | |
103 #else | |
104 SDL_SetError("WIN driver not configured with OpenGL"); | |
105 #endif | |
106 if ( gl_active ) { | |
107 retval = 0; | |
108 } else { | |
109 retval = -1; | |
110 } | |
111 return(retval); | |
112 } | |
113 | |
114 void WIN_GL_ShutDown(_THIS) | |
115 { | |
116 #ifdef HAVE_OPENGL | |
117 /* Clean up OpenGL */ | |
118 if ( GL_hrc ) { | |
119 this->gl_data->wglMakeCurrent(NULL, NULL); | |
120 this->gl_data->wglDeleteContext(GL_hrc); | |
121 GL_hrc = NULL; | |
122 } | |
123 if ( GL_hdc ) { | |
124 ReleaseDC(SDL_Window, GL_hdc); | |
125 GL_hdc = NULL; | |
126 } | |
127 gl_active = 0; | |
128 | |
129 WIN_GL_UnloadLibrary(this); | |
130 #endif /* HAVE_OPENGL */ | |
131 } | |
132 | |
133 #ifdef HAVE_OPENGL | |
134 | |
135 /* Make the current context active */ | |
136 int WIN_GL_MakeCurrent(_THIS) | |
137 { | |
138 int retval; | |
139 | |
140 retval = 0; | |
141 if ( ! this->gl_data->wglMakeCurrent(GL_hdc, GL_hrc) ) { | |
142 SDL_SetError("Unable to make GL context current"); | |
143 retval = -1; | |
144 } | |
145 return(retval); | |
146 } | |
147 | |
148 /* Get attribute data from glX. */ | |
149 int WIN_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value) | |
150 { | |
151 int retval; | |
152 | |
153 retval = 0; | |
154 switch( attrib ) { | |
155 case SDL_GL_RED_SIZE: | |
156 *value = GL_pfd.cRedBits; | |
157 break; | |
158 case SDL_GL_GREEN_SIZE: | |
159 *value = GL_pfd.cGreenBits; | |
160 break; | |
161 case SDL_GL_BLUE_SIZE: | |
162 *value = GL_pfd.cBlueBits; | |
163 break; | |
164 case SDL_GL_ALPHA_SIZE: | |
165 *value = GL_pfd.cAlphaBits; | |
166 break; | |
167 case SDL_GL_DOUBLEBUFFER: | |
168 if ( GL_pfd.dwFlags & PFD_DOUBLEBUFFER ) { | |
169 *value = 1; | |
170 } else { | |
171 *value = 0; | |
172 } | |
173 break; | |
174 case SDL_GL_BUFFER_SIZE: | |
175 *value = GL_pfd.cColorBits; | |
176 break; | |
177 case SDL_GL_DEPTH_SIZE: | |
178 *value = GL_pfd.cDepthBits; | |
179 break; | |
180 case SDL_GL_STENCIL_SIZE: | |
181 *value = GL_pfd.cStencilBits; | |
182 break; | |
183 case SDL_GL_ACCUM_RED_SIZE: | |
184 *value = GL_pfd.cAccumRedBits; | |
185 break; | |
186 case SDL_GL_ACCUM_GREEN_SIZE: | |
187 *value = GL_pfd.cAccumGreenBits; | |
188 break; | |
189 case SDL_GL_ACCUM_BLUE_SIZE: | |
190 *value = GL_pfd.cAccumBlueBits; | |
191 break; | |
192 case SDL_GL_ACCUM_ALPHA_SIZE: | |
193 *value = GL_pfd.cAccumAlphaBits; | |
194 break; | |
195 default: | |
196 retval = -1; | |
197 break; | |
198 } | |
199 return retval; | |
200 } | |
201 | |
202 void WIN_GL_SwapBuffers(_THIS) | |
203 { | |
204 SwapBuffers(GL_hdc); | |
205 } | |
206 | |
207 #endif /* HAVE_OPENGL */ | |
208 | |
209 #ifdef HAVE_OPENGL | |
210 | |
211 void WIN_GL_UnloadLibrary(_THIS) | |
212 { | |
213 if ( this->gl_config.driver_loaded ) { | |
214 FreeLibrary((HMODULE)this->gl_config.dll_handle); | |
215 | |
216 this->gl_data->wglGetProcAddress = NULL; | |
217 this->gl_data->wglCreateContext = NULL; | |
218 this->gl_data->wglDeleteContext = NULL; | |
219 this->gl_data->wglMakeCurrent = NULL; | |
220 | |
221 this->gl_config.dll_handle = NULL; | |
222 this->gl_config.driver_loaded = 0; | |
223 } | |
224 } | |
225 | |
226 /* Passing a NULL path means load pointers from the application */ | |
227 int WIN_GL_LoadLibrary(_THIS, const char* path) | |
228 { | |
229 HMODULE handle; | |
230 | |
231 if ( gl_active ) { | |
232 SDL_SetError("OpenGL context already created"); | |
233 return -1; | |
234 } | |
235 | |
236 if ( path == NULL ) { | |
237 path = DEFAULT_GL_DRIVER_PATH; | |
238 } | |
239 handle = LoadLibrary(path); | |
240 if ( handle == NULL ) { | |
241 SDL_SetError("Could not load OpenGL library"); | |
242 return -1; | |
243 } | |
244 | |
245 /* Unload the old driver and reset the pointers */ | |
246 WIN_GL_UnloadLibrary(this); | |
247 | |
248 /* Load new function pointers */ | |
249 this->gl_data->wglGetProcAddress = (void * (WINAPI *)(const char *)) | |
250 GetProcAddress(handle, "wglGetProcAddress"); | |
251 this->gl_data->wglCreateContext = (HGLRC (WINAPI *)(HDC)) | |
252 GetProcAddress(handle, "wglCreateContext"); | |
253 this->gl_data->wglDeleteContext = (BOOL (WINAPI *)(HGLRC)) | |
254 GetProcAddress(handle, "wglDeleteContext"); | |
255 this->gl_data->wglMakeCurrent = (BOOL (WINAPI *)(HDC, HGLRC)) | |
256 GetProcAddress(handle, "wglMakeCurrent"); | |
257 | |
258 if ( (this->gl_data->wglGetProcAddress == NULL) || | |
259 (this->gl_data->wglCreateContext == NULL) || | |
260 (this->gl_data->wglDeleteContext == NULL) || | |
261 (this->gl_data->wglMakeCurrent == NULL) ) { | |
262 SDL_SetError("Could not retrieve OpenGL functions"); | |
263 FreeLibrary(handle); | |
264 return -1; | |
265 } | |
266 | |
267 this->gl_config.dll_handle = handle; | |
268 strcpy(this->gl_config.driver_path, path); | |
269 this->gl_config.driver_loaded = 1; | |
270 return 0; | |
271 } | |
272 | |
273 void *WIN_GL_GetProcAddress(_THIS, const char* proc) | |
274 { | |
275 void *func; | |
276 | |
277 /* This is to pick up extensions */ | |
278 func = this->gl_data->wglGetProcAddress(proc); | |
279 if ( ! func ) { | |
280 /* This is probably a normal GL function */ | |
281 func = GetProcAddress(this->gl_config.dll_handle, proc); | |
282 } | |
283 return func; | |
284 } | |
285 | |
286 #endif /* HAVE_OPENGL */ |