src/video/wincommon/SDL_wingl.c
author Sam Lantinga
Tue, 22 Jul 2003 15:10:06 +0000
changeset 655 9c42ee1b7d77
parent 453 a6fa62b1be09
child 656 864e2d2a9a55
permissions -rw-r--r--
Date: Thu, 24 Apr 2003 15:13:47 -0400
From: Shawn Kirst
Subject: SDL-1.2.5 patch to add ARB_multisample support

Attached is a patch I have written for SDL-1.2.5 that adds ARB_multisample
support. I only have the X11 and Win32 video patched. The Win32 patch also
adds support for WGL_ARB_pixel_format, as it was required for getting a
multisample capable pixel format. No additional GL header files are required
to compile on either platform (though you need an up-to-date glx.h for X11).

Requesting a multisample pixel format is made possible using
SDL_GL_SetAttribute with the two new SDL_GLattr's I've added
(SDL_GL_SAMPLE_BUFFERS and SDL_GL_SAMPLES). I've been using SDL in my
projects for quite a while now, so I am happy to contribute back to the
project. Now you can have and control FSAA in your SDL/GL apps at the
application level!
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@655
    82
static int 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@655
    90
	hwnd = CreateWindow(NULL, "PFormat", 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@655
    99
	this->gl_data->wglMakeCurrent(hdc, hglrc);
slouken@655
   100
slouken@655
   101
	wglGetExtensionsStringARB = (const char * (WINAPI *)(HDC))
slouken@655
   102
		this->gl_data->wglGetProcAddress("wglGetExtensionsStringARB");
slouken@655
   103
slouken@655
   104
	if(wglGetExtensionsStringARB && strstr(wglGetExtensionsStringARB(hdc),"WGL_ARB_pixel_format")) {
slouken@655
   105
		this->gl_data->wglChoosePixelFormatARB =
slouken@655
   106
			(BOOL (WINAPI *)(HDC, const int *, const FLOAT *, UINT, int *, UINT *))
slouken@655
   107
			this->gl_data->wglGetProcAddress("wglChoosePixelFormatARB");
slouken@655
   108
		this->gl_data->wglGetPixelFormatAttribivARB =
slouken@655
   109
			(BOOL (WINAPI *)(HDC, int, int, UINT, const int *, int *))
slouken@655
   110
			this->gl_data->wglGetProcAddress("wglGetPixelFormatAttribivARB");
slouken@655
   111
slouken@655
   112
		if( (this->gl_data->wglChoosePixelFormatARB != NULL) &&
slouken@655
   113
		    (this->gl_data->wglGetPixelFormatAttribivARB != NULL) )
slouken@655
   114
			this->gl_data->wgl_arb_pixel_format = 1;
slouken@655
   115
		else 
slouken@655
   116
			this->gl_data->wgl_arb_pixel_format = 0;
slouken@655
   117
	} else {
slouken@655
   118
		this->gl_data->wgl_arb_pixel_format = 0;
slouken@655
   119
	}
slouken@655
   120
	
slouken@655
   121
	this->gl_data->wglMakeCurrent(NULL, NULL);
slouken@655
   122
	this->gl_data->wglDeleteContext(hglrc);
slouken@655
   123
	ReleaseDC(hwnd, hdc);
slouken@655
   124
	DestroyWindow(hwnd);
slouken@655
   125
}
slouken@655
   126
slouken@0
   127
