fix: ensure calling opengl only when app is active

This commit is contained in:
Mikhail Shulepov 2016-02-13 11:17:55 +03:00
parent ab17455b18
commit 014f8e107f
2 changed files with 30 additions and 3 deletions

View File

@ -31,6 +31,7 @@
@interface CCDirectorCaller : NSObject {
id displayLink;
int interval;
BOOL isAppActive;
}
@property (readwrite) int interval;
-(void) startMainLoop;

View File

@ -69,12 +69,36 @@ static id s_sharedDirectorCaller;
interval = 1;
}
- (instancetype)init
{
self = [super init];
if (self)
{
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];
}
return self;
}
-(void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[displayLink release];
[super dealloc];
}
- (void)appDidBecomeActive
{
isAppActive = YES;
}
- (void)appDidBecomeInactive
{
isAppActive = NO;
}
-(void) startMainLoop
{
// Director::setAnimationInterval() is called, we should invalidate it first
@ -105,9 +129,11 @@ static id s_sharedDirectorCaller;
-(void) doCaller: (id) sender
{
cocos2d::Director* director = cocos2d::Director::getInstance();
[EAGLContext setCurrentContext: [(CCEAGLView*)director->getOpenGLView()->getEAGLView() context]];
director->mainLoop();
if (isAppActive) {
cocos2d::Director* director = cocos2d::Director::getInstance();
[EAGLContext setCurrentContext: [(CCEAGLView*)director->getOpenGLView()->getEAGLView() context]];
director->mainLoop();
}
}
@end