src/video/wincommon/SDL_wingl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 09 Aug 2003 18:27:27 +0000
changeset 677 92a933bf2f0f
parent 671 338a62f9a528
child 766 ed57c876700d
permissions -rw-r--r--
*** empty log message ***
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@655
    28
#include <string.h>
slouken@655
    29
slouken@0
    30
/* WGL implementation of SDL OpenGL support */
slouken@0
    31
slouken@453
    32
#ifdef HAVE_OPENGL
slouken@327
    33
#include "SDL_opengl.h"
slouken@453
    34
#endif
slouken@0
    35
#include "SDL_error.h"
slouken@0
    36
#include "SDL_lowvideo.h"
slouken@0
    37
#include "SDL_wingl_c.h"
slouken@0
    38
slouken@0
    39
#ifdef HAVE_OPENGL
slouken@0
    40
#define DEFAULT_GL_DRIVER_PATH "OPENGL32.DLL"
slouken@0
    41
#endif
slouken@0
    42
slouken@373
    43
/* If setting the HDC fails, we may need to recreate the window (MSDN) */
slouken@373
    44
static int WIN_GL_ResetWindow(_THIS)
slouken@373
    45
{
slouken@373
    46
	int status = 0;
slouken@373
    47
	int can_reset = 1;
slouken@373
    48
slouken@373
    49
	/* If we were passed a window, then we can't create a new one */
slouken@373
    50
	if ( SDL_windowid ) {
slouken@373
    51
		can_reset = 0;
slouken@373
    52
	}
slouken@373
    53
#ifndef _WIN32_WCE /* FIXME WinCE needs the UNICODE version of CreateWindow() */
slouken@373
    54
	if ( can_reset ) {
slouken@373
    55
		/* Save the existing window attributes */
slouken@373
    56
		LONG style;
slouken@373
    57
		RECT rect = { 0, 0, 0, 0 };
slouken@373
    58
		style = GetWindowLong(SDL_Window, GWL_STYLE);
slouken@373
    59
		GetWindowRect(SDL_Window, &rect);
slouken@373
    60
		DestroyWindow(SDL_Window);
slouken@373
    61
		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
slouken@373
    62
		                          style,
slouken@373
    63
					  rect.left, rect.top,
slouken@373
    64
                                          (rect.right-rect.left)+1,
slouken@373
    65
                                          (rect.top-rect.bottom)+1,
slouken@373
    66
		                          NULL, NULL, SDL_Instance, NULL);
slouken@373
    67
		if ( SDL_Window ) {
slouken@373
    68
			this->SetCaption(this, this->wm_title, this->wm_icon);
slouken@373
    69
		} else {
slouken@373
    70
			SDL_SetError("Couldn't create window");
slouken@373
    71
			status = -1;
slouken@373
    72
		}
slouken@373
    73
	} else
slouken@373
    74
#endif /* !_WIN32_WCE */
slouken@373
    75
	{
slouken@373
    76
		SDL_SetError("Unable to reset window for OpenGL context");
slouken@373
    77
		status = -1;
slouken@373
    78
	}
slouken@373
    79
	return(status);
slouken@373
    80
}
slouken@0
    81
slouken@677
    82
