1 /**************************************************************************
3 * Copyright 2011 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
28 * Minimal Cocoa integration.
31 * - http://developer.apple.com/library/mac/#samplecode/CocoaGL/Introduction/Intro.html
32 * - http://developer.apple.com/library/mac/#samplecode/Cocoa_With_Carbon_or_CPP/Introduction/Intro.html
33 * - http://developer.apple.com/library/mac/#samplecode/glut/Introduction/Intro.html
34 * - http://developer.apple.com/library/mac/#samplecode/GLEssentials/Introduction/Intro.html
35 * - http://www.glfw.org/
46 #include <Cocoa/Cocoa.h>
54 NSAutoreleasePool *autoreleasePool = nil;
57 class CocoaVisual : public Visual
60 NSOpenGLPixelFormat *pixelFormat;
62 CocoaVisual(NSOpenGLPixelFormat *pf) :
67 [pixelFormat release];
72 class CocoaDrawable : public Drawable
76 NSOpenGLContext *currentContext;
78 CocoaDrawable(const Visual *vis, int w, int h, bool pbuffer) :
79 Drawable(vis, w, h, pbuffer),
82 NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
84 NSRect winRect = NSMakeRect(0, 0, w, h);
86 window = [[NSWindow alloc]
87 initWithContentRect:winRect
88 styleMask:NSTitledWindowMask |
89 NSClosableWindowMask |
90 NSMiniaturizableWindowMask
91 backing:NSBackingStoreRetained
93 assert(window != nil);
95 NSOpenGLView *view = [[NSOpenGLView alloc]
97 pixelFormat:pixelFormat];
100 [window setContentView:view];
101 [window setTitle:@"glretrace"];
110 resize(int w, int h) {
111 if (w == width && h == height) {
115 [window setContentSize:NSMakeSize(w, h)];
117 if (currentContext != nil) {
118 [currentContext update];
119 [window makeKeyAndOrderFront:nil];
120 [currentContext setView:[window contentView]];
121 [currentContext makeCurrentContext];
124 Drawable::resize(w, h);
137 void swapBuffers(void) {
138 if (currentContext != nil) {
139 [currentContext flushBuffer];
145 class CocoaContext : public Context
148 NSOpenGLContext *context;
150 CocoaContext(const Visual *vis, Profile prof, NSOpenGLContext *ctx) :
163 // Prevent glproc to load system's OpenGL, so that we can trace glretrace.
164 _libGlHandle = dlopen("OpenGL", RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
166 [NSApplication sharedApplication];
168 autoreleasePool = [[NSAutoreleasePool alloc] init];
170 [NSApp finishLaunching];
176 [autoreleasePool release];
181 createVisual(bool doubleBuffer, Profile profile) {
182 if (profile != PROFILE_COMPAT &&
183 profile != PROFILE_CORE) {
187 Attributes<NSOpenGLPixelFormatAttribute> attribs;
189 attribs.add(NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)1);
190 attribs.add(NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24);
192 attribs.add(NSOpenGLPFADoubleBuffer);
194 attribs.add(NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)1);
195 attribs.add(NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)1);
196 if (profile == PROFILE_CORE) {
198 attribs.add(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
204 // Use Apple software rendering for debugging purposes.
206 attribs.add(NSOpenGLPFARendererID, 0x00020200); // kCGLRendererGenericID
211 NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
212 initWithAttributes:attribs];
214 return new CocoaVisual(pixelFormat);
218 createDrawable(const Visual *visual, int width, int height, bool pbuffer)
220 return new CocoaDrawable(visual, width, height, pbuffer);
224 createContext(const Visual *visual, Context *shareContext, Profile profile, bool debug)
226 NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
227 NSOpenGLContext *share_context = nil;
228 NSOpenGLContext *context;
230 if (profile != PROFILE_COMPAT &&
231 profile != PROFILE_CORE) {
236 share_context = static_cast<CocoaContext*>(shareContext)->context;
239 context = [[NSOpenGLContext alloc]
240 initWithFormat:pixelFormat
241 shareContext:share_context];
242 assert(context != nil);
244 return new CocoaContext(visual, profile, context);
248 makeCurrent(Drawable *drawable, Context *context)
250 if (!drawable || !context) {
251 [NSOpenGLContext clearCurrentContext];
253 CocoaDrawable *cocoaDrawable = static_cast<CocoaDrawable *>(drawable);
254 CocoaContext *cocoaContext = static_cast<CocoaContext *>(context);
256 [cocoaDrawable->window makeKeyAndOrderFront:nil];
257 [cocoaContext->context setView:[cocoaDrawable->window contentView]];
258 [cocoaContext->context makeCurrentContext];
260 cocoaDrawable->currentContext = cocoaContext->context;
267 processEvents(void) {
271 event = [NSApp nextEventMatchingMask:NSAnyEventMask
272 untilDate:[NSDate distantPast]
273 inMode:NSDefaultRunLoopMode
276 [NSApp sendEvent:event];
283 } /* namespace glws */