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