src/video/x11/SDL_x11gl.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 17 May 2006 08:18:28 +0000
branchSDL-1.3
changeset 1659 14717b52abc0
parent 1656 96c2f89cc7e1
child 1662 782fd950bd46
permissions -rw-r--r--
Merge trunk-1.3-3
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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
#include "SDL_x11video.h"
slouken@1361
    25
#include "../../events/SDL_events_c.h"
slouken@0
    26
#include "SDL_x11dga_c.h"
slouken@0
    27
#include "SDL_x11gl_c.h"
slouken@0
    28
slouken@1402
    29
#if defined(__IRIX__)
slouken@862
    30
/* IRIX doesn't have a GL library versioning system */
slouken@862
    31
#define DEFAULT_OPENGL	"libGL.so"
slouken@1402
    32
#elif defined(__MACOSX__)
icculus@1191
    33
#define DEFAULT_OPENGL	"/usr/X11R6/lib/libGL.1.dylib"
slouken@1402
    34
#elif defined(__QNXNTO__)
slouken@1402
    35
#define DEFAULT_OPENGL	"libGL.so.3"
slouken@862
    36
#else
slouken@0
    37
#define DEFAULT_OPENGL	"libGL.so.1"
slouken@862
    38
#endif
slouken@0
    39
slouken@667
    40
#ifndef GLX_ARB_multisample
slouken@667
    41
#define GLX_ARB_multisample
slouken@667
    42
#define GLX_SAMPLE_BUFFERS_ARB             100000
slouken@667
    43
#define GLX_SAMPLES_ARB                    100001
slouken@667
    44
#endif
slouken@667
    45
slouken@1656
    46
#ifndef GLX_EXT_visual_rating
slouken@1656
    47
#define GLX_EXT_visual_rating
slouken@1656
    48
#define GLX_VISUAL_CAVEAT_EXT              0x20
slouken@1659
    49
#define GLX_NONE_EXT                       0x8000
slouken@1656
    50
#define GLX_SLOW_VISUAL_EXT                0x8001
slouken@1656
    51
#define GLX_NON_CONFORMANT_VISUAL_EXT      0x800D
slouken@1656
    52
#endif
slouken@1656
    53
slouken@1656
    54
#if SDL_VIDEO_OPENGL_GLX
slouken@1656
    55
static int glXExtensionSupported(_THIS, const char *extension)
slouken@1656
    56
{
slouken@1656
    57
	const char *extensions;
slouken@1656
    58
	const char *start;
slouken@1656
    59
	const char *where, *terminator;
slouken@1656
    60
slouken@1656
    61
	/* Extension names should not have spaces. */
slouken@1656
    62
	where = SDL_strchr(extension, ' ');
slouken@1656
    63
	if ( where || *extension == '\0' ) {
slouken@1656
    64
	      return 0;
slouken@1656
    65
	}
slouken@1656
    66
slouken@1656
    67
	extensions = this->gl_data->glXQueryExtensionsString(GFX_Display,SDL_Screen);
slouken@1656
    68
	/* It takes a bit of care to be fool-proof about parsing the
slouken@1656
    69
	 * OpenGL extensions string. Don't be fooled by sub-strings, etc.
slouken@1656
    70
	 */
slouken@1656
    71
	
slouken@1656
    72
	start = extensions;
slouken@1656
    73
	
slouken@1656
    74
	for (;;) {
slouken@1656
    75
		where = SDL_strstr(start, extension);
slouken@1656
    76
		if (!where) break;
slouken@1656
    77
		
slouken@1656
    78
		terminator = where + strlen(extension);
slouken@1656
    79
		if (where == start || *(where - 1) == ' ')
slouken@1656
    80
	        if (*terminator == ' ' || *terminator == '\0') return 1;
slouken@1656
    81
						  
slouken@1656
    82
		start = terminator;
slouken@1656
    83
	}
slouken@1656
    84
	return 0;
slouken@1656
    85
}
slouken@1656
    86
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@1656
    87
slouken@0
    88
