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         (NSOpenGLPixelFormatAttribute)nil
 
  94     const NSOpenGLPixelFormatAttribute *attribs;
 
  97         attribs = defaultAttribs;
 
 102         data[p++] = NSOpenGLPFAMinimumPolicy; // make _SIZE tags behave more like GLX
 
 104         for ( unsigned arg = 0; attribList[arg] !=0 && p < WXSIZEOF(data); )
 
 106             switch ( attribList[arg++] )
 
 109                     //data[p++] = AGL_RGBA;
 
 112                 case WX_GL_BUFFER_SIZE:
 
 113                     //data[p++] = AGL_BUFFER_SIZE;
 
 114                     //data[p++] = attribList[arg++];
 
 118                     //data[p++]=AGL_LEVEL;
 
 119                     //data[p++]=attribList[arg++];
 
 122                 case WX_GL_DOUBLEBUFFER:
 
 123                     data[p++] = NSOpenGLPFADoubleBuffer;
 
 127                     data[p++] = NSOpenGLPFAStereo;
 
 130                 case WX_GL_AUX_BUFFERS:
 
 131                     data[p++] = NSOpenGLPFAAuxBuffers;
 
 132                     data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
 
 136                     data[p++] = NSOpenGLPFAColorSize;
 
 137                     data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
 
 140                 case WX_GL_MIN_GREEN:
 
 141                     //data[p++] = AGL_GREEN_SIZE;
 
 142                     //data[p++] = attribList[arg++];
 
 146                     //data[p++] = AGL_BLUE_SIZE;
 
 147                     //data[p++] = attribList[arg++];
 
 150                 case WX_GL_MIN_ALPHA:
 
 151                     data[p++] = NSOpenGLPFAAlphaSize;
 
 152                     data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
 
 155                 case WX_GL_DEPTH_SIZE:
 
 156                     data[p++] = NSOpenGLPFADepthSize;
 
 157                     data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
 
 160                 case WX_GL_STENCIL_SIZE:
 
 161                     data[p++] = NSOpenGLPFAStencilSize;
 
 162                     data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
 
 165                 case WX_GL_MIN_ACCUM_RED:
 
 166                     data[p++] = NSOpenGLPFAAccumSize;
 
 167                     data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
 
 170                 case WX_GL_MIN_ACCUM_GREEN:
 
 171                     //data[p++] = AGL_ACCUM_GREEN_SIZE;
 
 172                     //data[p++] = attribList[arg++];
 
 175                 case WX_GL_MIN_ACCUM_BLUE:
 
 176                     //data[p++] = AGL_ACCUM_BLUE_SIZE;
 
 177                     //data[p++] = attribList[arg++];
 
 180                 case WX_GL_MIN_ACCUM_ALPHA:
 
 181                     //data[p++] = AGL_ACCUM_ALPHA_SIZE;
 
 182                     //data[p++] = attribList[arg++];
 
 185                 case WX_GL_SAMPLE_BUFFERS:
 
 186                     if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
 
 188                         if ( !attribList[arg++] )
 
 194                     data[p++] = NSOpenGLPFASampleBuffers;
 
 195                     if ( (data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]) == true )
 
 197                         // don't use software fallback
 
 198                         data[p++] = NSOpenGLPFANoRecovery;
 
 203                     if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
 
 205                         if ( !attribList[arg++] )
 
 211                     data[p++] = NSOpenGLPFASamples;
 
 212                     data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
 
 217         data[p] = (NSOpenGLPixelFormatAttribute)nil;
 
 222     return [[NSOpenGLPixelFormat alloc] initWithAttributes:(NSOpenGLPixelFormatAttribute*) attribs];
 
 225 @interface wxNSCustomOpenGLView : NSView
 
 227     NSOpenGLContext* context;
 
 232 @implementation wxNSCustomOpenGLView
 
 236     static BOOL initialized = NO;
 
 240         wxOSXCocoaClassAddWXMethods( self );
 
 251 bool wxGLCanvas::Create(wxWindow *parent,
 
 256                         const wxString& name,
 
 257                         const int *attribList,
 
 258                         const wxPalette& WXUNUSED(palette))
 
 260     m_glFormat = WXGLChoosePixelFormat(attribList);
 
 266     if ( !wxWindow::Create(parent, id, pos, size, style, name) )
 
 270     NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
 
 271     wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r];
 
 272     m_peer = new wxWidgetCocoaImpl( this, v );
 
 274     MacPostControlCreate(pos, size) ;
 
 279 wxGLCanvas::~wxGLCanvas()
 
 282         WXGLDestroyPixelFormat(m_glFormat);
 
 285 bool wxGLCanvas::SwapBuffers()
 
 287     WXGLContext context = WXGLGetCurrentContext();
 
 288     wxCHECK_MSG(context, false, wxT("should have current context"));
 
 290     [context flushBuffer];
 
 295 bool wxGLContext::SetCurrent(const wxGLCanvas& win) const
 
 300     [m_glContext setView: win.GetHandle() ];
 
 301     [m_glContext update];
 
 303     [m_glContext makeCurrentContext];
 
 308 #endif // wxUSE_GLCANVAS