2010-11-13 11:34:49 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-01-07 11:25:07 +08:00
|
|
|
|
2010-11-13 11:34:49 +08:00
|
|
|
http://www.cocos2d-x.org
|
2014-01-07 11:25:07 +08:00
|
|
|
|
2010-11-13 11:34:49 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2014-01-07 11:25:07 +08:00
|
|
|
|
2010-11-13 11:34:49 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2014-01-07 11:25:07 +08:00
|
|
|
|
2010-11-13 11:34:49 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2017-07-10 10:06:25 +08:00
|
|
|
#include <mach/mach_time.h>
|
|
|
|
|
2016-03-21 20:12:58 +08:00
|
|
|
#import "platform/ios/CCDirectorCaller-ios.h"
|
2014-09-10 07:50:02 +08:00
|
|
|
|
2010-11-12 17:16:15 +08:00
|
|
|
#import <Foundation/Foundation.h>
|
2014-01-27 18:08:34 +08:00
|
|
|
#import <OpenGLES/EAGL.h>
|
2014-09-10 07:50:02 +08:00
|
|
|
|
|
|
|
#import "base/CCDirector.h"
|
2016-03-21 20:12:58 +08:00
|
|
|
#import "platform/ios/CCEAGLView-ios.h"
|
2010-11-12 17:16:15 +08:00
|
|
|
|
|
|
|
static id s_sharedDirectorCaller;
|
|
|
|
|
|
|
|
@interface NSObject(CADisplayLink)
|
|
|
|
+(id) displayLinkWithTarget: (id)arg1 selector:(SEL)arg2;
|
|
|
|
-(void) addToRunLoop: (id)arg1 forMode: (id)arg2;
|
2014-03-20 11:32:43 +08:00
|
|
|
-(void) setFrameInterval: (NSInteger)interval;
|
2010-11-12 17:16:15 +08:00
|
|
|
-(void) invalidate;
|
|
|
|
@end
|
|
|
|
|
2013-06-21 08:46:22 +08:00
|
|
|
@implementation CCDirectorCaller
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2011-01-21 17:33:15 +08:00
|
|
|
@synthesize interval;
|
|
|
|
|
2010-11-12 17:16:15 +08:00
|
|
|
+(id) sharedDirectorCaller
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (s_sharedDirectorCaller == nil)
|
|
|
|
{
|
2017-07-10 10:06:25 +08:00
|
|
|
s_sharedDirectorCaller = [[CCDirectorCaller alloc] init];
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s_sharedDirectorCaller;
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
+(void) destroy
|
|
|
|
{
|
2014-05-28 15:19:07 +08:00
|
|
|
[s_sharedDirectorCaller stopMainLoop];
|
2012-04-19 14:35:52 +08:00
|
|
|
[s_sharedDirectorCaller release];
|
2013-03-22 15:02:46 +08:00
|
|
|
s_sharedDirectorCaller = nil;
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
2011-01-21 17:33:15 +08:00
|
|
|
|
2016-02-13 16:17:55 +08:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
2017-07-10 10:06:25 +08:00
|
|
|
if (self = [super init])
|
2016-02-13 16:17:55 +08:00
|
|
|
{
|
|
|
|
isAppActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
|
|
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
|
|
|
[nc addObserver:self selector:@selector(appDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
|
|
|
|
[nc addObserver:self selector:@selector(appDidBecomeInactive) name:UIApplicationWillResignActiveNotification object:nil];
|
2017-07-10 10:06:25 +08:00
|
|
|
|
|
|
|
self.interval = 1;
|
2016-02-13 16:17:55 +08:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-11-12 17:16:15 +08:00
|
|
|
-(void) dealloc
|
|
|
|
{
|
2016-02-13 16:17:55 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2012-04-19 14:35:52 +08:00
|
|
|
[displayLink release];
|
|
|
|
[super dealloc];
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 16:17:55 +08:00
|
|
|
- (void)appDidBecomeActive
|
|
|
|
{
|
2017-07-18 14:01:53 +08:00
|
|
|
// initialize initLastDisplayTime, or the dt will be invalid when
|
|
|
|
// - the app is lauched
|
|
|
|
// - the app resumes from background
|
|
|
|
[self initLastDisplayTime];
|
|
|
|
|
2016-02-13 16:17:55 +08:00
|
|
|
isAppActive = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)appDidBecomeInactive
|
|
|
|
{
|
|
|
|
isAppActive = NO;
|
|
|
|
}
|
|
|
|
|
2010-11-12 17:16:15 +08:00
|
|
|
-(void) startMainLoop
|
|
|
|
{
|
2017-07-18 14:01:53 +08:00
|
|
|
// Director::setAnimationInterval() is called, we should invalidate it first
|
2014-05-28 15:19:07 +08:00
|
|
|
[self stopMainLoop];
|
|
|
|
|
|
|
|
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
|
|
|
|
[displayLink setFrameInterval: self.interval];
|
|
|
|
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) stopMainLoop
|
|
|
|
{
|
|
|
|
[displayLink invalidate];
|
|
|
|
displayLink = nil;
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
2011-01-10 15:13:30 +08:00
|
|
|
|
2011-01-26 11:31:01 +08:00
|
|
|
-(void) setAnimationInterval:(double)intervalNew
|
2011-01-10 15:13:30 +08:00
|
|
|
{
|
2014-05-28 15:19:07 +08:00
|
|
|
// Director::setAnimationInterval() is called, we should invalidate it first
|
|
|
|
[self stopMainLoop];
|
2011-01-21 17:33:15 +08:00
|
|
|
|
2014-05-28 15:19:07 +08:00
|
|
|
self.interval = 60.0 * intervalNew;
|
2011-01-21 17:33:15 +08:00
|
|
|
|
2014-05-28 15:19:07 +08:00
|
|
|
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
|
|
|
|
[displayLink setFrameInterval: self.interval];
|
|
|
|
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
2011-01-10 15:13:30 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2010-11-12 17:16:15 +08:00
|
|
|
-(void) doCaller: (id) sender
|
|
|
|
{
|
2016-02-13 16:17:55 +08:00
|
|
|
if (isAppActive) {
|
|
|
|
cocos2d::Director* director = cocos2d::Director::getInstance();
|
2020-08-28 12:57:00 +08:00
|
|
|
#if defined(CC_USE_GLES)
|
|
|
|
EAGLContext* cocos2dxContext = [(CCEAGLView*)director->getOpenGLView()->getEAGLView() context];
|
|
|
|
if (cocos2dxContext != [EAGLContext currentContext])
|
|
|
|
glFlush();
|
|
|
|
|
|
|
|
[EAGLContext setCurrentContext: cocos2dxContext];
|
|
|
|
#endif
|
2017-05-23 08:46:13 +08:00
|
|
|
CFTimeInterval dt = ((CADisplayLink*)displayLink).timestamp - lastDisplayTime;
|
|
|
|
lastDisplayTime = ((CADisplayLink*)displayLink).timestamp;
|
|
|
|
director->mainLoop(dt);
|
2016-02-13 16:17:55 +08:00
|
|
|
}
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
2017-07-10 10:06:25 +08:00
|
|
|
-(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);
|
|
|
|
}
|
|
|
|
|
2010-11-12 17:16:15 +08:00
|
|
|
@end
|