src/video/x11/SDL_x11gl.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 22 Jul 2003 15:10:06 +0000
changeset 655 9c42ee1b7d77
parent 646 0f2029a70548
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@0
    28
#include <stdlib.h>	/* For getenv() prototype */
slouken@0
    29
#include <string.h>
slouken@0
    30
slouken@0
    31
#include "SDL_events_c.h"
slouken@0
    32
#include "SDL_error.h"
slouken@0
    33
#include "SDL_x11video.h"
slouken@0
    34
#include "SDL_x11dga_c.h"
slouken@0
    35
#include "SDL_x11gl_c.h"
slouken@0
    36
slouken@0
    37
#define DEFAULT_OPENGL	"libGL.so.1"
slouken@0
    38
slouken@0
    39
/* return the preferred visual to use for openGL graphics */
slouken@0
    40
XVisualInfo *X11_GL_GetVisual(_THIS)
slouken@0
    41
{
slouken@0
    42
#ifdef HAVE_OPENGL
slouken@0
    43
	/* 64 seems nice. */
slouken@0
    44
	int attribs[64];
slouken@0
    45
	int i;
slouken@0
    46
slouken@0
    47
	/* load the gl driver from a default path */
slouken@0
    48
	if ( ! this->gl_config.driver_loaded ) {
slouken@0
    49
	        /* no driver has been loaded, use default (ourselves) */
slouken@0
    50
	        if ( X11_GL_LoadLibrary(this, NULL) < 0 ) {
slouken@0
    51
		        return NULL;
slouken@0
    52
		}
slouken@0
    53
	}
slouken@0
    54
slouken@0
    55
	/* See if we already have a window which we must use */
slouken@0
    56
	if ( SDL_windowid ) {
slouken@0
    57
		XWindowAttributes a;
slouken@0
    58
		XVisualInfo vi_in;
slouken@0
    59
		int out_count;
slouken@0
    60
slouken@0
    61
		XGetWindowAttributes(SDL_Display, SDL_Window, &a);
slouken@0
    62
		vi_in.screen = SDL_Screen;
slouken@0
    63
		vi_in.visualid = XVisualIDFromVisual(a.visual);
slouken@0
    64
		glx_visualinfo = XGetVisualInfo(SDL_Display,
slouken@0
    65
	                     VisualScreenMask|VisualIDMask, &vi_in, &out_count);
slouken@0
    66
		return glx_visualinfo;
slouken@0
    67
	}
slouken@0
    68
slouken@0
    69
        /* Setup our GLX attributes according to the gl_config. */
slouken@450
    70
	i = 0;
slouken@450
    71
	attribs[i++] = GLX_RGBA;
slouken@0
    72
	attribs[i++] = GLX_RED_SIZE;
slouken@0
    73
	attribs[i++] = this->gl_config.red_size;
slouken@0
    74
	attribs[i++] = GLX_GREEN_SIZE;
slouken@0
    75
	attribs[i++] = this->gl_config.green_size;
slouken@0
    76
	attribs[i++] = GLX_BLUE_SIZE;
slouken@0
    77
	attribs[i++] = this->gl_config.blue_size;
slouken@0
    78
slouken@0
    79
	if( this->gl_config.alpha_size ) {
slouken@0
    80
		attribs[i++] = GLX_ALPHA_SIZE;
slouken@0
    81
		attribs[i++] = this->gl_config.alpha_size;
slouken@0
    82
	}
slouken@0
    83
slouken@0
    84
	if( this->gl_config.buffer_size ) {
slouken@450
    85
		attribs[i++] = GLX_BUFFER_SIZE;
slouken@450
    86
		attribs[i++] = this->gl_config.buffer_size;
slouken@0
    87
	}
slouken@0
    88
slouken@0
    89
	if( this->gl_config.double_buffer ) {
slouken@0
    90
		attribs[i++] = GLX_DOUBLEBUFFER;
slouken@0
    91
	}
slouken@0
    92
slouken@0
    93
	attribs[i++] = GLX_DEPTH_SIZE;
slouken@0
    94
	attribs[i++] = this->gl_config.depth_size;
slouken@0
    95
slouken@0
    96
	if( this->gl_config.stencil_size ) {
slouken@0
    97
		attribs[i++] = GLX_STENCIL_SIZE;
slouken@0
    98
		attribs[i++] = this->gl_config.stencil_size;
slouken@0
    99
	}
slouken@0
   100
slouken@0
   101
	if( this->gl_config.accum_red_size ) {
slouken@450
   102
		attribs[i++] = GLX_ACCUM_RED_SIZE;
slouken@0
   103
		attribs[i++] = this->gl_config.accum_red_size;
slouken@0
   104
	}
slouken@0
   105
slouken@0
   106
	if( this->gl_config.accum_green_size ) {
slouken@450
   107
		attribs[i++] = GLX_ACCUM_GREEN_SIZE;
slouken@0
   108
		attribs[i++] = this->gl_config.accum_green_size;
slouken@0
   109
	}
slouken@0
   110
slouken@0
   111
	if( this->gl_config.accum_blue_size ) {
slouken@450
   112
		attribs[i++] = GLX_ACCUM_BLUE_SIZE;
slouken@0
   113
		attribs[i++] = this->gl_config.accum_blue_size;
slouken@0
   114
	}
slouken@0
   115
slouken@0
   116
	if( this->gl_config.accum_alpha_size ) {
slouken@450
   117
		attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
slouken@0
   118
		attribs[i++] = this->gl_config.accum_alpha_size;
slouken@0
   119
	}
slouken@0
   120
slouken@450
   121
	if( this->gl_config.stereo ) {
slouken@450
   122
		attribs[i++] = GLX_STEREO;
slouken@450
   123
		attribs[i++] = this->gl_config.stereo;
slouken@450
   124
	}
slouken@655
   125
	
slouken@655
   126
	if( this->gl_config.sample_buffers ) {
slouken@655
   127
		attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
slouken@655
   128
		attribs[i++] = this->gl_config.sample_buffers;
slouken@655
   129
	}
slouken@655
   130
	
slouken@655
   131
	if( this->gl_config.samples ) {
slouken@655
   132
		attribs[i++] = GLX_SAMPLES_ARB;
slouken@655
   133
		attribs[i++] = this->gl_config.samples;
slouken@655
   134
	}
slouken@450
   135
slouken@0
   136
#ifdef GLX_DIRECT_COLOR /* Try for a DirectColor visual for gamma support */
slouken@0
   137
	attribs[i++] = GLX_X_VISUAL_TYPE;
slouken@0
   138
	attribs[i++] = GLX_DIRECT_COLOR;
slouken@0
   139
#endif
slouken@0
   140
	attribs[i++] = None;
slouken@0
   141
slouken@0
   142
 	glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display, 
slouken@0
   143
						  SDL_Screen, attribs);
