Okay, I am in over my head again...
I want to do a very simple animation in a subclass of NSOpenGLView. The trick is that I do not want it to "tear." To prevent this I need to synchronize the drawing with the screen refresh. How do I do this?
There is this on Apple's website:
------------------------------------------------------------
VBL syncing
Synchronize the frame rate of my application to the monitor's refresh rate (VBL syncing) can be enabled with thefollowing:
GLint swapInt = 1;
AGLSetInteger(aglContext, AGL_SWAP_INTERVAL, &swapInt);
Note that this will lock your frame rate to a divisible integer of your monitor's current refresh rate.
...
Frame Tears
Frame tears are a synchronization issue unrelated to swap speeds, contrary to popular belief. A tear occurs when thebuffers are swapped while the electron beam is in the middle of painting on the screen. VBL synching should solve this problem.
--------------------------------------------------------
The problem is that the context owned by a NSOpenGLView is a NSOpenGLContext not an AGLContext. As a result, it doesn't complile if you do something simple minded like this:
GLint swapInt = 1;
AGLSetInteger([self openGLContext], AGL_SWAP_INTERVAL, &swapInt);
I tried:
const long swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:AGL_SWAP_INTERVAL];
This compiles but it doesn't work (tears). This is the same:
const long swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
I think the problem is that I am not dealing with the contexts correctly (ie, I am setting the swap interval for one context but drawing into another). Unfortunately, I can't find any documentation that tells me how to do it correctly.
Here is the relevant code:
- (void)drawRect:NSRect)rect
{
int n;
const long swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
glClearColor(0.0, 0.0, 0.0, 0.0);
for(n = 0; n < 60; n++){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.5+(n-30)/70.0, -0.5);
glVertex2f(-0.5+(n-30)/70.0, 0.5);
glVertex2f(0.5+(n-30)/70.0, 0.5);
glVertex2f(0.5+(n-30)/70.0, -0.5);
glEnd();
glFlush();
}
}