src/video/wincommon/SDL_wingl.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 07 Nov 2011 16:01:03 -0500
branchSDL-1.2
changeset 6059 a04171d6fa11
parent 6057 d0b7c45e982e
child 6137 4720145f848b
permissions -rw-r--r--
Windows: If we don't care about OpenGL accel and FULL fails, try NO accel.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 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@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* WGL implementation of SDL OpenGL support */
slouken@0
    25
slouken@1361
    26
#if SDL_VIDEO_OPENGL
slouken@327
    27
#include "SDL_opengl.h"
slouken@453
    28
#endif
slouken@0
    29
#include "SDL_lowvideo.h"
slouken@0
    30
#include "SDL_wingl_c.h"
slouken@0
    31
slouken@1361
    32
#if SDL_VIDEO_OPENGL
slouken@0
    33
#define DEFAULT_GL_DRIVER_PATH "OPENGL32.DLL"
slouken@0
    34
#endif
slouken@0
    35
slouken@373
    36
/* If setting the HDC fails, we may need to recreate the window (MSDN) */
slouken@373
    37
static int WIN_GL_ResetWindow(_THIS)
slouken@373
    38
{
slouken@373
    39
	int status = 0;
slouken@373
    40
slouken@373
    41
#ifndef _WIN32_WCE /* FIXME WinCE needs the UNICODE version of CreateWindow() */
slouken@1527
    42
	/* This doesn't work with DirectX code (see CVS comments) */
slouken@1280
    43
	/* If we were passed a window, then we can't create a new one */
slouken@1527
    44
	if ( !SDL_windowid && SDL_strcmp(this->name, "windib") == 0 ) {
slouken@373
    45
		/* Save the existing window attributes */
slouken@373
    46
		LONG style;
slouken@373
    47
		RECT rect = { 0, 0, 0, 0 };
slouken@1480
    48
		style = GetWindowLong(SDL_Window, GWL_STYLE);
slouken@373
    49
		GetWindowRect(SDL_Window, &rect);
slouken@373
    50
		DestroyWindow(SDL_Window);
slouken@1523
    51
		WIN_FlushMessageQueue();
slouken@1523
    52
slouken@4082
    53
		SDL_resizing = 1;
slouken@373
    54
		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
slouken@373
    55
		                          style,
slouken@1316
    56
		                          rect.left, rect.top,
slouken@1316
    57
		                          (rect.right-rect.left)+1,
slouken@4082
    58
		                          (rect.bottom-rect.top)+1,
slouken@373
    59
		                          NULL, NULL, SDL_Instance, NULL);
slouken@1523
    60
		WIN_FlushMessageQueue();
slouken@4082
    61
		SDL_resizing = 0;
slouken@1523
    62
slouken@373
    63
		if ( SDL_Window ) {
slouken@373
    64
			this->SetCaption(this, this->wm_title, this->wm_icon);
slouken@373
    65
		} else {
slouken@373
    66
			SDL_SetError("Couldn't create window");
slouken@373
    67
			status = -1;
slouken@373
    68
		}
slouken@373
    69
	} else
slouken@373
    70
#endif /* !_WIN32_WCE */
slouken@373
    71
	{
slouken@373
    72
		SDL_SetError("Unable to reset window for OpenGL context");
slouken@373
    73
		status = -1;
slouken@373
    74
	}
slouken@373
    75
	return(status);
slouken@373
    76
}
slouken@0
    77
slouken@1361
    78
#if SDL_VIDEO_OPENGL
slouken@1315
    79
slouken@1315
    80
static int ExtensionSupported(const char *extension, const char *extensions)
slouken@1315
    81
{
slouken@1315
    82
	const char *start;
slouken@1315
    83
	const char *where, *terminator;
slouken@1315
    84
slouken@1315
    85
	/* Extension names should not have spaces. */
slouken@1336
    86
	where = SDL_strchr(extension, ' ');
slouken@1315
    87
	if ( where || *extension == '\0' )
slouken@1315
    88
	      return 0;
slouken@1315
    89
	
slouken@1315
    90
	if ( ! extensions )
slouken@1315
    91
		return 0;
slouken@1315
    92
slouken@1315
    93
	/* It takes a bit of care to be fool-proof about parsing the
slouken@1315
    94
	 *      OpenGL extensions string. Don't be fooled by sub-strings,
slouken@1315
    95
	 *           etc. */
slouken@1315
    96
	
slouken@1315
    97
	start = extensions;
slouken@1315
    98
	
slouken@1315
    99
	for (;;)
slouken@1315
   100
	{
slouken@1336
   101
		where = SDL_strstr(start, extension);
slouken@1315
   102
		if (!where) break;
slouken@1315
   103
		
slouken@1336
   104
		terminator = where + SDL_strlen(extension);
slouken@1315
   105
		if (where == start || *(where - 1) == ' ')
slouken@1315
   106
	        if (*terminator == ' ' || *terminator == '\0') return 1;
slouken@1315
   107
slouken@1315
   108
		start = terminator;
slouken@1315
   109
	}
slouken@1315
   110
	
slouken@1315
   111
	return 0;
slouken@1315
   112
}
slouken@1315
   113