slouken@0
   144
#ifdef GLX_DIRECT_COLOR
slouken@0
   145
	if( !glx_visualinfo ) { /* No DirectColor visual?  Try again.. */
slouken@0
   146
		attribs[i-3] = None;
slouken@0
   147
 		glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display, 
slouken@0
   148
						  SDL_Screen, attribs);
slouken@0
   149
	}
slouken@0
   150
#endif
slouken@0
   151
	if( !glx_visualinfo ) {
slouken@0
   152
		SDL_SetError( "Couldn't find matching GLX visual");
slouken@0
   153
		return NULL;
slouken@0
   154
	}
slouken@0
   155
	return glx_visualinfo;
slouken@0
   156
#else
slouken@0
   157
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   158
	return NULL;
slouken@0
   159
#endif
slouken@0
   160
}
slouken@0
   161
slouken@0
   162
int X11_GL_CreateWindow(_THIS, int w, int h)
slouken@0
   163
{
slouken@0
   164
	int retval;
slouken@0
   165
#ifdef HAVE_OPENGL
slouken@0
   166
	XSetWindowAttributes attributes;
slouken@0
   167
	unsigned long mask;
slouken@0
   168
	unsigned long black;
slouken@0
   169
slouken@0
   170
	black = (glx_visualinfo->visual == DefaultVisual(SDL_Display,
slouken@0
   171
						 	SDL_Screen))
slouken@0
   172
	       	? BlackPixel(SDL_Display, SDL_Screen) : 0;
slouken@0
   173
	attributes.background_pixel = black;
slouken@0
   174
	attributes.border_pixel = black;
slouken@0
   175
	attributes.colormap = SDL_XColorMap;
slouken@0
   176
	mask = CWBackPixel | CWBorderPixel | CWColormap;
slouken@0
   177
slouken@0
   178
	SDL_Window = XCreateWindow(SDL_Display, WMwindow,
slouken@0
   179
			0, 0, w, h, 0, glx_visualinfo->depth,
slouken@0
   180
			InputOutput, glx_visualinfo->visual,
slouken@0
   181
			mask, &attributes);
slouken@0
   182
	if ( !SDL_Window ) {
slouken@0
   183
		SDL_SetError("Could not create window");
slouken@0
   184
		return -1;
slouken@0
   185
	}
slouken@0
   186
	retval = 0;
slouken@0
   187
#else
slouken@0
   188
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   189
	retval = -1;
slouken@0
   190
#endif
slouken@0
   191
	return(retval);
slouken@0
   192
}
slouken@0
   193