XVisualInfo *X11_GL_GetVisual(_THIS)
slouken@0
    89
{
slouken@1361
    90
#if SDL_VIDEO_OPENGL_GLX
slouken@0
    91
	/* 64 seems nice. */
slouken@0
    92
	int attribs[64];
slouken@0
    93
	int i;
slouken@0
    94
slouken@0
    95
	/* load the gl driver from a default path */
slouken@0
    96
	if ( ! this->gl_config.driver_loaded ) {
slouken@0
    97
	        /* no driver has been loaded, use default (ourselves) */
slouken@0
    98
	        if ( X11_GL_LoadLibrary(this, NULL) < 0 ) {
slouken@0
    99
		        return NULL;
slouken@0
   100
		}
slouken@0
   101
	}
slouken@0
   102
slouken@0
   103
	/* See if we already have a window which we must use */
slouken@0
   104
	if ( SDL_windowid ) {
slouken@0
   105
		XWindowAttributes a;
slouken@0
   106
		XVisualInfo vi_in;
slouken@0
   107
		int out_count;
slouken@0
   108
icculus@1575
   109
		XGetWindowAttributes(SDL_Display, SDL_Window, &a);
slouken@0
   110
		vi_in.screen = SDL_Screen;
icculus@1575
   111
		vi_in.visualid = XVisualIDFromVisual(a.visual);
icculus@1575
   112
		glx_visualinfo = XGetVisualInfo(SDL_Display,
slouken@0
   113
	                     VisualScreenMask|VisualIDMask, &vi_in, &out_count);
slouken@0
   114
		return glx_visualinfo;
slouken@0
   115
	}
slouken@0
   116
slouken@0
   117
        /* Setup our GLX attributes according to the gl_config. */
slouken@450
   118
	i = 0;
slouken@450
   119
	attribs[i++] = GLX_RGBA;
slouken@0
   120
	attribs[i++] = GLX_RED_SIZE;
slouken@0
   121
	attribs[i++] = this->gl_config.red_size;
slouken@0
   122
	attribs[i++] = GLX_GREEN_SIZE;
slouken@0
   123
	attribs[i++] = this->gl_config.green_size;
slouken@0
   124
	attribs[i++] = GLX_BLUE_SIZE;
slouken@0
   125
	attribs[i++] = this->gl_config.blue_size;
slouken@0
   126
slouken@0
   127
	if( this->gl_config.alpha_size ) {
slouken@0
   128
		attribs[i++] = GLX_ALPHA_SIZE;
slouken@0
   129
		attribs[i++] = this->gl_config.alpha_size;
slouken@0
   130
	}
slouken@0
   131
slouken@0
   132
	if( this->gl_config.buffer_size ) {
slouken@450
   133
		attribs[i++] = GLX_BUFFER_SIZE;
slouken@450
   134
		attribs[i++] = this->gl_config.buffer_size;
slouken@0
   135
	}
slouken@0
   136
slouken@0
   137
	if( this->gl_config.double_buffer ) {
slouken@0
   138
		attribs[i++] = GLX_DOUBLEBUFFER;
slouken@0
   139
	}
slouken@0
   140
slouken@0
   141
	attribs[i++] = GLX_DEPTH_SIZE;
slouken@0
   142
	attribs[i++] = this->gl_config.depth_size;
slouken@0
   143
slouken@0
   144
	if( this->gl_config.stencil_size ) {
slouken@0
   145
		attribs[i++] = GLX_STENCIL_SIZE;
slouken@0
   146
		attribs[i++] = this->gl_config.stencil_size;
slouken@0
   147
	}
slouken@0
   148
slouken@0
   149
	if( this->gl_config.accum_red_size ) {
slouken@450
   150
		attribs[i++] = GLX_ACCUM_RED_SIZE;
slouken@0
   151
		attribs[i++] = this->gl_config.accum_red_size;
slouken@0
   152
	}
slouken@0
   153
slouken@0
   154
	if( this->gl_config.accum_green_size ) {
slouken@450
   155
		attribs[i++] = GLX_ACCUM_GREEN_SIZE;
slouken@0
   156
		attribs[i++] = this->gl_config.accum_green_size;
slouken@0
   157
	}
slouken@0
   158
slouken@0
   159
	if( this->gl_config.accum_blue_size ) {
slouken@450
   160
		attribs[i++] = GLX_ACCUM_BLUE_SIZE;
slouken@0
   161
		attribs[i++] = this->gl_config.accum_blue_size;
slouken@0
   162
	}
slouken@0
   163
slouken@0
   164
	if( this->gl_config.accum_alpha_size ) {
slouken@450
   165
		attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
slouken@0
   166
		attribs[i++] = this->gl_config.accum_alpha_size;
slouken@0
   167
	}
slouken@0
   168
slouken@450
   169
	if( this->gl_config.stereo ) {
slouken@450
   170
		attribs[i++] = GLX_STEREO;
slouken@450
   171
	}
slouken@655
   172
	
slouken@656
   173
	if( this->gl_config.multisamplebuffers ) {
slouken@655
   174
		attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
slouken@656
   175
		attribs[i++] = this->gl_config.multisamplebuffers;
slouken@655
   176
	}
slouken@655
   177
	
slouken@656
   178
	if( this->gl_config.multisamplesamples ) {
slouken@655
   179
		attribs[i++] = GLX_SAMPLES_ARB;
slouken@656
   180
		attribs[i++] = this->gl_config.multisamplesamples;
slouken@655
   181
	}
slouken@450
   182
slouken@1656
   183
	if( this->gl_config.accelerated >= 0 &&
slouken@1656
   184
	    glXExtensionSupported(this, "GLX_EXT_visual_rating") ) {
slouken@1656
   185
		attribs[i++] = GLX_VISUAL_CAVEAT_EXT;
slouken@1659
   186
		attribs[i++] = GLX_NONE_EXT;
slouken@1656
   187
	}
slouken@1656
   188
slouken@0
   189
#ifdef GLX_DIRECT_COLOR /* Try for a DirectColor visual for gamma support */
slouken@1336
   190
	if ( !SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ) {
slouken@1060
   191
		attribs[i++] = GLX_X_VISUAL_TYPE;
slouken@1060
   192
		attribs[i++] = GLX_DIRECT_COLOR;
slouken@1060
   193
	}
slouken@0
   194
#endif
slouken@0
   195
	attribs[i++] = None;
slouken@0
   196
slouken@0
   197
 	glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display, 
slouken@0
   198
						  SDL_Screen, attribs);