int WIN_GL_SetupWindow(_THIS)
slouken@0
   128
{
slouken@0
   129
	int retval;
slouken@0
   130
#ifdef HAVE_OPENGL
slouken@373
   131
	int i;
slouken@655
   132
	unsigned int matching;
slouken@655
   133
	int iAttribs[64];
slouken@655
   134
	int *iAttr;
slouken@655
   135
	float fAttribs[1] = { 0 };
slouken@0
   136
slouken@0
   137
	/* load the gl driver from a default path */
slouken@0
   138
	if ( ! this->gl_config.driver_loaded ) {
slouken@0
   139
		/* no driver has been loaded, use default (ourselves) */
slouken@0
   140
		if ( WIN_GL_LoadLibrary(this, NULL) < 0 ) {
slouken@0
   141
			return(-1);
slouken@0
   142
		}
slouken@0
   143
	}
slouken@0
   144
slouken@373
   145
	for ( i=0; ; ++i ) {
slouken@373
   146
		/* Get the window device context for our OpenGL drawing */
slouken@373
   147
		GL_hdc = GetDC(SDL_Window);
slouken@373
   148
		if ( GL_hdc == NULL ) {
slouken@373
   149
			SDL_SetError("Unable to get DC for SDL_Window");
slouken@373
   150
			return(-1);
slouken@373
   151
		}
slouken@0
   152
slouken@373
   153
		/* Set up the pixel format descriptor with our needed format */
slouken@373
   154
		memset(&GL_pfd, 0, sizeof(GL_pfd));
slouken@373
   155
		GL_pfd.nSize = sizeof(GL_pfd);
slouken@373
   156
		GL_pfd.nVersion = 1;
slouken@373
   157
		GL_pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL);
slouken@373
   158
		if ( this->gl_config.double_buffer ) {
slouken@373
   159
			GL_pfd.dwFlags |= PFD_DOUBLEBUFFER;
slouken@373
   160
		}
slouken@450
   161
		if ( this->gl_config.stereo ) {
slouken@450
   162
			GL_pfd.dwFlags |= PFD_STEREO;
slouken@450
   163
		}
slouken@373
   164
		GL_pfd.iPixelType = PFD_TYPE_RGBA;
slouken@373
   165
		GL_pfd.cColorBits = this->gl_config.buffer_size;
slouken@373
   166
		GL_pfd.cRedBits = this->gl_config.red_size;
slouken@373
   167
		GL_pfd.cGreenBits = this->gl_config.green_size;
slouken@373
   168
		GL_pfd.cBlueBits = this->gl_config.blue_size;
slouken@373
   169
		GL_pfd.cAlphaBits = this->gl_config.alpha_size;
slouken@373
   170
		GL_pfd.cAccumRedBits = this->gl_config.accum_red_size;
slouken@373
   171
		GL_pfd.cAccumGreenBits = this->gl_config.accum_green_size;
slouken@373
   172
		GL_pfd.cAccumBlueBits = this->gl_config.accum_blue_size;
slouken@373
   173
		GL_pfd.cAccumAlphaBits = this->gl_config.accum_alpha_size;
slouken@373
   174
		GL_pfd.cAccumBits =
slouken@373
   175
			(GL_pfd.cAccumRedBits + GL_pfd.cAccumGreenBits +
slouken@373
   176
			 GL_pfd.cAccumBlueBits + GL_pfd.cAccumAlphaBits);
slouken@373
   177
		GL_pfd.cDepthBits = this->gl_config.depth_size;
slouken@373
   178
		GL_pfd.cStencilBits = this->gl_config.stencil_size;
slouken@0
   179
slouken@655
   180
		/* initialize WGL_ARB_pixel_format */
slouken@655
   181
		Init_WGL_ARB_extensions(this);
slouken@655
   182
slouken@655
   183
		/* setup WGL_ARB_pixel_format attribs */
slouken@655
   184
		iAttr = &iAttribs[0];
slouken@655
   185
slouken@655
   186
		*iAttr++ = WGL_DRAW_TO_WINDOW_ARB;
slouken@655
   187
		*iAttr++ = GL_TRUE;
slouken@655
   188
		*iAttr++ = WGL_ACCELERATION_ARB;
slouken@655
   189
		*iAttr++ = WGL_FULL_ACCELERATION_ARB;
slouken@655
   190
		*iAttr++ = WGL_RED_BITS_ARB;
slouken@655
   191
		*iAttr++ = this->gl_config.red_size;
slouken@655
   192
		*iAttr++ = WGL_GREEN_BITS_ARB;
slouken@655
   193
		*iAttr++ = this->gl_config.green_size;
slouken@655
   194
		*iAttr++ = WGL_BLUE_BITS_ARB;
slouken@655
   195
		*iAttr++ = this->gl_config.blue_size;
slouken@655
   196
		
slouken@655
   197
		if ( this->gl_config.alpha_size ) {
slouken@655
   198
			*iAttr++ = WGL_ALPHA_BITS_ARB;
slouken@655
   199
			*iAttr++ = this->gl_config.alpha_size;
slouken@655
   200
		}
slouken@655
   201
slouken@655
   202
		if ( this->gl_config.double_buffer ) {
slouken@655
   203
			*iAttr ++ = WGL_DOUBLE_BUFFER_ARB;
slouken@655
   204
			*iAttr ++ = GL_TRUE;
slouken@655
   205
		}
slouken@655
   206
slouken@655
   207
		*iAttr++ = WGL_DEPTH_BITS_ARB;
slouken@655
   208
		*iAttr++ = this->gl_config.depth_size;
slouken@655
   209
slouken@655
   210
		if ( this->gl_config.stencil_size ) {
slouken@655
   211
			*iAttr++ = WGL_STENCIL_BITS_ARB;
slouken@655
   212
			*iAttr++ = this->gl_config.stencil_size;
slouken@655
   213
		}
slouken@655
   214
slouken@655
   215
		if ( this->gl_config.accum_red_size ) {
slouken@655
   216
			*iAttr++ = WGL_ACCUM_RED_BITS_ARB;
slouken@655
   217
			*iAttr++ = this->gl_config.accum_red_size;
slouken@655
   218
		}
slouken@655
   219
slouken@655
   220
		if ( this->gl_config.accum_green_size ) {
slouken@655
   221
			*iAttr++ = WGL_ACCUM_GREEN_BITS_ARB;
slouken@655
   222
			*iAttr++ = this->gl_config.accum_green_size;
slouken@655
   223
		}
slouken@655
   224
slouken@655
   225
		if ( this->gl_config.accum_blue_size ) {
slouken@655
   226
			*iAttr++ = WGL_ACCUM_BLUE_BITS_ARB;
slouken@655
   227
			*iAttr++ = this->gl_config.accum_blue_size;
slouken@655
   228
		}
slouken@655
   229
slouken@655
   230
		if ( this->gl_config.accum_alpha_size ) {
slouken@655
   231
			*iAttr++ = WGL_ACCUM_ALPHA_BITS_ARB;
slouken@655
   232
			*iAttr++ = this->gl_config.accum_alpha_size;
slouken@655
   233
		}
slouken@655
   234
slouken@655
   235
		if ( this->gl_config.stereo ) {
slouken@655
   236
			*iAttr++ = WGL_STEREO_ARB;
slouken@655
   237
			*iAttr++ = this->gl_config.stereo;
slouken@655
   238
		}
slouken@655
   239
slouken@655
   240
		if ( this->gl_config.sample_buffers ) {
slouken@655
   241
			*iAttr++ = WGL_SAMPLE_BUFFERS_ARB;
slouken@655
   242
			*iAttr++ = this->gl_config.sample_buffers;
slouken@655
   243
		}
slouken@655
   244
slouken@655
   245
		if ( this->gl_config.samples ) {
slouken@655
   246
			*iAttr++ = WGL_SAMPLES_ARB;
slouken@655
   247
			*iAttr++ = this->gl_config.samples;
slouken@655
   248
		}
slouken@655
   249
slouken@655
   250
		*iAttr = 0;
slouken@655
   251
slouken@373
   252
		/* Choose and set the closest available pixel format */
slouken@655
   253
		if ( !this->gl_data->wgl_arb_pixel_format ||
slouken@655
   254
		     !this->gl_data->wglChoosePixelFormatARB(GL_hdc, iAttribs, fAttribs, 1, &pixel_format, &matching) ||
slouken@655
   255
		     !matching ) {
slouken@655
   256
			pixel_format = ChoosePixelFormat(GL_hdc, &GL_pfd);
slouken@655
   257
			this->gl_data->wgl_arb_pixel_format = 0;
slouken@655
   258
		}
slouken@373
   259
		if ( !pixel_format ) {
slouken@373
   260
			SDL_SetError("No matching GL pixel format available");
slouken@373
   261
			return(-1);
slouken@373
   262
		}
slouken@655
   263
		if ( !SetPixelFormat(GL_hdc, pixel_format, &GL_pfd) ) {
slouken@373
   264
			if ( i == 0 ) {
slouken@373
   265
				/* First time through, try resetting the window */
slouken@373
   266
				if ( WIN_GL_ResetWindow(this) < 0 ) {
slouken@373
   267
					return(-1);
slouken@373
   268
				}
slouken@373
   269
				continue;
slouken@373
   270
			}
slouken@373
   271
			SDL_SetError("Unable to set HDC pixel format");
slouken@373
   272
			return(-1);
slouken@373
   273
		}
slouken@373
   274
		/* We either succeeded or failed by this point */
slouken@373
   275
		break;
slouken@0
   276
	}