slouken@4042
   114
static int ChoosePixelFormatARB(_THIS, const int *iAttribs, const FLOAT *fAttribs)
slouken@655
   115
{
slouken@655
   116
	HWND hwnd;
slouken@655
   117
	HDC hdc;
slouken@655
   118
	HGLRC hglrc;
slouken@655
   119
	const char * (WINAPI *wglGetExtensionsStringARB)(HDC) = 0;
slouken@1315
   120
	const char *extensions;
slouken@4042
   121
	int pformat = 0;
slouken@4042
   122
	UINT matches = 0;
slouken@4042
   123
slouken@671
   124
	hwnd = CreateWindow(SDL_Appname, SDL_Appname, WS_POPUP | WS_DISABLED,
slouken@655
   125
	                    0, 0, 10, 10,
slouken@1315
   126
	                    NULL, NULL, SDL_Instance, NULL);
slouken@1523
   127
	WIN_FlushMessageQueue();
slouken@1523
   128
slouken@655
   129
	hdc = GetDC(hwnd);
slouken@655
   130
slouken@4042
   131
	SetPixelFormat(hdc, ChoosePixelFormat(hdc, &GL_pfd), &GL_pfd);
slouken@655
   132
slouken@655
   133
	hglrc = this->gl_data->wglCreateContext(hdc);
slouken@671
   134
	if ( hglrc ) {
slouken@671
   135
		this->gl_data->wglMakeCurrent(hdc, hglrc);
slouken@671
   136
	}
slouken@655
   137
slouken@655
   138
	wglGetExtensionsStringARB = (const char * (WINAPI *)(HDC))
slouken@655
   139
		this->gl_data->wglGetProcAddress("wglGetExtensionsStringARB");
slouken@655
   140
slouken@1315
   141
	if( wglGetExtensionsStringARB ) {
slouken@1315
   142
		extensions = wglGetExtensionsStringARB(hdc);
slouken@1315
   143
	} else {
slouken@1315
   144
		extensions = NULL;
slouken@1315
   145
	}
slouken@1315
   146
slouken@1315
   147
	this->gl_data->WGL_ARB_pixel_format = 0;
slouken@1315
   148
	if( ExtensionSupported("WGL_ARB_pixel_format", extensions) ) {
slouken@4042
   149
		BOOL (WINAPI *wglChoosePixelFormatARB)(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
slouken@4042
   150
		wglChoosePixelFormatARB =
slouken@655
   151
			(BOOL (WINAPI *)(HDC, const int *, const FLOAT *, UINT, int *, UINT *))
slouken@655
   152
			this->gl_data->wglGetProcAddress("wglChoosePixelFormatARB");
slouken@4042
   153
		if( wglChoosePixelFormatARB &&
slouken@4042
   154
		    wglChoosePixelFormatARB(hdc, iAttribs, fAttribs, 1, &pformat, &matches) && pformat ) {
slouken@1315
   155
			this->gl_data->WGL_ARB_pixel_format = 1;
slouken@1315
   156
		}
slouken@655
   157
	}
slouken@655
   158
	
slouken@671
   159
	if ( hglrc ) {
slouken@671
   160
		this->gl_data->wglMakeCurrent(NULL, NULL);
slouken@671
   161
		this->gl_data->wglDeleteContext(hglrc);
slouken@671
   162
	}
slouken@655
   163
	ReleaseDC(hwnd, hdc);
slouken@655
   164
	DestroyWindow(hwnd);
slouken@1523
   165
	WIN_FlushMessageQueue();
slouken@4042
   166
slouken@4042
   167
	return pformat;
slouken@655
   168
}
slouken@1315
   169
slouken@1361
   170
#endif /* SDL_VIDEO_OPENGL */
slouken@655
   171
slouken@0
   172
int WIN_GL_SetupWindow(_THIS)
slouken@0
   173
{
slouken@0
   174
	int retval;
slouken@1361
   175
#if SDL_VIDEO_OPENGL
slouken@373
   176
	int i;
slouken@655
   177
	int iAttribs[64];
slouken@655
   178
	int *iAttr;
icculus@6059
   179
	int *iAccelAttr = NULL;
slouken@655
   180
	float fAttribs[1] = { 0 };
slouken@1769
   181
	const GLubyte *(WINAPI *glGetStringFunc)(GLenum);
slouken@1736
   182
	const char *wglext;
slouken@0
   183
slouken@0
   184
	/* load the gl driver from a default path */
slouken@0
   185
	if ( ! this->gl_config.driver_loaded ) {
slouken@0
   186
		/* no driver has been loaded, use default (ourselves) */
slouken@0
   187
		if ( WIN_GL_LoadLibrary(this, NULL) < 0 ) {
slouken@0
   188
			return(-1);
slouken@0
   189
		}
slouken@0
   190
	}
slouken@0
   191
slouken@4042
   192
	/* Set up the pixel format descriptor with our needed format */
slouken@4042
   193
	SDL_memset(&GL_pfd, 0, sizeof(GL_pfd));
slouken@4042
   194
	GL_pfd.nSize = sizeof(GL_pfd);
slouken@4042
   195
	GL_pfd.nVersion = 1;
slouken@4042
   196
	GL_pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL);
slouken@4042
   197
	if ( this->gl_config.double_buffer ) {
slouken@4042
   198
		GL_pfd.dwFlags |= PFD_DOUBLEBUFFER;
slouken@4042
   199
	}
slouken@4042
   200
	if ( this->gl_config.stereo ) {
slouken@4042
   201
		GL_pfd.dwFlags |= PFD_STEREO;
slouken@4042
   202
	}
slouken@4042
   203
	GL_pfd.iPixelType = PFD_TYPE_RGBA;
slouken@4042
   204
	GL_pfd.cColorBits = this->gl_config.buffer_size;
slouken@4042
   205
	GL_pfd.cRedBits = this->gl_config.red_size;
slouken@4042
   206
	GL_pfd.cGreenBits = this->gl_config.green_size;
slouken@4042
   207
	GL_pfd.cBlueBits = this->gl_config.blue_size;
slouken@4042
   208
	GL_pfd.cAlphaBits = this->gl_config.alpha_size;
slouken@4042
   209
	GL_pfd.cAccumRedBits = this->gl_config.accum_red_size;
slouken@4042
   210
	GL_pfd.cAccumGreenBits = this->gl_config.accum_green_size;
slouken@4042
   211
	GL_pfd.cAccumBlueBits = this->gl_config.accum_blue_size;
slouken@4042
   212
	GL_pfd.cAccumAlphaBits = this->gl_config.accum_alpha_size;
slouken@4042
   213
	GL_pfd.cAccumBits =
slouken@4042
   214
		(GL_pfd.cAccumRedBits + GL_pfd.cAccumGreenBits +
slouken@4042
   215
		 GL_pfd.cAccumBlueBits + GL_pfd.cAccumAlphaBits);
slouken@4042
   216
	GL_pfd.cDepthBits = this->gl_config.depth_size;
slouken@4042
   217
	GL_pfd.cStencilBits = this->gl_config.stencil_size;
slouken@4042
   218
slouken@4042
   219
	/* setup WGL_ARB_pixel_format attribs */
slouken@4042
   220
	iAttr = &iAttribs[0];
slouken@4042
   221
slouken@4042
   222
	*iAttr++ = WGL_DRAW_TO_WINDOW_ARB;
slouken@4042
   223
	*iAttr++ = GL_TRUE;
slouken@4042
   224
	*iAttr++ = WGL_RED_BITS_ARB;
slouken@4042
   225
	*iAttr++ = this->gl_config.red_size;
slouken@4042
   226
	*iAttr++ = WGL_GREEN_BITS_ARB;
slouken@4042
   227
	*iAttr++ = this->gl_config.green_size;
slouken@4042
   228
	*iAttr++ = WGL_BLUE_BITS_ARB;
slouken@4042
   229
	*iAttr++ = this->gl_config.blue_size;
icculus@6057
   230
icculus@6057
   231
	/* We always choose either FULL or NO accel on Windows, because of flaky
icculus@6057
   232
	   drivers. If the app didn't specify, we use FULL, because that's
icculus@6057
   233
	   probably what they wanted (and if you didn't care and got FULL, that's
icculus@6057
   234
	   a perfectly valid result in any case. */
icculus@6057
   235
	*iAttr++ = WGL_ACCELERATION_ARB;
icculus@6059
   236
	iAccelAttr = iAttr;
icculus@6057
   237
	if (this->gl_config.accelerated) {
icculus@6057
   238
		*iAttr++ = WGL_FULL_ACCELERATION_ARB;
icculus@6057
   239
	} else {
icculus@6057
   240
		*iAttr++ = WGL_NO_ACCELERATION_ARB;
icculus@6057
   241
	}
icculus@6057
   242
slouken@4042
   243
	if ( this->gl_config.alpha_size ) {
slouken@4042
   244
		*iAttr++ = WGL_ALPHA_BITS_ARB;
slouken@4042
   245
		*iAttr++ = this->gl_config.alpha_size;
slouken@4042
   246
	}
slouken@4042
   247
slouken@4042
   248
	*iAttr++ = WGL_DOUBLE_BUFFER_ARB;
slouken@4042
   249
	*iAttr++ = this->gl_config.double_buffer;
slouken@4042
   250
slouken@4042
   251
	*iAttr++ = WGL_DEPTH_BITS_ARB;
slouken@4042
   252
	*iAttr++ = this->gl_config.depth_size;
slouken@4042
   253
slouken@4042
   254
	if ( this->gl_config.stencil_size ) {
slouken@4042
   255
		*iAttr++ = WGL_STENCIL_BITS_ARB;
slouken@4042
   256
		*iAttr++ = this->gl_config.stencil_size;
slouken@4042
   257
	}
slouken@4042
   258
slouken@4042
   259
	if ( this->gl_config.accum_red_size ) {
slouken@4042
   260
		*iAttr++ = WGL_ACCUM_RED_BITS_ARB;
slouken@4042
   261
		*iAttr++ = this->gl_config.accum_red_size;
slouken@4042
   262
	}
slouken@4042
   263
slouken@4042
   264
	if ( this->gl_config.accum_green_size ) {
slouken@4042
   265
		*iAttr++ = WGL_ACCUM_GREEN_BITS_ARB;
slouken@4042
   266
		*iAttr++ = this->gl_config.accum_green_size;
slouken@4042
   267
	}
slouken@4042
   268
slouken@4042
   269
	if ( this->gl_config.accum_blue_size ) {
slouken@4042
   270
		*iAttr++ = WGL_ACCUM_BLUE_BITS_ARB;
slouken@4042
   271
		*iAttr++ = this->gl_config.accum_blue_size;
slouken@4042
   272
	}
slouken@4042
   273
slouken@4042
   274
	if ( this->gl_config.accum_alpha_size ) {
slouken@4042
   275
		*iAttr++ = WGL_ACCUM_ALPHA_BITS_ARB;
slouken@4042
   276
		*iAttr++ = this->gl_config.accum_alpha_size;
slouken@4042
   277
	}
slouken@4042
   278
slouken@4042
   279
	if ( this->gl_config.stereo ) {
slouken@4042
   280
		*iAttr++ = WGL_STEREO_ARB;
slouken@4042
   281
		*iAttr++ = GL_TRUE;
slouken@4042
   282
	}
slouken@4042
   283
slouken@4042
   284
	if ( this->gl_config.multisamplebuffers ) {
slouken@4042
   285
		*iAttr++ = WGL_SAMPLE_BUFFERS_ARB;
slouken@4042
   286
		*iAttr++ = this->gl_config.multisamplebuffers;
slouken@4042
   287
	}
slouken@4042
   288
slouken@4042
   289
	if ( this->gl_config.multisamplesamples ) {
slouken@4042
   290
		*iAttr++ = WGL_SAMPLES_ARB;
slouken@4042
   291
		*iAttr++ = this->gl_config.multisamplesamples;
slouken@4042
   292
	}
slouken@4042
   293
slouken@4042
   294
	*iAttr = 0;
slouken@4042
   295
slouken@373
   296
	for ( i=0; ; ++i ) {
slouken@373
   297
		/* Get the window device context for our OpenGL drawing */
slouken@373
   298
		GL_hdc = GetDC(SDL_Window);
slouken@373
   299
		if ( GL_hdc == NULL ) {
slouken@373
   300
			SDL_SetError("Unable to get DC for SDL_Window");
slouken@373
   301
			return(-1);
slouken@373
   302
		}
slouken@0
   303
slouken@373
   304
		/* Choose and set the closest available pixel format */
slouken@4042
   305
		pixel_format = ChoosePixelFormatARB(this, iAttribs, fAttribs);
icculus@6059
   306
		/* App said "don't care about accel" and FULL accel failed. Try NO. */
icculus@6059
   307
		if ( ( !pixel_format ) && ( this->gl_config.accelerated < 0 ) ) {
icculus@6059
   308
			*iAccelAttr = WGL_NO_ACCELERATION_ARB;
icculus@6059
   309
			pixel_format = ChoosePixelFormatARB(this, iAttribs, fAttribs);
icculus@6059
   310
			*iAccelAttr = WGL_FULL_ACCELERATION_ARB;  /* if we try again. */
icculus@6059
   311
		}
slouken@4042
   312
		if ( !pixel_format ) {
slouken@655
   313
			pixel_format = ChoosePixelFormat(GL_hdc, &GL_pfd);
slouken@655
   314
		}
slouken@373
   315
		if ( !pixel_format ) {
slouken@373
   316
			SDL_SetError("No matching GL pixel format available");
slouken@373
   317
			return(-1);
slouken@373
   318
		}
slouken@655
   319
		if ( !SetPixelFormat(GL_hdc, pixel_format, &GL_pfd) ) {
slouken@373
   320
			if ( i == 0 ) {
slouken@373
   321
				/* First time through, try resetting the window */
slouken@373
   322
				if ( WIN_GL_ResetWindow(this) < 0 ) {
slouken@373
   323
					return(-1);
slouken@373
   324
				}
slouken@373
   325
				continue;
slouken@373
   326
			}
slouken@373
   327
			SDL_SetError("Unable to set HDC pixel format");
slouken@373
   328
			return(-1);
slouken@373
   329
		}
slouken@373
   330
		/* We either succeeded or failed by this point */
slouken@373
   331
		break;
slouken@0
   332
	}
slouken@0
   333
	DescribePixelFormat(GL_hdc, pixel_format, sizeof(GL_pfd), &GL_pfd);
slouken@0
   334
slouken@0
   335
	GL_hrc = this->gl_data->wglCreateContext(GL_hdc);
slouken@655
   336
	if ( GL_hrc == NULL ) {
slouken@0
   337
		SDL_SetError("Unable to create GL context");
slouken@0
   338
		return(-1);
slouken@0
   339
	}
slouken@1736
   340
	if ( WIN_GL_MakeCurrent(this) < 0 ) {
slouken@1736
   341
		return(-1);
slouken@1736
   342
	}
slouken@0
   343
	gl_active = 1;
slouken@1736
   344
slouken@4042
   345
	/* Get the wglGetPixelFormatAttribivARB pointer for the context */
slouken@4042
   346
	if ( this->gl_data->WGL_ARB_pixel_format ) {
slouken@4042
   347
		this->gl_data->wglGetPixelFormatAttribivARB =
slouken@4042
   348
			(BOOL (WINAPI *)(HDC, int, int, UINT, const int *, int *))
slouken@4042
   349
			this->gl_data->wglGetProcAddress("wglGetPixelFormatAttribivARB");
slouken@4042
   350
	} else {
slouken@4042
   351
		this->gl_data->wglGetPixelFormatAttribivARB = NULL;
slouken@4042
   352
	}
slouken@4042
   353
slouken@1736
   354
	/* Vsync control under Windows.  Checking glGetString here is
slouken@1736
   355
	 * somewhat a documented and reliable hack - it was originally
slouken@1736
   356
	 * as a feature added by mistake, but since so many people rely
slouken@1736
   357
	 * on it, it will not be removed.  strstr should be safe here.*/
slouken@1754
   358
	glGetStringFunc = WIN_GL_GetProcAddress(this, "glGetString");
slouken@1754
   359
	if ( glGetStringFunc ) {
slouken@1757
   360
		wglext = (const char *)glGetStringFunc(GL_EXTENSIONS);
slouken@1754
   361
	} else {
slouken@1754
   362
		/* Uh oh, something is seriously wrong here... */
slouken@1754
   363
		wglext = NULL;
slouken@1754
   364
	}
slouken@1880
   365
	if ( wglext && SDL_strstr(wglext, "WGL_EXT_swap_control") ) {
slouken@1880
   366
		this->gl_data->wglSwapIntervalEXT = WIN_GL_GetProcAddress(this, "wglSwapIntervalEXT");
slouken@1880
   367
		this->gl_data->wglGetSwapIntervalEXT = WIN_GL_GetProcAddress(this, "wglGetSwapIntervalEXT");
slouken@1880
   368
	} else {
slouken@1736
   369
		this->gl_data->wglSwapIntervalEXT = NULL;
slouken@1736
   370
		this->gl_data->wglGetSwapIntervalEXT = NULL;
slouken@1736
   371
	}
slouken@1736
   372
	if ( this->gl_config.swap_control >= 0 ) {
slouken@1736
   373
		if ( this->gl_data->wglSwapIntervalEXT ) {
slouken@1736
   374
			this->gl_data->wglSwapIntervalEXT(this->gl_config.swap_control);
slouken@1736
   375
		}
slouken@1736
   376
	}
slouken@0
   377
#else
slouken@0
   378
	SDL_SetError("WIN driver not configured with OpenGL");
slouken@0
   379
#endif
slouken@0
   380
	if ( gl_active ) {
slouken@0
   381
		retval = 0;
slouken@0
   382
	} else {
slouken@0
   383
		retval = -1;
slouken@0
   384
	}
slouken@0
   385
	return(retval);
slouken@0
   386
}
slouken@0
   387