slouken@0
   199
#ifdef GLX_DIRECT_COLOR
slouken@1336
   200
	if( !glx_visualinfo && !SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ) { /* No DirectColor visual?  Try again.. */
slouken@0
   201
		attribs[i-3] = None;
slouken@0
   202
 		glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display, 
slouken@0
   203
						  SDL_Screen, attribs);
slouken@0
   204
	}
slouken@0
   205
#endif
slouken@0
   206
	if( !glx_visualinfo ) {
slouken@0
   207
		SDL_SetError( "Couldn't find matching GLX visual");
slouken@0
   208
		return NULL;
slouken@0
   209
	}
slouken@1315
   210
/*
slouken@1315
   211
	printf("Found GLX visual 0x%x\n", glx_visualinfo->visualid);
slouken@1315
   212
*/
slouken@0
   213
	return glx_visualinfo;
slouken@0
   214
#else
slouken@0
   215
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   216
	return NULL;
slouken@0
   217
#endif
slouken@0
   218
}
slouken@0
   219
slouken@0
   220
int X11_GL_CreateWindow(_THIS, int w, int h)
slouken@0
   221
{
slouken@0
   222
	int retval;
slouken@1361
   223
#if SDL_VIDEO_OPENGL_GLX
slouken@0
   224
	XSetWindowAttributes attributes;
slouken@0
   225
	unsigned long mask;
slouken@0
   226
	unsigned long black;
slouken@0
   227
slouken@0
   228
	black = (glx_visualinfo->visual == DefaultVisual(SDL_Display,
slouken@0
   229
						 	SDL_Screen))
slouken@0
   230
	       	? BlackPixel(SDL_Display, SDL_Screen) : 0;
slouken@0
   231
	attributes.background_pixel = black;
slouken@0
   232
	attributes.border_pixel = black;
slouken@0
   233
	attributes.colormap = SDL_XColorMap;
slouken@0
   234
	mask = CWBackPixel | CWBorderPixel | CWColormap;
slouken@0
   235
icculus@1575
   236
	SDL_Window = XCreateWindow(SDL_Display, WMwindow,
slouken@0
   237
			0, 0, w, h, 0, glx_visualinfo->depth,
slouken@0
   238
			InputOutput, glx_visualinfo->visual,
slouken@0
   239
			mask, &attributes);
slouken@0
   240
	if ( !SDL_Window ) {
slouken@0
   241
		SDL_SetError("Could not create window");
slouken@0
   242
		return -1;
slouken@0
   243
	}
slouken@0
   244
	retval = 0;
slouken@0
   245
#else
slouken@0
   246
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   247
	retval = -1;
slouken@0
   248
#endif
slouken@0
   249
	return(retval);
slouken@0
   250
}
slouken@0
   251
