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 NSOpenGLPFAMinimumPolicy,
81 NSOpenGLPFAColorSize,8,
82 NSOpenGLPFAAlphaSize,0,
83 NSOpenGLPFADepthSize,8,
84 (NSOpenGLPixelFormatAttribute)nil
87 const NSOpenGLPixelFormatAttribute *attribs;
90 attribs = defaultAttribs;
95 data[p++] = NSOpenGLPFAMinimumPolicy; // make _SIZE tags behave more like GLX
97 for ( unsigned arg = 0; attribList[arg] !=0 && p < WXSIZEOF(data); )
99 switch ( attribList[arg++] )
102 //data[p++] = AGL_RGBA;
105 case WX_GL_BUFFER_SIZE:
106 //data[p++] = AGL_BUFFER_SIZE;
107 //data[p++] = attribList[arg++];
111 //data[p++]=AGL_LEVEL;
112 //data[p++]=attribList[arg++];
115 case WX_GL_DOUBLEBUFFER:
116 data[p++] = NSOpenGLPFADoubleBuffer;
120 data[p++] = NSOpenGLPFAStereo;
123 case WX_GL_AUX_BUFFERS:
124 data[p++] = NSOpenGLPFAAuxBuffers;
125 data[p++] = attribList[arg++];
129 data[p++] = NSOpenGLPFAColorSize;
130 data[p++] = attribList[arg++];
133 case WX_GL_MIN_GREEN:
134 //data[p++] = AGL_GREEN_SIZE;
135 //data[p++] = attribList[arg++];
139 //data[p++] = AGL_BLUE_SIZE;
140 //data[p++] = attribList[arg++];
143 case WX_GL_MIN_ALPHA:
144 data[p++] = NSOpenGLPFAAlphaSize;
145 data[p++] = attribList[arg++];
148 case WX_GL_DEPTH_SIZE:
149 data[p++] = NSOpenGLPFADepthSize;
150 data[p++] = attribList[arg++];
153 case WX_GL_STENCIL_SIZE:
154 data[p++] = NSOpenGLPFAStencilSize;
155 data[p++] = attribList[arg++];
158 case WX_GL_MIN_ACCUM_RED:
159 data[p++] = NSOpenGLPFAAccumSize;
160 data[p++] = attribList[arg++];
163 case WX_GL_MIN_ACCUM_GREEN:
164 //data[p++] = AGL_ACCUM_GREEN_SIZE;
165 //data[p++] = attribList[arg++];
168 case WX_GL_MIN_ACCUM_BLUE:
169 //data[p++] = AGL_ACCUM_BLUE_SIZE;
170 //data[p++] = attribList[arg++];
173 case WX_GL_MIN_ACCUM_ALPHA:
174 //data[p++] = AGL_ACCUM_ALPHA_SIZE;
175 //data[p++] = attribList[arg++];
178 case WX_GL_SAMPLE_BUFFERS:
179 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
181 if ( !attribList[arg++] )
187 data[p++] = NSOpenGLPFASampleBuffers;
188 if ( (data[p++] = attribList[arg++]) == true )
190 // don't use software fallback
191 data[p++] = NSOpenGLPFANoRecovery;
196 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
198 if ( !attribList[arg++] )
204 data[p++] = NSOpenGLPFASamples;
205 data[p++] = attribList[arg++];
210 data[p] = (NSOpenGLPixelFormatAttribute)nil;
215 return [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
218 bool wxGLContext::SetCurrent(const wxGLCanvas& win) const
223 [m_glContext setView: win.GetHandle() ];
224 [m_glContext update];
226 [m_glContext makeCurrentContext];
231 @interface wxNSCustomOpenGLView : NSView
234 NSOpenGLContext* context;
237 - (id)initWithFrame:(NSRect)frame;
238 - (void)setImplementation: (wxWidgetImpl *) theImplementation;
239 - (wxWidgetImpl*) implementation;
244 @implementation wxNSCustomOpenGLView
246 - (id)initWithFrame:(NSRect)frame
248 [super initWithFrame:frame];
253 - (void)setImplementation: (wxWidgetImpl *) theImplementation
255 impl = theImplementation;
258 - (wxWidgetImpl*) implementation
275 bool wxGLCanvas::Create(wxWindow *parent,
280 const wxString& name,
281 const int *attribList,
282 const wxPalette& WXUNUSED(palette))
284 m_glFormat = WXGLChoosePixelFormat(attribList);
288 // m_macIsUserPane = false ;
290 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
294 NSView* sv = (parent->GetHandle() );
296 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
297 wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r];
299 m_peer = new wxWidgetCocoaImpl( this, v );
300 [v setImplementation:m_peer];
302 MacPostControlCreate(pos, size) ;
307 wxGLCanvas::~wxGLCanvas()
310 WXGLDestroyPixelFormat(m_glFormat);
314 #endif // wxUSE_GLCANVAS