1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/glcanvas.mm
3 // Purpose: wxGLCanvas, for using OpenGL with wxWidgets under Macintosh
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: glcanvas.cpp 54129 2008-06-11 19:30:52Z SC $
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];
42 wxFAIL_MSG("NSOpenGLContext creation failed");
46 void WXGLDestroyContext( WXGLContext context )
54 void WXGLSwapBuffers( WXGLContext context )
56 [context flushBuffer];
59 WXGLContext WXGLGetCurrentContext()
61 return [NSOpenGLContext currentContext];
64 void WXGLDestroyPixelFormat( WXGLPixelFormat pixelFormat )
68 [pixelFormat release];
73 WXGLPixelFormat WXGLChoosePixelFormat(const int *attribList)
75 NSOpenGLPixelFormatAttribute data[512];
76 const NSOpenGLPixelFormatAttribute defaultAttribs[] =
78 NSOpenGLPFADoubleBuffer,
79 NSOpenGLPFAMinimumPolicy,
80 NSOpenGLPFAColorSize,(NSOpenGLPixelFormatAttribute)8,
81 NSOpenGLPFAAlphaSize,(NSOpenGLPixelFormatAttribute)0,
82 NSOpenGLPFADepthSize,(NSOpenGLPixelFormatAttribute)8,
83 (NSOpenGLPixelFormatAttribute)nil
86 const NSOpenGLPixelFormatAttribute *attribs;
89 attribs = defaultAttribs;
94 data[p++] = NSOpenGLPFAMinimumPolicy; // make _SIZE tags behave more like GLX
96 for ( unsigned arg = 0; attribList[arg] !=0 && p < WXSIZEOF(data); )
98 switch ( attribList[arg++] )
101 //data[p++] = AGL_RGBA;
104 case WX_GL_BUFFER_SIZE:
105 //data[p++] = AGL_BUFFER_SIZE;
106 //data[p++] = attribList[arg++];
110 //data[p++]=AGL_LEVEL;
111 //data[p++]=attribList[arg++];
114 case WX_GL_DOUBLEBUFFER:
115 data[p++] = NSOpenGLPFADoubleBuffer;
119 data[p++] = NSOpenGLPFAStereo;
122 case WX_GL_AUX_BUFFERS:
123 data[p++] = NSOpenGLPFAAuxBuffers;
124 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
128 data[p++] = NSOpenGLPFAColorSize;
129 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
132 case WX_GL_MIN_GREEN:
133 //data[p++] = AGL_GREEN_SIZE;
134 //data[p++] = attribList[arg++];
138 //data[p++] = AGL_BLUE_SIZE;
139 //data[p++] = attribList[arg++];
142 case WX_GL_MIN_ALPHA:
143 data[p++] = NSOpenGLPFAAlphaSize;
144 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
147 case WX_GL_DEPTH_SIZE:
148 data[p++] = NSOpenGLPFADepthSize;
149 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
152 case WX_GL_STENCIL_SIZE:
153 data[p++] = NSOpenGLPFAStencilSize;
154 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
157 case WX_GL_MIN_ACCUM_RED:
158 data[p++] = NSOpenGLPFAAccumSize;
159 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
162 case WX_GL_MIN_ACCUM_GREEN:
163 //data[p++] = AGL_ACCUM_GREEN_SIZE;
164 //data[p++] = attribList[arg++];
167 case WX_GL_MIN_ACCUM_BLUE:
168 //data[p++] = AGL_ACCUM_BLUE_SIZE;
169 //data[p++] = attribList[arg++];
172 case WX_GL_MIN_ACCUM_ALPHA:
173 //data[p++] = AGL_ACCUM_ALPHA_SIZE;
174 //data[p++] = attribList[arg++];
177 case WX_GL_SAMPLE_BUFFERS:
178 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
180 if ( !attribList[arg++] )
186 data[p++] = NSOpenGLPFASampleBuffers;
187 if ( (data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]) == true )
189 // don't use software fallback
190 data[p++] = NSOpenGLPFANoRecovery;
195 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
197 if ( !attribList[arg++] )
203 data[p++] = NSOpenGLPFASamples;
204 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
209 data[p] = (NSOpenGLPixelFormatAttribute)nil;
214 return [[NSOpenGLPixelFormat alloc] initWithAttributes:(NSOpenGLPixelFormatAttribute*) attribs];
217 bool wxGLContext::SetCurrent(const wxGLCanvas& win) const
222 [m_glContext setView: win.GetHandle() ];
223 [m_glContext update];
225 [m_glContext makeCurrentContext];
230 @interface wxNSCustomOpenGLView : NSView
232 NSOpenGLContext* context;
237 @implementation wxNSCustomOpenGLView
241 static BOOL initialized = NO;
245 wxOSXCocoaClassAddWXMethods( self );
256 bool wxGLCanvas::Create(wxWindow *parent,
261 const wxString& name,
262 const int *attribList,
263 const wxPalette& WXUNUSED(palette))
265 m_glFormat = WXGLChoosePixelFormat(attribList);
269 // m_macIsUserPane = false ;
271 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
275 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
276 wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r];
277 m_peer = new wxWidgetCocoaImpl( this, v );
279 MacPostControlCreate(pos, size) ;
284 wxGLCanvas::~wxGLCanvas()
287 WXGLDestroyPixelFormat(m_glFormat);
291 #endif // wxUSE_GLCANVAS