slouken@0
   388
void WIN_GL_ShutDown(_THIS)
slouken@0
   389
{
slouken@1361
   390
#if SDL_VIDEO_OPENGL
slouken@0
   391
	/* Clean up OpenGL */
slouken@0
   392
	if ( GL_hrc ) {
slouken@0
   393
		this->gl_data->wglMakeCurrent(NULL, NULL);
slouken@0
   394
		this->gl_data->wglDeleteContext(GL_hrc);
slouken@0
   395
		GL_hrc = NULL;
slouken@0
   396
	}
slouken@0
   397
	if ( GL_hdc ) {
slouken@0
   398
		ReleaseDC(SDL_Window, GL_hdc);
slouken@0
   399
		GL_hdc = NULL;
slouken@0
   400
	}
slouken@0
   401
	gl_active = 0;
slouken@0
   402
slouken@0
   403
	WIN_GL_UnloadLibrary(this);
slouken@1361
   404
#endif /* SDL_VIDEO_OPENGL */
slouken@0
   405
}
slouken@0
   406
slouken@1361
   407
#if SDL_VIDEO_OPENGL
slouken@0
   408
slouken@0
   409
/* Make the current context active */
slouken@0
   410
int WIN_GL_MakeCurrent(_THIS)
slouken@0
   411
{
slouken@0
   412
	int retval;
slouken@0
   413
slouken@0
   414
	retval = 0;
slouken@0
   415
	if ( ! this->gl_data->wglMakeCurrent(GL_hdc, GL_hrc) ) {
slouken@0
   416
		SDL_SetError("Unable to make GL context current");
slouken@0
   417
		retval = -1;
slouken@0
   418
	}
slouken@0
   419
	return(retval);
slouken@0
   420
}
slouken@0
   421