static void Init_WGL_ARB_extensions(_THIS)
slouken@655
    83
{
slouken@655
    84
	HWND hwnd;
slouken@655
    85
	HDC hdc;
slouken@655
    86
	HGLRC hglrc;
slouken@655
    87
	int pformat;
slouken@655
    88
	const char * (WINAPI *wglGetExtensionsStringARB)(HDC) = 0;
slouken@655
    89
	
slouken@671
    90
	hwnd = CreateWindow(SDL_Appname, SDL_Appname, WS_POPUP | WS_DISABLED,
slouken@655
    91
	                    0, 0, 10, 10,
slouken@655
    92
	                    NULL, NULL, SDL_Instance,NULL);
slouken@655
    93
	hdc = GetDC(hwnd);
slouken@655
    94
slouken@655
    95
	pformat = ChoosePixelFormat(hdc, &GL_pfd);
slouken@655
    96
	SetPixelFormat(hdc, pformat, &GL_pfd);
slouken@655
    97
slouken@655
    98
	hglrc = this->gl_data->wglCreateContext(hdc);
slouken@671
    99
	if ( hglrc ) {
slouken@671
   100
		this->gl_data->wglMakeCurrent(hdc, hglrc);
slouken@671
   101
	}
slouken@655
   102
slouken@655
   103
	wglGetExtensionsStringARB = (const char * (WINAPI *)(HDC))
slouken@655
   104
		this->gl_data->wglGetProcAddress("wglGetExtensionsStringARB");
slouken@655
   105
slouken@655
   106
	if(wglGetExtensionsStringARB && strstr(wglGetExtensionsStringARB(hdc),"WGL_ARB_pixel_format")) {
slouken@655
   107
		this->gl_data->wglChoosePixelFormatARB =
slouken@655
   108
			(BOOL (WINAPI *)(HDC, const int *, const FLOAT *, UINT, int *, UINT *))
slouken@655
   109
			this->gl_data->wglGetProcAddress("wglChoosePixelFormatARB");
slouken@655
   110
		this->gl_data->wglGetPixelFormatAttribivARB =
slouken@655
   111
			(BOOL (WINAPI *)(HDC, int, int, UINT, const int *, int *))
slouken@655
   112
			this->gl_data->wglGetProcAddress("wglGetPixelFormatAttribivARB");
slouken@655
   113
slouken@655
   114
		if( (this->gl_data->wglChoosePixelFormatARB != NULL) &&
slouken@655
   115
		    (this->gl_data->wglGetPixelFormatAttribivARB != NULL) )
slouken@655
   116
			this->gl_data->wgl_arb_pixel_format = 1;
slouken@655
   117
		else 
slouken@655
   118
			this->gl_data->wgl_arb_pixel_format = 0;
slouken@655
   119
	} else {
slouken@655
   120
		this->gl_data->wgl_arb_pixel_format = 0;
slouken@655
   121
	}
slouken@655
   122
	
slouken@671
   123
	if ( hglrc ) {
slouken@671
   124
		this->gl_data->wglMakeCurrent(NULL, NULL);
slouken@671
   125
		this->gl_data->wglDeleteContext(hglrc);
slouken@671
   126
	}
slouken@655
   127
	ReleaseDC(hwnd, hdc);
slouken@655
   128
	DestroyWindow(hwnd);
slouken@655
   129
}
slouken@655
   130
slouken@0
   131
