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"
39 WXGLContext WXGLCreateContext( WXGLPixelFormat pixelFormat, WXGLContext shareContext )
41 WXGLContext context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext: shareContext];
43 wxFAIL_MSG("NSOpenGLContext creation failed");
47 void WXGLDestroyContext( WXGLContext context )
55 void WXGLSwapBuffers( WXGLContext context )
57 [context flushBuffer];
60 WXGLContext WXGLGetCurrentContext()
62 return [NSOpenGLContext currentContext];
65 void WXGLDestroyPixelFormat( WXGLPixelFormat pixelFormat )
69 [pixelFormat release];
74 WXGLPixelFormat WXGLChoosePixelFormat(const int *attribList)
76 NSOpenGLPixelFormatAttribute data[512];
77 const NSOpenGLPixelFormatAttribute defaultAttribs[] =
79 NSOpenGLPFADoubleBuffer,
80 (NSOpenGLPixelFormatAttribute)nil
83 const NSOpenGLPixelFormatAttribute *attribs;
86 attribs = defaultAttribs;
91 data[p++] = NSOpenGLPFAMinimumPolicy; // make _SIZE tags behave more like GLX
93 for ( unsigned arg = 0; attribList[arg] !=0 && p < WXSIZEOF(data); )
95 switch ( attribList[arg++] )
98 //data[p++] = AGL_RGBA;
101 case WX_GL_BUFFER_SIZE:
102 //data[p++] = AGL_BUFFER_SIZE;
103 //data[p++] = attribList[arg++];
107 //data[p++]=AGL_LEVEL;
108 //data[p++]=attribList[arg++];
111 case WX_GL_DOUBLEBUFFER:
112 data[p++] = NSOpenGLPFADoubleBuffer;
116 data[p++] = NSOpenGLPFAStereo;
119 case WX_GL_AUX_BUFFERS:
120 data[p++] = NSOpenGLPFAAuxBuffers;
121 data[p++] = attribList[arg++];
125 data[p++] = NSOpenGLPFAColorSize;
126 data[p++] = attribList[arg++];
129 case WX_GL_MIN_GREEN:
130 //data[p++] = AGL_GREEN_SIZE;
131 //data[p++] = attribList[arg++];
135 //data[p++] = AGL_BLUE_SIZE;
136 //data[p++] = attribList[arg++];
139 case WX_GL_MIN_ALPHA:
140 data[p++] = NSOpenGLPFAAlphaSize;
141 data[p++] = attribList[arg++];
144 case WX_GL_DEPTH_SIZE:
145 data[p++] = NSOpenGLPFADepthSize;
146 data[p++] = attribList[arg++];
149 case WX_GL_STENCIL_SIZE:
150 data[p++] = NSOpenGLPFAStencilSize;
151 data[p++] = attribList[arg++];
154 case WX_GL_MIN_ACCUM_RED:
155 data[p++] = NSOpenGLPFAAccumSize;
156 data[p++] = attribList[arg++];
159 case WX_GL_MIN_ACCUM_GREEN:
160 //data[p++] = AGL_ACCUM_GREEN_SIZE;
161 //data[p++] = attribList[arg++];
164 case WX_GL_MIN_ACCUM_BLUE:
165 //data[p++] = AGL_ACCUM_BLUE_SIZE;
166 //data[p++] = attribList[arg++];
169 case WX_GL_MIN_ACCUM_ALPHA:
170 //data[p++] = AGL_ACCUM_ALPHA_SIZE;
171 //data[p++] = attribList[arg++];
174 case WX_GL_SAMPLE_BUFFERS:
175 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
177 if ( !attribList[arg++] )
183 data[p++] = NSOpenGLPFASampleBuffers;
184 if ( (data[p++] = attribList[arg++]) == true )
186 // don't use software fallback
187 data[p++] = NSOpenGLPFANoRecovery;
192 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
194 if ( !attribList[arg++] )
200 data[p++] = NSOpenGLPFASamples;
201 data[p++] = attribList[arg++];
206 data[p] = (NSOpenGLPixelFormatAttribute)nil;
211 return [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
214 bool wxGLContext::SetCurrent(const wxGLCanvas& win) const
219 [m_glContext setView: win.GetHandle() ];
221 [m_glContext makeCurrentContext];
226 @interface wxNSCustomOpenGLView : NSView
229 NSOpenGLContext* context;
232 - (id)initWithFrame:(NSRect)frame;
233 - (void)setImplementation: (wxWidgetImpl *) theImplementation;
234 - (wxWidgetImpl*) implementation;
239 @implementation wxNSCustomOpenGLView
241 - (id)initWithFrame:(NSRect)frame
243 [super initWithFrame:frame];
248 - (void)setImplementation: (wxWidgetImpl *) theImplementation
250 impl = theImplementation;
253 - (wxWidgetImpl*) implementation
270 bool wxGLCanvas::Create(wxWindow *parent,
275 const wxString& name,
276 const int *attribList,
277 const wxPalette& WXUNUSED(palette))
279 m_glFormat = WXGLChoosePixelFormat(attribList);
283 m_macIsUserPane = false ;
285 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
289 NSView* sv = (parent->GetHandle() );
291 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
292 wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r];
294 m_peer = new wxWidgetCocoaImpl( this, v );
295 [v setImplementation:m_peer];
297 MacPostControlCreate(pos, size) ;
302 #endif // wxUSE_GLCANVAS