icculus@3937
   422
/* Get attribute data from wgl. */
slouken@0
   423
int WIN_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   424
{
slouken@0
   425
	int retval;
icculus@3937
   426
icculus@3937
   427
	if (attrib == SDL_GL_SWAP_CONTROL) {
icculus@3937
   428
		if ( this->gl_data->wglGetSwapIntervalEXT ) {
icculus@3937
   429
			*value = this->gl_data->wglGetSwapIntervalEXT();
icculus@3937
   430
			return 0;
icculus@3937
   431
		}
icculus@3937
   432
		return -1;
icculus@3937
   433
	}
icculus@3937
   434
slouken@4042
   435
	if ( this->gl_data->wglGetPixelFormatAttribivARB ) {
slouken@655
   436
		int wgl_attrib;
slouken@655
   437
slouken@655
   438
		switch(attrib) {
slouken@655
   439
		    case SDL_GL_RED_SIZE:
slouken@655
   440
			wgl_attrib = WGL_RED_BITS_ARB;
slouken@655
   441
			break;
slouken@655
   442
		    case SDL_GL_GREEN_SIZE:
slouken@655
   443
			wgl_attrib = WGL_GREEN_BITS_ARB;
slouken@655
   444
			break;
slouken@655
   445
		    case SDL_GL_BLUE_SIZE:
slouken@655
   446
			wgl_attrib = WGL_BLUE_BITS_ARB;
slouken@655
   447
			break;
slouken@655
   448
		    case SDL_GL_ALPHA_SIZE:
slouken@655
   449
			wgl_attrib = WGL_ALPHA_BITS_ARB;
slouken@655
   450
			break;
slouken@655
   451
		    case SDL_GL_DOUBLEBUFFER:
slouken@655
   452
			wgl_attrib = WGL_DOUBLE_BUFFER_ARB;
slouken@655
   453
			break;
slouken@655
   454
		    case SDL_GL_BUFFER_SIZE:
slouken@655
   455
			wgl_attrib = WGL_COLOR_BITS_ARB;
slouken@655
   456
			break;
slouken@655
   457
		    case SDL_GL_DEPTH_SIZE:
slouken@655
   458
			wgl_attrib = WGL_DEPTH_BITS_ARB;
slouken@655
   459
			break;
slouken@655
   460
		    case SDL_GL_STENCIL_SIZE:
slouken@655
   461
			wgl_attrib = WGL_STENCIL_BITS_ARB;
slouken@655
   462
			break;
slouken@655
   463
		    case SDL_GL_ACCUM_RED_SIZE:
slouken@655
   464
			wgl_attrib = WGL_ACCUM_RED_BITS_ARB;
slouken@655
   465
			break;
slouken@655
   466
		    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@655
   467
			wgl_attrib = WGL_ACCUM_GREEN_BITS_ARB;
slouken@655
   468
			break;
slouken@655
   469
		    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@655
   470
			wgl_attrib = WGL_ACCUM_BLUE_BITS_ARB;
slouken@655
   471
			break;
slouken@655
   472
		    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@655
   473
			wgl_attrib = WGL_ACCUM_ALPHA_BITS_ARB;
slouken@655
   474
			break;
slouken@655
   475
		    case SDL_GL_STEREO:
slouken@655
   476
			wgl_attrib = WGL_STEREO_ARB;
slouken@655
   477
			break;
slouken@656
   478
		    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@655
   479
			wgl_attrib = WGL_SAMPLE_BUFFERS_ARB;
slouken@655
   480
			break;
slouken@656
   481
		    case SDL_GL_MULTISAMPLESAMPLES:
slouken@655
   482
			wgl_attrib = WGL_SAMPLES_ARB;
slouken@655
   483
			break;
slouken@1737
   484
		    case SDL_GL_ACCELERATED_VISUAL:
slouken@1737
   485
			wgl_attrib = WGL_ACCELERATION_ARB;
slouken@1737
   486
			this->gl_data->wglGetPixelFormatAttribivARB(GL_hdc, pixel_format, 0, 1, &wgl_attrib, value);
slouken@1737
   487
			if ( *value == WGL_NO_ACCELERATION_ARB ) {
slouken@1737
   488
				*value = SDL_FALSE;
slouken@1737
   489
			} else {
slouken@1737
   490
				*value = SDL_TRUE;
slouken@1737
   491
			}
slouken@1737
   492
			return 0;
slouken@655
   493
		    default:
slouken@655
   494
			return(-1);
slouken@655
   495
		}
slouken@655
   496
		this->gl_data->wglGetPixelFormatAttribivARB(GL_hdc, pixel_format, 0, 1, &wgl_attrib, value);
slouken@655
   497
slouken@655
   498
		return 0;
slouken@655
   499
	}
slouken@0
   500
slouken@0
   501
	retval = 0;
slouken@655
   502
	switch ( attrib ) {
slouken@0
   503
	    case SDL_GL_RED_SIZE:
slouken@0
   504
		*value = GL_pfd.cRedBits;
slouken@0
   505
		break;
slouken@0
   506
	    case SDL_GL_GREEN_SIZE:
slouken@0
   507
		*value = GL_pfd.cGreenBits;
slouken@0
   508
		break;
slouken@0
   509
	    case SDL_GL_BLUE_SIZE:
slouken@0
   510
		*value = GL_pfd.cBlueBits;
slouken@0
   511
		break;
slouken@0
   512
	    case SDL_GL_ALPHA_SIZE:
slouken@0
   513
		*value = GL_pfd.cAlphaBits;
slouken@0
   514
		break;
slouken@0
   515
	    case SDL_GL_DOUBLEBUFFER:
slouken@0
   516
		if ( GL_pfd.dwFlags & PFD_DOUBLEBUFFER ) {
slouken@0
   517
			*value = 1;
slouken@0
   518
		} else {
slouken@0
   519
			*value = 0;
slouken@0
   520
		}
slouken@0
   521
		break;
slouken@0
   522
	    case SDL_GL_BUFFER_SIZE:
slouken@0
   523
		*value = GL_pfd.cColorBits;
slouken@0
   524
		break;
slouken@0
   525
	    case SDL_GL_DEPTH_SIZE:
slouken@0
   526
		*value = GL_pfd.cDepthBits;
slouken@0
   527
		break;
slouken@0
   528
	    case SDL_GL_STENCIL_SIZE:
slouken@0
   529
		*value = GL_pfd.cStencilBits;
slouken@0
   530
		break;
slouken@0
   531
	    case SDL_GL_ACCUM_RED_SIZE:
slouken@0
   532
		*value = GL_pfd.cAccumRedBits;
slouken@0
   533
		break;
slouken@0
   534
	    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@0
   535
		*value = GL_pfd.cAccumGreenBits;
slouken@0
   536
		break;
slouken@0
   537
	    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@0
   538
		*value = GL_pfd.cAccumBlueBits;
slouken@0
   539
		break;
slouken@0
   540
	    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@0
   541
		*value = GL_pfd.cAccumAlphaBits;
slouken@0
   542
		break;
slouken@450
   543
	    case SDL_GL_STEREO:
slouken@450
   544
		if ( GL_pfd.dwFlags & PFD_STEREO ) {
slouken@450
   545
			*value = 1;
slouken@450
   546
		} else {
slouken@450
   547
			*value = 0;
slouken@450
   548
		}
slouken@450
   549
		break;
slouken@1261
   550
	    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@1261
   551
		*value = 0;
slouken@1261
   552
		break;
slouken@1261
   553
	    case SDL_GL_MULTISAMPLESAMPLES:
slouken@1261
   554
		*value = 1;
slouken@1261
   555
		break;
slouken@1880
   556
	    case SDL_GL_SWAP_CONTROL:
slouken@1880
   557
		if ( this->gl_data->wglGetSwapIntervalEXT ) {
slouken@1880
   558
			*value = this->gl_data->wglGetSwapIntervalEXT();
slouken@1880
   559
			return 0;
slouken@1880
   560
		} else {
slouken@1880
   561
			return -1;
slouken@1880
   562
		}
slouken@1880
   563
		break;
slouken@0
   564
	    default:
slouken@0
   565
		retval = -1;
slouken@0
   566
		break;
slouken@0
   567
	}
slouken@0
   568
	return retval;
slouken@0
   569
}
slouken@0
   570
