src/video/x11/SDL_x11opengl.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 20 Feb 2012 23:37:57 -0500
changeset 6296 b42657486c0d
parent 6282 64ae376becce
child 6359 b2a768065c32
permissions -rwxr-xr-x
Add OpenGL 3.X context creation support Matthias Bentrup 2011-10-30 03:58:24 PDT I've updated the context creation patch to include the bugfixes by Martin Schreiber and also included a profile bit to request a ES2 compatible profile. The wgl context creation may use 2 call to wglChoosePixelFormat if no acceleration attribute is selected, this should work around a bug with buggy AMD drivers (see #1254).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
     2
  Simple DirectMedia Layer
6138
4c64952a58fb Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
     7
  arising from the use of this software.
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
    11
  freely, subject to the following restrictions:
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5395
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#include "SDL_config.h"
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5981
diff changeset
    22
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5981
diff changeset
    23
#if SDL_VIDEO_DRIVER_X11
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5981
diff changeset
    24
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#include "SDL_x11video.h"
5981
75caa8a7d559 Fixed a whole slew of compiler warnings that -Wall exposed.
Ryan C. Gordon <icculus@icculus.org>
parents: 5630
diff changeset
    26
#include "SDL_assert.h"
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
/* GLX implementation of SDL OpenGL support */
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
#if SDL_VIDEO_OPENGL_GLX
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
#include "SDL_loadso.h"
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
#if defined(__IRIX__)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
/* IRIX doesn't have a GL library versioning system */
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
#define DEFAULT_OPENGL	"libGL.so"
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
#elif defined(__MACOSX__)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
#define DEFAULT_OPENGL	"/usr/X11R6/lib/libGL.1.dylib"
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
#elif defined(__QNXNTO__)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
#define DEFAULT_OPENGL	"libGL.so.3"
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
#else
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
#define DEFAULT_OPENGL	"libGL.so.1"
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
#endif
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
3241
08c5964f2a34 Fixed a few issues compiling with Mac OS X 10.6
Sam Lantinga <slouken@libsdl.org>
parents: 3139
diff changeset
    44
#ifndef GLX_NONE_EXT
08c5964f2a34 Fixed a few issues compiling with Mac OS X 10.6
Sam Lantinga <slouken@libsdl.org>
parents: 3139
diff changeset
    45
#define GLX_NONE_EXT                       0x8000
08c5964f2a34 Fixed a few issues compiling with Mac OS X 10.6
Sam Lantinga <slouken@libsdl.org>
parents: 3139
diff changeset
    46
#endif
08c5964f2a34 Fixed a few issues compiling with Mac OS X 10.6
Sam Lantinga <slouken@libsdl.org>
parents: 3139
diff changeset
    47
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
#ifndef GLX_ARB_multisample
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
#define GLX_ARB_multisample
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
#define GLX_SAMPLE_BUFFERS_ARB             100000
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
#define GLX_SAMPLES_ARB                    100001
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
#endif
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
#ifndef GLX_EXT_visual_rating
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
#define GLX_EXT_visual_rating
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
#define GLX_VISUAL_CAVEAT_EXT              0x20
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
#define GLX_NONE_EXT                       0x8000
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
#define GLX_SLOW_VISUAL_EXT                0x8001
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
#define GLX_NON_CONFORMANT_VISUAL_EXT      0x800D
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
#endif
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
3100
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
    62
#ifndef GLX_ARB_create_context
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
    63
#define GLX_ARB_create_context
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
    64
#define GLX_CONTEXT_MAJOR_VERSION_ARB      0x2091
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
    65
#define GLX_CONTEXT_MINOR_VERSION_ARB      0x2092
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
    66
#define GLX_CONTEXT_FLAGS_ARB              0x2094
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
    67
#define GLX_CONTEXT_DEBUG_BIT_ARB          0x0001
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
    68
#define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
5021
cd3a1d87cab7 Fixed build against 10.4 SDK
Sam Lantinga <slouken@libsdl.org>
parents: 5020
diff changeset
    69
cd3a1d87cab7 Fixed build against 10.4 SDK
Sam Lantinga <slouken@libsdl.org>
parents: 5020
diff changeset
    70
/* Typedef for the GL 3.0 context creation function */
cd3a1d87cab7 Fixed build against 10.4 SDK
Sam Lantinga <slouken@libsdl.org>
parents: 5020
diff changeset
    71
typedef GLXContext(*PFNGLXCREATECONTEXTATTRIBSARBPROC) (Display * dpy,
cd3a1d87cab7 Fixed build against 10.4 SDK
Sam Lantinga <slouken@libsdl.org>
parents: 5020
diff changeset
    72
                                                        GLXFBConfig config,
cd3a1d87cab7 Fixed build against 10.4 SDK
Sam Lantinga <slouken@libsdl.org>
parents: 5020
diff changeset
    73
                                                        GLXContext
cd3a1d87cab7 Fixed build against 10.4 SDK
Sam Lantinga <slouken@libsdl.org>
parents: 5020
diff changeset
    74
                                                        share_context,
cd3a1d87cab7 Fixed build against 10.4 SDK
Sam Lantinga <slouken@libsdl.org>
parents: 5020
diff changeset
    75
                                                        Bool direct,
cd3a1d87cab7 Fixed build against 10.4 SDK
Sam Lantinga <slouken@libsdl.org>
parents: 5020
diff changeset
    76
                                                        const int
cd3a1d87cab7 Fixed build against 10.4 SDK
Sam Lantinga <slouken@libsdl.org>
parents: 5020
diff changeset
    77
                                                        *attrib_list);
