Created
January 9, 2010 03:32
-
-
Save reklis/272691 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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