slouken@0
   194
int X11_GL_CreateContext(_THIS)
slouken@0
   195
{
slouken@0
   196
	int retval;
slouken@0
   197
#ifdef HAVE_OPENGL
slouken@0
   198
	/* We do this to create a clean separation between X and GLX errors. */
slouken@0
   199
	XSync( SDL_Display, False );
slouken@0
   200
	glx_context = this->gl_data->glXCreateContext(GFX_Display, 
slouken@0
   201
				     glx_visualinfo, NULL, True);
slouken@0
   202
	XSync( GFX_Display, False );
slouken@0
   203
slouken@0
   204
	if (glx_context == NULL) {
slouken@0
   205
		SDL_SetError("Could not create GL context");
slouken@0
   206
		return -1;
slouken@0
   207
	}
slouken@0
   208
slouken@0
   209
	gl_active = 1;
slouken@0
   210
#else
slouken@0
   211
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   212
#endif
slouken@0
   213
	if ( gl_active ) {
slouken@0
   214
		retval = 0;
slouken@0
   215
	} else {
slouken@0
   216
		retval = -1;
slouken@0
   217
	}
slouken@0
   218
	return(retval);
slouken@0
   219
}
slouken@0
   220
slouken@0
   221
void X11_GL_Shutdown(_THIS)
slouken@0
   222
{
slouken@0
   223
#ifdef HAVE_OPENGL
slouken@0
   224
	/* Clean up OpenGL */
slouken@0
   225
	if( glx_context ) {
slouken@0
   226
		this->gl_data->glXMakeCurrent(GFX_Display, None, NULL);
slouken@0
   227
slouken@0
   228
		if (glx_context != NULL)
slouken@0
   229
			this->gl_data->glXDestroyContext(GFX_Display, glx_context);
slouken@0
   230
slouken@0
   231
		if( this->gl_data->glXReleaseBuffersMESA ) {
slouken@0
   232
		    this->gl_data->glXReleaseBuffersMESA(GFX_Display,SDL_Window);
slouken@0
   233
		}
slouken@0
   234
		glx_context = NULL;
slouken@0
   235
	}
slouken@0
   236
	gl_active = 0;
slouken@0
   237
#endif /* HAVE_OPENGL */
slouken@0
   238
}
slouken@0
   239
slouken@0
   240