6296
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    78
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    79
#ifndef GLX_ARB_create_context_profile
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    80
#define GLX_ARB_create_context_profile
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    81
#define GLX_CONTEXT_PROFILE_MASK_ARB       0x9126
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    82
#define GLX_CONTEXT_CORE_PROFILE_BIT_ARB   0x00000001
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    83
#define GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    84
#endif
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    85
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    86
#ifndef GLX_ARB_create_context_robustness
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    87
#define GLX_ARB_create_context_robustness
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    88
#define GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB  0x00000004
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    89
#define GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB     0x8256
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    90
#define GLX_NO_RESET_NOTIFICATION_ARB                   0x8261
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    91
#define GLX_LOSE_CONTEXT_ON_RESET_ARB                   0x8252
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    92
#endif
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    93
#endif
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    94
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    95
#ifndef GLX_EXT_create_context_es2_profile
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    96
#define GLX_EXT_create_context_es2_profile
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    97
#define GLX_CONTEXT_ES2_PROFILE_BIT_EXT    0x00000002
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    98
#endif
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
    99
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   100
#ifndef GLX_EXT_swap_control
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   101
#define GLX_SWAP_INTERVAL_EXT              0x20F1
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   102
#define GLX_MAX_SWAP_INTERVAL_EXT          0x20F2
3100
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   103
#endif
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   104
6183
6d9a266e04bb The #define was asking to use dlopen(), but the code wasn't doing it.
Sam Lantinga <slouken@libsdl.org>
parents: 6181
diff changeset
   105
#define OPENGL_REQUIRES_DLOPEN
6d9a266e04bb The #define was asking to use dlopen(), but the code wasn't doing it.
Sam Lantinga <slouken@libsdl.org>
parents: 6181
diff changeset
   106
#if defined(OPENGL_REQUIRES_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
#include <dlfcn.h>
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
#define GL_LoadObject(X)	dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
#define GL_LoadFunction		dlsym
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
#define GL_UnloadObject		dlclose
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
#else
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
#define GL_LoadObject	SDL_LoadObject
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
#define GL_LoadFunction	SDL_LoadFunction
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
#define GL_UnloadObject	SDL_UnloadObject
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
#endif
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   117
static void X11_GL_InitExtensions(_THIS);
2322
c25d45b7add3 Fixing valgrind errors.
Bob Pendleton <bob@pendleton.com>
parents: 2320
diff changeset
   118
3100
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   119
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
int
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
X11_GL_LoadLibrary(_THIS, const char *path)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   122
{
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
    void *handle;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   125
    /* Load the OpenGL library */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   126
    if (path == NULL) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   127
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   128
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   129
    if (path == NULL) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   130
        path = DEFAULT_OPENGL;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   131
    }
6183
6d9a266e04bb The #define was asking to use dlopen(), but the code wasn't doing it.
Sam Lantinga <slouken@libsdl.org>
parents: 6181
diff changeset
   132
    _this->gl_config.dll_handle = GL_LoadObject(path);
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   133
    if (!_this->gl_config.dll_handle) {
6183
6d9a266e04bb The #define was asking to use dlopen(), but the code wasn't doing it.
Sam Lantinga <slouken@libsdl.org>
parents: 6181
diff changeset
   134
#if defined(OPENGL_REQUIRES_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
6d9a266e04bb The #define was asking to use dlopen(), but the code wasn't doing it.
Sam Lantinga <slouken@libsdl.org>
parents: 6181
diff changeset
   135
        SDL_SetError("Failed loading %s: %s", path, dlerror());
6d9a266e04bb The #define was asking to use dlopen(), but the code wasn't doing it.
Sam Lantinga <slouken@libsdl.org>
parents: 6181
diff changeset
   136
#endif
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   137
        return -1;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   138
    }
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   139
    SDL_strlcpy(_this->gl_config.driver_path, path,
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   140
                SDL_arraysize(_this->gl_config.driver_path));
2244
Sam Lantinga <slouken@libsdl.org>
parents: 2243
diff changeset
   141
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   142
    /* Allocate OpenGL memory */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   143
    _this->gl_data =
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   144
        (struct SDL_GLDriverData *) SDL_calloc(1,
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   145
                                               sizeof(struct
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   146
                                                      SDL_GLDriverData));
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   147
    if (!_this->gl_data) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   148
        SDL_OutOfMemory();
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   149
        return -1;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   150
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   151
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   152
    /* Load function pointers */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   153
    handle = _this->gl_config.dll_handle;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
    _this->gl_data->glXGetProcAddress =
4550
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   155
        (void *(*)(const GLubyte *))
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   156
            GL_LoadFunction(handle, "glXGetProcAddressARB");
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
    _this->gl_data->glXChooseVisual =
4550
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   158
        (XVisualInfo * (*)(Display *, int, int *))
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   159
            X11_GL_GetProcAddress(_this, "glXChooseVisual");
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   160
    _this->gl_data->glXCreateContext =
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
        (GLXContext(*)(Display *, XVisualInfo *, GLXContext, int))
4550
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   162
            X11_GL_GetProcAddress(_this, "glXCreateContext");
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
    _this->gl_data->glXDestroyContext =
4550
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   164
        (void (*)(Display *, GLXContext))
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   165
            X11_GL_GetProcAddress(_this, "glXDestroyContext");
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
    _this->gl_data->glXMakeCurrent =
4550
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   167
        (int (*)(Display *, GLXDrawable, GLXContext))
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   168
            X11_GL_GetProcAddress(_this, "glXMakeCurrent");
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
    _this->gl_data->glXSwapBuffers =
4550
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   170
        (void (*)(Display *, GLXDrawable))
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   171
            X11_GL_GetProcAddress(_this, "glXSwapBuffers");
5572
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   172
    _this->gl_data->glXQueryDrawable =
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   173
        (void (*)(Display*,GLXDrawable,int,unsigned int*))
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   174
            X11_GL_GetProcAddress(_this, "glXQueryDrawable");
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
    if (!_this->gl_data->glXChooseVisual ||
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   177
        !_this->gl_data->glXCreateContext ||
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   178
        !_this->gl_data->glXDestroyContext ||
4550
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   179
        !_this->gl_data->glXMakeCurrent ||
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   180
        !_this->gl_data->glXSwapBuffers) {
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
        SDL_SetError("Could not retrieve OpenGL functions");
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
        return -1;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   185
    /* Initialize extensions */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   186
    X11_GL_InitExtensions(_this);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   187
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
    return 0;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
}
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   190
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   191
void *
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
X11_GL_GetProcAddress(_THIS, const char *proc)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
{
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
    if (_this->gl_data->glXGetProcAddress) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   195
        return _this->gl_data->glXGetProcAddress((const GLubyte *) proc);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   196
    }
