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

Latest commit

 

History

History
366 lines (296 loc) · 9.21 KB

SDL_cocoaopengl.m

File metadata and controls

366 lines (296 loc) · 9.21 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#include "SDL_cocoavideo.h"
/* NSOpenGL implementation of SDL OpenGL support */
Jul 28, 2006
Jul 28, 2006
28
#if SDL_VIDEO_OPENGL_CGL
29
30
31
32
33
34
#include <OpenGL/CGLTypes.h>
#include "SDL_loadso.h"
#include "SDL_opengl.h"
Jul 28, 2006
Jul 28, 2006
35
#define DEFAULT_OPENGL "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* This is implemented in Mac OS X 10.3 and above */
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_3
@implementation NSOpenGLContext(CGLContextAccess)
- (CGLContextObj)CGLContextObj;
{
return _contextAuxiliary;
}
@end
#endif /* < 10.3 */
int
Cocoa_GL_LoadLibrary(_THIS, const char *path)
{
if (_this->gl_config.driver_loaded) {
if (path) {
SDL_SetError("OpenGL library already loaded");
return -1;
} else {
++_this->gl_config.driver_loaded;
return 0;
}
}
if (path == NULL) {
Jul 28, 2006
Jul 28, 2006
60
61
62
63
path = SDL_getenv("SDL_OPENGL_LIBRARY");
}
if (path == NULL) {
path = DEFAULT_OPENGL;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
}
_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));
_this->gl_config.driver_loaded = 1;
return 0;
}
void *
Cocoa_GL_GetProcAddress(_THIS, const char *proc)
{
return SDL_LoadFunction(_this->gl_config.dll_handle, proc);
}
static void
Cocoa_GL_UnloadLibrary(_THIS)
{
if (_this->gl_config.driver_loaded > 0) {
if (--_this->gl_config.driver_loaded > 0) {
return;
}
SDL_UnloadObject(_this->gl_config.dll_handle);
_this->gl_config.dll_handle = NULL;
}
}
static int
Cocoa_GL_Initialize(_THIS)
{
if (_this->gl_data) {
++_this->gl_data->initialized;
return 0;
}
_this->gl_data =
(struct SDL_GLDriverData *) SDL_calloc(1,
sizeof(struct
SDL_GLDriverData));
if (!_this->gl_data) {
SDL_OutOfMemory();
return -1;
}
_this->gl_data->initialized = 1;
if (Cocoa_GL_LoadLibrary(_this, NULL) < 0) {
return -1;
}
return 0;
}
Jul 28, 2006
Jul 28, 2006
117
118
119
120
121
122
123
124
125
126
127
128
129
static void
Cocoa_GL_Shutdown(_THIS)
{
if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
return;
}
Cocoa_GL_UnloadLibrary(_this);
SDL_free(_this->gl_data);
_this->gl_data = NULL;
}
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
int
Cocoa_GL_SetupWindow(_THIS, SDL_Window * window)
{
if (Cocoa_GL_Initialize(_this) < 0) {
return -1;
}
return 0;
}
void
Cocoa_GL_CleanupWindow(_THIS, SDL_Window * window)
{
Cocoa_GL_Shutdown(_this);
}
SDL_GLContext
Cocoa_GL_CreateContext(_THIS, SDL_Window * window)
{
NSAutoreleasePool *pool;
SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
NSOpenGLPixelFormatAttribute attr[32];
NSOpenGLPixelFormat *fmt;
Jul 12, 2007
Jul 12, 2007
153
NSOpenGLContext *context;
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
int i = 0;
pool = [[NSAutoreleasePool alloc] init];
if (window->flags & SDL_WINDOW_FULLSCREEN) {
attr[i++] = NSOpenGLPFAFullScreen;
}
attr[i++] = NSOpenGLPFAColorSize;
attr[i++] = SDL_BYTESPERPIXEL(display->current_mode.format)*8;
attr[i++] = NSOpenGLPFADepthSize;
attr[i++] = _this->gl_config.depth_size;
if (_this->gl_config.double_buffer) {
attr[i++] = NSOpenGLPFADoubleBuffer;
}
if (_this->gl_config.stereo) {
attr[i++] = NSOpenGLPFAStereo;
}
if (_this->gl_config.stencil_size) {
attr[i++] = NSOpenGLPFAStencilSize;
attr[i++] = _this->gl_config.stencil_size;
}
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;
}
if (_this->gl_config.multisamplebuffers) {
attr[i++] = NSOpenGLPFASampleBuffers;
attr[i++] = _this->gl_config.multisamplebuffers;
}
if (_this->gl_config.multisamplesamples) {
attr[i++] = NSOpenGLPFASamples;
attr[i++] = _this->gl_config.multisamplesamples;
attr[i++] = NSOpenGLPFANoRecovery;
}
if (_this->gl_config.accelerated > 0) {
attr[i++] = NSOpenGLPFAAccelerated;
}
attr[i++] = NSOpenGLPFAScreenMask;
attr[i++] = CGDisplayIDToOpenGLDisplayMask(displaydata->display);
attr[i] = 0;
fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
if (fmt == nil) {
SDL_SetError ("Failed creating OpenGL pixel format");
[pool release];
return NULL;
}
Jul 12, 2007
Jul 12, 2007
215
context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
216
217
218
[fmt release];
Jul 12, 2007
Jul 12, 2007
219
if (context == nil) {
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
SDL_SetError ("Failed creating OpenGL context");
[pool release];
return NULL;
}
/*
* Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
* "You are blowing a couple of the internal OpenGL function caches. This
* appears to be happening in the VAO case. You can tell OpenGL to up
* the cache size by issuing the following calls right after you create
* the OpenGL context. The default cache size is 16." --ryan.
*/
#ifndef GLI_ARRAY_FUNC_CACHE_MAX
#define GLI_ARRAY_FUNC_CACHE_MAX 284
#endif
#ifndef GLI_SUBMIT_FUNC_CACHE_MAX
#define GLI_SUBMIT_FUNC_CACHE_MAX 280
#endif
{
long cache_max = 64;
Jul 12, 2007
Jul 12, 2007
243
CGLContextObj ctx = [context CGLContextObj];
244
245
246
247
248
249
250
CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
}
/* End Wisdom from Apple Engineer section. --ryan. */
[pool release];
Jul 12, 2007
Jul 12, 2007
251
252
253
254
255
256
257
if ( Cocoa_GL_MakeCurrent(_this, window, context) < 0 ) {
Cocoa_GL_DeleteContext(_this, context);
return NULL;
}
return context;
258
259
260
261
262
263
264
265
266
267
268
269
270
}
int
Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
if (context) {
SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
Aug 6, 2006
Aug 6, 2006
271
272
273
274
275
276
if (window->flags & SDL_WINDOW_FULLSCREEN) {
[nscontext setFullScreen];
} else {
[nscontext setView:[windowdata->window contentView]];
[nscontext update];
}
277
278
279
280
281
282
283
284
285
286
287
288
289
290
[nscontext makeCurrentContext];
} else {
[NSOpenGLContext clearCurrentContext];
}
[pool release];
return 0;
}
int
Cocoa_GL_SetSwapInterval(_THIS, int interval)
{
NSAutoreleasePool *pool;
NSOpenGLContext *nscontext;
Jul 20, 2008
Jul 20, 2008
291
GLint value;
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
int status;
pool = [[NSAutoreleasePool alloc] init];
nscontext = [NSOpenGLContext currentContext];
if (nscontext != nil) {
value = interval;
[nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
status = 0;
} else {
SDL_SetError("No current OpenGL context");
status = -1;
}
[pool release];
return status;
}
int
Cocoa_GL_GetSwapInterval(_THIS)
{
NSAutoreleasePool *pool;
NSOpenGLContext *nscontext;
Jul 20, 2008
Jul 20, 2008
315
GLint value;
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
int status;
pool = [[NSAutoreleasePool alloc] init];
nscontext = [NSOpenGLContext currentContext];
if (nscontext != nil) {
[nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
status = (int)value;
} else {
SDL_SetError("No current OpenGL context");
status = -1;
}
[pool release];
return status;
}
void
Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
{
NSAutoreleasePool *pool;
NSOpenGLContext *nscontext;
pool = [[NSAutoreleasePool alloc] init];
/* FIXME: Do we need to get the context for the window? */
nscontext = [NSOpenGLContext currentContext];
if (nscontext != nil) {
[nscontext flushBuffer];
}
[pool release];
}
void
Cocoa_GL_DeleteContext(_THIS, SDL_GLContext context)
{
NSAutoreleasePool *pool;
NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
pool = [[NSAutoreleasePool alloc] init];
[nscontext clearDrawable];
[nscontext release];
[pool release];
}
Jul 28, 2006
Jul 28, 2006
364
#endif /* SDL_VIDEO_OPENGL_CGL */
365
366
/* vi: set ts=4 sw=4 expandtab: */