slouken@0
   252
int X11_GL_CreateContext(_THIS)
slouken@0
   253
{
slouken@0
   254
	int retval;
slouken@1361
   255
#if SDL_VIDEO_OPENGL_GLX
slouken@1656
   256
slouken@0
   257
	/* We do this to create a clean separation between X and GLX errors. */
icculus@1575
   258
	XSync( SDL_Display, False );
slouken@0
   259
	glx_context = this->gl_data->glXCreateContext(GFX_Display, 
slouken@0
   260
				     glx_visualinfo, NULL, True);
icculus@1575
   261
	XSync( GFX_Display, False );
slouken@0
   262
slouken@1656
   263
	if ( glx_context == NULL ) {
slouken@0
   264
		SDL_SetError("Could not create GL context");
slouken@1656
   265
		return(-1);
slouken@0
   266
	}
slouken@1656
   267
	if ( X11_GL_MakeCurrent(this) < 0 ) {
slouken@1656
   268
		return(-1);
slouken@1656
   269
	}
slouken@1656
   270
	gl_active = 1;
slouken@0
   271
slouken@1656
   272
	if ( !glXExtensionSupported(this, "SGI_swap_control") ) {
slouken@1656
   273
		this->gl_data->glXSwapIntervalSGI = NULL;
slouken@1656
   274
	}
slouken@1656
   275
	if ( !glXExtensionSupported(this, "GLX_MESA_swap_control") ) {
slouken@1656
   276
		this->gl_data->glXSwapIntervalMESA = NULL;
slouken@1656
   277
		this->gl_data->glXGetSwapIntervalMESA = NULL;
slouken@1656
   278
	}
slouken@1656
   279
	if ( this->gl_config.swap_control >= 0 ) {
slouken@1656
   280
		if ( this->gl_data->glXSwapIntervalMESA ) {
slouken@1656
   281
			this->gl_data->glXSwapIntervalMESA(this->gl_config.swap_control);
slouken@1656
   282
		} else if ( this->gl_data->glXSwapIntervalSGI ) {
slouken@1656
   283
			this->gl_data->glXSwapIntervalSGI(this->gl_config.swap_control);
slouken@1656
   284
		}
slouken@1656
   285
	}
slouken@0
   286
#else
slouken@0
   287
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   288
#endif
slouken@0
   289
	if ( gl_active ) {
slouken@0
   290
		retval = 0;
slouken@0
   291
	} else {
slouken@0
   292
		retval = -1;
slouken@0
   293
	}
slouken@0
   294
	return(retval);
slouken@0
   295
}
slouken@0
   296
slouken@0
   297
void X11_GL_Shutdown(_THIS)
slouken@0
   298
{
slouken@1361
   299
#if SDL_VIDEO_OPENGL_GLX
slouken@0
   300
	/* Clean up OpenGL */
slouken@0
   301
	if( glx_context ) {
slouken@0
   302
		this->gl_data->glXMakeCurrent(GFX_Display, None, NULL);
slouken@0
   303
slouken@0
   304
		if (glx_context != NULL)
slouken@0
   305
			this->gl_data->glXDestroyContext(GFX_Display, glx_context);
slouken@0
   306
slouken@0
   307
		glx_context = NULL;
slouken@0
   308
	}
slouken@0
   309
	gl_active = 0;
slouken@1361
   310
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@0
   311
}
slouken@0
   312
slouken@1361
   313
#if SDL_VIDEO_OPENGL_GLX
slouken@0
   314
slouken@0
   315
/* Make the current context active */
slouken@0
   316