int WIN_GL_SetupWindow(_THIS)
slouken@0
   132
{
slouken@0
   133
	int retval;
slouken@0
   134
#ifdef HAVE_OPENGL
slouken@373
   135
	int i;
slouken@655
   136
	unsigned int matching;
slouken@655
   137
	int iAttribs[64];
slouken@655
   138
	int *iAttr;
slouken@655
   139
	float fAttribs[1] = { 0 };
slouken@0
   140
slouken@0
   141
	/* load the gl driver from a default path */
slouken@0
   142
	if ( ! this->gl_config.driver_loaded ) {
slouken@0
   143
		/* no driver has been loaded, use default (ourselves) */
slouken@0
   144
		if ( WIN_GL_LoadLibrary(this, NULL) < 0 ) {
slouken@0
   145
			return(-1);
slouken@0
   146
		}
slouken@0
   147
	}
slouken@0
   148
slouken@373
   149
	for ( i=0; ; ++i ) {
slouken@373
   150
		/* Get the window device context for our OpenGL drawing */
slouken@373
   151
		GL_hdc = GetDC(SDL_Window);
slouken@373
   152
		if ( GL_hdc == NULL ) {
slouken@373
   153
			SDL_SetError("Unable to get DC for SDL_Window");
slouken@373
   154
			return(-1);
slouken@373
   155
		}
slouken@0
   156
slouken@373
   157
		/* Set up the pixel format descriptor with our needed format */
slouken@373
   158
		memset(&GL_pfd, 0, sizeof(GL_pfd));
slouken@373
   159
		GL_pfd.nSize = sizeof(GL_pfd);
slouken@373
   160
		GL_pfd.nVersion = 1;
slouken@373
   161
		GL_pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL);
slouken@373
   162
		if ( this->gl_config.double_buffer ) {
slouken@373
   163
			GL_pfd.dwFlags |= PFD_DOUBLEBUFFER;
slouken@373
   164
		}
slouken@450
   165
		if ( this->gl_config.stereo ) {
slouken@450
   166
			GL_pfd.dwFlags |= PFD_STEREO;
slouken@450
   167
		}
slouken@373
   168
		GL_pfd.iPixelType = PFD_TYPE_RGBA;
slouken@373
   169
		GL_pfd.cColorBits = this->gl_config.buffer_size;
slouken@373
   170
		GL_pfd.cRedBits = this->gl_config.red_size;
slouken@373
   171
		GL_pfd.cGreenBits = this->gl_config.green_size;
slouken@373
   172
		GL_pfd.cBlueBits = this->gl_config.blue_size;
slouken@373
   173
		GL_pfd.cAlphaBits = this->gl_config.alpha_size;
slouken@373
   174
		GL_pfd.cAccumRedBits = this->gl_config.accum_red_size;
slouken@373
   175
		GL_pfd.cAccumGreenBits = this->gl_config.accum_green_size;
slouken@373
   176
		GL_pfd.cAccumBlueBits = this->gl_config.accum_blue_size;
slouken@373
   177
		GL_pfd.cAccumAlphaBits = this->gl_config.accum_alpha_size;
slouken@373
   178
		GL_pfd.cAccumBits =
slouken@373
   179
			(GL_pfd.cAccumRedBits + GL_pfd.cAccumGreenBits +
slouken@373
   180
			 GL_pfd.cAccumBlueBits + GL_pfd.cAccumAlphaBits);
slouken@373
   181
		GL_pfd.cDepthBits = this->gl_config.depth_size;
slouken@373
   182
		GL_pfd.cStencilBits = this->gl_config.stencil_size;
slouken@0
   183
slouken@655
   184
		/* initialize WGL_ARB_pixel_format */
slouken@655
   185
		Init_WGL_ARB_extensions(this);
slouken@655
   186
slouken@655
   187
		/* setup WGL_ARB_pixel_format attribs */
slouken@655
   188
		iAttr = &iAttribs[0];
slouken@655
   189
slouken@655
   190
		*iAttr++ = WGL_DRAW_TO_WINDOW_ARB;
slouken@655
   191
		*iAttr++ = GL_TRUE;
slouken@655
   192
		*iAttr++ = WGL_ACCELERATION_ARB;
slouken@655
   193
		*iAttr++ = WGL_FULL_ACCELERATION_ARB;
slouken@655
   194
		*iAttr++ = WGL_RED_BITS_ARB;
slouken@655
   195
		*iAttr++ = this->gl_config.red_size;
slouken@655
   196
		*iAttr++ = WGL_GREEN_BITS_ARB;
slouken@655
   197
		*iAttr++ = this->gl_config.green_size;
slouken@655
   198
		*iAttr++ = WGL_BLUE_BITS_ARB;
slouken@655
   199
		*iAttr++ = this->gl_config.blue_size;
slouken@655
   200
		
slouken@655
   201
		if ( this->gl_config.alpha_size ) {
slouken@655
   202
			*iAttr++ = WGL_ALPHA_BITS_ARB;
slouken@655
   203
			*iAttr++ = this->gl_config.alpha_size;
slouken@655
   204
		}
slouken@655
   205
slouken@655
   206
		if ( this->gl_config.double_buffer ) {
slouken@655
   207
			*iAttr ++ = WGL_DOUBLE_BUFFER_ARB;
slouken@655
   208
			*iAttr ++ = GL_TRUE;
slouken@655
   209
		}
slouken@655
   210
slouken@655
   211
		*iAttr++ = WGL_DEPTH_BITS_ARB;
slouken@655
   212
		*iAttr++ = this->gl_config.depth_size;
slouken@655
   213
slouken@655
   214
		if ( this->gl_config.stencil_size ) {
slouken@655
   215
			*iAttr++ = WGL_STENCIL_BITS_ARB;
slouken@655
   216
			*iAttr++ = this->gl_config.stencil_size;
slouken@655
   217
		}
slouken@655
   218
slouken@655
   219
		if ( this->gl_config.accum_red_size ) {
slouken@655
   220
			*iAttr++ = WGL_ACCUM_RED_BITS_ARB;
slouken@655
   221
			*iAttr++ = this->gl_config.accum_red_size;
slouken@655
   222
		}
slouken@655
   223
slouken@655
   224
		if ( this->gl_config.accum_green_size ) {
slouken@655
   225
			*iAttr++ = WGL_ACCUM_GREEN_BITS_ARB;
slouken@655
   226
			*iAttr++ = this->gl_config.accum_green_size;
slouken@655
   227
		}
slouken@655
   228
slouken@655
   229
		if ( this->gl_config.accum_blue_size ) {
slouken@655
   230
			*iAttr++ = WGL_ACCUM_BLUE_BITS_ARB;
slouken@655
   231
			*iAttr++ = this->gl_config.accum_blue_size;
slouken@655
   232
		}
slouken@655
   233
slouken@655
   234
		if ( this->gl_config.accum_alpha_size ) {
slouken@655
   235
			*iAttr++ = WGL_ACCUM_ALPHA_BITS_ARB;
slouken@655
   236
			*iAttr++ = this->gl_config.accum_alpha_size;
slouken@655
   237
		}
slouken@655
   238
slouken@655
   239
		if ( this->gl_config.stereo ) {
slouken@655
   240
			*iAttr++ = WGL_STEREO_ARB;
slouken@655
   241
			*iAttr++ = this->gl_config.stereo;
slouken@655
   242
		}
slouken@655
   243
slouken@656
   244
		if ( this->gl_config.multisamplebuffers ) {
slouken@655
   245
			*iAttr++ = WGL_SAMPLE_BUFFERS_ARB;
slouken@656
   246
			*iAttr++ = this->gl_config.multisamplebuffers;
slouken@655
   247
		}
slouken@655
   248
slouken@656
   249
		if ( this->gl_config.multisamplesamples ) {
slouken@655
   250
			*iAttr++ = WGL_SAMPLES_ARB;
slouken@656
   251
			*iAttr++ = this->gl_config.multisamplesamples;
slouken@655
   252
		}
slouken@655
   253
slouken@655
   254
		*iAttr = 0;
slouken@655
   255
slouken@373
   256
		/* Choose and set the closest available pixel format */
slouken@655
   257
		if ( !this->gl_data->wgl_arb_pixel_format ||
slouken@655
   258
		     !this->gl_data->wglChoosePixelFormatARB(GL_hdc, iAttribs, fAttribs, 1, &pixel_format, &matching) ||
slouken@655
   259
		     !matching ) {
slouken@655
   260
			pixel_format = ChoosePixelFormat(GL_hdc, &GL_pfd);
slouken@655
   261
			this->gl_data->wgl_arb_pixel_format = 0;
slouken@655
   262
		}
slouken@373
   263
		if ( !pixel_format ) {
slouken@373
   264
			SDL_SetError("No matching GL pixel format available");
slouken@373
   265
			return(-1);
slouken@373
   266
		}
slouken@655
   267
		if ( !SetPixelFormat(GL_hdc, pixel_format, &GL_pfd) ) {
slouken@373
   268
			if ( i == 0 ) {
slouken@373
   269
				/* First time through, try resetting the window */
slouken@373
   270
				if ( WIN_GL_ResetWindow(this) < 0 ) {
slouken@373
   271
					return(-1);
slouken@373
   272
				}
slouken@373
   273
				continue;
slouken@373
   274
			}
slouken@373
   275
			SDL_SetError("Unable to set HDC pixel format");
slouken@373
   276
			return(-1);
slouken@373
   277
		}
slouken@373
   278
		/* We either succeeded or failed by this point */
slouken@373
   279
		break;
slouken@0
   280
	}
