19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012  Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Library General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Library General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Library General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_QuartzVideo.h"
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * GL_ARB_Multisample is supposed to be available in 10.1, according to Apple:
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *   http://developer.apple.com/graphicsimaging/opengl/extensions.html#GL_ARB_multisample
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  ...but it isn't in the system headers, according to Sam:
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *   http://lists.libsdl.org/pipermail/sdl-libsdl.org/2003-December/039794.html
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * These are normally enums and not #defines in the system headers.
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *   --ryan.
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if (MAC_OS_X_VERSION_MAX_ALLOWED < 1020)
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define NSOpenGLPFASampleBuffers ((NSOpenGLPixelFormatAttribute) 55)
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define NSOpenGLPFASamples ((NSOpenGLPixelFormatAttribute) 56)
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __powerpc__   /* we lost this in 10.6, which has no PPC support. */
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@implementation NSOpenGLContext (CGLContextAccess)
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall- (CGLContextObj) cglContext;
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return _contextAuxiliary;
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall@end
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallCGLContextObj QZ_GetCGLContextObj(NSOpenGLContext *nsctx)
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return [nsctx cglContext];
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallCGLContextObj QZ_GetCGLContextObj(NSOpenGLContext *nsctx)
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (CGLContextObj) [nsctx CGLContextObj];
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* OpenGL helper functions (used internally) */
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint QZ_SetupOpenGL (_THIS, int bpp, Uint32 flags) {
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSOpenGLPixelFormatAttribute attr[32];
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NSOpenGLPixelFormat *fmt;
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i = 0;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int colorBits = bpp;
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* if a GL library hasn't been loaded at this point, load the default. */
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (!this->gl_config.driver_loaded) {
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (QZ_GL_LoadLibrary(this, NULL) == -1)
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return 0;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( flags & SDL_FULLSCREEN ) {
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = NSOpenGLPFAFullScreen;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* In windowed mode, the OpenGL pixel depth must match device pixel depth */
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else if ( colorBits != device_bpp ) {
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        colorBits = device_bpp;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    attr[i++] = NSOpenGLPFAColorSize;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    attr[i++] = colorBits;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    attr[i++] = NSOpenGLPFADepthSize;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    attr[i++] = this->gl_config.depth_size;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this->gl_config.double_buffer ) {
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = NSOpenGLPFADoubleBuffer;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this->gl_config.stereo ) {
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = NSOpenGLPFAStereo;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this->gl_config.stencil_size != 0 ) {
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = NSOpenGLPFAStencilSize;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = this->gl_config.stencil_size;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( (this->gl_config.accum_red_size +
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          this->gl_config.accum_green_size +
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          this->gl_config.accum_blue_size +
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          this->gl_config.accum_alpha_size) > 0 ) {
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = NSOpenGLPFAAccumSize;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = this->gl_config.accum_red_size + this->gl_config.accum_green_size + this->gl_config.accum_blue_size + this->gl_config.accum_alpha_size;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this->gl_config.multisamplebuffers != 0 ) {
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = NSOpenGLPFASampleBuffers;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = this->gl_config.multisamplebuffers;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this->gl_config.multisamplesamples != 0 ) {
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = NSOpenGLPFASamples;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = this->gl_config.multisamplesamples;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = NSOpenGLPFANoRecovery;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this->gl_config.accelerated > 0 ) {
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        attr[i++] = NSOpenGLPFAAccelerated;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    attr[i++] = NSOpenGLPFAScreenMask;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    attr[i++] = CGDisplayIDToOpenGLDisplayMask (display_id);
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    attr[i] = 0;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fmt = [ [ NSOpenGLPixelFormat alloc ] initWithAttributes:attr ];
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (fmt == nil) {
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError ("Failed creating OpenGL pixel format");
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    gl_context = [ [ NSOpenGLContext alloc ] initWithFormat:fmt
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                               shareContext:nil];
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [ fmt release ];
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (gl_context == nil) {
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError ("Failed creating OpenGL context");
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Synchronize QZ_GL_SwapBuffers() to vertical retrace.
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * (Apple's documentation is not completely clear about what this setting
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * exactly does, IMHO - for a detailed explanation see
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * http://lists.apple.com/archives/mac-opengl/2006/Jan/msg00080.html )
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     */
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this->gl_config.swap_control >= 0 ) {
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        GLint value;
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value = this->gl_config.swap_control;
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        [ gl_context setValues: &value forParameter: NSOpenGLCPSwapInterval ];
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /*
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *  "You are blowing a couple of the internal OpenGL function caches. This
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *  appears to be happening in the VAO case.  You can tell OpenGL to up
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *  the cache size by issuing the following calls right after you create
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *  the OpenGL context.  The default cache size is 16."    --ryan.
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     */
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #ifndef GLI_ARRAY_FUNC_CACHE_MAX
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #define GLI_ARRAY_FUNC_CACHE_MAX 284
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #endif
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #ifndef GLI_SUBMIT_FUNC_CACHE_MAX
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #define GLI_SUBMIT_FUNC_CACHE_MAX 280
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #endif
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        GLint cache_max = 64;
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        CGLContextObj ctx = QZ_GetCGLContextObj(gl_context);
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* End Wisdom from Apple Engineer section. --ryan. */
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 1;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid QZ_TearDownOpenGL (_THIS) {
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [ NSOpenGLContext clearCurrentContext ];
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [ gl_context clearDrawable ];
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [ gl_context release ];
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* SDL OpenGL functions */
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *DEFAULT_OPENGL_LIB_NAME =
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib";
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint    QZ_GL_LoadLibrary    (_THIS, const char *location) {
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( gl_context != NULL ) {
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("OpenGL context already created");
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (opengl_library != NULL)
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_UnloadObject(opengl_library);
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (location == NULL)
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        location = DEFAULT_OPENGL_LIB_NAME;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    opengl_library = SDL_LoadObject(location);
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (opengl_library != NULL) {
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        this->gl_config.driver_loaded = 1;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->gl_config.driver_loaded = 0;
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid*  QZ_GL_GetProcAddress (_THIS, const char *proc) {
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return SDL_LoadFunction(opengl_library, proc);
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint    QZ_GL_GetAttribute   (_THIS, SDL_GLattr attrib, int* value) {
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    GLenum attr = 0;
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    QZ_GL_MakeCurrent (this);
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    switch (attrib) {
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_RED_SIZE: attr = GL_RED_BITS;   break;
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_BLUE_SIZE: attr = GL_BLUE_BITS;  break;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_GREEN_SIZE: attr = GL_GREEN_BITS; break;
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_ALPHA_SIZE: attr = GL_ALPHA_BITS; break;
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_DOUBLEBUFFER: attr = GL_DOUBLEBUFFER; break;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_DEPTH_SIZE: attr = GL_DEPTH_BITS;  break;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_STENCIL_SIZE: attr = GL_STENCIL_BITS; break;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_ACCUM_RED_SIZE: attr = GL_ACCUM_RED_BITS; break;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_ACCUM_GREEN_SIZE: attr = GL_ACCUM_GREEN_BITS; break;
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_ACCUM_BLUE_SIZE: attr = GL_ACCUM_BLUE_BITS; break;
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_ACCUM_ALPHA_SIZE: attr = GL_ACCUM_ALPHA_BITS; break;
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_STEREO: attr = GL_STEREO; break;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_MULTISAMPLEBUFFERS: attr = GL_SAMPLE_BUFFERS_ARB; break;
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_MULTISAMPLESAMPLES: attr = GL_SAMPLES_ARB; break;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_BUFFER_SIZE:
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            GLint bits = 0;
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            GLint component;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* there doesn't seem to be a single flag in OpenGL for this! */
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            glGetIntegerv (GL_RED_BITS, &component);   bits += component;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            glGetIntegerv (GL_GREEN_BITS,&component);  bits += component;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            glGetIntegerv (GL_BLUE_BITS, &component);  bits += component;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            glGetIntegerv (GL_ALPHA_BITS, &component); bits += component;
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *value = bits;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return 0;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_ACCELERATED_VISUAL:
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            GLint val;
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* FIXME: How do we get this information here?
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            [fmt getValues: &val forAttribute: NSOpenGLPFAAccelerated attr forVirtualScreen: 0];
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    */
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    val = (this->gl_config.accelerated != 0);;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *value = val;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return 0;
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case SDL_GL_SWAP_CONTROL:
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            GLint val;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            [ gl_context getValues: &val forParameter: NSOpenGLCPSwapInterval ];
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *value = val;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return 0;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    glGetIntegerv (attr, (GLint *)value);
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint    QZ_GL_MakeCurrent    (_THIS) {
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [ gl_context makeCurrentContext ];
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid   QZ_GL_SwapBuffers    (_THIS) {
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    [ gl_context flushBuffer ];
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
293