axmol/core/platform/ios/CCDirectorCaller-ios.mm

165 lines
5.1 KiB
Plaintext
Raw Normal View History

2010-11-13 11:34:49 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2010-11-13 11:34:49 +08:00
http://www.cocos2d-x.org
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:
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.
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.
****************************************************************************/
#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>
#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;
2021-12-28 11:00:34 +08:00
@interface NSObject (CADisplayLink)
+ (id)displayLinkWithTarget:(id)arg1 selector:(SEL)arg2;
- (void)addToRunLoop:(id)arg1 forMode:(id)arg2;
- (void)setFrameInterval:(NSInteger)interval;
- (void)invalidate;
2010-11-12 17:16:15 +08:00
@end
@implementation CCDirectorCaller
2010-11-12 17:16:15 +08:00
2011-01-21 17:33:15 +08:00
@synthesize interval;
2021-12-28 11:00:34 +08:00
+ (id)sharedDirectorCaller
2010-11-12 17:16:15 +08:00
{
if (s_sharedDirectorCaller == nil)
{
s_sharedDirectorCaller = [[CCDirectorCaller alloc] init];
}
2021-12-28 11:00:34 +08:00
return s_sharedDirectorCaller;
2010-11-12 17:16:15 +08:00
}
2021-12-28 11:00:34 +08:00
+ (void)destroy
2010-11-12 17:16:15 +08:00
{
[s_sharedDirectorCaller stopMainLoop];
[s_sharedDirectorCaller release];
s_sharedDirectorCaller = nil;
2010-11-12 17:16:15 +08:00
}
- (instancetype)init
{
if (self = [super init])
{
2021-12-28 11:00:34 +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];
self.interval = 1;
}
return self;
}
2021-12-28 11:00:34 +08:00
- (void)dealloc
2010-11-12 17:16:15 +08:00
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[displayLink release];
[super dealloc];
2010-11-12 17:16:15 +08:00
}
- (void)appDidBecomeActive
{
// initialize initLastDisplayTime, or the dt will be invalid when
// - the app is lauched
// - the app resumes from background
[self initLastDisplayTime];
isAppActive = YES;
}
- (void)appDidBecomeInactive
{
isAppActive = NO;
}
2021-12-28 11:00:34 +08:00
- (void)startMainLoop
2010-11-12 17:16:15 +08:00
{
// Director::setAnimationInterval() is called, we should invalidate it first
[self stopMainLoop];
2021-12-28 11:00:34 +08:00
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
2021-12-28 11:00:34 +08:00
[displayLink setFrameInterval:self.interval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
2021-12-28 11:00:34 +08:00
- (void)stopMainLoop
{
[displayLink invalidate];
displayLink = nil;
2010-11-12 17:16:15 +08:00
}
2021-12-28 11:00:34 +08:00
- (void)setAnimationInterval:(double)intervalNew
{
// Director::setAnimationInterval() is called, we should invalidate it first
[self stopMainLoop];
2021-12-28 11:00:34 +08:00
self.interval = 60.0 * intervalNew;
2021-12-28 11:00:34 +08:00
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
2021-12-28 11:00:34 +08:00
[displayLink setFrameInterval:self.interval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
2021-12-28 11:00:34 +08:00
- (void)doCaller:(id)sender
2010-11-12 17:16:15 +08:00
{
2021-12-28 11:00:34 +08:00
if (isAppActive)
{
cocos2d::Director* director = cocos2d::Director::getInstance();
#if defined(CC_USE_GLES)
EAGLContext* cocos2dxContext = [(CCEAGLView*)director->getOpenGLView()->getEAGLView() context];
if (cocos2dxContext != [EAGLContext currentContext])
glFlush();
2021-12-28 11:00:34 +08:00
[EAGLContext setCurrentContext:cocos2dxContext];
#endif
2017-05-23 08:46:13 +08:00
CFTimeInterval dt = ((CADisplayLink*)displayLink).timestamp - lastDisplayTime;
2021-12-28 11:00:34 +08:00
lastDisplayTime = ((CADisplayLink*)displayLink).timestamp;
2017-05-23 08:46:13 +08:00
director->mainLoop(dt);
}
2010-11-12 17:16:15 +08:00
}
2021-12-28 11:00:34 +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