#ifdef HAVE_OPENGL
slouken@0
   241
slouken@655
   242
static int ExtensionSupported(const char *extension)
slouken@638
   243
{
slouken@638
   244
	const GLubyte *extensions = NULL;
slouken@638
   245
	const GLubyte *start;
slouken@638
   246
	GLubyte *where, *terminator;
slouken@638
   247
slouken@638
   248
	/* Extension names should not have spaces. */
slouken@638
   249
	where = (GLubyte *) strchr(extension, ' ');
slouken@638
   250
	if (where || *extension == '\0')
slouken@638
   251
	      return 0;
slouken@638
   252
	
icculus@643
   253
	extensions = current_video->glGetString(GL_EXTENSIONS);
slouken@638
   254
	/* It takes a bit of care to be fool-proof about parsing the
slouken@638
   255
	 *      OpenGL extensions string. Don't be fooled by sub-strings,
slouken@638
   256
	 *           etc. */
slouken@638
   257
	
slouken@638
   258
	start = extensions;
slouken@638
   259
	
slouken@638
   260
	for (;;)
slouken@638
   261
	{
slouken@638
   262
		where = (GLubyte *) strstr((const char *) start, extension);
slouken@638
   263
		if (!where) break;
slouken@638
   264
		
slouken@638
   265
		terminator = where + strlen(extension);
slouken@638
   266
		if (where == start || *(where - 1) == ' ')
slouken@638
   267
	        if (*terminator == ' ' || *terminator == '\0') return 1;
slouken@638
   268
						  
slouken@638
   269
		start = terminator;
slouken@638
   270
	}
slouken@638
   271
	
slouken@638
   272
	return 0;
slouken@638
   273
}
slouken@638
   274
slouken@0
   275
/* Make the current context active */
slouken@0
   276
int X11_GL_MakeCurrent(_THIS)
slouken@0
   277
{
slouken@0
   278
	int retval;
slouken@638
   279
	
slouken@0
   280
	retval = 0;
slouken@0
   281
	if ( ! this->gl_data->glXMakeCurrent(GFX_Display,
slouken@0
   282
	                                     SDL_Window, glx_context) ) {
slouken@0
   283
		SDL_SetError("Unable to make GL context current");
slouken@0
   284
		retval = -1;
slouken@0
   285
	}
slouken@0
   286
	XSync( GFX_Display, False );
slouken@0
   287
slouken@638
   288
	/* 
slouken@638
   289
	 * The context is now current, check for glXReleaseBuffersMESA() 
slouken@638
   290
	 * extension. If extension is _not_ supported, destroy the pointer 
slouken@638
   291
	 * (to make sure it will not be called in X11_GL_Shutdown() ).
slouken@638
   292
	 * 
slouken@638
   293
	 * DRI/Mesa drivers include glXReleaseBuffersMESA() in the libGL.so, 
slouken@638
   294
	 * but there's no need to call it (is is only needed for some old 
slouken@638
   295
	 * non-DRI drivers).
slouken@638
   296
	 * 
slouken@638
   297
	 * When using for example glew (http://glew.sf.net), dlsym() for
slouken@638
   298
	 * glXReleaseBuffersMESA() returns the pointer from the glew library
slouken@638
   299
	 * (namespace conflict).
slouken@638
   300
	 *
slouken@638
   301
	 * The glXReleaseBuffersMESA() pointer in the glew is NULL, if the 
slouken@638
   302
	 * driver doesn't support this extension. So blindly calling it will
slouken@638
   303
	 * cause segfault with DRI/Mesa drivers!
slouken@638
   304
	 * 
slouken@638
   305
	 */
slouken@638
   306
	
slouken@655
   307
	if ( ! ExtensionSupported("glXReleaseBuffersMESA") ) {
slouken@655
   308
		this->gl_data->glXReleaseBuffersMESA = NULL;
slouken@655
   309
	}
slouken@655
   310
slouken@0
   311
	/* More Voodoo X server workarounds... Grr... */
slouken@0
   312
	SDL_Lock_EventThread();
slouken@0
   313
	X11_CheckDGAMouse(this);
slouken@0
   314
	SDL_Unlock_EventThread();
slouken@0
   315
slouken@0
   316
	return(retval);
slouken@0
   317
}
slouken@0
   318