int X11_GL_MakeCurrent(_THIS)
slouken@0
   317
{
slouken@0
   318
	int retval;
slouken@638
   319
	
slouken@0
   320
	retval = 0;
slouken@0
   321
	if ( ! this->gl_data->glXMakeCurrent(GFX_Display,
slouken@0
   322
	                                     SDL_Window, glx_context) ) {
slouken@0
   323
		SDL_SetError("Unable to make GL context current");
slouken@0
   324
		retval = -1;
slouken@0
   325
	}
icculus@1575
   326
	XSync( GFX_Display, False );
slouken@0
   327
slouken@0
   328
	/* More Voodoo X server workarounds... Grr... */
slouken@0
   329
	SDL_Lock_EventThread();
slouken@0
   330
	X11_CheckDGAMouse(this);
slouken@0
   331
	SDL_Unlock_EventThread();
slouken@0
   332
slouken@0
   333
	return(retval);
slouken@0
   334
}
slouken@0
   335
slouken@0
   336
/* Get attribute data from glX. */
slouken@0
   337
int X11_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   338
{
slouken@0
   339
	int retval;
slouken@0
   340
	int glx_attrib = None;
slouken@0
   341
slouken@0
   342
	switch( attrib ) {
slouken@0
   343
	    case SDL_GL_RED_SIZE:
slouken@0
   344
		glx_attrib = GLX_RED_SIZE;
slouken@0
   345
		break;
slouken@0
   346
	    case SDL_GL_GREEN_SIZE:
slouken@0
   347
		glx_attrib = GLX_GREEN_SIZE;
slouken@0
   348
		break;
slouken@0
   349
	    case SDL_GL_BLUE_SIZE:
slouken@0
   350
		glx_attrib = GLX_BLUE_SIZE;
slouken@0
   351
		break;
slouken@0
   352
	    case SDL_GL_ALPHA_SIZE:
slouken@0
   353
		glx_attrib = GLX_ALPHA_SIZE;
slouken@0
   354
		break;
slouken@0
   355
	    case SDL_GL_DOUBLEBUFFER:
slouken@0
   356
		glx_attrib = GLX_DOUBLEBUFFER;
slouken@0
   357
		break;
slouken@0
   358
	    case SDL_GL_BUFFER_SIZE:
slouken@0
   359
		glx_attrib = GLX_BUFFER_SIZE;
slouken@0
   360
		break;
slouken@0
   361
	    case SDL_GL_DEPTH_SIZE:
slouken@0
   362
		glx_attrib = GLX_DEPTH_SIZE;
slouken@0
   363
		break;
slouken@0
   364
	    case SDL_GL_STENCIL_SIZE:
slouken@0
   365
		glx_attrib = GLX_STENCIL_SIZE;
slouken@0
   366
		break;
slouken@0
   367
	    case SDL_GL_ACCUM_RED_SIZE:
slouken@0
   368
		glx_attrib = GLX_ACCUM_RED_SIZE;
slouken@0
   369
		break;
slouken@0
   370
	    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@0
   371
		glx_attrib = GLX_ACCUM_GREEN_SIZE;
slouken@0
   372
		break;
slouken@0
   373
	    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@0
   374
		glx_attrib = GLX_ACCUM_BLUE_SIZE;
slouken@0
   375
		break;
slouken@0
   376
	    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@0
   377
		glx_attrib = GLX_ACCUM_ALPHA_SIZE;
slouken@0
   378
		break;
slouken@450
   379
	    case SDL_GL_STEREO:
slouken@450
   380
		glx_attrib = GLX_STEREO;
slouken@450
   381
		break;
slouken@656
   382
 	    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@655
   383
 		glx_attrib = GLX_SAMPLE_BUFFERS_ARB;
slouken@655
   384
 		break;
slouken@656
   385
 	    case SDL_GL_MULTISAMPLESAMPLES:
slouken@655
   386
 		glx_attrib = GLX_SAMPLES_ARB;
slouken@655
   387
 		break;
slouken@1656
   388
 	    case SDL_GL_ACCELERATED_VISUAL:
slouken@1656
   389
		if ( glXExtensionSupported(this, "GLX_EXT_visual_rating") ) {
slouken@1656
   390
			glx_attrib = GLX_VISUAL_CAVEAT_EXT;
slouken@1656
   391
			retval = this->gl_data->glXGetConfig(GFX_Display, glx_visualinfo, glx_attrib, value);
slouken@1656
   392
			if ( *value == GLX_SLOW_VISUAL_EXT ) {
slouken@1656
   393
				*value = SDL_FALSE;
slouken@1656
   394
			} else {
slouken@1656
   395
				*value = SDL_TRUE;
slouken@1656
   396
			}
slouken@1656
   397
			return retval;
slouken@1656
   398
		} else {
slouken@1656
   399
			return(-1);
slouken@1656
   400
		}
slouken@1656
   401
		break;
slouken@1656
   402
	    case SDL_GL_SWAP_CONTROL:
slouken@1656
   403
		if ( this->gl_data->glXGetSwapIntervalMESA ) {
slouken@1656
   404
			return this->gl_data->glXGetSwapIntervalMESA();
slouken@1656
   405
		} else {
slouken@1656
   406
			return(-1)/*(this->gl_config.swap_control > 0)*/;
slouken@1656
   407
		}
slouken@1656
   408
		break;
slouken@0
   409
	    default:
slouken@0
   410
		return(-1);
slouken@0
   411
	}
slouken@0
   412
slouken@0
   413
	retval = this->gl_data->glXGetConfig(GFX_Display, glx_visualinfo, glx_attrib, value);
slouken@0
   414
slouken@0
   415
	return retval;
slouken@0
   416
}
slouken@0
   417
slouken@0
   418
void X11_GL_SwapBuffers(_THIS)
slouken@0
   419
{
slouken@0
   420
	this->gl_data->glXSwapBuffers(GFX_Display, SDL_Window);
slouken@0
   421
}
slouken@0
   422
slouken@1361
   423
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@0
   424
slouken@0
   425
void X11_GL_UnloadLibrary(_THIS)
slouken@0
   426
{
slouken@1361
   427
#if SDL_VIDEO_OPENGL_GLX
slouken@0
   428
	if ( this->gl_config.driver_loaded ) {
icculus@1191
   429
icculus@1191
   430
		SDL_UnloadObject(this->gl_config.dll_handle);
slouken@0
   431
slouken@0
   432
		this->gl_data->glXGetProcAddress = NULL;
slouken@0
   433
		this->gl_data->glXChooseVisual = NULL;
slouken@0
   434
		this->gl_data->glXCreateContext = NULL;
slouken@0
   435
		this->gl_data->glXDestroyContext = NULL;
slouken@0
   436
		this->gl_data->glXMakeCurrent = NULL;
slouken@0
   437
		this->gl_data->glXSwapBuffers = NULL;
slouken@1656
   438
		this->gl_data->glXSwapIntervalSGI = NULL;
slouken@1656
   439
		this->gl_data->glXSwapIntervalMESA = NULL;
slouken@1656
   440
		this->gl_data->glXGetSwapIntervalMESA = NULL;
slouken@0
   441
slouken@0
   442
		this->gl_config.dll_handle = NULL;
slouken@0
   443
		this->gl_config.driver_loaded = 0;
slouken@0
   444
	}
slouken@0
   445
#endif
slouken@0
   446
}
slouken@0
   447
slouken@1361
   448
#if SDL_VIDEO_OPENGL_GLX
icculus@1182
   449
slouken@0
   450
/* Passing a NULL path means load pointers from the application */
slouken@0
   451
