]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/osx/cocoa/glcanvas.mm | |
3 | // Purpose: wxGLCanvas, for using OpenGL with wxWidgets under Macintosh | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id: glcanvas.cpp 54129 2008-06-11 19:30:52Z SC $ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #if defined(__BORLANDC__) | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #if wxUSE_GLCANVAS | |
27 | ||
28 | #include "wx/glcanvas.h" | |
29 | ||
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/frame.h" | |
32 | #include "wx/log.h" | |
33 | #include "wx/settings.h" | |
34 | #endif | |
35 | ||
36 | #include "wx/osx/private.h" | |
37 | ||
38 | WXGLContext WXGLCreateContext( WXGLPixelFormat pixelFormat, WXGLContext shareContext ) | |
39 | { | |
40 | WXGLContext context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext: shareContext]; | |
41 | if ( !context ) | |
42 | wxFAIL_MSG("NSOpenGLContext creation failed"); | |
43 | return context ; | |
44 | } | |
45 | ||
46 | void WXGLDestroyContext( WXGLContext context ) | |
47 | { | |
48 | if ( context ) | |
49 | { | |
50 | [context release]; | |
51 | } | |
52 | } | |
53 | ||
54 | void WXGLSwapBuffers( WXGLContext context ) | |
55 | { | |
56 | [context flushBuffer]; | |
57 | } | |
58 | ||
59 | WXGLContext WXGLGetCurrentContext() | |
60 | { | |
61 | return [NSOpenGLContext currentContext]; | |
62 | } | |
63 | ||
64 | void WXGLDestroyPixelFormat( WXGLPixelFormat pixelFormat ) | |
65 | { | |
66 | if ( pixelFormat ) | |
67 | { | |
68 | [pixelFormat release]; | |
69 | } | |
70 | } | |
71 | ||
72 | ||
73 | WXGLPixelFormat WXGLChoosePixelFormat(const int *attribList) | |
74 | { | |
75 | NSOpenGLPixelFormatAttribute data[512]; | |
76 | const NSOpenGLPixelFormatAttribute defaultAttribs[] = | |
77 | { | |
78 | NSOpenGLPFADoubleBuffer, | |
79 | NSOpenGLPFAMinimumPolicy, | |
80 | NSOpenGLPFAColorSize,(NSOpenGLPixelFormatAttribute)8, | |
81 | NSOpenGLPFAAlphaSize,(NSOpenGLPixelFormatAttribute)0, | |
82 | NSOpenGLPFADepthSize,(NSOpenGLPixelFormatAttribute)8, | |
83 | (NSOpenGLPixelFormatAttribute)nil | |
84 | }; | |
85 | ||
86 | const NSOpenGLPixelFormatAttribute *attribs; | |
87 | if ( !attribList ) | |
88 | { | |
89 | attribs = defaultAttribs; | |
90 | } | |
91 | else | |
92 | { | |
93 | unsigned p = 0; | |
94 | data[p++] = NSOpenGLPFAMinimumPolicy; // make _SIZE tags behave more like GLX | |
95 | ||
96 | for ( unsigned arg = 0; attribList[arg] !=0 && p < WXSIZEOF(data); ) | |
97 | { | |
98 | switch ( attribList[arg++] ) | |
99 | { | |
100 | case WX_GL_RGBA: | |
101 | //data[p++] = AGL_RGBA; | |
102 | break; | |
103 | ||
104 | case WX_GL_BUFFER_SIZE: | |
105 | //data[p++] = AGL_BUFFER_SIZE; | |
106 | //data[p++] = attribList[arg++]; | |
107 | break; | |
108 | ||
109 | case WX_GL_LEVEL: | |
110 | //data[p++]=AGL_LEVEL; | |
111 | //data[p++]=attribList[arg++]; | |
112 | break; | |
113 | ||
114 | case WX_GL_DOUBLEBUFFER: | |
115 | data[p++] = NSOpenGLPFADoubleBuffer; | |
116 | break; | |
117 | ||
118 | case WX_GL_STEREO: | |
119 | data[p++] = NSOpenGLPFAStereo; | |
120 | break; | |
121 | ||
122 | case WX_GL_AUX_BUFFERS: | |
123 | data[p++] = NSOpenGLPFAAuxBuffers; | |
124 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; | |
125 | break; | |
126 | ||
127 | case WX_GL_MIN_RED: | |
128 | data[p++] = NSOpenGLPFAColorSize; | |
129 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; | |
130 | break; | |
131 | ||
132 | case WX_GL_MIN_GREEN: | |
133 | //data[p++] = AGL_GREEN_SIZE; | |
134 | //data[p++] = attribList[arg++]; | |
135 | break; | |
136 | ||
137 | case WX_GL_MIN_BLUE: | |
138 | //data[p++] = AGL_BLUE_SIZE; | |
139 | //data[p++] = attribList[arg++]; | |
140 | break; | |
141 | ||
142 | case WX_GL_MIN_ALPHA: | |
143 | data[p++] = NSOpenGLPFAAlphaSize; | |
144 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; | |
145 | break; | |
146 | ||
147 | case WX_GL_DEPTH_SIZE: | |
148 | data[p++] = NSOpenGLPFADepthSize; | |
149 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; | |
150 | break; | |
151 | ||
152 | case WX_GL_STENCIL_SIZE: | |
153 | data[p++] = NSOpenGLPFAStencilSize; | |
154 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; | |
155 | break; | |
156 | ||
157 | case WX_GL_MIN_ACCUM_RED: | |
158 | data[p++] = NSOpenGLPFAAccumSize; | |
159 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; | |
160 | break; | |
161 | ||
162 | case WX_GL_MIN_ACCUM_GREEN: | |
163 | //data[p++] = AGL_ACCUM_GREEN_SIZE; | |
164 | //data[p++] = attribList[arg++]; | |
165 | break; | |
166 | ||
167 | case WX_GL_MIN_ACCUM_BLUE: | |
168 | //data[p++] = AGL_ACCUM_BLUE_SIZE; | |
169 | //data[p++] = attribList[arg++]; | |
170 | break; | |
171 | ||
172 | case WX_GL_MIN_ACCUM_ALPHA: | |
173 | //data[p++] = AGL_ACCUM_ALPHA_SIZE; | |
174 | //data[p++] = attribList[arg++]; | |
175 | break; | |
176 | ||
177 | case WX_GL_SAMPLE_BUFFERS: | |
178 | if ( !wxGLCanvas::IsAGLMultiSampleAvailable() ) | |
179 | { | |
180 | if ( !attribList[arg++] ) | |
181 | break; | |
182 | ||
183 | return false; | |
184 | } | |
185 | ||
186 | data[p++] = NSOpenGLPFASampleBuffers; | |
187 | if ( (data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]) == true ) | |
188 | { | |
189 | // don't use software fallback | |
190 | data[p++] = NSOpenGLPFANoRecovery; | |
191 | } | |
192 | break; | |
193 | ||
194 | case WX_GL_SAMPLES: | |
195 | if ( !wxGLCanvas::IsAGLMultiSampleAvailable() ) | |
196 | { | |
197 | if ( !attribList[arg++] ) | |
198 | break; | |
199 | ||
200 | return false; | |
201 | } | |
202 | ||
203 | data[p++] = NSOpenGLPFASamples; | |
204 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; | |
205 | break; | |
206 | } | |
207 | } | |
208 | ||
209 | data[p] = (NSOpenGLPixelFormatAttribute)nil; | |
210 | ||
211 | attribs = data; | |
212 | } | |
213 | ||
214 | return [[NSOpenGLPixelFormat alloc] initWithAttributes:(NSOpenGLPixelFormatAttribute*) attribs]; | |
215 | } | |
216 | ||
217 | bool wxGLContext::SetCurrent(const wxGLCanvas& win) const | |
218 | { | |
219 | if ( !m_glContext ) | |
220 | return false; | |
221 | ||
222 | [m_glContext setView: win.GetHandle() ]; | |
223 | [m_glContext update]; | |
224 | ||
225 | [m_glContext makeCurrentContext]; | |
226 | ||
227 | return true; | |
228 | } | |
229 | ||
230 | @interface wxNSCustomOpenGLView : NSView | |
231 | { | |
232 | WXCOCOAIMPL_COMMON_MEMBERS | |
233 | NSOpenGLContext* context; | |
234 | } | |
235 | ||
236 | - (id)initWithFrame:(NSRect)frame; | |
237 | ||
238 | WXCOCOAIMPL_COMMON_INTERFACE | |
239 | ||
240 | @end | |
241 | ||
242 | @implementation wxNSCustomOpenGLView | |
243 | ||
244 | - (id)initWithFrame:(NSRect)frame | |
245 | { | |
246 | [super initWithFrame:frame]; | |
247 | impl = NULL; | |
248 | return self; | |
249 | } | |
250 | ||
251 | WXCOCOAIMPL_COMMON_IMPLEMENTATION | |
252 | ||
253 | - (BOOL)isOpaque | |
254 | { | |
255 | return YES; | |
256 | } | |
257 | ||
258 | @end | |
259 | ||
260 | bool wxGLCanvas::Create(wxWindow *parent, | |
261 | wxWindowID id, | |
262 | const wxPoint& pos, | |
263 | const wxSize& size, | |
264 | long style, | |
265 | const wxString& name, | |
266 | const int *attribList, | |
267 | const wxPalette& WXUNUSED(palette)) | |
268 | { | |
269 | m_glFormat = WXGLChoosePixelFormat(attribList); | |
270 | if ( !m_glFormat ) | |
271 | return false; | |
272 | ||
273 | // m_macIsUserPane = false ; | |
274 | ||
275 | if ( !wxWindow::Create(parent, id, pos, size, style, name) ) | |
276 | return false; | |
277 | ||
278 | /* | |
279 | NSRect r = wxOSXGetFrameForControl( this, pos , size ) ; | |
280 | wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r]; | |
281 | m_peer = new wxWidgetCocoaImpl( this, v ); | |
282 | [v setImplementation:m_peer]; | |
283 | ||
284 | MacPostControlCreate(pos, size) ; | |
285 | */ | |
286 | return true; | |
287 | } | |
288 | ||
289 | wxGLCanvas::~wxGLCanvas() | |
290 | { | |
291 | if ( m_glFormat ) | |
292 | WXGLDestroyPixelFormat(m_glFormat); | |
293 | } | |
294 | ||
295 | ||
296 | #endif // wxUSE_GLCANVAS |