I'm really tearing my head out on this one!
I'm subclassing NSOpenGLView to do some animations on the view. When i say animations, i mean that am moving some images from left to right of the view and so on.
This is what i do
a) Initialise the OpenGL system Code:
- (id)initWithFrame:(NSRect)frame
{
NSOpenGLPixelFormatAttribute attrs[] = {
NSOpenGLPFANoRecovery, // Enable automatic use of OpenGL "share" contexts.
NSOpenGLPFAColorSize, 24,
NSOpenGLPFAAlphaSize, 8,
NSOpenGLPFADepthSize, 16,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
0
};
// Create our pixel format.
NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
self = [super initWithFrame:frame pixelFormat:pixelFormat];
return self;
}
// Synchronize buffer swaps with vertical refresh rate
- (void)prepareOpenGL
{
GLint swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
}
b) Setup the Timers on Start
Code:
// Put our timer in -awakeFromNib, so it can start up right from the beginning
-(void)awakeFromNib
{
if( gameTimer != nil )
[gameTimer invalidate];
gameTimer = [NSTimer timerWithTimeInterval:0.02 //time interval
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:gameTimer
forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer:gameTimer
forMode:NSEventTrackingRunLoopMode]; //Ensure timer fires during resize*/
}
// Timer callback method
- (void)timerFired:(id)sender
{
//The timer fires this method every second
currentTime ++;
// All we do here is tell the display it needs a refresh
[self setNeedsDisplay:YES];
}
c) Animate my stuff in drawRect
Code:
- (void)drawRect:(NSRect)rect
{
[self animateFrame:rect];
// the correct way to do double buffering is this:
[[self openGLContext] flushBuffer];
}
d) The animateFrame method just draws images in various rectangle positions
Code:
[curImage drawInRect:targetRect
fromRect:sourceRect
operation:NSCompositeSourceOver
fraction:1.0f];
So here is the problem
When i start the app - i can see that time timer is being fired, drawRect is being invoked and the images are being drawn.
However i can only see the animation of the images, when i drag the window and move the window.. When the window is still the images just stay frozen. When i move the window - i see the images are moving... Or even if i get the window out of focus and back in focus - i can see the image changes positions...
I feel that the OpenGLView is not painting itself when static... I dont know what else is to be done...
Do i have to invoke - [[self openGLContext] flushBuffer]; How do i ensure the View is getting painted always?
Can someone shed light on what is going on here or what have i missed?
Appreciate some help. Thanks in advance! KamyFC
Ok i started to see the animations - once i replaced NSOpenGLView by NSView.
Maybe drawing images is not an OpenGL task and can be accomplished by the simpler NSView.