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 Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 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    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* AGL implementation of SDL OpenGL support */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_lowvideo.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_macgl_c.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_loadso.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* krat: adding OpenGL support */
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint Mac_GL_Init(_THIS)
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	AGLPixelFormat format;
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   	int i = 0;
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GLint attributes [ 26 ]; /* 26 is max possible in this setup */
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GLboolean noerr;
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* load the gl driver from a default path */
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! this->gl_config.driver_loaded ) {
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* no driver has been loaded, use default (ourselves) */
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( Mac_GL_LoadLibrary(this, NULL) < 0 ) {
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attributes[i++] = AGL_RGBA;
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->gl_config.red_size   != 0 &&
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     this->gl_config.blue_size  != 0 &&
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     this->gl_config.green_size != 0 ) {
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_RED_SIZE;
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = this->gl_config.red_size;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_GREEN_SIZE;
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = this->gl_config.green_size;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_BLUE_SIZE;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = this->gl_config.blue_size;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_ALPHA_SIZE;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = this->gl_config.alpha_size;
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->gl_config.double_buffer ) {
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_DOUBLEBUFFER;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->gl_config.depth_size != 0 ) {
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_DEPTH_SIZE;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = this->gl_config.depth_size;
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->gl_config.stencil_size != 0 ) {
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_STENCIL_SIZE;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = this->gl_config.stencil_size;
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->gl_config.accum_red_size   != 0 &&
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     this->gl_config.accum_blue_size  != 0 &&
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     this->gl_config.accum_green_size != 0 ) {
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_ACCUM_RED_SIZE;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = this->gl_config.accum_red_size;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_ACCUM_GREEN_SIZE;
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = this->gl_config.accum_green_size;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_ACCUM_BLUE_SIZE;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = this->gl_config.accum_blue_size;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_ACCUM_ALPHA_SIZE;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = this->gl_config.accum_alpha_size;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->gl_config.stereo ) {
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_STEREO;
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(AGL_SAMPLE_BUFFERS_ARB) && defined(AGL_SAMPLES_ARB)
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->gl_config.multisamplebuffers != 0 ) {
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_SAMPLE_BUFFERS_ARB;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = this->gl_config.multisamplebuffers;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->gl_config.multisamplesamples != 0 ) {
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_SAMPLES_ARB;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = this->gl_config.multisamplesamples;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->gl_config.accelerated > 0 ) {
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_ACCELERATED;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attributes[i++] = AGL_NO_RECOVERY;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attributes[i++] = AGL_ALL_RENDERERS;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attributes[i]	= AGL_NONE;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format = aglChoosePixelFormat(NULL, 0, attributes);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( format == NULL ) {
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't match OpenGL desired format");
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	glContext = aglCreateContext(format, NULL);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( glContext == NULL ) {
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't create OpenGL context");
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aglDestroyPixelFormat(format);
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #if  TARGET_API_MAC_CARBON
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	noerr = aglSetDrawable(glContext, GetWindowPort(SDL_Window));
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #else
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	noerr = aglSetDrawable(glContext, (AGLDrawable)SDL_Window);
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #endif
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(!noerr) {
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to bind GL context to window");
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("OpenGL support not configured");
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(-1);
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Mac_GL_Quit(_THIS)
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( glContext != NULL ) {
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		aglSetCurrentContext(NULL);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		aglSetDrawable(glContext, NULL);
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		aglDestroyContext(glContext);
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glContext = NULL;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Make the current context active */
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint Mac_GL_MakeCurrent(_THIS)
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = 0;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( ! aglSetCurrentContext(glContext) ) {
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to make GL context current");
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Mac_GL_SwapBuffers(_THIS)
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aglSwapBuffers(glContext);
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint Mac_GL_LoadLibrary(_THIS, const char *location)
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (location == NULL)
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if __MACH__
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		location = "/System/Library/Frameworks/OpenGL.framework/OpenGL";
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		location = "OpenGLLibrary";
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->libraryHandle = SDL_LoadObject(location);
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_config.driver_loaded = 1;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (this->hidden->libraryHandle != NULL) ? 0 : -1;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Mac_GL_UnloadLibrary(_THIS)
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnloadObject(this->hidden->libraryHandle);
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->libraryHandle = NULL;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_config.driver_loaded = 0;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid* Mac_GL_GetProcAddress(_THIS, const char *proc)
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return SDL_LoadFunction( this->hidden->libraryHandle, proc );
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_OPENGL */
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
198