slouken@0
   281
	DescribePixelFormat(GL_hdc, pixel_format, sizeof(GL_pfd), &GL_pfd);
slouken@0
   282
slouken@0
   283
	GL_hrc = this->gl_data->wglCreateContext(GL_hdc);
slouken@655
   284
	if ( GL_hrc == NULL ) {
slouken@0
   285
		SDL_SetError("Unable to create GL context");
slouken@0
   286
		return(-1);
slouken@0
   287
	}
slouken@0
   288
	gl_active = 1;
slouken@0
   289
#else
slouken@0
   290
	SDL_SetError("WIN driver not configured with OpenGL");
slouken@0
   291
#endif
slouken@0
   292
	if ( gl_active ) {
slouken@0
   293
		retval = 0;
slouken@0
   294
	} else {
slouken@0
   295
		retval = -1;
slouken@0
   296
	}
slouken@0
   297
	return(retval);
slouken@0
   298
}
slouken@0
   299
slouken@0
   300
void WIN_GL_ShutDown(_THIS)
slouken@0
   301
{
slouken@0
   302
#ifdef HAVE_OPENGL
slouken@0
   303
	/* Clean up OpenGL */
slouken@0
   304
	if ( GL_hrc ) {
slouken@0
   305
		this->gl_data->wglMakeCurrent(NULL, NULL);
slouken@0
   306
		this->gl_data->wglDeleteContext(GL_hrc);
slouken@0
   307
		GL_hrc = NULL;
slouken@0
   308
	}
slouken@0
   309
	if ( GL_hdc ) {
slouken@0
   310
		ReleaseDC(SDL_Window, GL_hdc);
slouken@0
   311
		GL_hdc = NULL;
slouken@0
   312
	}
slouken@0
   313
	gl_active = 0;
slouken@0
   314
slouken@0
   315
	WIN_GL_UnloadLibrary(this);
slouken@0
   316
#endif /* HAVE_OPENGL */
slouken@0
   317
}
slouken@0
   318
