fix invalid dt at start on iOS release mode (#18041)

This commit is contained in:
minggo 2017-07-10 10:06:25 +08:00 committed by GitHub
parent 74a6fd3293
commit e7c69e253a
1 changed files with 20 additions and 7 deletions

View File

@ -26,6 +26,8 @@
#include "platform/CCPlatformConfig.h" #include "platform/CCPlatformConfig.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
#include <mach/mach_time.h>
#import "platform/ios/CCDirectorCaller-ios.h" #import "platform/ios/CCDirectorCaller-ios.h"
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
@ -51,7 +53,7 @@ static id s_sharedDirectorCaller;
{ {
if (s_sharedDirectorCaller == nil) if (s_sharedDirectorCaller == nil)
{ {
s_sharedDirectorCaller = [CCDirectorCaller new]; s_sharedDirectorCaller = [[CCDirectorCaller alloc] init];
} }
return s_sharedDirectorCaller; return s_sharedDirectorCaller;
@ -64,20 +66,17 @@ static id s_sharedDirectorCaller;
s_sharedDirectorCaller = nil; s_sharedDirectorCaller = nil;
} }
-(void) alloc
{
interval = 1;
}
- (instancetype)init - (instancetype)init
{ {
self = [super init]; if (self = [super init])
if (self)
{ {
isAppActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive; isAppActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(appDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil]; [nc addObserver:self selector:@selector(appDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
[nc addObserver:self selector:@selector(appDidBecomeInactive) name:UIApplicationWillResignActiveNotification object:nil]; [nc addObserver:self selector:@selector(appDidBecomeInactive) name:UIApplicationWillResignActiveNotification object:nil];
self.interval = 1;
} }
return self; return self;
} }
@ -104,6 +103,10 @@ static id s_sharedDirectorCaller;
// Director::setAnimationInterval() is called, we should invalidate it first // Director::setAnimationInterval() is called, we should invalidate it first
[self stopMainLoop]; [self stopMainLoop];
// initialize initLastDisplayTime, or the dt of of first frame is invalid
// should init before start displayLink
[self initLastDisplayTime];
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)]; displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
[displayLink setFrameInterval: self.interval]; [displayLink setFrameInterval: self.interval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
@ -143,6 +146,16 @@ static id s_sharedDirectorCaller;
} }
} }
-(void)initLastDisplayTime
{
struct mach_timebase_info timeBaseInfo;
mach_timebase_info(&timeBaseInfo);
CGFloat clockFrequency = (CGFloat)timeBaseInfo.denom / (CGFloat)timeBaseInfo.numer;
clockFrequency *= 1000000000.0;
// convert absolute time to seconds and should minus one frame time interval
lastDisplayTime = (mach_absolute_time() / clockFrequency) - ((1.0 / 60) * self.interval);
}
@end @end
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_IOS #endif // CC_TARGET_PLATFORM == CC_PLATFORM_IOS