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
77 NSOpenGLContext *currentContext;
79 CocoaDrawable(const Visual *vis, int w, int h, bool pbuffer) :
80 Drawable(vis, w, h, pbuffer),
83 NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
85 NSRect winRect = NSMakeRect(0, 0, w, h);
87 window = [[NSWindow alloc]
88 initWithContentRect:winRect
89 styleMask:NSTitledWindowMask |
90 NSClosableWindowMask |
91 NSMiniaturizableWindowMask
92 backing:NSBackingStoreRetained
94 assert(window != nil);
96 view = [[NSOpenGLView alloc]
98 pixelFormat:pixelFormat];
101 [window setContentView:view];
102 [window setTitle:@"glretrace"];
111 resize(int w, int h) {
112 if (w == width && h == height) {
116 [window setContentSize:NSMakeSize(w, h)];
118 if (currentContext != nil) {
119 [currentContext update];
120 [window makeKeyAndOrderFront:nil];
121 [currentContext setView:view];
122 [currentContext makeCurrentContext];
125 Drawable::resize(w, h);
138 void swapBuffers(void) {
139 if (currentContext != nil) {
140 [currentContext flushBuffer];
146 class CocoaContext : public Context
149 NSOpenGLContext *context;
151 CocoaContext(const Visual *vis, Profile prof, NSOpenGLContext *ctx) :
164 // Prevent glproc to load system's OpenGL, so that we can trace glretrace.
165 _libGlHandle = dlopen("OpenGL", RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
167 [NSApplication sharedApplication];
169 autoreleasePool = [[NSAutoreleasePool alloc] init];
171 [NSApp finishLaunching];
177 [autoreleasePool release];
182 createVisual(bool doubleBuffer, Profile profile) {
183 if (profile != PROFILE_COMPAT &&
184 profile != PROFILE_CORE) {
188 Attributes<NSOpenGLPixelFormatAttribute> attribs;
190 attribs.add(NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)1);
191 attribs.add(NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24);
193 attribs.add(NSOpenGLPFADoubleBuffer);
195 attribs.add(NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)1);
196 attribs.add(NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)1);
197 if (profile == PROFILE_CORE) {
199 attribs.add(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
205 // Use Apple software rendering for debugging purposes.
207 attribs.add(NSOpenGLPFARendererID, 0x00020200); // kCGLRendererGenericID
212 NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
213 initWithAttributes:attribs];
215 return new CocoaVisual(pixelFormat);
219 createDrawable(const Visual *visual, int width, int height, bool pbuffer)
221 return new CocoaDrawable(visual, width, height, pbuffer);
225 createContext(const Visual *visual, Context *shareContext, Profile profile, bool debug)
227 NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
228 NSOpenGLContext *share_context = nil;
229 NSOpenGLContext *context;
231 if (profile != PROFILE_COMPAT &&
232 profile != PROFILE_CORE) {
237 share_context = static_cast<CocoaContext*>(shareContext)->context;
240 context = [[NSOpenGLContext alloc]
241 initWithFormat:pixelFormat
242 shareContext:share_context];
243 assert(context != nil);
245 return new CocoaContext(visual, profile, context);
249 makeCurrent(Drawable *drawable, Context *context)
251 if (!drawable || !context) {
252 [NSOpenGLContext clearCurrentContext];
254 CocoaDrawable *cocoaDrawable = static_cast<CocoaDrawable *>(drawable);
255 CocoaContext *cocoaContext = static_cast<CocoaContext *>(context);
257 [cocoaDrawable->window makeKeyAndOrderFront:nil];
258 [cocoaContext->context setView:[cocoaDrawable->window contentView]];
259 [cocoaContext->context makeCurrentContext];
261 cocoaDrawable->currentContext = cocoaContext->context;
268 processEvents(void) {
272 event = [NSApp nextEventMatchingMask:NSAnyEventMask
273 untilDate:[NSDate distantPast]
274 inMode:NSDefaultRunLoopMode
277 [NSApp sendEvent:event];
284 } /* namespace glws */