int X11_GL_LoadLibrary(_THIS, const char* path) 
slouken@0
   452
{
icculus@1191
   453
	void* handle = NULL;
slouken@0
   454
icculus@1182
   455
	if ( gl_active ) {
icculus@1182
   456
		SDL_SetError("OpenGL context already created");
icculus@1182
   457
		return -1;
icculus@1182
   458
	}
icculus@1182
   459
icculus@1182
   460
	if ( path == NULL ) {
slouken@1336
   461
		path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
slouken@0
   462
		if ( path == NULL ) {
slouken@0
   463
			path = DEFAULT_OPENGL;
slouken@0
   464
		}
icculus@1191
   465
	}
icculus@1191
   466
slouken@1361
   467
	handle = SDL_LoadObject(path);
slouken@1361
   468
	if ( handle == NULL ) {
slouken@1361
   469
		/* SDL_LoadObject() will call SDL_SetError() for us. */
slouken@1361
   470
		return -1;
slouken@0
   471
	}
slouken@0
   472
slouken@0
   473
	/* Unload the old driver and reset the pointers */
slouken@0
   474
	X11_GL_UnloadLibrary(this);
slouken@0
   475
slouken@0
   476
	/* Load new function pointers */
slouken@180
   477
	this->gl_data->glXGetProcAddress =
slouken@1361
   478
		(void *(*)(const GLubyte *)) SDL_LoadFunction(handle, "glXGetProcAddressARB");
slouken@180
   479
	this->gl_data->glXChooseVisual =
slouken@1361
   480
		(XVisualInfo *(*)(Display *, int, int *)) SDL_LoadFunction(handle, "glXChooseVisual");
slouken@180
   481
	this->gl_data->glXCreateContext =
slouken@1361
   482
		(GLXContext (*)(Display *, XVisualInfo *, GLXContext, int)) SDL_LoadFunction(handle, "glXCreateContext");
slouken@180
   483
	this->gl_data->glXDestroyContext =
slouken@1361
   484
		(void (*)(Display *, GLXContext)) SDL_LoadFunction(handle, "glXDestroyContext");
slouken@180
   485
	this->gl_data->glXMakeCurrent =
slouken@1361
   486
		(int (*)(Display *, GLXDrawable, GLXContext)) SDL_LoadFunction(handle, "glXMakeCurrent");
slouken@180
   487
	this->gl_data->glXSwapBuffers =
slouken@1361
   488
		(void (*)(Display *, GLXDrawable)) SDL_LoadFunction(handle, "glXSwapBuffers");
slouken@180
   489
	this->gl_data->glXGetConfig =
slouken@1361
   490
		(int (*)(Display *, XVisualInfo *, int, int *)) SDL_LoadFunction(handle, "glXGetConfig");
slouken@638
   491
	this->gl_data->glXQueryExtensionsString =
slouken@1361
   492
		(const char *(*)(Display *, int)) SDL_LoadFunction(handle, "glXQueryExtensionsString");
slouken@1656
   493
	this->gl_data->glXSwapIntervalSGI =
slouken@1656
   494
		(int (*)(int)) SDL_LoadFunction(handle, "glXSwapIntervalSGI");
slouken@1656
   495
	this->gl_data->glXSwapIntervalMESA =
slouken@1656
   496
		(GLint (*)(unsigned)) SDL_LoadFunction(handle, "glXSwapIntervalMESA");
slouken@1656
   497
	this->gl_data->glXGetSwapIntervalMESA =
slouken@1656
   498
		(GLint (*)(void)) SDL_LoadFunction(handle, "glXGetSwapIntervalMESA");
slouken@1315
   499
slouken@0
   500
	if ( (this->gl_data->glXChooseVisual == NULL) || 
slouken@0
   501
	     (this->gl_data->glXCreateContext == NULL) ||
slouken@0
   502
	     (this->gl_data->glXDestroyContext == NULL) ||
slouken@0
   503
	     (this->gl_data->glXMakeCurrent == NULL) ||
slouken@0
   504
	     (this->gl_data->glXSwapBuffers == NULL) ||
slouken@638
   505
	     (this->gl_data->glXGetConfig == NULL) ||
slouken@638
   506
	     (this->gl_data->glXQueryExtensionsString == NULL)) {
slouken@0
   507
		SDL_SetError("Could not retrieve OpenGL functions");
slouken@0
   508
		return -1;
slouken@0
   509
	}
slouken@0
   510
slouken@0
   511
	this->gl_config.dll_handle = handle;
slouken@0
   512
	this->gl_config.driver_loaded = 1;
slouken@0
   513
	if ( path ) {
slouken@1379
   514
		SDL_strlcpy(this->gl_config.driver_path, path,
slouken@1379
   515
			SDL_arraysize(this->gl_config.driver_path));
slouken@0
   516
	} else {
slouken@1379
   517
		*this->gl_config.driver_path = '\0';
slouken@0
   518
	}
slouken@0
   519
	return 0;
slouken@0
   520
}
slouken@0
   521
slouken@0
   522
void *X11_GL_GetProcAddress(_THIS, const char* proc)
slouken@0
   523
{
slouken@0
   524
	void* handle;
slouken@0
   525
	
slouken@0
   526
	handle = this->gl_config.dll_handle;
slouken@0
   527
	if ( this->gl_data->glXGetProcAddress ) {
slouken@1196
   528
		return this->gl_data->glXGetProcAddress((const GLubyte *)proc);
slouken@0
   529
	}
slouken@1361
   530
	return SDL_LoadFunction(handle, proc);
slouken@0
   531
}
slouken@0
   532
slouken@1361
   533
#endif /* SDL_VIDEO_OPENGL_GLX */