slouken@0
   277
	DescribePixelFormat(GL_hdc, pixel_format, sizeof(GL_pfd), &GL_pfd);
slouken@0
   278
slouken@0
   279
	GL_hrc = this->gl_data->wglCreateContext(GL_hdc);
slouken@655
   280
	if ( GL_hrc == NULL ) {
slouken@0
   281
		SDL_SetError("Unable to create GL context");
slouken@0
   282
		return(-1);
slouken@0
   283
	}
slouken@0
   284
	gl_active = 1;
slouken@0
   285
#else
slouken@0
   286
	SDL_SetError("WIN driver not configured with OpenGL");
slouken@0
   287
#endif
slouken@0
   288
	if ( gl_active ) {
slouken@0
   289
		retval = 0;
slouken@0
   290
	} else {
slouken@0
   291
		retval = -1;
slouken@0
   292
	}
slouken@0
   293
	return(retval);
slouken@0
   294
}
slouken@0
   295
slouken@0
   296
void WIN_GL_ShutDown(_THIS)
slouken@0
   297
{
slouken@0
   298
#ifdef HAVE_OPENGL
slouken@0
   299
	/* Clean up OpenGL */
slouken@0
   300
	if ( GL_hrc ) {
slouken@0
   301
		this->gl_data->wglMakeCurrent(NULL, NULL);
slouken@0
   302
		this->gl_data->wglDeleteContext(GL_hrc);
slouken@0
   303
		GL_hrc = NULL;
slouken@0
   304
	}
slouken@0
   305
	if ( GL_hdc ) {
slouken@0
   306
		ReleaseDC(SDL_Window, GL_hdc);
slouken@0
   307
		GL_hdc = NULL;
slouken@0
   308
	}
slouken@0
   309
	gl_active = 0;
slouken@0
   310
slouken@0
   311
	WIN_GL_UnloadLibrary(this);
slouken@0
   312
#endif /* HAVE_OPENGL */
slouken@0
   313
}
slouken@0
   314