slouken@0
   319
/* Get attribute data from glX. */
slouken@0
   320
int X11_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   321
{
slouken@0
   322
	int retval;
slouken@0
   323
	int glx_attrib = None;
slouken@0
   324
slouken@0
   325
	switch( attrib ) {
slouken@0
   326
	    case SDL_GL_RED_SIZE:
slouken@0
   327
		glx_attrib = GLX_RED_SIZE;
slouken@0
   328
		break;
slouken@0
   329
	    case SDL_GL_GREEN_SIZE:
slouken@0
   330
		glx_attrib = GLX_GREEN_SIZE;
slouken@0
   331
		break;
slouken@0
   332
	    case SDL_GL_BLUE_SIZE:
slouken@0
   333
		glx_attrib = GLX_BLUE_SIZE;
slouken@0
   334
		break;
slouken@0
   335
	    case SDL_GL_ALPHA_SIZE:
slouken@0
   336
		glx_attrib = GLX_ALPHA_SIZE;
slouken@0
   337
		break;
slouken@0
   338
	    case SDL_GL_DOUBLEBUFFER:
slouken@0
   339
		glx_attrib = GLX_DOUBLEBUFFER;
slouken@0
   340
		break;
slouken@0
   341
	    case SDL_GL_BUFFER_SIZE:
slouken@0
   342
		glx_attrib = GLX_BUFFER_SIZE;
slouken@0
   343
		break;
slouken@0
   344
	    case SDL_GL_DEPTH_SIZE:
slouken@0
   345
		glx_attrib = GLX_DEPTH_SIZE;
slouken@0
   346
		break;
slouken@0
   347
	    case SDL_GL_STENCIL_SIZE:
slouken@0
   348
		glx_attrib = GLX_STENCIL_SIZE;
slouken@0
   349
		break;
slouken@0
   350
	    case SDL_GL_ACCUM_RED_SIZE:
slouken@0
   351
		glx_attrib = GLX_ACCUM_RED_SIZE;
slouken@0
   352
		break;
slouken@0
   353
	    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@0
   354
		glx_attrib = GLX_ACCUM_GREEN_SIZE;
slouken@0
   355
		break;
slouken@0
   356
	    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@0
   357
		glx_attrib = GLX_ACCUM_BLUE_SIZE;
slouken@0
   358
		break;
slouken@0
   359
	    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@0
   360
		glx_attrib = GLX_ACCUM_ALPHA_SIZE;
slouken@0
   361
		break;
slouken@450
   362
	    case SDL_GL_STEREO:
slouken@450
   363
		glx_attrib = GLX_STEREO;
slouken@450
   364
		break;
slouken@655
   365
 	    case SDL_GL_SAMPLE_BUFFERS:
slouken@655
   366
 		glx_attrib = GLX_SAMPLE_BUFFERS_ARB;
slouken@655
   367
 		break;
slouken@655
   368
 	    case SDL_GL_SAMPLES:
slouken@655
   369
 		glx_attrib = GLX_SAMPLES_ARB;
slouken@655
   370
 		break;
slouken@0
   371
	    default:
slouken@0
   372
		return(-1);
slouken@0
   373
	}
slouken@0
   374
slouken@0
   375
	retval = this->gl_data->glXGetConfig(GFX_Display, glx_visualinfo, glx_attrib, value);
slouken@0
   376
slouken@0
   377
	return retval;
slouken@0
   378
}
slouken@0
   379
slouken@0
   380
