Sample Code - DDCustomOpenGLView
I've been working on a project that required an OpenGL view with double buffering and full screen support. Unfortunately, this means I needed to abandon the comfort of NSOpenGLView. I also wanted to use a Core Video display link to drive the animation, instead of an NSTimer. Due to some difficulties, I ended up pulling out this code from the project to work on it in isolation. Now that it's working, I think this code could be useful to others, so I'm releasing it in the tradition of Apple's sample code. I'm sure it's not perfect, so if you come across any improvements or areas where I did something really dumb, feel free to let me know. Without further ado, grab the tarball, and here's the readme:
This sample code demonstrates how to create a custom OpenGL NSView subclass that can enter full screen mode and use a Core Video display link to drive the animation. It is sort of a mash-up of the Custom Cocoa OpenGL the OpenGL game template (which seems to have been removed) sample code, with the addition of Core Video. The view uses two OpenGL contexts, one for windowed mode and one for full screen mode. The full screen context is shared with the windowed context, so textures created for the windowed mode are available in the full screen mode. This code also shows how to create textures using Core Video. The DDCustomOpenGLView class may be used in other projects by subclassing.
The code is released under an MIT license.
Some things I learned:
Don't use the OpenGL lock around the display link. This can cause deadlocks, as the display link has it's own mutex.
OpenGL textures loaded from an Core Graphics image need to be flipped.
For double-buffered contexts, call [NSOpenGLContext flushBuffer], and for single-buffered contexts, call glFlush().
Document Revision History
Date | Notes |
---|---|
2006-10-23 | Initial release. |