]>
Commit | Line | Data |
---|---|---|
0f9b48d1 SC |
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 | ||
0f9b48d1 SC |
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 | ||
b6ccc13c VZ |
64 | bool WXGLSetCurrentContext(WXGLContext context) |
65 | { | |
66 | [context makeCurrentContext]; | |
67 | ||
68 | return true; | |
69 | } | |
70 | ||
0f9b48d1 SC |
71 | void WXGLDestroyPixelFormat( WXGLPixelFormat pixelFormat ) |
72 | { | |
73 | if ( pixelFormat ) | |
74 | { | |
75 | [pixelFormat release]; | |
76 | } | |
77 | } | |
78 | ||
79 | ||
80 | WXGLPixelFormat WXGLChoosePixelFormat(const int *attribList) | |
81 | { | |
82 | NSOpenGLPixelFormatAttribute data[512]; | |
83 | const NSOpenGLPixelFormatAttribute defaultAttribs[] = | |
84 | { | |
85 | NSOpenGLPFADoubleBuffer, | |
524c47aa | 86 | NSOpenGLPFAMinimumPolicy, |
7ac5e1c9 SC |
87 | NSOpenGLPFAColorSize,(NSOpenGLPixelFormatAttribute)8, |
88 | NSOpenGLPFAAlphaSize,(NSOpenGLPixelFormatAttribute)0, | |
89 | NSOpenGLPFADepthSize,(NSOpenGLPixelFormatAttribute)8, | |
0f9b48d1 SC |
90 | (NSOpenGLPixelFormatAttribute)nil |
91 | }; | |
92 | ||
93 | const NSOpenGLPixelFormatAttribute *attribs; | |
94 | if ( !attribList ) | |
95 | { | |
96 | attribs = defaultAttribs; | |
97 | } | |
98 | else | |
99 | { | |
100 | unsigned p = 0; | |
101 | data[p++] = NSOpenGLPFAMinimumPolicy; // make _SIZE tags behave more like GLX | |
102 | ||
103 | for ( unsigned arg = 0; attribList[arg] !=0 && p < WXSIZEOF(data); ) | |
104 | { | |
105 | switch ( attribList[arg++] ) | |
106 | { | |
107 | case WX_GL_RGBA: | |
108 | //data[p++] = AGL_RGBA; | |
109 | break; | |
110 | ||
111 | case WX_GL_BUFFER_SIZE: | |
112 | //data[p++] = AGL_BUFFER_SIZE; | |
113 | //data[p++] = attribList[arg++]; | |
114 | break; | |
115 | ||
116 | case WX_GL_LEVEL: | |
117 | //data[p++]=AGL_LEVEL; | |
118 | //data[p++]=attribList[arg++]; | |
119 | break; | |
120 | ||
121 | case WX_GL_DOUBLEBUFFER: | |
122 | data[p++] = NSOpenGLPFADoubleBuffer; | |
123 | break; | |
124 | ||
125 | case WX_GL_STEREO: | |
126 | data[p++] = NSOpenGLPFAStereo; | |
127 | break; | |
128 | ||
129 | case WX_GL_AUX_BUFFERS: | |
130 | data[p++] = NSOpenGLPFAAuxBuffers; | |
7ac5e1c9 | 131 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; |
0f9b48d1 SC |
132 | break; |
133 | ||
134 | case WX_GL_MIN_RED: | |
135 | data[p++] = NSOpenGLPFAColorSize; | |
7ac5e1c9 | 136 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; |
0f9b48d1 SC |
137 | break; |
138 | ||
139 | case WX_GL_MIN_GREEN: | |
140 | //data[p++] = AGL_GREEN_SIZE; | |
141 | //data[p++] = attribList[arg++]; | |
142 | break; | |
143 | ||
144 | case WX_GL_MIN_BLUE: | |
145 | //data[p++] = AGL_BLUE_SIZE; | |
146 | //data[p++] = attribList[arg++]; | |
147 | break; | |
148 | ||
149 | case WX_GL_MIN_ALPHA: | |
150 | data[p++] = NSOpenGLPFAAlphaSize; | |
7ac5e1c9 | 151 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; |
0f9b48d1 SC |
152 | break; |
153 | ||
154 | case WX_GL_DEPTH_SIZE: | |
155 | data[p++] = NSOpenGLPFADepthSize; | |
7ac5e1c9 | 156 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; |
0f9b48d1 SC |
157 | break; |
158 | ||
159 | case WX_GL_STENCIL_SIZE: | |
160 | data[p++] = NSOpenGLPFAStencilSize; | |
7ac5e1c9 | 161 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; |
0f9b48d1 SC |
162 | break; |
163 | ||
164 | case WX_GL_MIN_ACCUM_RED: | |
165 | data[p++] = NSOpenGLPFAAccumSize; | |
7ac5e1c9 | 166 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; |
0f9b48d1 SC |
167 | break; |
168 | ||
169 | case WX_GL_MIN_ACCUM_GREEN: | |
170 | //data[p++] = AGL_ACCUM_GREEN_SIZE; | |
171 | //data[p++] = attribList[arg++]; | |
172 | break; | |
173 | ||
174 | case WX_GL_MIN_ACCUM_BLUE: | |
175 | //data[p++] = AGL_ACCUM_BLUE_SIZE; | |
176 | //data[p++] = attribList[arg++]; | |
177 | break; | |
178 | ||
179 | case WX_GL_MIN_ACCUM_ALPHA: | |
180 | //data[p++] = AGL_ACCUM_ALPHA_SIZE; | |
181 | //data[p++] = attribList[arg++]; | |
182 | break; | |
183 | ||
184 | case WX_GL_SAMPLE_BUFFERS: | |
185 | if ( !wxGLCanvas::IsAGLMultiSampleAvailable() ) | |
186 | { | |
187 | if ( !attribList[arg++] ) | |
188 | break; | |
189 | ||
190 | return false; | |
191 | } | |
192 | ||
193 | data[p++] = NSOpenGLPFASampleBuffers; | |
7ac5e1c9 | 194 | if ( (data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]) == true ) |
0f9b48d1 SC |
195 | { |
196 | // don't use software fallback | |
197 | data[p++] = NSOpenGLPFANoRecovery; | |
198 | } | |
199 | break; | |
200 | ||
201 | case WX_GL_SAMPLES: | |
202 | if ( !wxGLCanvas::IsAGLMultiSampleAvailable() ) | |
203 | { | |
204 | if ( !attribList[arg++] ) | |
205 | break; | |
206 | ||
207 | return false; | |
208 | } | |
209 | ||
210 | data[p++] = NSOpenGLPFASamples; | |
7ac5e1c9 | 211 | data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]; |
0f9b48d1 SC |
212 | break; |
213 | } | |
214 | } | |
215 | ||
216 | data[p] = (NSOpenGLPixelFormatAttribute)nil; | |
217 | ||
218 | attribs = data; | |
219 | } | |
220 | ||
7ac5e1c9 | 221 | return [[NSOpenGLPixelFormat alloc] initWithAttributes:(NSOpenGLPixelFormatAttribute*) attribs]; |
0f9b48d1 SC |
222 | } |
223 | ||
224 | bool wxGLContext::SetCurrent(const wxGLCanvas& win) const | |
225 | { | |
226 | if ( !m_glContext ) | |
227 | return false; | |
228 | ||
229 | [m_glContext setView: win.GetHandle() ]; | |
524c47aa | 230 | [m_glContext update]; |
0f9b48d1 SC |
231 | |
232 | [m_glContext makeCurrentContext]; | |
233 | ||
234 | return true; | |
235 | } | |
236 | ||
237 | @interface wxNSCustomOpenGLView : NSView | |
238 | { | |
0f9b48d1 SC |
239 | NSOpenGLContext* context; |
240 | } | |
241 | ||
0f9b48d1 SC |
242 | @end |
243 | ||
244 | @implementation wxNSCustomOpenGLView | |
245 | ||
4dd9fdf8 | 246 | + (void)initialize |
0f9b48d1 | 247 | { |
4dd9fdf8 SC |
248 | static BOOL initialized = NO; |
249 | if (!initialized) | |
250 | { | |
251 | initialized = YES; | |
252 | wxOSXCocoaClassAddWXMethods( self ); | |
253 | } | |
0f9b48d1 SC |
254 | } |
255 | ||
0f9b48d1 SC |
256 | - (BOOL)isOpaque |
257 | { | |
258 | return YES; | |
259 | } | |
260 | ||
261 | @end | |
262 | ||
263 | bool wxGLCanvas::Create(wxWindow *parent, | |
264 | wxWindowID id, | |
265 | const wxPoint& pos, | |
266 | const wxSize& size, | |
267 | long style, | |
268 | const wxString& name, | |
269 | const int *attribList, | |
270 | const wxPalette& WXUNUSED(palette)) | |
271 | { | |
272 | m_glFormat = WXGLChoosePixelFormat(attribList); | |
273 | if ( !m_glFormat ) | |
274 | return false; | |
275 | ||
524c47aa | 276 | // m_macIsUserPane = false ; |
0f9b48d1 SC |
277 | |
278 | if ( !wxWindow::Create(parent, id, pos, size, style, name) ) | |
279 | return false; | |
280 | ||
524c47aa | 281 | /* |
0f9b48d1 SC |
282 | NSRect r = wxOSXGetFrameForControl( this, pos , size ) ; |
283 | wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r]; | |
0f9b48d1 | 284 | m_peer = new wxWidgetCocoaImpl( this, v ); |
0f9b48d1 SC |
285 | |
286 | MacPostControlCreate(pos, size) ; | |
524c47aa SC |
287 | */ |
288 | return true; | |
289 | } | |
290 | ||
524c47aa SC |
291 | wxGLCanvas::~wxGLCanvas() |
292 | { | |
293 | if ( m_glFormat ) | |
294 | WXGLDestroyPixelFormat(m_glFormat); | |
295 | } | |
296 | ||
297 | ||
0f9b48d1 | 298 | #endif // wxUSE_GLCANVAS |