1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/glcanvas.mm
3 // Purpose: wxGLCanvas, for using OpenGL with wxWidgets under Macintosh
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
22 #if defined(__BORLANDC__)
28 #include "wx/glcanvas.h"
33 #include "wx/settings.h"
36 #include "wx/osx/private.h"
38 WXGLContext WXGLCreateContext( WXGLPixelFormat pixelFormat, WXGLContext shareContext )
40 WXGLContext context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext: shareContext];
43 wxFAIL_MSG("NSOpenGLContext creation failed");
48 void WXGLDestroyContext( WXGLContext context )
56 void WXGLSwapBuffers( WXGLContext context )
58 [context flushBuffer];
61 WXGLContext WXGLGetCurrentContext()
63 return [NSOpenGLContext currentContext];
66 bool WXGLSetCurrentContext(WXGLContext context)
68 [context makeCurrentContext];
73 void WXGLDestroyPixelFormat( WXGLPixelFormat pixelFormat )
77 [pixelFormat release];
82 WXGLPixelFormat WXGLChoosePixelFormat(const int *attribList)
84 NSOpenGLPixelFormatAttribute data[512];
85 const NSOpenGLPixelFormatAttribute defaultAttribs[] =
87 NSOpenGLPFADoubleBuffer,
88 NSOpenGLPFAMinimumPolicy,
89 NSOpenGLPFAColorSize,(NSOpenGLPixelFormatAttribute)8,
90 NSOpenGLPFAAlphaSize,(NSOpenGLPixelFormatAttribute)0,
91 NSOpenGLPFADepthSize,(NSOpenGLPixelFormatAttribute)8,
92 (NSOpenGLPixelFormatAttribute)nil
95 const NSOpenGLPixelFormatAttribute *attribs;
98 attribs = defaultAttribs;
103 data[p++] = NSOpenGLPFAMinimumPolicy; // make _SIZE tags behave more like GLX
105 for ( unsigned arg = 0; attribList[arg] !=0 && p < WXSIZEOF(data); )
107 switch ( attribList[arg++] )
110 //data[p++] = AGL_RGBA;
113 case WX_GL_BUFFER_SIZE:
114 //data[p++] = AGL_BUFFER_SIZE;
115 //data[p++] = attribList[arg++];
119 //data[p++]=AGL_LEVEL;
120 //data[p++]=attribList[arg++];
123 case WX_GL_DOUBLEBUFFER:
124 data[p++] = NSOpenGLPFADoubleBuffer;
128 data[p++] = NSOpenGLPFAStereo;
131 case WX_GL_AUX_BUFFERS:
132 data[p++] = NSOpenGLPFAAuxBuffers;
133 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
137 data[p++] = NSOpenGLPFAColorSize;
138 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
141 case WX_GL_MIN_GREEN:
142 //data[p++] = AGL_GREEN_SIZE;
143 //data[p++] = attribList[arg++];
147 //data[p++] = AGL_BLUE_SIZE;
148 //data[p++] = attribList[arg++];
151 case WX_GL_MIN_ALPHA:
152 data[p++] = NSOpenGLPFAAlphaSize;
153 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
156 case WX_GL_DEPTH_SIZE:
157 data[p++] = NSOpenGLPFADepthSize;
158 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
161 case WX_GL_STENCIL_SIZE:
162 data[p++] = NSOpenGLPFAStencilSize;
163 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
166 case WX_GL_MIN_ACCUM_RED:
167 data[p++] = NSOpenGLPFAAccumSize;
168 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
171 case WX_GL_MIN_ACCUM_GREEN:
172 //data[p++] = AGL_ACCUM_GREEN_SIZE;
173 //data[p++] = attribList[arg++];
176 case WX_GL_MIN_ACCUM_BLUE:
177 //data[p++] = AGL_ACCUM_BLUE_SIZE;
178 //data[p++] = attribList[arg++];
181 case WX_GL_MIN_ACCUM_ALPHA:
182 //data[p++] = AGL_ACCUM_ALPHA_SIZE;
183 //data[p++] = attribList[arg++];
186 case WX_GL_SAMPLE_BUFFERS:
187 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
189 if ( !attribList[arg++] )
195 data[p++] = NSOpenGLPFASampleBuffers;
196 if ( (data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]) == true )
198 // don't use software fallback
199 data[p++] = NSOpenGLPFANoRecovery;
204 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
206 if ( !attribList[arg++] )
212 data[p++] = NSOpenGLPFASamples;
213 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
218 data[p] = (NSOpenGLPixelFormatAttribute)nil;
223 return [[NSOpenGLPixelFormat alloc] initWithAttributes:(NSOpenGLPixelFormatAttribute*) attribs];
226 @interface wxNSCustomOpenGLView : NSView
228 NSOpenGLContext* context;
233 @implementation wxNSCustomOpenGLView
237 static BOOL initialized = NO;
241 wxOSXCocoaClassAddWXMethods( self );
252 bool wxGLCanvas::Create(wxWindow *parent,
257 const wxString& name,
258 const int *attribList,
259 const wxPalette& WXUNUSED(palette))
261 m_glFormat = WXGLChoosePixelFormat(attribList);
267 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
271 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
272 wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r];
273 m_peer = new wxWidgetCocoaImpl( this, v );
275 MacPostControlCreate(pos, size) ;
280 wxGLCanvas::~wxGLCanvas()
283 WXGLDestroyPixelFormat(m_glFormat);
286 bool wxGLCanvas::SwapBuffers()
288 WXGLContext context = WXGLGetCurrentContext();
289 wxCHECK_MSG(context, false, wxT("should have current context"));
291 [context flushBuffer];
296 bool wxGLContext::SetCurrent(const wxGLCanvas& win) const
301 [m_glContext setView: win.GetHandle() ];
302 [m_glContext update];
304 [m_glContext makeCurrentContext];
309 #endif // wxUSE_GLCANVAS