slouken@0
   319
#ifdef HAVE_OPENGL
slouken@0
   320
slouken@0
   321
/* Make the current context active */
slouken@0
   322
int WIN_GL_MakeCurrent(_THIS)
slouken@0
   323
{
slouken@0
   324
	int retval;
slouken@0
   325
slouken@0
   326
	retval = 0;
slouken@0
   327
	if ( ! this->gl_data->wglMakeCurrent(GL_hdc, GL_hrc) ) {
slouken@0
   328
		SDL_SetError("Unable to make GL context current");
slouken@0
   329
		retval = -1;
slouken@0
   330
	}
slouken@0
   331
	return(retval);
slouken@0
   332
}
slouken@0
   333
slouken@0
   334
/* Get attribute data from glX. */
slouken@0
   335
int WIN_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   336
{
slouken@0
   337
	int retval;
slouken@655
   338
	
slouken@655
   339
	if ( this->gl_data->wgl_arb_pixel_format ) {
slouken@655
   340
		int wgl_attrib;
slouken@655
   341
slouken@655
   342
		switch(attrib) {
slouken@655
   343
		    case SDL_GL_RED_SIZE:
slouken@655
   344
			wgl_attrib = WGL_RED_BITS_ARB;
slouken@655
   345
			break;
slouken@655
   346
		    case SDL_GL_GREEN_SIZE:
slouken@655
   347
			wgl_attrib = WGL_GREEN_BITS_ARB;
slouken@655
   348
			break;
slouken@655
   349
		    case SDL_GL_BLUE_SIZE:
slouken@655
   350
			wgl_attrib = WGL_BLUE_BITS_ARB;
slouken@655
   351
			break;
slouken@655
   352
		    case SDL_GL_ALPHA_SIZE:
slouken@655
   353
			wgl_attrib = WGL_ALPHA_BITS_ARB;
slouken@655
   354
			break;
slouken@655
   355
		    case SDL_GL_DOUBLEBUFFER:
slouken@655
   356
			wgl_attrib = WGL_DOUBLE_BUFFER_ARB;
slouken@655
   357
			break;
slouken@655
   358
		    case SDL_GL_BUFFER_SIZE:
slouken@655
   359
			wgl_attrib = WGL_COLOR_BITS_ARB;
slouken@655
   360
			break;
slouken@655
   361
		    case SDL_GL_DEPTH_SIZE:
slouken@655
   362
			wgl_attrib = WGL_DEPTH_BITS_ARB;
slouken@655
   363
			break;
slouken@655
   364
		    case SDL_GL_STENCIL_SIZE:
slouken@655
   365
			wgl_attrib = WGL_STENCIL_BITS_ARB;
slouken@655
   366
			break;
slouken@655
   367
		    case SDL_GL_ACCUM_RED_SIZE:
slouken@655
   368
			wgl_attrib = WGL_ACCUM_RED_BITS_ARB;
slouken@655
   369
			break;
slouken@655
   370
		    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@655
   371
			wgl_attrib = WGL_ACCUM_GREEN_BITS_ARB;
slouken@655
   372
			break;
slouken@655
   373
		    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@655
   374
			wgl_attrib = WGL_ACCUM_BLUE_BITS_ARB;
slouken@655
   375
			break;
slouken@655
   376
		    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@655
   377
			wgl_attrib = WGL_ACCUM_ALPHA_BITS_ARB;
slouken@655
   378
			break;
slouken@655
   379
		    case SDL_GL_STEREO:
slouken@655
   380
			wgl_attrib = WGL_STEREO_ARB;
slouken@655
   381
			break;
slouken@656
   382
		    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@655
   383
			wgl_attrib = WGL_SAMPLE_BUFFERS_ARB;
slouken@655
   384
			break;
slouken@656
   385
		    case SDL_GL_MULTISAMPLESAMPLES:
slouken@655
   386
			wgl_attrib = WGL_SAMPLES_ARB;
slouken@655
   387
			break;
slouken@655
   388
		    default:
slouken@655
   389
			return(-1);
slouken@655
   390
		}
slouken@655
   391
		this->gl_data->wglGetPixelFormatAttribivARB(GL_hdc, pixel_format, 0, 1, &wgl_attrib, value);
slouken@655
   392
slouken@655
   393
		return 0;
slouken@655
   394
	}
slouken@0
   395
slouken@0
   396
	retval = 0;
slouken@655
   397
	switch ( attrib ) {
slouken@0
   398
	    case SDL_GL_RED_SIZE:
slouken@0
   399
		*value = GL_pfd.cRedBits;
slouken@0
   400
		break;
slouken@0
   401
	    case SDL_GL_GREEN_SIZE:
slouken@0
   402
		*value = GL_pfd.cGreenBits;
slouken@0
   403
		break;
slouken@0
   404
	    case SDL_GL_BLUE_SIZE:
slouken@0
   405
		*value = GL_pfd.cBlueBits;
slouken@0
   406
		break;
slouken@0
   407
	    case SDL_GL_ALPHA_SIZE:
slouken@0
   408
		*value = GL_pfd.cAlphaBits;
slouken@0
   409
		break;
slouken@0
   410
	    case SDL_GL_DOUBLEBUFFER:
slouken@0
   411
		if ( GL_pfd.dwFlags & PFD_DOUBLEBUFFER ) {
slouken@0
   412
			*value = 1;
slouken@0
   413
		} else {
slouken@0
   414
			*value = 0;
slouken@0
   415
		}
slouken@0
   416
		break;
slouken@0
   417
	    case SDL_GL_BUFFER_SIZE:
slouken@0
   418
		*value = GL_pfd.cColorBits;
slouken@0
   419
		break;
slouken@0
   420
	    case SDL_GL_DEPTH_SIZE:
slouken@0
   421
		*value = GL_pfd.cDepthBits;
slouken@0
   422
		break;
slouken@0
   423
	    case SDL_GL_STENCIL_SIZE:
slouken@0
   424
		*value = GL_pfd.cStencilBits;
slouken@0
   425
		break;
slouken@0
   426
	    case SDL_GL_ACCUM_RED_SIZE:
slouken@0
   427
		*value = GL_pfd.cAccumRedBits;
slouken@0
   428
		break;
slouken@0
   429
	    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@0
   430
		*value = GL_pfd.cAccumGreenBits;
slouken@0
   431
		break;
slouken@0
   432
	    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@0
   433
		*value = GL_pfd.cAccumBlueBits;
slouken@0
   434
		break;
slouken@0
   435
	    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@0
   436
		*value = GL_pfd.cAccumAlphaBits;
slouken@0
   437
		break;
slouken@450
   438
	    case SDL_GL_STEREO:
slouken@450
   439
		if ( GL_pfd.dwFlags & PFD_STEREO ) {
slouken@450
   440
			*value = 1;
slouken@450
   441
		} else {
slouken@450
   442
			*value = 0;
slouken@450
   443
		}
slouken@450
   444
		break;
slouken@0
   445
	    default:
slouken@0
   446
		retval = -1;
slouken@0
   447
		break;
slouken@0
   448
	}
slouken@0
   449
	return retval;
slouken@0
   450
}
slouken@0
   451