4550
e4e462f591d7 Fixed bug 1006
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   197
    return GL_LoadFunction(_this->gl_config.dll_handle, proc);
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
}
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   200
void
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
X11_GL_UnloadLibrary(_THIS)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   202
{
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   203
    /* Don't actually unload the library, since it may have registered
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   204
     * X11 shutdown hooks, per the notes at:
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   205
     * http://dri.sourceforge.net/doc/DRIuserguide.html
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   206
     */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   207
#if 0
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   208
    GL_UnloadObject(_this->gl_config.dll_handle);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   209
    _this->gl_config.dll_handle = NULL;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   210
#endif
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   211
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   212
    /* Free OpenGL memory */
6181
e29f01fa2750 Fixed bug 1313 - Segfault on SDL_CreateWindow when gl lib cannot be loaded
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   213
    if (_this->gl_data) {
e29f01fa2750 Fixed bug 1313 - Segfault on SDL_CreateWindow when gl lib cannot be loaded
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   214
        SDL_free(_this->gl_data);
e29f01fa2750 Fixed bug 1313 - Segfault on SDL_CreateWindow when gl lib cannot be loaded
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   215
        _this->gl_data = NULL;
e29f01fa2750 Fixed bug 1313 - Segfault on SDL_CreateWindow when gl lib cannot be loaded
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   216
    }
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   217
}
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   218
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   219
static SDL_bool
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   220
HasExtension(const char *extension, const char *extensions)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   221
{
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   222
    const char *start;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   223
    const char *where, *terminator;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   225
    /* Extension names should not have spaces. */
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   226
    where = SDL_strchr(extension, ' ');
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   227
    if (where || *extension == '\0')
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
        return SDL_FALSE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   229
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   230
    if (!extensions)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   231
        return SDL_FALSE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   232
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   233
    /* It takes a bit of care to be fool-proof about parsing the
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   234
     * OpenGL extensions string. Don't be fooled by sub-strings,
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   235
     * etc. */
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   236
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   237
    start = extensions;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
    for (;;) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   240
        where = SDL_strstr(start, extension);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   241
        if (!where)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   242
            break;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   243
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   244
        terminator = where + SDL_strlen(extension);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   245
        if (where == start || *(where - 1) == ' ')
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   246
            if (*terminator == ' ' || *terminator == '\0')
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   247
                return SDL_TRUE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   248
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
        start = terminator;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   250
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
    return SDL_FALSE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
}
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   253
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   254
static void
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   255
X11_GL_InitExtensions(_THIS)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   256
{
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
5244
762e40fb8e28 Be explicit about what display you're querying. The default display is 0.
Sam Lantinga <slouken@libsdl.org>
parents: 5243
diff changeset
   258
    int screen = DefaultScreen(display);
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   259
    XVisualInfo *vinfo;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   260
    XSetWindowAttributes xattr;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   261
    Window w;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   262
    GLXContext context;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   263
    const char *(*glXQueryExtensionsStringFunc) (Display *, int);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   264
    const char *extensions;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   265
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   266
    vinfo = X11_GL_GetVisual(_this, display, screen);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   267
    if (!vinfo) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   268
        return;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   269
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   270
    xattr.background_pixel = 0;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   271
    xattr.border_pixel = 0;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   272
    xattr.colormap =
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   273
        XCreateColormap(display, RootWindow(display, screen), vinfo->visual,
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   274
                        AllocNone);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   275
    w = XCreateWindow(display, RootWindow(display, screen), 0, 0, 32, 32, 0,
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   276
                      vinfo->depth, InputOutput, vinfo->visual,
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   277
                      (CWBackPixel | CWBorderPixel | CWColormap), &xattr);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   278
    context = _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   279
    if (context) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   280
        _this->gl_data->glXMakeCurrent(display, w, context);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   281
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
    XFree(vinfo);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   284
    glXQueryExtensionsStringFunc =
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   285
        (const char *(*)(Display *, int)) X11_GL_GetProcAddress(_this,
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
                                                                "glXQueryExtensionsString");
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
    if (glXQueryExtensionsStringFunc) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
        extensions = glXQueryExtensionsStringFunc(display, screen);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   289
    } else {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   290
        extensions = NULL;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   291
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   292
5572
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   293
    /* Check for GLX_EXT_swap_control */
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   294
    if (HasExtension("GLX_EXT_swap_control", extensions)) {
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   295
        _this->gl_data->glXSwapIntervalEXT =
5630
39e74dab5ebb Apparently glXSwapIntervalEXT() _does_ return a value.
Ryan C. Gordon <icculus@icculus.org>
parents: 5573
diff changeset
   296
            (int (*)(Display*,GLXDrawable,int))
5572
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   297
                X11_GL_GetProcAddress(_this, "glXSwapIntervalEXT");
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   298
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   299
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   300
    /* Check for GLX_MESA_swap_control */
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   301
    if (HasExtension("GLX_MESA_swap_control", extensions)) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   302
        _this->gl_data->glXSwapIntervalMESA =
5572
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   303
            (int(*)(int)) X11_GL_GetProcAddress(_this, "glXSwapIntervalMESA");
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   304
        _this->gl_data->glXGetSwapIntervalMESA =
5572
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   305
            (int(*)(void)) X11_GL_GetProcAddress(_this,
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   306
                                                   "glXGetSwapIntervalMESA");
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   307
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   308
5572
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   309
    /* Check for GLX_SGI_swap_control */
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   310
    if (HasExtension("GLX_SGI_swap_control", extensions)) {
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   311
        _this->gl_data->glXSwapIntervalSGI =
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   312
            (int (*)(int)) X11_GL_GetProcAddress(_this, "glXSwapIntervalSGI");
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   313
    }
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   314
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   315
    /* Check for GLX_EXT_visual_rating */
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   316
    if (HasExtension("GLX_EXT_visual_rating", extensions)) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   317
        _this->gl_data->HAS_GLX_EXT_visual_rating = SDL_TRUE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   318
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   319
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   320
    if (context) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   321
        _this->gl_data->glXMakeCurrent(display, None, NULL);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   322
        _this->gl_data->glXDestroyContext(display, context);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   323
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   324
    XDestroyWindow(display, w);
2323
4ac07ae446d3 Fixed many valgrind errors. But, I broke testdyngl.
Bob Pendleton <bob@pendleton.com>
parents: 2322
diff changeset
   325
    X11_PumpEvents(_this);
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   326
}
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   327
6296
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   328
/* glXChooseVisual and glXChooseFBConfig have some small differences in
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   329
 * the attribute encoding, it can be chosen with the for_FBConfig parameter. 
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   330
 */
5395
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   331
int 
6296
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   332
X11_GL_GetAttributes(_THIS, Display * display, int screen, int * attribs, int size, Bool for_FBConfig)
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   333
{
5395
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   334
    int i = 0;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   335
5395
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   336
    /* assert buffer is large enough to hold all SDL attributes. */ 
6281
e46d6f4b469e Replaced some assert macros with SDL_assert.
Ryan C. Gordon <icculus@icculus.org>
parents: 6183
diff changeset
   337
    SDL_assert(size >= 32);
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   338
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   339
    /* Setup our GLX attributes according to the gl_config. */
6296
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   340
    if( for_FBConfig ) {
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   341
        attribs[i++] = GLX_RENDER_TYPE;
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   342
	attribs[i++] = GLX_RGBA_BIT;
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   343
    } else {
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   344
        attribs[i++] = GLX_RGBA;
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   345
    }
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   346
    attribs[i++] = GLX_RED_SIZE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   347
    attribs[i++] = _this->gl_config.red_size;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   348
    attribs[i++] = GLX_GREEN_SIZE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   349
    attribs[i++] = _this->gl_config.green_size;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   350
    attribs[i++] = GLX_BLUE_SIZE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   351
    attribs[i++] = _this->gl_config.blue_size;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   352
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   353
    if (_this->gl_config.alpha_size) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   354
        attribs[i++] = GLX_ALPHA_SIZE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   355
        attribs[i++] = _this->gl_config.alpha_size;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   356
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   357
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   358
    if (_this->gl_config.double_buffer) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   359
        attribs[i++] = GLX_DOUBLEBUFFER;
6296
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   360
	if( for_FBConfig )
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   361
	    attribs[i++] = True;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   362
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   363
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   364
    attribs[i++] = GLX_DEPTH_SIZE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   365
    attribs[i++] = _this->gl_config.depth_size;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   366
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   367
    if (_this->gl_config.stencil_size) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   368
        attribs[i++] = GLX_STENCIL_SIZE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   369
        attribs[i++] = _this->gl_config.stencil_size;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   370
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   371
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   372
    if (_this->gl_config.accum_red_size) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   373
        attribs[i++] = GLX_ACCUM_RED_SIZE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   374
        attribs[i++] = _this->gl_config.accum_red_size;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   375
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   376
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   377
    if (_this->gl_config.accum_green_size) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   378
        attribs[i++] = GLX_ACCUM_GREEN_SIZE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   379
        attribs[i++] = _this->gl_config.accum_green_size;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   380
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   381
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   382
    if (_this->gl_config.accum_blue_size) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   383
        attribs[i++] = GLX_ACCUM_BLUE_SIZE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   384
        attribs[i++] = _this->gl_config.accum_blue_size;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   385
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   386
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   387
    if (_this->gl_config.accum_alpha_size) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   388
        attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   389
        attribs[i++] = _this->gl_config.accum_alpha_size;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   390
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   391
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   392
    if (_this->gl_config.stereo) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   393
        attribs[i++] = GLX_STEREO;
6296
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   394
	if( for_FBConfig )
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   395
	    attribs[i++] = True;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   396
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   397
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   398
    if (_this->gl_config.multisamplebuffers) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   399
        attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   400
        attribs[i++] = _this->gl_config.multisamplebuffers;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   401
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   402
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   403
    if (_this->gl_config.multisamplesamples) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   404
        attribs[i++] = GLX_SAMPLES_ARB;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   405
        attribs[i++] = _this->gl_config.multisamplesamples;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   406
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   407
3571
19691cebb866 Default to allow either accelerated or not
Sam Lantinga <slouken@libsdl.org>
parents: 3570
diff changeset
   408
    if (_this->gl_config.accelerated >= 0 &&
19691cebb866 Default to allow either accelerated or not
Sam Lantinga <slouken@libsdl.org>
parents: 3570
diff changeset
   409
        _this->gl_data->HAS_GLX_EXT_visual_rating) {
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   410
        attribs[i++] = GLX_VISUAL_CAVEAT_EXT;
3570
7812d3e9564e OpenGL hardware acceleration defaults on
Sam Lantinga <slouken@libsdl.org>
parents: 3361
diff changeset
   411
        attribs[i++] = _this->gl_config.accelerated ? GLX_NONE_EXT :
7812d3e9564e OpenGL hardware acceleration defaults on
Sam Lantinga <slouken@libsdl.org>
parents: 3361
diff changeset
   412
                                                      GLX_SLOW_VISUAL_EXT;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   413
    }
3570
7812d3e9564e OpenGL hardware acceleration defaults on
Sam Lantinga <slouken@libsdl.org>
parents: 3361
diff changeset
   414
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   415
    attribs[i++] = None;
5395
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   416
 
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   417
    return i;
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   418
}
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   419
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   420
XVisualInfo *
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   421
X11_GL_GetVisual(_THIS, Display * display, int screen)
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   422
{
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   423
    XVisualInfo *vinfo;
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   424
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   425
    /* 64 seems nice. */
6296
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   426
    int attribs[64];
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   427
    int i = X11_GL_GetAttributes(_this,display,screen,attribs,64,SDL_FALSE);
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   428
6181
e29f01fa2750 Fixed bug 1313 - Segfault on SDL_CreateWindow when gl lib cannot be loaded
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   429
    if (!_this->gl_data) {
e29f01fa2750 Fixed bug 1313 - Segfault on SDL_CreateWindow when gl lib cannot be loaded
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   430
        /* The OpenGL library wasn't loaded, SDL_GetError() should have info */
e29f01fa2750 Fixed bug 1313 - Segfault on SDL_CreateWindow when gl lib cannot be loaded
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   431
        return NULL;
e29f01fa2750 Fixed bug 1313 - Segfault on SDL_CreateWindow when gl lib cannot be loaded
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   432
    }
e29f01fa2750 Fixed bug 1313 - Segfault on SDL_CreateWindow when gl lib cannot be loaded
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   433
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   434
    vinfo = _this->gl_data->glXChooseVisual(display, screen, attribs);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   435
    if (!vinfo) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   436
        SDL_SetError("Couldn't find matching GLX visual");
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   437
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   438
    return vinfo;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   439
}
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   440
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   441
SDL_GLContext
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   442
X11_GL_CreateContext(_THIS, SDL_Window * window)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   443
{
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   444
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   445
    Display *display = data->videodata->display;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   446
    int screen =
5246
58265e606e4e Window coordinates are in the global space and windows are not tied to a particular display.
Sam Lantinga <slouken@libsdl.org>
parents: 5244
diff changeset
   447
        ((SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata)->screen;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   448
    XWindowAttributes xattr;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   449
    XVisualInfo v, *vinfo;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   450
    int n;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   451
    GLXContext context = NULL;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   452
1955
Sam Lantinga <slouken@libsdl.org>
parents: 1954
diff changeset
   453
    /* We do this to create a clean separation between X and GLX errors. */
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   454
    XSync(display, False);
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3571
diff changeset
   455
    XGetWindowAttributes(display, data->xwindow, &xattr);
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   456
    v.screen = screen;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   457
    v.visualid = XVisualIDFromVisual(xattr.visual);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   458
    vinfo = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &v, &n);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   459
    if (vinfo) {
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   460
        if (_this->gl_config.major_version < 3) {
3100
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   461
            context =
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   462
                _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   463
        } else {
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   464
            /* If we want a GL 3.0 context or later we need to get a temporary
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   465
               context to grab the new context creation function */
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   466
            GLXContext temp_context =
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   467
                _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
3100
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   468
            if (!temp_context) {
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   469
                SDL_SetError("Could not create GL context");
3100
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   470
                return NULL;
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   471
            } else {
6296
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   472
	        /* max 8 attributes plus terminator */
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   473
                int attribs[9] = {
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   474
                    GLX_CONTEXT_MAJOR_VERSION_ARB,
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   475
                    _this->gl_config.major_version,
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   476
                    GLX_CONTEXT_MINOR_VERSION_ARB,
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   477
                    _this->gl_config.minor_version,
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   478
                    0
3100
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   479
                };
6296
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   480
		int iattr = 4;
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   481
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   482
		/* SDL profile bits match GLX profile bits */
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   483
		if( _this->gl_config.profile_mask != 0 ) {
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   484
		    attribs[iattr++] = GLX_CONTEXT_PROFILE_MASK_ARB;
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   485
		    attribs[iattr++] = _this->gl_config.profile_mask;
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   486
		}
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   487
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   488
		/* SDL flags match GLX flags */
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   489
		if( _this->gl_config.flags != 0 ) {
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   490
		    attribs[iattr++] = GLX_CONTEXT_FLAGS_ARB;
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   491
		    attribs[iattr++] = _this->gl_config.flags;
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   492
		}
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   493
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   494
		attribs[iattr++] = 0;
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   495
3100
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   496
                /* Get a pointer to the context creation function for GL 3.0 */
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   497
                PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs =
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   498
                    (PFNGLXCREATECONTEXTATTRIBSARBPROC) _this->gl_data->
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   499
                    glXGetProcAddress((GLubyte *)
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   500
                                      "glXCreateContextAttribsARB");
3100
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   501
                if (!glXCreateContextAttribs) {
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   502
                    SDL_SetError("GL 3.x is not supported");
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   503
                    context = temp_context;
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   504
                } else {
5395
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   505
                    int glxAttribs[64];
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   506
3105
cdeee9f9b14b Fixed bug #721
Sam Lantinga <slouken@libsdl.org>
parents: 3100
diff changeset
   507
                    /* Create a GL 3.x context */
3100
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   508
                    GLXFBConfig *framebuffer_config = NULL;
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   509
                    int fbcount = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   510
                    GLXFBConfig *(*glXChooseFBConfig) (Display * disp,
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   511
                                                       int screen,
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   512
                                                       const int *attrib_list,
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   513
                                                       int *nelements);
3105
cdeee9f9b14b Fixed bug #721
Sam Lantinga <slouken@libsdl.org>
parents: 3100
diff changeset
   514
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   515
                    glXChooseFBConfig =
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   516
                        (GLXFBConfig *
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   517
                         (*)(Display *, int, const int *,
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   518
                             int *)) _this->gl_data->
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   519
                        glXGetProcAddress((GLubyte *) "glXChooseFBConfig");
3105
cdeee9f9b14b Fixed bug #721
Sam Lantinga <slouken@libsdl.org>
parents: 3100
diff changeset
   520
6296
b42657486c0d Add OpenGL 3.X context creation support
Sam Lantinga <slouken@libsdl.org>
parents: 6282
diff changeset
   521
                    X11_GL_GetAttributes(_this,display,screen,glxAttribs,64,SDL_TRUE);
5395
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   522
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   523
                    if (!glXChooseFBConfig
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   524
                        || !(framebuffer_config =
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   525
                             glXChooseFBConfig(display,
5395
f0e399961f3a Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   526
                                               DefaultScreen(display), glxAttribs,
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   527
                                               &fbcount))) {
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   528
                        SDL_SetError
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   529
                            ("No good framebuffers found. GL 3.x disabled");
3100
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   530
                        context = temp_context;
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   531
                    } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   532
                        context =
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   533
                            glXCreateContextAttribs(display,
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   534
                                                    framebuffer_config[0],
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   535
                                                    NULL, True, attribs);
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   536
                        _this->gl_data->glXDestroyContext(display,
Sam Lantinga <slouken@libsdl.org>
parents: 3105
diff changeset
   537
                                                          temp_context);
3100
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   538
                    }
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   539
                }
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   540
            }
7dc982143c06 Date: Sun, 22 Mar 2009 12:52:29 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   541
        }
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   542
        XFree(vinfo);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   543
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   544
    XSync(display, False);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   545
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   546
    if (!context) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   547
        SDL_SetError("Could not create GL context");
2178
114a541cfae2 Creating a context makes it current, per the documentation.
Sam Lantinga <slouken@libsdl.org>
parents: 2105
diff changeset
   548
        return NULL;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   549
    }
2178
114a541cfae2 Creating a context makes it current, per the documentation.
Sam Lantinga <slouken@libsdl.org>
parents: 2105
diff changeset
   550
114a541cfae2 Creating a context makes it current, per the documentation.
Sam Lantinga <slouken@libsdl.org>
parents: 2105
diff changeset
   551
    if (X11_GL_MakeCurrent(_this, window, context) < 0) {
114a541cfae2 Creating a context makes it current, per the documentation.
Sam Lantinga <slouken@libsdl.org>
parents: 2105
diff changeset
   552
        X11_GL_DeleteContext(_this, context);
114a541cfae2 Creating a context makes it current, per the documentation.
Sam Lantinga <slouken@libsdl.org>
parents: 2105
diff changeset
   553
        return NULL;
114a541cfae2 Creating a context makes it current, per the documentation.
Sam Lantinga <slouken@libsdl.org>
parents: 2105
diff changeset
   554
    }
114a541cfae2 Creating a context makes it current, per the documentation.
Sam Lantinga <slouken@libsdl.org>
parents: 2105
diff changeset
   555
114a541cfae2 Creating a context makes it current, per the documentation.
Sam Lantinga <slouken@libsdl.org>
parents: 2105
diff changeset
   556
    return context;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   557
}
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   558
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   559
int
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   560
X11_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   561
{
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   562
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   563
    Window drawable =
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3571
diff changeset
   564
        (window ? ((SDL_WindowData *) window->driverdata)->xwindow : None);
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   565
    GLXContext glx_context = (GLXContext) context;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   566
    int status;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   567
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   568
    status = 0;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   569
    if (!_this->gl_data->glXMakeCurrent(display, drawable, glx_context)) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   570
        SDL_SetError("Unable to make GL context current");
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   571
        status = -1;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   572
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   573
    XSync(display, False);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   574
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   575
    return (status);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   576
}
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   577
2327
7b53a8401195 In testdyngl.c the event type was being anded (&) with SDL_KEYDOWN and if the result was none zero the program was quiting. This is very weird because it was
Bob Pendleton <bob@pendleton.com>
parents: 2324
diff changeset
   578