slouken@0
   315
#ifdef HAVE_OPENGL
slouken@0
   316
slouken@0
   317
/* Make the current context active */
slouken@0
   318
int WIN_GL_MakeCurrent(_THIS)
slouken@0
   319
{
slouken@0
   320
	int retval;
slouken@0
   321
slouken@0
   322
	retval = 0;
slouken@0
   323
	if ( ! this->gl_data->wglMakeCurrent(GL_hdc, GL_hrc) ) {
slouken@0
   324
		SDL_SetError("Unable to make GL context current");
slouken@0
   325
		retval = -1;
slouken@0
   326
	}
slouken@0
   327
	return(retval);
slouken@0
   328
}
slouken@0
   329
slouken@0
   330
/* Get attribute data from glX. */
slouken@0
   331
int WIN_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   332
{
slouken@0
   333
	int retval;
slouken@655
   334
	
slouken@655
   335
	if ( this->gl_data->wgl_arb_pixel_format ) {
slouken@655
   336
		int wgl_attrib;
slouken@655
   337
slouken@655
   338
		switch(attrib) {
slouken@655
   339
		    case SDL_GL_RED_SIZE:
slouken@655
   340
			wgl_attrib = WGL_RED_BITS_ARB;
slouken@655
   341
			break;
slouken@655
   342
		    case SDL_GL_GREEN_SIZE:
slouken@655
   343
			wgl_attrib = WGL_GREEN_BITS_ARB;
slouken@655
   344
			break;
slouken@655
   345
		    case SDL_GL_BLUE_SIZE:
slouken@655
   346
			wgl_attrib = WGL_BLUE_BITS_ARB;
slouken@655
   347
			break;
slouken@655
   348
		    case SDL_GL_ALPHA_SIZE:
slouken@655
   349
			wgl_attrib = WGL_ALPHA_BITS_ARB;
slouken@655
   350
			break;
slouken@655
   351
		    case SDL_GL_DOUBLEBUFFER:
slouken@655
   352
			wgl_attrib = WGL_DOUBLE_BUFFER_ARB;
slouken@655
   353
			break;
slouken@655
   354
		    case SDL_GL_BUFFER_SIZE:
slouken@655
   355
			wgl_attrib = WGL_COLOR_BITS_ARB;
slouken@655
   356
			break;
slouken@655
   357
		    case SDL_GL_DEPTH_SIZE:
slouken@655
   358
			wgl_attrib = WGL_DEPTH_BITS_ARB;
slouken@655
   359
			break;
slouken@655
   360
		    case SDL_GL_STENCIL_SIZE:
slouken@655
   361
			wgl_attrib = WGL_STENCIL_BITS_ARB;
slouken@655
   362
			break;
slouken@655
   363
		    case SDL_GL_ACCUM_RED_SIZE:
slouken@655
   364
			wgl_attrib = WGL_ACCUM_RED_BITS_ARB;
slouken@655
   365
			break;
slouken@655
   366
		    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@655
   367
			wgl_attrib = WGL_ACCUM_GREEN_BITS_ARB;
slouken@655
   368
			break;
slouken@655
   369
		    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@655
   370
			wgl_attrib = WGL_ACCUM_BLUE_BITS_ARB;
slouken@655
   371
			break;
slouken@655
   372
		    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@655
   373
			wgl_attrib = WGL_ACCUM_ALPHA_BITS_ARB;
slouken@655
   374
			break;
slouken@655
   375
		    case SDL_GL_STEREO:
slouken@655
   376
			wgl_attrib = WGL_STEREO_ARB;
slouken@655
   377
			break;
slouken@655
   378
		    case SDL_GL_SAMPLE_BUFFERS:
slouken@655
   379
			wgl_attrib = WGL_SAMPLE_BUFFERS_ARB;
slouken@655
   380
			break;
slouken@655
   381
		    case SDL_GL_SAMPLES:
slouken@655
   382
			wgl_attrib = WGL_SAMPLES_ARB;
slouken@655
   383
			break;
slouken@655
   384
		    default:
slouken@655
   385
			return(-1);
slouken@655
   386
		}
slouken@655
   387
		this->gl_data->wglGetPixelFormatAttribivARB(GL_hdc, pixel_format, 0, 1, &wgl_attrib, value);
slouken@655
   388
slouken@655
   389
		return 0;
slouken@655
   390
	}
slouken@0
   391
slouken@0
   392
	retval = 0;
slouken@655
   393
	switch ( attrib ) {
slouken@0
   394
	    case SDL_GL_RED_SIZE:
slouken@0
   395
		*value = GL_pfd.cRedBits;
slouken@0
   396
		break;
slouken@0
   397
	    case SDL_GL_GREEN_SIZE:
slouken@0
   398
		*value = GL_pfd.cGreenBits;
slouken@0
   399
		break;
slouken@0
   400
	    case SDL_GL_BLUE_SIZE:
slouken@0
   401
		*value = GL_pfd.cBlueBits;
slouken@0
   402
		break;
slouken@0
   403
	    case SDL_GL_ALPHA_SIZE:
slouken@0
   404
		*value = GL_pfd.cAlphaBits;
slouken@0
   405
		break;
slouken@0
   406
	    case SDL_GL_DOUBLEBUFFER:
slouken@0
   407
		if ( GL_pfd.dwFlags & PFD_DOUBLEBUFFER ) {
slouken@0
   408
			*value = 1;
slouken@0
   409
		} else {
slouken@0
   410
			*value = 0;
slouken@0
   411
		}
slouken@0
   412
		break;
slouken@0
   413
	    case SDL_GL_BUFFER_SIZE:
slouken@0
   414
		*value = GL_pfd.cColorBits;
slouken@0
   415
		break;
slouken@0
   416
	    case SDL_GL_DEPTH_SIZE:
slouken@0
   417
		*value = GL_pfd.cDepthBits;
slouken@0
   418
		break;
slouken@0
   419
	    case SDL_GL_STENCIL_SIZE:
slouken@0
   420
		*value = GL_pfd.cStencilBits;
slouken@0
   421
		break;
slouken@0
   422
	    case SDL_GL_ACCUM_RED_SIZE:
slouken@0
   423
		*value = GL_pfd.cAccumRedBits;
slouken@0
   424
		break;
slouken@0
   425
	    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@0
   426
		*value = GL_pfd.cAccumGreenBits;
slouken@0
   427
		break;
slouken@0
   428
	    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@0
   429
		*value = GL_pfd.cAccumBlueBits;
slouken@0
   430
		break;
slouken@0
   431
	    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@0
   432
		*value = GL_pfd.cAccumAlphaBits;
slouken@0
   433
		break;
slouken@450
   434
	    case SDL_GL_STEREO:
slouken@450
   435
		if ( GL_pfd.dwFlags & PFD_STEREO ) {
slouken@450
   436
			*value = 1;
slouken@450
   437
		} else {
slouken@450
   438
			*value = 0;
slouken@450
   439
		}
slouken@450
   440
		break;
slouken@0
   441
	    default:
slouken@0
   442
		retval = -1;
slouken@0
   443
		break;
slouken@0
   444
	}
slouken@0
   445
	return retval;
slouken@0
   446
}
slouken@0
   447
