1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/glcanvas.mm
3 // Purpose: wxGLCanvas, for using OpenGL with wxWidgets under Macintosh
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
21 #if defined(__BORLANDC__)
27 #include "wx/glcanvas.h"
32 #include "wx/settings.h"
35 #include "wx/osx/private.h"
37 WXGLContext WXGLCreateContext( WXGLPixelFormat pixelFormat, WXGLContext shareContext )
39 WXGLContext context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext: shareContext];
42 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 bool WXGLSetCurrentContext(WXGLContext context)
67 [context makeCurrentContext];
72 void WXGLDestroyPixelFormat( WXGLPixelFormat pixelFormat )
76 [pixelFormat release];
81 WXGLPixelFormat WXGLChoosePixelFormat(const int *attribList)
83 NSOpenGLPixelFormatAttribute data[512];
84 const NSOpenGLPixelFormatAttribute defaultAttribs[] =
86 NSOpenGLPFADoubleBuffer,
87 NSOpenGLPFAMinimumPolicy,
88 NSOpenGLPFAColorSize,(NSOpenGLPixelFormatAttribute)8,
89 NSOpenGLPFAAlphaSize,(NSOpenGLPixelFormatAttribute)0,
90 NSOpenGLPFADepthSize,(NSOpenGLPixelFormatAttribute)8,
91 NSOpenGLPFAAccelerated, // use hardware accelerated context
92 (NSOpenGLPixelFormatAttribute)nil
95 const NSOpenGLPixelFormatAttribute *attribs;
98 attribs = defaultAttribs;
103 data[p++] = NSOpenGLPFAMinimumPolicy; // make _SIZE tags behave more like GLX
104 data[p++] = NSOpenGLPFAAccelerated; // use hardware accelerated context
106 for ( unsigned arg = 0; attribList[arg] !=0 && p < WXSIZEOF(data); )
108 switch ( attribList[arg++] )
111 //data[p++] = AGL_RGBA;
114 case WX_GL_BUFFER_SIZE:
115 //data[p++] = AGL_BUFFER_SIZE;
116 //data[p++] = attribList[arg++];
120 //data[p++]=AGL_LEVEL;
121 //data[p++]=attribList[arg++];
124 case WX_GL_DOUBLEBUFFER:
125 data[p++] = NSOpenGLPFADoubleBuffer;
129 data[p++] = NSOpenGLPFAStereo;
132 case WX_GL_AUX_BUFFERS:
133 data[p++] = NSOpenGLPFAAuxBuffers;
134 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
138 data[p++] = NSOpenGLPFAColorSize;
139 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
142 case WX_GL_MIN_GREEN:
143 //data[p++] = AGL_GREEN_SIZE;
144 //data[p++] = attribList[arg++];
148 //data[p++] = AGL_BLUE_SIZE;
149 //data[p++] = attribList[arg++];
152 case WX_GL_MIN_ALPHA:
153 data[p++] = NSOpenGLPFAAlphaSize;
154 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
157 case WX_GL_DEPTH_SIZE:
158 data[p++] = NSOpenGLPFADepthSize;
159 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
162 case WX_GL_STENCIL_SIZE:
163 data[p++] = NSOpenGLPFAStencilSize;
164 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
167 case WX_GL_MIN_ACCUM_RED:
168 data[p++] = NSOpenGLPFAAccumSize;
169 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
172 case WX_GL_MIN_ACCUM_GREEN:
173 //data[p++] = AGL_ACCUM_GREEN_SIZE;
174 //data[p++] = attribList[arg++];
177 case WX_GL_MIN_ACCUM_BLUE:
178 //data[p++] = AGL_ACCUM_BLUE_SIZE;
179 //data[p++] = attribList[arg++];
182 case WX_GL_MIN_ACCUM_ALPHA:
183 //data[p++] = AGL_ACCUM_ALPHA_SIZE;
184 //data[p++] = attribList[arg++];
187 case WX_GL_SAMPLE_BUFFERS:
188 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
190 if ( !attribList[arg++] )
196 data[p++] = NSOpenGLPFASampleBuffers;
197 if ( (data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]) == true )
199 // don't use software fallback
200 data[p++] = NSOpenGLPFANoRecovery;
205 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
207 if ( !attribList[arg++] )
213 data[p++] = NSOpenGLPFASamples;
214 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
219 data[p] = (NSOpenGLPixelFormatAttribute)nil;
224 return [[NSOpenGLPixelFormat alloc] initWithAttributes:(NSOpenGLPixelFormatAttribute*) attribs];
227 @interface wxNSCustomOpenGLView : NSView
229 NSOpenGLContext* context;
234 @implementation wxNSCustomOpenGLView
238 static BOOL initialized = NO;
242 wxOSXCocoaClassAddWXMethods( self );
253 bool wxGLCanvas::Create(wxWindow *parent,
258 const wxString& name,
259 const int *attribList,
260 const wxPalette& WXUNUSED(palette))
262 m_glFormat = WXGLChoosePixelFormat(attribList);
268 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
272 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
273 wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r];
274 m_peer = new wxWidgetCocoaImpl( this, v );
276 MacPostControlCreate(pos, size) ;
281 wxGLCanvas::~wxGLCanvas()
284 WXGLDestroyPixelFormat(m_glFormat);
287 bool wxGLCanvas::SwapBuffers()
289 WXGLContext context = WXGLGetCurrentContext();
290 wxCHECK_MSG(context, false, wxT("should have current context"));
292 [context flushBuffer];
297 bool wxGLContext::SetCurrent(const wxGLCanvas& win) const
302 [m_glContext setView: win.GetHandle() ];
303 [m_glContext update];
305 [m_glContext makeCurrentContext];
310 #endif // wxUSE_GLCANVAS