slouken@0
   571
void WIN_GL_SwapBuffers(_THIS)
slouken@0
   572
{
slouken@0
   573
	SwapBuffers(GL_hdc);
slouken@0
   574
}
slouken@0
   575
slouken@0
   576
void WIN_GL_UnloadLibrary(_THIS)
slouken@0
   577
{
slouken@0
   578
	if ( this->gl_config.driver_loaded ) {
slouken@0
   579
		FreeLibrary((HMODULE)this->gl_config.dll_handle);
slouken@0
   580
slouken@0
   581
		this->gl_data->wglGetProcAddress = NULL;
slouken@0
   582
		this->gl_data->wglCreateContext = NULL;
slouken@0
   583
		this->gl_data->wglDeleteContext = NULL;
slouken@0
   584
		this->gl_data->wglMakeCurrent = NULL;
slouken@655
   585
		this->gl_data->wglGetPixelFormatAttribivARB = NULL;
slouken@1736
   586
		this->gl_data->wglSwapIntervalEXT = NULL;
slouken@1736
   587
		this->gl_data->wglGetSwapIntervalEXT = NULL;
slouken@0
   588
slouken@0
   589
		this->gl_config.dll_handle = NULL;
slouken@0
   590
		this->gl_config.driver_loaded = 0;
slouken@0
   591
	}
slouken@0
   592
}
slouken@0
   593
