mirror of https://github.com/axmolengine/axmol.git
fix: ensure calling opengl only when app is active
This commit is contained in:
parent
ab17455b18
commit
014f8e107f
|
@ -31,6 +31,7 @@
|
|||
@interface CCDirectorCaller : NSObject {
|
||||
id displayLink;
|
||||
int interval;
|
||||
BOOL isAppActive;
|
||||
}
|
||||
@property (readwrite) int interval;
|
||||
-(void) startMainLoop;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue