Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
301 lines (241 loc) · 8.19 KB

SDL_cocoaopengl.m

File metadata and controls

301 lines (241 loc) · 8.19 KB
 
1
2
/*
Simple DirectMedia Layer
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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 "SDL_config.h"
/* NSOpenGL implementation of SDL OpenGL support */
#if SDL_VIDEO_OPENGL_CGL
#include "SDL_cocoavideo.h"
#include <OpenGL/CGLTypes.h>
#include <OpenGL/OpenGL.h>
#include <OpenGL/CGLRenderers.h>
#include "SDL_loadso.h"
#include "SDL_opengl.h"
#define DEFAULT_OPENGL "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"
Mar 3, 2013
Mar 3, 2013
38
#ifndef kCGLPFAOpenGLProfile
39
#define kCGLPFAOpenGLProfile 99
Mar 3, 2013
Mar 3, 2013
40
41
#endif
#ifndef kCGLOGLPVersion_Legacy
42
#define kCGLOGLPVersion_Legacy 0x1000
Mar 3, 2013
Mar 3, 2013
43
44
#endif
#ifndef kCGLOGLPVersion_3_2_Core
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#define kCGLOGLPVersion_3_2_Core 0x3200
#endif
int
Cocoa_GL_LoadLibrary(_THIS, const char *path)
{
/* Load the OpenGL library */
if (path == NULL) {
path = SDL_getenv("SDL_OPENGL_LIBRARY");
}
if (path == NULL) {
path = DEFAULT_OPENGL;
}
_this->gl_config.dll_handle = SDL_LoadObject(path);
if (!_this->gl_config.dll_handle) {
return -1;
}
SDL_strlcpy(_this->gl_config.driver_path, path,
SDL_arraysize(_this->gl_config.driver_path));
return 0;
}
void *
Cocoa_GL_GetProcAddress(_THIS, const char *proc)
{
return SDL_LoadFunction(_this->gl_config.dll_handle, proc);
}
void
Cocoa_GL_UnloadLibrary(_THIS)
{
SDL_UnloadObject(_this->gl_config.dll_handle);
_this->gl_config.dll_handle = NULL;
}
SDL_GLContext
Cocoa_GL_CreateContext(_THIS, SDL_Window * window)
{
const int wantver = (_this->gl_config.major_version << 8) |
(_this->gl_config.minor_version);
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
Feb 12, 2013
Feb 12, 2013
87
NSAutoreleasePool *pool;
88
89
90
91
92
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
NSOpenGLPixelFormatAttribute attr[32];
NSOpenGLPixelFormat *fmt;
NSOpenGLContext *context;
May 6, 2013
May 6, 2013
93
NSOpenGLContext *share_context = nil;
94
95
96
97
98
99
100
101
102
103
104
105
106
107
int i = 0;
if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
SDL_SetError ("OpenGL ES not supported on this platform");
return NULL;
}
/* Sadly, we'll have to update this as life progresses, since we need to
set an enum for context profiles, not a context version number */
if (wantver > 0x0302) {
SDL_SetError ("OpenGL > 3.2 is not supported on this platform");
return NULL;
}
Feb 12, 2013
Feb 12, 2013
108
109
110
111
112
113
114
115
pool = [[NSAutoreleasePool alloc] init];
/* specify a profile if we're on Lion (10.7) or later. */
if (data->osversion >= 0x1070) {
NSOpenGLPixelFormatAttribute profile = kCGLOGLPVersion_Legacy;
if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_CORE) {
if (wantver == 0x0302) {
profile = kCGLOGLPVersion_3_2_Core;
Feb 12, 2013
Feb 12, 2013
118
119
120
attr[i++] = kCGLPFAOpenGLProfile;
attr[i++] = profile;
}
Feb 12, 2013
Feb 12, 2013
122
123
attr[i++] = NSOpenGLPFAColorSize;
attr[i++] = SDL_BYTESPERPIXEL(display->current_mode.format)*8;
Feb 12, 2013
Feb 12, 2013
125
126
attr[i++] = NSOpenGLPFADepthSize;
attr[i++] = _this->gl_config.depth_size;
Feb 12, 2013
Feb 12, 2013
128
129
130
if (_this->gl_config.double_buffer) {
attr[i++] = NSOpenGLPFADoubleBuffer;
}
Feb 12, 2013
Feb 12, 2013
132
133
134
if (_this->gl_config.stereo) {
attr[i++] = NSOpenGLPFAStereo;
}
Feb 12, 2013
Feb 12, 2013
136
137
138
139
if (_this->gl_config.stencil_size) {
attr[i++] = NSOpenGLPFAStencilSize;
attr[i++] = _this->gl_config.stencil_size;
}
Feb 12, 2013
Feb 12, 2013
141
142
143
144
145
146
147
if ((_this->gl_config.accum_red_size +
_this->gl_config.accum_green_size +
_this->gl_config.accum_blue_size +
_this->gl_config.accum_alpha_size) > 0) {
attr[i++] = NSOpenGLPFAAccumSize;
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;
}
Feb 12, 2013
Feb 12, 2013
149
150
151
152
if (_this->gl_config.multisamplebuffers) {
attr[i++] = NSOpenGLPFASampleBuffers;
attr[i++] = _this->gl_config.multisamplebuffers;
}
Feb 12, 2013
Feb 12, 2013
154
155
156
157
158
if (_this->gl_config.multisamplesamples) {
attr[i++] = NSOpenGLPFASamples;
attr[i++] = _this->gl_config.multisamplesamples;
attr[i++] = NSOpenGLPFANoRecovery;
}
Feb 12, 2013
Feb 12, 2013
160
161
162
163
164
165
if (_this->gl_config.accelerated >= 0) {
if (_this->gl_config.accelerated) {
attr[i++] = NSOpenGLPFAAccelerated;
} else {
attr[i++] = NSOpenGLPFARendererID;
attr[i++] = kCGLRendererGenericFloatID;
Jan 6, 2013
Jan 6, 2013
166
}
Feb 12, 2013
Feb 12, 2013
167
}
Feb 12, 2013
Feb 12, 2013
169
170
171
attr[i++] = NSOpenGLPFAScreenMask;
attr[i++] = CGDisplayIDToOpenGLDisplayMask(displaydata->display);
attr[i] = 0;
Feb 12, 2013
Feb 12, 2013
173
174
175
176
177
178
fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
if (fmt == nil) {
SDL_SetError ("Failed creating OpenGL pixel format");
[pool release];
return NULL;
}
May 6, 2013
May 6, 2013
180
181
182
183
184
if (_this->gl_config.share_with_current_context) {
share_context = (NSOpenGLContext*)(_this->current_glctx);
}
context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:share_context];
Feb 12, 2013
Feb 12, 2013
186
[fmt release];
Feb 12, 2013
Feb 12, 2013
188
189
190
191
192
if (context == nil) {
SDL_SetError ("Failed creating OpenGL context");
[pool release];
return NULL;
}
Feb 12, 2013
Feb 12, 2013
194
195
[pool release];
196
197
198
199
200
201
202
203
204
205
206
if ( Cocoa_GL_MakeCurrent(_this, window, context) < 0 ) {
Cocoa_GL_DeleteContext(_this, context);
return NULL;
}
return context;
}
int
Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
{
Feb 12, 2013
Feb 12, 2013
207
208
209
210
211
212
213
214
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
if (context) {
SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
Jul 5, 2013
Jul 5, 2013
215
if ([nscontext view] != [windowdata->nswindow contentView]) {
Apr 22, 2013
Apr 22, 2013
216
217
[nscontext setView:[windowdata->nswindow contentView]];
[nscontext update];
Feb 12, 2013
Feb 12, 2013
219
220
221
[nscontext makeCurrentContext];
} else {
[NSOpenGLContext clearCurrentContext];
Feb 12, 2013
Feb 12, 2013
224
[pool release];
225
226
227
228
229
230
return 0;
}
int
Cocoa_GL_SetSwapInterval(_THIS, int interval)
{
Feb 12, 2013
Feb 12, 2013
231
NSAutoreleasePool *pool;
232
233
234
235
NSOpenGLContext *nscontext;
GLint value;
int status;
Feb 12, 2013
Feb 12, 2013
236
237
238
239
240
241
242
243
pool = [[NSAutoreleasePool alloc] init];
nscontext = [NSOpenGLContext currentContext];
if (nscontext != nil) {
value = interval;
[nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
status = 0;
} else {
Mar 31, 2013
Mar 31, 2013
244
status = SDL_SetError("No current OpenGL context");
Feb 12, 2013
Feb 12, 2013
247
[pool release];
248
249
250
251
252
253
return status;
}
int
Cocoa_GL_GetSwapInterval(_THIS)
{
Feb 12, 2013
Feb 12, 2013
254
NSAutoreleasePool *pool;
255
256
257
258
NSOpenGLContext *nscontext;
GLint value;
int status = 0;
Feb 12, 2013
Feb 12, 2013
259
260
261
262
263
264
pool = [[NSAutoreleasePool alloc] init];
nscontext = [NSOpenGLContext currentContext];
if (nscontext != nil) {
[nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
status = (int)value;
Feb 12, 2013
Feb 12, 2013
267
[pool release];
268
269
270
271
272
273
return status;
}
void
Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
{
Feb 12, 2013
Feb 12, 2013
274
NSAutoreleasePool *pool;
275
276
NSOpenGLContext *nscontext;
Feb 12, 2013
Feb 12, 2013
277
278
279
pool = [[NSAutoreleasePool alloc] init];
/* FIXME: Do we need to get the context for the window? */
Apr 22, 2013
Apr 22, 2013
280
[[NSOpenGLContext currentContext] flushBuffer];
Feb 12, 2013
Feb 12, 2013
281
282
[pool release];
283
284
285
286
287
}
void
Cocoa_GL_DeleteContext(_THIS, SDL_GLContext context)
{
Feb 12, 2013
Feb 12, 2013
288
NSAutoreleasePool *pool;
289
290
NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
Feb 12, 2013
Feb 12, 2013
291
292
293
294
295
296
pool = [[NSAutoreleasePool alloc] init];
[nscontext clearDrawable];
[nscontext release];
[pool release];
297
298
299
300
301
}
#endif /* SDL_VIDEO_OPENGL_CGL */
/* vi: set ts=4 sw=4 expandtab: */