21 #include "../../SDL_internal.h" 25 #if SDL_VIDEO_OPENGL_CGL 30 #include <OpenGL/CGLTypes.h> 31 #include <OpenGL/OpenGL.h> 32 #include <OpenGL/CGLRenderers.h> 37 #define DEFAULT_OPENGL "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib" 39 @implementation SDLOpenGLContext : NSOpenGLContext
41 - (
id)initWithFormat:(NSOpenGLPixelFormat *)format
42 shareContext:(NSOpenGLContext *)share
44 self = [
super initWithFormat:format shareContext:share];
52 - (
void)scheduleUpdate
58 - (
void)updateIfNeeded
71 [
self scheduleUpdate];
72 [
self updateIfNeeded];
82 NSMutableArray *contexts = oldwindowdata->
nscontexts;
83 @synchronized (contexts) {
84 [contexts removeObject:self];
88 self->window = newWindow;
94 NSMutableArray *contexts = windowdata->
nscontexts;
95 @synchronized (contexts) {
96 [contexts addObject:self];
99 if ([
self view] != [windowdata->
nswindow contentView]) {
100 [
self setView:[windowdata->nswindow contentView]];
101 if (
self == [NSOpenGLContext currentContext]) {
104 [
self scheduleUpdate];
108 [
self clearDrawable];
109 if (
self == [NSOpenGLContext currentContext]) {
112 [
self scheduleUpdate];
121 Cocoa_GL_LoadLibrary(
_THIS,
const char *
path)
128 path = DEFAULT_OPENGL;
140 Cocoa_GL_GetProcAddress(
_THIS,
const char *proc)
146 Cocoa_GL_UnloadLibrary(
_THIS)
158 SDL_bool lion_or_later =
floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6;
159 NSOpenGLPixelFormatAttribute attr[32];
160 NSOpenGLPixelFormat *fmt;
162 NSOpenGLContext *share_context = nil;
164 const char *glversion;
169 #if SDL_VIDEO_OPENGL_EGL 171 Cocoa_GL_UnloadLibrary(
_this);
182 if (Cocoa_GLES_LoadLibrary(
_this,
NULL) != 0) {
185 return Cocoa_GLES_CreateContext(
_this, window);
192 SDL_SetError (
"OpenGL Core Profile is not supported on this platform version");
196 attr[i++] = NSOpenGLPFAAllowOfflineRenderers;
200 NSOpenGLPixelFormatAttribute profile = NSOpenGLProfileVersionLegacy;
202 profile = NSOpenGLProfileVersion3_2Core;
204 attr[i++] = NSOpenGLPFAOpenGLProfile;
208 attr[i++] = NSOpenGLPFAColorSize;
211 attr[i++] = NSOpenGLPFADepthSize;
215 attr[i++] = NSOpenGLPFADoubleBuffer;
219 attr[i++] = NSOpenGLPFAStereo;
223 attr[i++] = NSOpenGLPFAStencilSize;
231 attr[i++] = NSOpenGLPFAAccumSize;
236 attr[i++] = NSOpenGLPFASampleBuffers;
241 attr[i++] = NSOpenGLPFASamples;
243 attr[i++] = NSOpenGLPFANoRecovery;
248 attr[i++] = NSOpenGLPFAAccelerated;
250 attr[i++] = NSOpenGLPFARendererID;
251 attr[i++] = kCGLRendererGenericFloatID;
255 attr[i++] = NSOpenGLPFAScreenMask;
256 attr[i++] = CGDisplayIDToOpenGLDisplayMask(displaydata->
display);
259 fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
269 context = [[SDLOpenGLContext alloc] initWithFormat:fmt shareContext:share_context];
273 if (context == nil) {
278 if ( Cocoa_GL_MakeCurrent(
_this, window, context) < 0 ) {
279 Cocoa_GL_DeleteContext(
_this, context);
292 if (!glGetStringFunc) {
293 Cocoa_GL_DeleteContext(
_this, context);
294 SDL_SetError (
"Failed getting OpenGL glGetString entry point");
298 glversion = (
const char *)glGetStringFunc(
GL_VERSION);
299 if (glversion ==
NULL) {
300 Cocoa_GL_DeleteContext(
_this, context);
305 if (
SDL_sscanf(glversion,
"%d.%d", &glversion_major, &glversion_minor) != 2) {
306 Cocoa_GL_DeleteContext(
_this, context);
311 if ((glversion_major < _this->gl_config.major_version) ||
313 Cocoa_GL_DeleteContext(
_this, context);
314 SDL_SetError (
"Failed creating OpenGL context at version requested");
332 SDLOpenGLContext *nscontext = (SDLOpenGLContext *)context;
333 [nscontext setWindow:window];
334 [nscontext updateIfNeeded];
335 [nscontext makeCurrentContext];
337 [NSOpenGLContext clearCurrentContext];
347 NSView *contentView = [windata->
nswindow contentView];
348 NSRect
viewport = [contentView bounds];
352 if ([contentView respondsToSelector:
@selector(convertRectToBacking:)]) {
353 viewport = [contentView convertRectToBacking:viewport];
366 Cocoa_GL_SetSwapInterval(
_THIS,
int interval)
369 NSOpenGLContext *nscontext;
374 return SDL_SetError(
"Late swap tearing currently unsupported");
378 if (nscontext != nil) {
380 [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
390 Cocoa_GL_GetSwapInterval(
_THIS)
393 NSOpenGLContext *nscontext;
398 if (nscontext != nil) {
399 [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
411 [nscontext flushBuffer];
412 [nscontext updateIfNeeded];
420 SDLOpenGLContext *nscontext = (SDLOpenGLContext *)context;
422 [nscontext setWindow:NULL];
NSMutableArray * nscontexts
GLfloat GLfloat GLfloat GLfloat h
int(* GL_SetSwapInterval)(_THIS, int interval)
static screen_context_t context
#define SDL_BYTESPERPIXEL(X)
CGDirectDisplayID display
int(* GL_LoadLibrary)(_THIS, const char *path)
static SDL_VideoDevice * _this
void * SDL_GLContext
An opaque handle to an OpenGL context.
SDL_GLContext(* GL_CreateContext)(_THIS, SDL_Window *window)
int(* GL_MakeCurrent)(_THIS, SDL_Window *window, SDL_GLContext context)
#define SDL_GL_GetProcAddress
SDL_DisplayMode current_mode
GLubyte GLubyte GLubyte GLubyte w
GLsizei const GLfloat * value
void(* GL_UnloadLibrary)(_THIS)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
int share_with_current_context
#define SDL_GL_GetCurrentContext
SDL_VideoDisplay * SDL_GetDisplayForWindow(SDL_Window *window)
EGLSurface EGLNativeWindowType * window
int(* GL_SwapWindow)(_THIS, SDL_Window *window)
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
The type used to identify a window.
#define SDL_arraysize(array)
GLsizei const GLchar *const * path
struct SDL_VideoDevice::@34 gl_config
void * SDL_LoadFunction(void *handle, const char *name)
void(* GL_DeleteContext)(_THIS, SDL_GLContext context)
int(* GL_GetSwapInterval)(_THIS)
void *(* GL_GetProcAddress)(_THIS, const char *proc)