slouken@0
   448
void WIN_GL_SwapBuffers(_THIS)
slouken@0
   449
{
slouken@0
   450
	SwapBuffers(GL_hdc);
slouken@0
   451
}
slouken@0
   452
slouken@0
   453
void WIN_GL_UnloadLibrary(_THIS)
slouken@0
   454
{
slouken@0
   455
	if ( this->gl_config.driver_loaded ) {
slouken@0
   456
		FreeLibrary((HMODULE)this->gl_config.dll_handle);
slouken@0
   457
slouken@0
   458
		this->gl_data->wglGetProcAddress = NULL;
slouken@0
   459
		this->gl_data->wglCreateContext = NULL;
slouken@0
   460
		this->gl_data->wglDeleteContext = NULL;
slouken@0
   461
		this->gl_data->wglMakeCurrent = NULL;
slouken@655
   462
		this->gl_data->wglChoosePixelFormatARB = NULL;
slouken@655
   463
		this->gl_data->wglGetPixelFormatAttribivARB = NULL;
slouken@0
   464
slouken@0
   465
		this->gl_config.dll_handle = NULL;
slouken@0
   466
		this->gl_config.driver_loaded = 0;
slouken@0
   467
	}
slouken@0
   468
}
slouken@0
   469
slouken@0
   470
