Hi Mac Experts,
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
b) Setup the Timers on Start
c) Animate my stuff in drawRect
d) The animateFrame method just draws images in various rectangle positions
------------------------------------------------------------------------
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...
Ideally i want the images to move even if the window is static!
Can someone shed light on what is going on here or what have i missed?
Appreciate some help.
Thanks in advance!
KamyFC
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...
Ideally i want the images to move even if the window is static!
Can someone shed light on what is going on here or what have i missed?
Appreciate some help.
Thanks in advance!
KamyFC