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 bool WXGLSetCurrentContext(WXGLContext context)
66 [context makeCurrentContext];
71 void WXGLDestroyPixelFormat( WXGLPixelFormat pixelFormat )
75 [pixelFormat release];
80 WXGLPixelFormat WXGLChoosePixelFormat(const int *attribList)
82 NSOpenGLPixelFormatAttribute data[512];
83 const NSOpenGLPixelFormatAttribute defaultAttribs[] =
85 NSOpenGLPFADoubleBuffer,
86 NSOpenGLPFAMinimumPolicy,
87 NSOpenGLPFAColorSize,(NSOpenGLPixelFormatAttribute)8,
88 NSOpenGLPFAAlphaSize,(NSOpenGLPixelFormatAttribute)0,
89 NSOpenGLPFADepthSize,(NSOpenGLPixelFormatAttribute)8,
90 (NSOpenGLPixelFormatAttribute)nil
93 const NSOpenGLPixelFormatAttribute *attribs;
96 attribs = defaultAttribs;
101 data[p++] = NSOpenGLPFAMinimumPolicy; // make _SIZE tags behave more like GLX
103 for ( unsigned arg = 0; attribList[arg] !=0 && p < WXSIZEOF(data); )
105 switch ( attribList[arg++] )
108 //data[p++] = AGL_RGBA;
111 case WX_GL_BUFFER_SIZE:
112 //data[p++] = AGL_BUFFER_SIZE;
113 //data[p++] = attribList[arg++];
117 //data[p++]=AGL_LEVEL;
118 //data[p++]=attribList[arg++];
121 case WX_GL_DOUBLEBUFFER:
122 data[p++] = NSOpenGLPFADoubleBuffer;
126 data[p++] = NSOpenGLPFAStereo;
129 case WX_GL_AUX_BUFFERS:
130 data[p++] = NSOpenGLPFAAuxBuffers;
131 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
135 data[p++] = NSOpenGLPFAColorSize;
136 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
139 case WX_GL_MIN_GREEN:
140 //data[p++] = AGL_GREEN_SIZE;
141 //data[p++] = attribList[arg++];
145 //data[p++] = AGL_BLUE_SIZE;
146 //data[p++] = attribList[arg++];
149 case WX_GL_MIN_ALPHA:
150 data[p++] = NSOpenGLPFAAlphaSize;
151 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
154 case WX_GL_DEPTH_SIZE:
155 data[p++] = NSOpenGLPFADepthSize;
156 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
159 case WX_GL_STENCIL_SIZE:
160 data[p++] = NSOpenGLPFAStencilSize;
161 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
164 case WX_GL_MIN_ACCUM_RED:
165 data[p++] = NSOpenGLPFAAccumSize;
166 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
169 case WX_GL_MIN_ACCUM_GREEN:
170 //data[p++] = AGL_ACCUM_GREEN_SIZE;
171 //data[p++] = attribList[arg++];
174 case WX_GL_MIN_ACCUM_BLUE:
175 //data[p++] = AGL_ACCUM_BLUE_SIZE;
176 //data[p++] = attribList[arg++];
179 case WX_GL_MIN_ACCUM_ALPHA:
180 //data[p++] = AGL_ACCUM_ALPHA_SIZE;
181 //data[p++] = attribList[arg++];
184 case WX_GL_SAMPLE_BUFFERS:
185 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
187 if ( !attribList[arg++] )
193 data[p++] = NSOpenGLPFASampleBuffers;
194 if ( (data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]) == true )
196 // don't use software fallback
197 data[p++] = NSOpenGLPFANoRecovery;
202 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
204 if ( !attribList[arg++] )
210 data[p++] = NSOpenGLPFASamples;
211 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
216 data[p] = (NSOpenGLPixelFormatAttribute)nil;
221 return [[NSOpenGLPixelFormat alloc] initWithAttributes:(NSOpenGLPixelFormatAttribute*) attribs];
224 @interface wxNSCustomOpenGLView : NSView
226 NSOpenGLContext* context;
231 @implementation wxNSCustomOpenGLView
235 static BOOL initialized = NO;
239 wxOSXCocoaClassAddWXMethods( self );
250 bool wxGLCanvas::Create(wxWindow *parent,
255 const wxString& name,
256 const int *attribList,
257 const wxPalette& WXUNUSED(palette))
259 m_glFormat = WXGLChoosePixelFormat(attribList);
263 // m_macIsUserPane = false ;
265 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
269 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
270 wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r];
271 m_peer = new wxWidgetCocoaImpl( this, v );
273 MacPostControlCreate(pos, size) ;
278 wxGLCanvas::~wxGLCanvas()
281 WXGLDestroyPixelFormat(m_glFormat);
284 bool wxGLCanvas::SwapBuffers()
286 WXGLContext context = WXGLGetCurrentContext();
287 wxCHECK_MSG(context, false, wxT("should have current context"));
289 [context flushBuffer];
294 bool wxGLContext::SetCurrent(const wxGLCanvas& win) const
299 [m_glContext setView: win.GetHandle() ];
300 [m_glContext update];
302 [m_glContext makeCurrentContext];
307 #endif // wxUSE_GLCANVAS