void X11_GL_SwapBuffers(_THIS)
slouken@0
   381
{
slouken@0
   382
	this->gl_data->glXSwapBuffers(GFX_Display, SDL_Window);
slouken@0
   383
}
slouken@0
   384
slouken@0
   385
#endif /* HAVE_OPENGL */
slouken@0
   386
slouken@0
   387
void X11_GL_UnloadLibrary(_THIS)
slouken@0
   388
{
slouken@0
   389
#ifdef HAVE_OPENGL
slouken@0
   390
	if ( this->gl_config.driver_loaded ) {
slouken@0
   391
		dlclose(this->gl_config.dll_handle);
slouken@0
   392
slouken@0
   393
		this->gl_data->glXGetProcAddress = NULL;
slouken@0
   394
		this->gl_data->glXChooseVisual = NULL;
slouken@0
   395
		this->gl_data->glXCreateContext = NULL;
slouken@0
   396
		this->gl_data->glXDestroyContext = NULL;
slouken@0
   397
		this->gl_data->glXMakeCurrent = NULL;
slouken@0
   398
		this->gl_data->glXSwapBuffers = NULL;
slouken@0
   399
slouken@0
   400
		this->gl_config.dll_handle = NULL;
slouken@0
   401
		this->gl_config.driver_loaded = 0;
slouken@0
   402
	}
slouken@0
   403
#endif
slouken@0
   404
}
slouken@0
   405
slouken@0
   406
#ifdef HAVE_OPENGL
slouken@0
   407
slouken@0
   408
/* Passing a NULL path means load pointers from the application */
slouken@0
   409