/* Passing a NULL path means load pointers from the application */
slouken@0
   471
int WIN_GL_LoadLibrary(_THIS, const char* path) 
slouken@0
   472
{
slouken@0
   473
	HMODULE handle;
slouken@0
   474
slouken@0
   475
 	if ( gl_active ) {
slouken@0
   476
 		SDL_SetError("OpenGL context already created");
slouken@0
   477
 		return -1;
slouken@0
   478
 	}
slouken@0
   479
slouken@0
   480
	if ( path == NULL ) {
slouken@0
   481
		path = DEFAULT_GL_DRIVER_PATH;
slouken@0
   482
	}
slouken@0
   483
	handle = LoadLibrary(path);
slouken@0
   484
	if ( handle == NULL ) {
slouken@0
   485
		SDL_SetError("Could not load OpenGL library");
slouken@0
   486
		return -1;
slouken@0
   487
	}
slouken@0
   488
slouken@0
   489
	/* Unload the old driver and reset the pointers */
slouken@0
   490
	WIN_GL_UnloadLibrary(this);
slouken@0
   491
slouken@0
   492
	/* Load new function pointers */
slouken@655
   493
	memset(this->gl_data, 0, sizeof(*this->gl_data));
slouken@0
   494
	this->gl_data->wglGetProcAddress = (void * (WINAPI *)(const char *))
slouken@0
   495
		GetProcAddress(handle, "wglGetProcAddress");
slouken@0
   496
	this->gl_data->wglCreateContext = (HGLRC (WINAPI *)(HDC))
slouken@0
   497
		GetProcAddress(handle, "wglCreateContext");
slouken@0
   498
	this->gl_data->wglDeleteContext = (BOOL (WINAPI *)(HGLRC))
slouken@0
   499
		GetProcAddress(handle, "wglDeleteContext");
slouken@0
   500
	this->gl_data->wglMakeCurrent = (BOOL (WINAPI *)(HDC, HGLRC))
slouken@0
   501
		GetProcAddress(handle, "wglMakeCurrent");
slouken@0
   502
slouken@0
   503
	if ( (this->gl_data->wglGetProcAddress == NULL) ||
slouken@0
   504
	     (this->gl_data->wglCreateContext == NULL) ||
slouken@0
   505
	     (this->gl_data->wglDeleteContext == NULL) ||
slouken@0
   506
	     (this->gl_data->wglMakeCurrent == NULL) ) {
slouken@0
   507
		SDL_SetError("Could not retrieve OpenGL functions");
slouken@0
   508
		FreeLibrary(handle);
slouken@0
   509
		return -1;
slouken@0
   510
	}
slouken@0
   511
slouken@0
   512
	this->gl_config.dll_handle = handle;
slouken@0
   513
	strcpy(this->gl_config.driver_path, path);
slouken@0
   514
	this->gl_config.driver_loaded = 1;
slouken@0
   515
	return 0;
slouken@0
   516
}
slouken@0
   517
slouken@0
   518
void *WIN_GL_GetProcAddress(_THIS, const char* proc)
slouken@0
   519
{
slouken@0
   520
	void *func;
slouken@0
   521
slouken@0
   522
	/* This is to pick up extensions */
slouken@0
   523
	func = this->gl_data->wglGetProcAddress(proc);
slouken@0
   524
	if ( ! func ) {
slouken@0
   525
		/* This is probably a normal GL function */
slouken@0
   526
		func = GetProcAddress(this->gl_config.dll_handle, proc);
slouken@0
   527
	}
slouken@0
   528
	return func;
slouken@0
   529
}
slouken@0
   530
slouken@0
   531
#endif /* HAVE_OPENGL */