1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-27 04:28:52 -05:00
glfw/src/nsgl_context.m

297 lines
9.0 KiB
Mathematica
Raw Normal View History

2010-09-07 11:34:51 -04:00
//========================================================================
2015-06-01 16:55:06 -04:00
// GLFW 3.2 OS X - www.glfw.org
2010-09-07 11:34:51 -04:00
//------------------------------------------------------------------------
// Copyright (c) 2009-2010 Camilla Berglund <elmindreda@elmindreda.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include "internal.h"
2010-09-15 21:25:36 -04:00
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
2010-09-15 21:25:36 -04:00
//////////////////////////////////////////////////////////////////////////
2010-09-07 11:34:51 -04:00
2012-08-13 10:03:44 -04:00
// Initialize OpenGL support
2013-02-04 07:22:10 -05:00
//
int _glfwInitContextAPI(void)
2012-08-13 10:03:44 -04:00
{
2015-05-04 21:31:20 -04:00
if (!_glfwCreateContextTLS())
2015-08-23 13:30:04 -04:00
return GLFW_FALSE;
2012-08-13 10:03:44 -04:00
_glfw.nsgl.framework =
CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
if (_glfw.nsgl.framework == NULL)
{
_glfwInputError(GLFW_API_UNAVAILABLE,
"NSGL: Failed to locate OpenGL framework");
2015-08-23 13:30:04 -04:00
return GLFW_FALSE;
}
2015-08-23 13:30:04 -04:00
return GLFW_TRUE;
2012-08-13 10:03:44 -04:00
}
// Terminate OpenGL support
2013-02-04 07:22:10 -05:00
//
void _glfwTerminateContextAPI(void)
2012-08-13 10:03:44 -04:00
{
2015-05-04 21:31:20 -04:00
_glfwDestroyContextTLS();
2012-08-13 10:03:44 -04:00
}
// Create the OpenGL context
2013-02-04 07:22:10 -05:00
//
int _glfwCreateContext(_GLFWwindow* window,
const _GLFWctxconfig* ctxconfig,
const _GLFWfbconfig* fbconfig)
{
unsigned int attributeCount = 0;
if (ctxconfig->api == GLFW_OPENGL_ES_API)
{
_glfwInputError(GLFW_API_UNAVAILABLE,
"NSGL: OpenGL ES is not available on OS X");
2015-08-23 13:30:04 -04:00
return GLFW_FALSE;
}
if (ctxconfig->major == 3 && ctxconfig->minor < 2)
{
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
"NSGL: The targeted version of OS X does not support OpenGL 3.0 or 3.1");
2015-08-23 13:30:04 -04:00
return GLFW_FALSE;
}
if (ctxconfig->major > 2)
{
if (!ctxconfig->forward)
{
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
"NSGL: The targeted version of OS X only supports forward-compatible contexts for OpenGL 3.2 and above");
2015-08-23 13:30:04 -04:00
return GLFW_FALSE;
}
if (ctxconfig->profile != GLFW_OPENGL_CORE_PROFILE)
{
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
"NSGL: The targeted version of OS X only supports core profile contexts for OpenGL 3.2 and above");
2015-08-23 13:30:04 -04:00
return GLFW_FALSE;
}
}
// Context robustness modes (GL_KHR_robustness) are not yet supported on
// OS X but are not a hard constraint, so ignore and continue
// Context release behaviors (GL_KHR_context_flush_control) are not yet
// supported on OS X but are not a hard constraint, so ignore and continue
#define ADD_ATTR(x) { attributes[attributeCount++] = x; }
#define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); }
// Arbitrary array size here
NSOpenGLPixelFormatAttribute attributes[40];
ADD_ATTR(NSOpenGLPFAAccelerated);
2013-05-02 11:35:09 -04:00
ADD_ATTR(NSOpenGLPFAClosestPolicy);
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
if (ctxconfig->major >= 4)
{
ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion4_1Core);
}
else
#endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
if (ctxconfig->major >= 3)
{
ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
}
if (ctxconfig->major <= 2)
{
if (fbconfig->auxBuffers != GLFW_DONT_CARE)
ADD_ATTR2(NSOpenGLPFAAuxBuffers, fbconfig->auxBuffers);
if (fbconfig->accumRedBits != GLFW_DONT_CARE &&
fbconfig->accumGreenBits != GLFW_DONT_CARE &&
fbconfig->accumBlueBits != GLFW_DONT_CARE &&
fbconfig->accumAlphaBits != GLFW_DONT_CARE)
{
const int accumBits = fbconfig->accumRedBits +
fbconfig->accumGreenBits +
fbconfig->accumBlueBits +
fbconfig->accumAlphaBits;
ADD_ATTR2(NSOpenGLPFAAccumSize, accumBits);
}
}
if (fbconfig->redBits != GLFW_DONT_CARE &&
fbconfig->greenBits != GLFW_DONT_CARE &&
fbconfig->blueBits != GLFW_DONT_CARE)
{
int colorBits = fbconfig->redBits +
fbconfig->greenBits +
fbconfig->blueBits;
2015-01-05 15:55:15 -05:00
// OS X needs non-zero color size, so set reasonable values
if (colorBits == 0)
colorBits = 24;
else if (colorBits < 15)
colorBits = 15;
ADD_ATTR2(NSOpenGLPFAColorSize, colorBits);
}
if (fbconfig->alphaBits != GLFW_DONT_CARE)
ADD_ATTR2(NSOpenGLPFAAlphaSize, fbconfig->alphaBits);
if (fbconfig->depthBits != GLFW_DONT_CARE)
ADD_ATTR2(NSOpenGLPFADepthSize, fbconfig->depthBits);
if (fbconfig->stencilBits != GLFW_DONT_CARE)
ADD_ATTR2(NSOpenGLPFAStencilSize, fbconfig->stencilBits);
if (fbconfig->stereo)
ADD_ATTR(NSOpenGLPFAStereo);
2014-04-24 13:21:10 -04:00
if (fbconfig->doublebuffer)
ADD_ATTR(NSOpenGLPFADoubleBuffer);
if (fbconfig->samples != GLFW_DONT_CARE)
{
if (fbconfig->samples == 0)
{
ADD_ATTR2(NSOpenGLPFASampleBuffers, 0);
}
else
{
ADD_ATTR2(NSOpenGLPFASampleBuffers, 1);
ADD_ATTR2(NSOpenGLPFASamples, fbconfig->samples);
}
}
// NOTE: All NSOpenGLPixelFormats on the relevant cards support sRGB
2015-01-05 15:55:15 -05:00
// framebuffer, so there's no need (and no way) to request it
ADD_ATTR(0);
#undef ADD_ATTR
#undef ADD_ATTR2
window->nsgl.pixelFormat =
[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
if (window->nsgl.pixelFormat == nil)
{
_glfwInputError(GLFW_FORMAT_UNAVAILABLE,
"NSGL: Failed to find a suitable pixel format");
2015-08-23 13:30:04 -04:00
return GLFW_FALSE;
}
NSOpenGLContext* share = NULL;
if (ctxconfig->share)
share = ctxconfig->share->nsgl.context;
window->nsgl.context =
[[NSOpenGLContext alloc] initWithFormat:window->nsgl.pixelFormat
shareContext:share];
if (window->nsgl.context == nil)
{
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
2013-08-26 16:48:07 -04:00
"NSGL: Failed to create OpenGL context");
2015-08-23 13:30:04 -04:00
return GLFW_FALSE;
}
2015-08-23 13:30:04 -04:00
return GLFW_TRUE;
}
// Destroy the OpenGL context
2013-02-04 07:22:10 -05:00
//
void _glfwDestroyContext(_GLFWwindow* window)
{
[window->nsgl.pixelFormat release];
window->nsgl.pixelFormat = nil;
[window->nsgl.context release];
window->nsgl.context = nil;
}
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
void _glfwPlatformMakeContextCurrent(_GLFWwindow* window)
{
if (window)
[window->nsgl.context makeCurrentContext];
else
[NSOpenGLContext clearCurrentContext];
2015-05-04 21:31:20 -04:00
_glfwSetContextTLS(window);
}
void _glfwPlatformSwapBuffers(_GLFWwindow* window)
{
// ARP appears to be unnecessary, but this is future-proof
[window->nsgl.context flushBuffer];
}
void _glfwPlatformSwapInterval(int interval)
{
2012-08-13 10:03:44 -04:00
_GLFWwindow* window = _glfwPlatformGetCurrentContext();
GLint sync = interval;
[window->nsgl.context setValues:&sync forParameter:NSOpenGLCPSwapInterval];
}
2010-09-15 12:57:25 -04:00
int _glfwPlatformExtensionSupported(const char* extension)
2010-09-07 11:34:51 -04:00
{
2011-03-04 08:29:05 -05:00
// There are no NSGL extensions
2015-08-23 13:30:04 -04:00
return GLFW_FALSE;
2010-09-07 11:34:51 -04:00
}
GLFWglproc _glfwPlatformGetProcAddress(const char* procname)
2010-09-07 11:34:51 -04:00
{
2010-09-15 12:57:25 -04:00
CFStringRef symbolName = CFStringCreateWithCString(kCFAllocatorDefault,
procname,
kCFStringEncodingASCII);
2010-09-07 11:34:51 -04:00
GLFWglproc symbol = CFBundleGetFunctionPointerForName(_glfw.nsgl.framework,
symbolName);
2010-09-07 11:34:51 -04:00
2010-09-15 12:57:25 -04:00
CFRelease(symbolName);
2010-09-07 11:34:51 -04:00
return symbol;
}
//////////////////////////////////////////////////////////////////////////
////// GLFW native API //////
//////////////////////////////////////////////////////////////////////////
GLFWAPI id glfwGetNSGLContext(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
_GLFW_REQUIRE_INIT_OR_RETURN(nil);
return window->nsgl.context;
}