int X11_GL_LoadLibrary(_THIS, const char* path) 
slouken@0
   410
{
slouken@0
   411
	void* handle;
slouken@0
   412
	int dlopen_flags;
slouken@0
   413
slouken@0
   414
 	if ( gl_active ) {
slouken@0
   415
 		SDL_SetError("OpenGL context already created");
slouken@0
   416
 		return -1;
slouken@0
   417
 	}
slouken@0
   418
slouken@0
   419
#ifdef RTLD_GLOBAL
slouken@0
   420
	dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
slouken@0
   421
#else
slouken@0
   422
	dlopen_flags = RTLD_LAZY;
slouken@0
   423
#endif
slouken@0
   424
	handle = dlopen(path, dlopen_flags);
slouken@0
   425
	/* Catch the case where the application isn't linked with GL */
slouken@0
   426
	if ( (dlsym(handle, "glXChooseVisual") == NULL) && (path == NULL) ) {
slouken@0
   427
		dlclose(handle);
slouken@0
   428
		path = getenv("SDL_VIDEO_GL_DRIVER");
slouken@0
   429
		if ( path == NULL ) {
slouken@0
   430
			path = DEFAULT_OPENGL;
slouken@0
   431
		}
slouken@0
   432
		handle = dlopen(path, dlopen_flags);
slouken@0
   433
	}
slouken@0
   434
	if ( handle == NULL ) {
slouken@0
   435
		SDL_SetError("Could not load OpenGL library");
slouken@0
   436
		return -1;
slouken@0
   437
	}
slouken@0
   438
slouken@0
   439
	/* Unload the old driver and reset the pointers */
slouken@0
   440
	X11_GL_UnloadLibrary(this);
slouken@0
   441
slouken@0
   442
	/* Load new function pointers */
slouken@180
   443
	this->gl_data->glXGetProcAddress =
slouken@180
   444
		(void *(*)(const GLubyte *)) dlsym(handle, "glXGetProcAddressARB");
slouken@180
   445
	this->gl_data->glXChooseVisual =
slouken@180
   446
		(XVisualInfo *(*)(Display *, int, int *)) dlsym(handle, "glXChooseVisual");
slouken@180
   447
	this->gl_data->glXCreateContext =
slouken@180
   448
		(GLXContext (*)(Display *, XVisualInfo *, GLXContext, int)) dlsym(handle, "glXCreateContext");
slouken@180
   449
	this->gl_data->glXDestroyContext =
slouken@180
   450
		(void (*)(Display *, GLXContext)) dlsym(handle, "glXDestroyContext");
slouken@180
   451
	this->gl_data->glXMakeCurrent =
slouken@180
   452
		(int (*)(Display *, GLXDrawable, GLXContext)) dlsym(handle, "glXMakeCurrent");
slouken@180
   453
	this->gl_data->glXSwapBuffers =
slouken@180
   454
		(void (*)(Display *, GLXDrawable)) dlsym(handle, "glXSwapBuffers");
slouken@180
   455
	this->gl_data->glXGetConfig =
slouken@180
   456
		(int (*)(Display *, XVisualInfo *, int, int *)) dlsym(handle, "glXGetConfig");
slouken@638
   457
	this->gl_data->glXQueryExtensionsString =
slouken@646
   458
		(const char *(*)(Display *, int)) dlsym(handle, "glXQueryExtensionsString");
slouken@638
   459
	
slouken@0
   460
	/* We don't compare below for this in case we're not using Mesa. */
slouken@180
   461
	this->gl_data->glXReleaseBuffersMESA =
slouken@180
   462
		(void (*)(Display *, GLXDrawable)) dlsym( handle, "glXReleaseBuffersMESA" );
slouken@638
   463
	
slouken@638
   464
	
slouken@0
   465
	if ( (this->gl_data->glXChooseVisual == NULL) || 
slouken@0
   466
	     (this->gl_data->glXCreateContext == NULL) ||
slouken@0
   467
	     (this->gl_data->glXDestroyContext == NULL) ||
slouken@0
   468
	     (this->gl_data->glXMakeCurrent == NULL) ||
slouken@0
   469
	     (this->gl_data->glXSwapBuffers == NULL) ||
slouken@638
   470
	     (this->gl_data->glXGetConfig == NULL) ||
slouken@638
   471
	     (this->gl_data->glXQueryExtensionsString == NULL)) {
slouken@0
   472
		SDL_SetError("Could not retrieve OpenGL functions");
slouken@0
   473
		return -1;
slouken@0
   474
	}
slouken@0
   475
slouken@0
   476
	this->gl_config.dll_handle = handle;
slouken@0
   477
	this->gl_config.driver_loaded = 1;
slouken@0
   478
	if ( path ) {
slouken@0
   479
		strncpy(this->gl_config.driver_path, path,
slouken@0
   480
			sizeof(this->gl_config.driver_path)-1);
slouken@0
   481
	} else {
slouken@0
   482
		strcpy(this->gl_config.driver_path, "");
slouken@0
   483
	}
slouken@0
   484
	return 0;
slouken@0
   485
}
slouken@0
   486
slouken@0
   487
void *X11_GL_GetProcAddress(_THIS, const char* proc)
slouken@0
   488
{
slouken@110
   489
	static char procname[1024];
slouken@0
   490
	void* handle;
slouken@110
   491
	void* retval;
slouken@0
   492
	
slouken@0
   493
	handle = this->gl_config.dll_handle;
slouken@0
   494
	if ( this->gl_data->glXGetProcAddress ) {
slouken@566
   495
		return this->gl_data->glXGetProcAddress(proc);
slouken@0
   496
	}
slouken@110
   497
#if defined(__OpenBSD__) && !defined(__ELF__)
slouken@110
   498
#undef dlsym(x,y);
slouken@110
   499
#endif
slouken@110
   500
	retval = dlsym(handle, proc);
slouken@110
   501
	if (!retval && strlen(proc) <= 1022) {
slouken@127
   502
		procname[0] = '_';
slouken@110
   503
		strcpy(procname + 1, proc);
slouken@110
   504
		retval = dlsym(handle, procname);
slouken@110
   505
	}
slouken@110
   506
	return retval;
slouken@0
   507
}
slouken@0
   508
slouken@0
   509
#endif /* HAVE_OPENGL */