]> git.cworth.org Git - apitrace/blob - glws_cocoa.mm
Add a few comments/links to Cocoa development resources.
[apitrace] / glws_cocoa.mm
1 /**************************************************************************
2  *
3  * Copyright 2011 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 /**
28  * Minimal Cocoa integration.
29  *
30  * See also:
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/
36  */
37
38
39 #include <stdlib.h>
40 #include <iostream>
41
42 #include <Cocoa/Cocoa.h>
43
44 #include "glws.hpp"
45
46
47 namespace glws {
48
49
50 NSAutoreleasePool *autoreleasePool = nil;
51
52
53 class CocoaVisual : public Visual
54 {
55 public:
56     NSOpenGLPixelFormat *pixelFormat;
57
58     CocoaVisual(NSOpenGLPixelFormat *pf) :
59         pixelFormat(pf)
60     {}
61
62     ~CocoaVisual() {
63         [pixelFormat release];
64     }
65 };
66  
67
68 class CocoaDrawable : public Drawable
69 {
70 public:
71     NSWindow *window;
72     NSOpenGLContext *currentContext;
73
74     CocoaDrawable(const Visual *vis, int w, int h) :
75         Drawable(vis, w, h),
76         currentContext(nil)
77     {
78         NSOpenGLPixelFormat *pixelFormat = dynamic_cast<const CocoaVisual *>(visual)->pixelFormat;
79
80         NSRect winRect = NSMakeRect(0, 0, w, h);
81
82         window = [[NSWindow alloc]
83                          initWithContentRect:winRect
84                                    styleMask:NSTitledWindowMask |
85                                              NSClosableWindowMask |
86                                              NSMiniaturizableWindowMask
87                                      backing:NSBackingStoreRetained
88                                        defer:NO];
89         assert(window != nil);
90
91         NSOpenGLView *view = [[NSOpenGLView alloc]
92                               initWithFrame:winRect
93                                 pixelFormat:pixelFormat];
94         assert(view != nil);
95
96         [window setContentView:view];
97         [window setTitle:@"glretrace"];
98
99     }
100
101     ~CocoaDrawable() {
102         [window release];
103     }
104
105     void
106     resize(int w, int h) {
107         Drawable::resize(w, h);
108
109         [window setContentSize:NSMakeSize(w, h)];
110
111         if (currentContext != nil) {
112             [currentContext update];
113             [window makeKeyAndOrderFront:nil];
114             [currentContext setView:[window contentView]];
115             [currentContext makeCurrentContext];
116         }
117     }
118
119     void show(void) {
120         if (!visible) {
121             // TODO
122             Drawable::show();
123         }
124     }
125
126     void swapBuffers(void) {
127         if (currentContext != nil) {
128             [currentContext flushBuffer];
129         }
130     }
131 };
132
133
134 class CocoaContext : public Context
135 {
136 public:
137     NSOpenGLContext *context;
138
139     CocoaContext(const Visual *vis, NSOpenGLContext *ctx) :
140         Context(vis),
141         context(ctx)
142     {}
143
144     ~CocoaContext() {
145         [context release];
146     }
147 };
148
149
150 void
151 init(void) {
152     [NSApplication sharedApplication];
153
154     autoreleasePool = [[NSAutoreleasePool alloc] init];
155
156     [NSApp finishLaunching];
157 }
158
159
160 void
161 cleanup(void) {
162     [autoreleasePool release];
163 }
164
165
166 Visual *
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
174     };
175
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
183     };
184
185     NSOpenGLPixelFormatAttribute *attribs = doubleBuffer ? double_attribs : single_attribs;
186
187     NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
188                                      initWithAttributes:attribs];
189
190     return new CocoaVisual(pixelFormat);
191 }
192
193 Drawable *
194 createDrawable(const Visual *visual, int width, int height)
195 {
196     return new CocoaDrawable(visual, width, height);
197 }
198
199 Context *
200 createContext(const Visual *visual, Context *shareContext)
201 {
202     NSOpenGLPixelFormat *pixelFormat = dynamic_cast<const CocoaVisual *>(visual)->pixelFormat;
203     NSOpenGLContext *share_context = nil;
204     NSOpenGLContext *context;
205
206     if (shareContext) {
207         share_context = dynamic_cast<CocoaContext*>(shareContext)->context;
208     }
209
210     context = [[NSOpenGLContext alloc]
211                initWithFormat:pixelFormat
212                shareContext:share_context];
213     assert(context != nil);
214
215     return new CocoaContext(visual, context);
216 }
217
218 bool
219 makeCurrent(Drawable *drawable, Context *context)
220 {
221     if (!drawable || !context) {
222         [NSOpenGLContext clearCurrentContext];
223     } else {
224         CocoaDrawable *cocoaDrawable = dynamic_cast<CocoaDrawable *>(drawable);
225         CocoaContext *cocoaContext = dynamic_cast<CocoaContext *>(context);
226
227         [cocoaDrawable->window makeKeyAndOrderFront:nil];
228         [cocoaContext->context setView:[cocoaDrawable->window contentView]];
229         [cocoaContext->context makeCurrentContext];
230
231         cocoaDrawable->currentContext = cocoaContext->context;
232     }
233
234     return TRUE;
235 }
236
237 bool
238 processEvents(void) {
239    NSEvent* event;
240
241     do {
242         event = [NSApp nextEventMatchingMask:NSAnyEventMask
243                                    untilDate:[NSDate distantPast]
244                                       inMode:NSDefaultRunLoopMode
245                                      dequeue:YES];
246         if (event)
247             [NSApp sendEvent:event];
248     } while (event);
249
250     return true;
251 }
252
253
254 } /* namespace glws */