]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/glcanvas.mm
PCH-less build fix after r59452
[wxWidgets.git] / src / osx / cocoa / glcanvas.mm
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 NSOpenGLContext* context;
233 }
234
235 @end
236
237 @implementation wxNSCustomOpenGLView
238
239 + (void)initialize
240 {
241 static BOOL initialized = NO;
242 if (!initialized)
243 {
244 initialized = YES;
245 wxOSXCocoaClassAddWXMethods( self );
246 }
247 }
248
249 - (BOOL)isOpaque
250 {
251 return YES;
252 }
253
254 @end
255
256 bool wxGLCanvas::Create(wxWindow *parent,
257 wxWindowID id,
258 const wxPoint& pos,
259 const wxSize& size,
260 long style,
261 const wxString& name,
262 const int *attribList,
263 const wxPalette& WXUNUSED(palette))
264 {
265 m_glFormat = WXGLChoosePixelFormat(attribList);
266 if ( !m_glFormat )
267 return false;
268
269 // m_macIsUserPane = false ;
270
271 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
272 return false;
273
274 /*
275 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
276 wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r];
277 m_peer = new wxWidgetCocoaImpl( this, v );
278
279 MacPostControlCreate(pos, size) ;
280 */
281 return true;
282 }
283
284 wxGLCanvas::~wxGLCanvas()
285 {
286 if ( m_glFormat )
287 WXGLDestroyPixelFormat(m_glFormat);
288 }
289
290
291 #endif // wxUSE_GLCANVAS