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/
42 #include <Cocoa/Cocoa.h>
50 NSAutoreleasePool *autoreleasePool = nil;
53 class CocoaVisual : public Visual
56 NSOpenGLPixelFormat *pixelFormat;
58 CocoaVisual(NSOpenGLPixelFormat *pf) :
63 [pixelFormat release];
68 class CocoaDrawable : public Drawable
72 NSOpenGLContext *currentContext;
74 CocoaDrawable(const Visual *vis, int w, int h) :
78 NSOpenGLPixelFormat *pixelFormat = dynamic_cast<const CocoaVisual *>(visual)->pixelFormat;
80 NSRect winRect = NSMakeRect(0, 0, w, h);
82 window = [[NSWindow alloc]
83 initWithContentRect:winRect
84 styleMask:NSTitledWindowMask |
85 NSClosableWindowMask |
86 NSMiniaturizableWindowMask
87 backing:NSBackingStoreRetained
89 assert(window != nil);
91 NSOpenGLView *view = [[NSOpenGLView alloc]
93 pixelFormat:pixelFormat];
96 [window setContentView:view];
97 [window setTitle:@"glretrace"];
106 resize(int w, int h) {
107 Drawable::resize(w, h);
109 [window setContentSize:NSMakeSize(w, h)];
111 if (currentContext != nil) {
112 [currentContext update];
113 [window makeKeyAndOrderFront:nil];
114 [currentContext setView:[window contentView]];
115 [currentContext makeCurrentContext];
126 void swapBuffers(void) {
127 if (currentContext != nil) {
128 [currentContext flushBuffer];
134 class CocoaContext : public Context
137 NSOpenGLContext *context;
139 CocoaContext(const Visual *vis, NSOpenGLContext *ctx) :
152 [NSApplication sharedApplication];
154 autoreleasePool = [[NSAutoreleasePool alloc] init];
156 [NSApp finishLaunching];
162 [autoreleasePool release];
167 createVisual(bool doubleBuffer) {
168 NSOpenGLPixelFormatAttribute single_attribs[] = {
169 NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)1,
170 NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24,
171 NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)1,
172 NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)1,
173 (NSOpenGLPixelFormatAttribute)0
176 NSOpenGLPixelFormatAttribute double_attribs[] = {
177 NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)1,
178 NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24,
179 NSOpenGLPFADoubleBuffer,
180 NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)1,
181 NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)1,
182 (NSOpenGLPixelFormatAttribute)0
185 NSOpenGLPixelFormatAttribute *attribs = doubleBuffer ? double_attribs : single_attribs;
187 NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
188 initWithAttributes:attribs];
190 return new CocoaVisual(pixelFormat);
194 createDrawable(const Visual *visual, int width, int height)
196 return new CocoaDrawable(visual, width, height);
200 createContext(const Visual *visual, Context *shareContext)
202 NSOpenGLPixelFormat *pixelFormat = dynamic_cast<const CocoaVisual *>(visual)->pixelFormat;
203 NSOpenGLContext *share_context = nil;
204 NSOpenGLContext *context;
207 share_context = dynamic_cast<CocoaContext*>(shareContext)->context;
210 context = [[NSOpenGLContext alloc]
211 initWithFormat:pixelFormat
212 shareContext:share_context];
213 assert(context != nil);
215 return new CocoaContext(visual, context);
219 makeCurrent(Drawable *drawable, Context *context)
221 if (!drawable || !context) {
222 [NSOpenGLContext clearCurrentContext];
224 CocoaDrawable *cocoaDrawable = dynamic_cast<CocoaDrawable *>(drawable);
225 CocoaContext *cocoaContext = dynamic_cast<CocoaContext *>(context);
227 [cocoaDrawable->window makeKeyAndOrderFront:nil];
228 [cocoaContext->context setView:[cocoaDrawable->window contentView]];
229 [cocoaContext->context makeCurrentContext];
231 cocoaDrawable->currentContext = cocoaContext->context;
238 processEvents(void) {
242 event = [NSApp nextEventMatchingMask:NSAnyEventMask
243 untilDate:[NSDate distantPast]
244 inMode:NSDefaultRunLoopMode
247 [NSApp sendEvent:event];
254 } /* namespace glws */