slouken@0
   452
void WIN_GL_SwapBuffers(_THIS)
slouken@0
   453
{
slouken@0
   454
	SwapBuffers(GL_hdc);
slouken@0
   455
}
slouken@0
   456
slouken@0
   457
void WIN_GL_UnloadLibrary(_THIS)
slouken@0
   458
{
slouken@0
   459
	if ( this->gl_config.driver_loaded ) {
slouken@0
   460
		FreeLibrary((HMODULE)this->gl_config.dll_handle);
slouken@0
   461
slouken@0
   462
		this->gl_data->wglGetProcAddress = NULL;
slouken@0
   463
		this->gl_data->wglCreateContext = NULL;
slouken@0
   464
		this->gl_data->wglDeleteContext = NULL;
slouken@0
   465
		this->gl_data->wglMakeCurrent = NULL;
slouken@655
   466
		this->gl_data->wglChoosePixelFormatARB = NULL;
slouken@655
   467
		this->gl_data->wglGetPixelFormatAttribivARB = NULL;
slouken@0
   468
slouken@0
   469
		this->gl_config.dll_handle = NULL;
slouken@0
   470
		this->gl_config.driver_loaded = 0;
slouken@0
   471
	}
slouken@0
   472
}
slouken@0
   473
slouken@0
   474