/* 
5572
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   579
   0 is a valid argument to glxSwapInterval(MESA|EXT) and setting it to 0
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   580
   will undo the effect of a previous call with a value that is greater
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   581
   than zero (or at least that is what the docs say). OTOH, 0 is an invalid
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   582
   argument to glxSwapIntervalSGI and it returns an error if you call it
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   583
   with 0 as an argument.
2327
7b53a8401195 In testdyngl.c the event type was being anded (&) with SDL_KEYDOWN and if the result was none zero the program was quiting. This is very weird because it was
Bob Pendleton <bob@pendleton.com>
parents: 2324
diff changeset
   584
*/
7b53a8401195 In testdyngl.c the event type was being anded (&) with SDL_KEYDOWN and if the result was none zero the program was quiting. This is very weird because it was
Bob Pendleton <bob@pendleton.com>
parents: 2324
diff changeset
   585
7b53a8401195 In testdyngl.c the event type was being anded (&) with SDL_KEYDOWN and if the result was none zero the program was quiting. This is very weird because it was
Bob Pendleton <bob@pendleton.com>
parents: 2324
diff changeset
   586
static int swapinterval = -1;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   587
int
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   588
X11_GL_SetSwapInterval(_THIS, int interval)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   589
{
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   590
    int status;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   591
5572
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   592
    if (_this->gl_data->glXSwapIntervalEXT) {
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   593
        Display *display = ((SDL_VideoData *) _this->driverdata)->display;
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   594
        const SDL_WindowData *windowdata = (SDL_WindowData *)
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   595
            _this->current_glwin->driverdata;
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   596
        Window drawable = windowdata->xwindow;
5630
39e74dab5ebb Apparently glXSwapIntervalEXT() _does_ return a value.
Ryan C. Gordon <icculus@icculus.org>
parents: 5573
diff changeset
   597
        status = _this->gl_data->glXSwapIntervalEXT(display,drawable,interval);
39e74dab5ebb Apparently glXSwapIntervalEXT() _does_ return a value.
Ryan C. Gordon <icculus@icculus.org>
parents: 5573
diff changeset
   598
        if (status != 0) {
39e74dab5ebb Apparently glXSwapIntervalEXT() _does_ return a value.
Ryan C. Gordon <icculus@icculus.org>
parents: 5573
diff changeset
   599
            SDL_SetError("glxSwapIntervalEXT failed");
39e74dab5ebb Apparently glXSwapIntervalEXT() _does_ return a value.
Ryan C. Gordon <icculus@icculus.org>
parents: 5573
diff changeset
   600
            status = -1;
39e74dab5ebb Apparently glXSwapIntervalEXT() _does_ return a value.
Ryan C. Gordon <icculus@icculus.org>
parents: 5573
diff changeset
   601
        } else {
39e74dab5ebb Apparently glXSwapIntervalEXT() _does_ return a value.
Ryan C. Gordon <icculus@icculus.org>
parents: 5573
diff changeset
   602
            swapinterval = interval;
39e74dab5ebb Apparently glXSwapIntervalEXT() _does_ return a value.
Ryan C. Gordon <icculus@icculus.org>
parents: 5573
diff changeset
   603
        }
5572
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   604
    } else if (_this->gl_data->glXSwapIntervalMESA) {
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   605
        status = _this->gl_data->glXSwapIntervalMESA(interval);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   606
        if (status != 0) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   607
            SDL_SetError("glxSwapIntervalMESA failed");
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   608
            status = -1;
2327
7b53a8401195 In testdyngl.c the event type was being anded (&) with SDL_KEYDOWN and if the result was none zero the program was quiting. This is very weird because it was
Bob Pendleton <bob@pendleton.com>
parents: 2324
diff changeset
   609
        } else {
7b53a8401195 In testdyngl.c the event type was being anded (&) with SDL_KEYDOWN and if the result was none zero the program was quiting. This is very weird because it was
Bob Pendleton <bob@pendleton.com>
parents: 2324
diff changeset
   610
            swapinterval = interval;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   611
        }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   612
    } else if (_this->gl_data->glXSwapIntervalSGI) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   613
        status = _this->gl_data->glXSwapIntervalSGI(interval);
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   614
        if (status != 0) {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   615
            SDL_SetError("glxSwapIntervalSGI failed");
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   616
            status = -1;
2327
7b53a8401195 In testdyngl.c the event type was being anded (&) with SDL_KEYDOWN and if the result was none zero the program was quiting. This is very weird because it was
Bob Pendleton <bob@pendleton.com>
parents: 2324
diff changeset
   617
        } else {
7b53a8401195 In testdyngl.c the event type was being anded (&) with SDL_KEYDOWN and if the result was none zero the program was quiting. This is very weird because it was
Bob Pendleton <bob@pendleton.com>
parents: 2324
diff changeset
   618
            swapinterval = interval;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   619
        }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   620
    } else {
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   621
        SDL_Unsupported();
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   622
        status = -1;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   623
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   624
    return status;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   625
}
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   626
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   627
int
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   628
X11_GL_GetSwapInterval(_THIS)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   629
{
5572
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   630
    if (_this->gl_data->glXSwapIntervalEXT) {
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   631
        Display *display = ((SDL_VideoData *) _this->driverdata)->display;
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   632
        const SDL_WindowData *windowdata = (SDL_WindowData *)
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   633
            _this->current_glwin->driverdata;
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   634
        Window drawable = windowdata->xwindow;
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   635
        unsigned int value = 0;
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   636
        _this->gl_data->glXQueryDrawable(display, drawable,
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   637
                                         GLX_SWAP_INTERVAL_EXT, &value);
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   638
        return (int) value;
32a785da3f49 Added support for GLX_EXT_swap_control, and cleaned up some other extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   639
    } else if (_this->gl_data->glXGetSwapIntervalMESA) {
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   640
        return _this->gl_data->glXGetSwapIntervalMESA();
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   641
    } else {
2327
7b53a8401195 In testdyngl.c the event type was being anded (&) with SDL_KEYDOWN and if the result was none zero the program was quiting. This is very weird because it was
Bob Pendleton <bob@pendleton.com>
parents: 2324
diff changeset
   642
        return swapinterval;
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   643
    }
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   644
}
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   645
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   646
void
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   647
X11_GL_SwapWindow(_THIS, SDL_Window * window)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   648
{
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   649
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   650
    Display *display = data->videodata->display;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   651
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3571
diff changeset
   652
    _this->gl_data->glXSwapBuffers(display, data->xwindow);
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   653
}
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   654
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   655
void
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   656
X11_GL_DeleteContext(_THIS, SDL_GLContext context)
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   657
{
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   658
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   659
    GLXContext glx_context = (GLXContext) context;
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   660
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   661
    _this->gl_data->glXDestroyContext(display, glx_context);
2324
3202e4826c57 more valgrind errors fixed. Plus I ran make indent which changed a few files.
Bob Pendleton <bob@pendleton.com>
parents: 2323
diff changeset
   662
    XSync(display, False);
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   663
}
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   664
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   665
#endif /* SDL_VIDEO_OPENGL_GLX */
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   666
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5981
diff changeset
   667
#endif /* SDL_VIDEO_DRIVER_X11 */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5981
diff changeset
   668
1952
420716272158 Implemented X11 OpenGL support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   669
/* vi: set ts=4 sw=4 expandtab: */