Skip to content

Instantly share code, notes, and snippets.

@reklis
Created January 9, 2010 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reklis/272691 to your computer and use it in GitHub Desktop.
Save reklis/272691 to your computer and use it in GitHub Desktop.
#pragma mark -
#pragma mark DisplayLinkDirector
// Allows building DisplayLinkDirector for pre-3.1 SDKS
// without getting compiler warnings.
@interface NSObject(CADisplayLink)
+ (id) displayLinkWithTarget:(id)arg1 selector:(SEL)arg2;
- (void) addToRunLoop:(id)arg1 forMode:(id)arg2;
- (void) setFrameInterval:(int)interval;
- (void) invalidate;
@end
@implementation CCDisplayLinkDirector
- (void)setAnimationInterval:(NSTimeInterval)interval
{
animationInterval = interval;
if(displayLink){
[self stopAnimation];
[self startAnimation];
}
}
- (void) startAnimation
{
if ( gettimeofday( &lastUpdate, NULL) != 0 ) {
CCLOG(@"cocos2d: DisplayLinkDirector: Error on gettimeofday");
}
// approximate frame rate
// assumes device refreshes at 60 fps
int frameInterval = (int) floor(animationInterval * 60.0f);
CCLOG(@"cocos2d: Frame interval: %d", frameInterval);
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(preMainLoop:)];
[displayLink setFrameInterval:frameInterval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void) preMainLoop:(id)sender
{
[self mainLoop];
}
- (void) stopAnimation
{
[displayLink invalidate];
displayLink = nil;
}
-(void) dealloc
{
[displayLink release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment