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