slouken@0
   594
/* Passing a NULL path means load pointers from the application */
slouken@0
   595
int WIN_GL_LoadLibrary(_THIS, const char* path) 
slouken@0
   596
{
slouken@0
   597
	HMODULE handle;
slouken@0
   598
slouken@0
   599
 	if ( gl_active ) {
slouken@0
   600
 		SDL_SetError("OpenGL context already created");
slouken@0
   601
 		return -1;
slouken@0
   602
 	}
slouken@0
   603
slouken@0
   604
	if ( path == NULL ) {
slouken@0
   605
		path = DEFAULT_GL_DRIVER_PATH;
slouken@0
   606
	}
slouken@0
   607
	handle = LoadLibrary(path);
slouken@0
   608
	if ( handle == NULL ) {
slouken@0
   609
		SDL_SetError("Could not load OpenGL library");
slouken@0
   610
		return -1;
slouken@0
   611
	}
slouken@0
   612
slouken@0
   613
	/* Unload the old driver and reset the pointers */
slouken@0
   614
	WIN_GL_UnloadLibrary(this);
slouken@0
   615
slouken@0
   616
	/* Load new function pointers */
slouken@1336
   617
	SDL_memset(this->gl_data, 0, sizeof(*this->gl_data));
slouken@0
   618
	this->gl_data->wglGetProcAddress = (void * (WINAPI *)(const char *))
slouken@0
   619
		GetProcAddress(handle, "wglGetProcAddress");
slouken@0
   620
	this->gl_data->wglCreateContext = (HGLRC (WINAPI *)(HDC))
slouken@0
   621
		GetProcAddress(handle, "wglCreateContext");
slouken@0
   622
	this->gl_data->wglDeleteContext = (BOOL (WINAPI *)(HGLRC))
slouken@0
   623
		GetProcAddress(handle, "wglDeleteContext");
slouken@0
   624
	this->gl_data->wglMakeCurrent = (BOOL (WINAPI *)(HDC, HGLRC))
slouken@0
   625
		GetProcAddress(handle, "wglMakeCurrent");
slouken@1736
   626
	this->gl_data->wglSwapIntervalEXT = (void (WINAPI *)(int))
slouken@1736
   627
		GetProcAddress(handle, "wglSwapIntervalEXT");
slouken@1736
   628
	this->gl_data->wglGetSwapIntervalEXT = (int (WINAPI *)(void))
slouken@1736
   629
		GetProcAddress(handle, "wglGetSwapIntervalEXT");
slouken@0
   630
slouken@0
   631
	if ( (this->gl_data->wglGetProcAddress == NULL) ||
slouken@0
   632
	     (this->gl_data->wglCreateContext == NULL) ||
slouken@0
   633
	     (this->gl_data->wglDeleteContext == NULL) ||
slouken@0
   634
	     (this->gl_data->wglMakeCurrent == NULL) ) {
slouken@0
   635
		SDL_SetError("Could not retrieve OpenGL functions");
slouken@0
   636
		FreeLibrary(handle);
slouken@0
   637
		return -1;
slouken@0
   638
	}
slouken@0
   639
slouken@0
   640
	this->gl_config.dll_handle = handle;
slouken@1379
   641
	SDL_strlcpy(this->gl_config.driver_path, path, SDL_arraysize(this->gl_config.driver_path));
slouken@0
   642
	this->gl_config.driver_loaded = 1;
slouken@0
   643
	return 0;
slouken@0
   644
}
slouken@0
   645
slouken@0
   646
void *WIN_GL_GetProcAddress(_THIS, const char* proc)
slouken@0
   647
{
slouken@0
   648
	void *func;
slouken@0
   649
slouken@0
   650
	/* This is to pick up extensions */
slouken@0
   651
	func = this->gl_data->wglGetProcAddress(proc);
slouken@0
   652
	if ( ! func ) {
slouken@0
   653
		/* This is probably a normal GL function */
slouken@0
   654
		func = GetProcAddress(this->gl_config.dll_handle, proc);
slouken@0
   655
	}
slouken@0
   656
	return func;
slouken@0
   657
}
slouken@0
   658
slouken@1361
   659
#endif /* SDL_VIDEO_OPENGL */