/* Passing a NULL path means load pointers from the application */
slouken@0
   475
int WIN_GL_LoadLibrary(_THIS, const char* path) 
slouken@0
   476
{
slouken@0
   477
	HMODULE handle;
slouken@0
   478
slouken@0
   479
 	if ( gl_active ) {
slouken@0
   480
 		SDL_SetError("OpenGL context already created");
slouken@0
   481
 		return -1;
slouken@0
   482
 	}
slouken@0
   483
slouken@0
   484
	if ( path == NULL ) {
slouken@0
   485
		path = DEFAULT_GL_DRIVER_PATH;
slouken@0
   486
	}
slouken@0
   487
	handle = LoadLibrary(path);
slouken@0
   488
	if ( handle == NULL ) {
slouken@0
   489
		SDL_SetError("Could not load OpenGL library");
slouken@0
   490
		return -1;
slouken@0
   491
	}
slouken@0
   492
slouken@0
   493
	/* Unload the old driver and reset the pointers */
slouken@0
   494
	WIN_GL_UnloadLibrary(this);
slouken@0
   495
slouken@0
   496
	/* Load new function pointers */
slouken@655
   497
	memset(this->gl_data, 0, sizeof(*this->gl_data));
slouken@0
   498
	this->gl_data->wglGetProcAddress = (void * (WINAPI *)(const char *))
slouken@0
   499
		GetProcAddress(handle, "wglGetProcAddress");
slouken@0
   500
	this->gl_data->wglCreateContext = (HGLRC (WINAPI *)(HDC))
slouken@0
   501
		GetProcAddress(handle, "wglCreateContext");
slouken@0
   502
	this->gl_data->wglDeleteContext = (BOOL (WINAPI *)(HGLRC))
slouken@0
   503
		GetProcAddress(handle, "wglDeleteContext");
slouken@0
   504
	this->gl_data->wglMakeCurrent = (BOOL (WINAPI *)(HDC, HGLRC))
slouken@0
   505
		GetProcAddress(handle, "wglMakeCurrent");
slouken@0
   506
slouken@0
   507
	if ( (this->gl_data->wglGetProcAddress == NULL) ||
slouken@0
   508
	     (this->gl_data->wglCreateContext == NULL) ||
slouken@0
   509
	     (this->gl_data->wglDeleteContext == NULL) ||
slouken@0
   510
	     (this->gl_data->wglMakeCurrent == NULL) ) {
slouken@0
   511
		SDL_SetError("Could not retrieve OpenGL functions");
slouken@0
   512
		FreeLibrary(handle);
slouken@0
   513
		return -1;
slouken@0
   514
	}
slouken@0
   515
slouken@0
   516
	this->gl_config.dll_handle = handle;
slouken@0
   517
	strcpy(this->gl_config.driver_path, path);
slouken@0
   518
	this->gl_config.driver_loaded = 1;
slouken@0
   519
	return 0;
slouken@0
   520
}
slouken@0
   521
slouken@0
   522
void *WIN_GL_GetProcAddress(_THIS, const char* proc)
slouken@0
   523
{
slouken@0
   524
	void *func;
slouken@0
   525
slouken@0
   526
	/* This is to pick up extensions */
slouken@0
   527
	func = this->gl_data->wglGetProcAddress(proc);
slouken@0
   528
	if ( ! func ) {
slouken@0
   529
		/* This is probably a normal GL function */
slouken@0
   530
		func = GetProcAddress(this->gl_config.dll_handle, proc);
slouken@0
   531
	}
slouken@0
   532
	return func;
slouken@0
   533
}
slouken@0
   534
slouken@0
   535
#endif /* HAVE_OPENGL */