1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
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 **************************************************************************/
29 #include <Cocoa/Cocoa.h>
37 NSAutoreleasePool *autoreleasePool = nil;
40 class CocoaVisual : public Visual
43 NSOpenGLPixelFormat *pixelFormat;
45 CocoaVisual(NSOpenGLPixelFormat *pf) :
50 [pixelFormat release];
55 class CocoaDrawable : public Drawable
59 NSOpenGLContext *currentContext;
61 CocoaDrawable(const Visual *vis, int w, int h) :
65 NSOpenGLPixelFormat *pixelFormat = dynamic_cast<const CocoaVisual *>(visual)->pixelFormat;
67 NSRect winRect = NSMakeRect(0, 0, w, h);
69 window = [[NSWindow alloc]
70 initWithContentRect:winRect
71 styleMask:NSTitledWindowMask |
72 NSClosableWindowMask |
73 NSMiniaturizableWindowMask
74 backing:NSBackingStoreRetained
76 assert(window != nil);
78 NSOpenGLView *view = [[NSOpenGLView alloc]
80 pixelFormat:pixelFormat];
83 [window setContentView:view];
84 [window setTitle:@"glretrace"];
93 resize(int w, int h) {
94 Drawable::resize(w, h);
96 [window setContentSize:NSMakeSize(w, h)];
98 if (currentContext != nil) {
99 [currentContext update];
100 [window makeKeyAndOrderFront:nil];
101 [currentContext setView:[window contentView]];
102 [currentContext makeCurrentContext];
113 void swapBuffers(void) {
114 if (currentContext != nil) {
115 [currentContext flushBuffer];
121 class CocoaContext : public Context
124 NSOpenGLContext *context;
126 CocoaContext(const Visual *vis, NSOpenGLContext *ctx) :
139 [NSApplication sharedApplication];
141 autoreleasePool = [[NSAutoreleasePool alloc] init];
143 [NSApp finishLaunching];
149 [autoreleasePool release];
154 createVisual(bool doubleBuffer) {
155 NSOpenGLPixelFormatAttribute single_attribs[] = {
156 NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)1,
157 NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24,
158 NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)1,
159 NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)1,
160 (NSOpenGLPixelFormatAttribute)0
163 NSOpenGLPixelFormatAttribute double_attribs[] = {
164 NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)1,
165 NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24,
166 NSOpenGLPFADoubleBuffer,
167 NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)1,
168 NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)1,
169 (NSOpenGLPixelFormatAttribute)0
172 NSOpenGLPixelFormatAttribute *attribs = doubleBuffer ? double_attribs : single_attribs;
174 NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
175 initWithAttributes:attribs];
177 return new CocoaVisual(pixelFormat);
181 createDrawable(const Visual *visual, int width, int height)
183 return new CocoaDrawable(visual, width, height);
187 createContext(const Visual *visual, Context *shareContext)
189 NSOpenGLPixelFormat *pixelFormat = dynamic_cast<const CocoaVisual *>(visual)->pixelFormat;
190 NSOpenGLContext *share_context = nil;
191 NSOpenGLContext *context;
194 share_context = dynamic_cast<CocoaContext*>(shareContext)->context;
197 context = [[NSOpenGLContext alloc]
198 initWithFormat:pixelFormat
199 shareContext:share_context];
200 assert(context != nil);
202 return new CocoaContext(visual, context);
206 makeCurrent(Drawable *drawable, Context *context)
208 if (!drawable || !context) {
209 [NSOpenGLContext clearCurrentContext];
211 CocoaDrawable *cocoaDrawable = dynamic_cast<CocoaDrawable *>(drawable);
212 CocoaContext *cocoaContext = dynamic_cast<CocoaContext *>(context);
214 [cocoaDrawable->window makeKeyAndOrderFront:nil];
215 [cocoaContext->context setView:[cocoaDrawable->window contentView]];
216 [cocoaContext->context makeCurrentContext];
218 cocoaDrawable->currentContext = cocoaContext->context;
225 processEvents(void) {
229 event = [NSApp nextEventMatchingMask:NSAnyEventMask
230 untilDate:[NSDate distantPast]
231 inMode:NSDefaultRunLoopMode
234 [NSApp sendEvent:event];
241 } /* namespace glws */