OpenGL with Cocoa: Part 2
Following on from yesterdays entry, OpenGL with Cocoa: Part 1, this entry will expand upon that.
First of all, we need to add some new functions to our OpenGL view class, as well as a few attributes. "m_rectView" will be used to store the current window rectangle, whilst "m_timer" will be used for updating the window for redraw.
The "initWithFrame" function is called before the OpenGL context is setup, it is used to set the pixel format conditions for the context. The other functions are called by our own code.
I've image to the right shows the source for the new functions. Within the "initWithFrame" you can see we're requesting a format with a double buffer, accelerated hardware, 32 bit colour buffer, with 24 bit depth buffer and an 8 bit stencil buffer. We then call the same function within the NSOpenGLView class we're inheriting off.
The "setupAnimationTimerWithInterval" function sets up a timer object that fires "timerUpdate" at a specified interval. That function calls "setNeedsDisplay" to make the window redraw. The framerate can be killed if we draw in both the "timerUpdate" function and the "drawRect" function.
Within the "prepareOpenGL" function, we call the "setupAnimationTimerWithInterval". Now within the "drawRect" function, we call "resizeGL" which if the rectangle is different to a previously stored one, we make a call to glViewport() to update the destination bounds.
We also change the glFlush() call to a call to a call to "flushBuffer" call from the current OpenGL context, this is because we are now rendering with a double buffer instead of a single buffer.
You may have also noticed there is a call to glRotated(), since we don't call glLoadIdentity() every frame, this will rotate our rectangle, showing that our application is running and updating consistently.
If all goes well, you should have an output like the one in the right image (well at some point since it is rotating).
Well that is all for using OpenGL with Cocoa for the moment, the next part will cover basic keyboard input.
Download Source: CocoaGL Source (Part 2)
