CCDirectorCaller-ios.mm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "platform/CCPlatformConfig.h"
  22. #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
  23. #import "platform/ios/CCDirectorCaller-ios.h"
  24. #import <Foundation/Foundation.h>
  25. #import <OpenGLES/EAGL.h>
  26. #import "base/CCDirector.h"
  27. #import "platform/ios/CCEAGLView-ios.h"
  28. static id s_sharedDirectorCaller;
  29. @interface NSObject(CADisplayLink)
  30. +(id) displayLinkWithTarget: (id)arg1 selector:(SEL)arg2;
  31. -(void) addToRunLoop: (id)arg1 forMode: (id)arg2;
  32. -(void) setFrameInterval: (NSInteger)interval;
  33. -(void) invalidate;
  34. @end
  35. @implementation CCDirectorCaller
  36. @synthesize interval;
  37. +(id) sharedDirectorCaller
  38. {
  39. if (s_sharedDirectorCaller == nil)
  40. {
  41. s_sharedDirectorCaller = [CCDirectorCaller new];
  42. }
  43. return s_sharedDirectorCaller;
  44. }
  45. +(void) destroy
  46. {
  47. [s_sharedDirectorCaller stopMainLoop];
  48. [s_sharedDirectorCaller release];
  49. s_sharedDirectorCaller = nil;
  50. }
  51. -(void) alloc
  52. {
  53. interval = 1;
  54. }
  55. - (instancetype)init
  56. {
  57. self = [super init];
  58. if (self)
  59. {
  60. isAppActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
  61. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  62. [nc addObserver:self selector:@selector(appDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
  63. [nc addObserver:self selector:@selector(appDidBecomeInactive) name:UIApplicationWillResignActiveNotification object:nil];
  64. }
  65. return self;
  66. }
  67. -(void) dealloc
  68. {
  69. [[NSNotificationCenter defaultCenter] removeObserver:self];
  70. [displayLink release];
  71. [super dealloc];
  72. }
  73. - (void)appDidBecomeActive
  74. {
  75. isAppActive = YES;
  76. }
  77. - (void)appDidBecomeInactive
  78. {
  79. isAppActive = NO;
  80. }
  81. -(void) startMainLoop
  82. {
  83. // Director::setAnimationInterval() is called, we should invalidate it first
  84. [self stopMainLoop];
  85. displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
  86. [displayLink setFrameInterval: self.interval];
  87. [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  88. }
  89. -(void) stopMainLoop
  90. {
  91. [displayLink invalidate];
  92. displayLink = nil;
  93. }
  94. -(void) setAnimationInterval:(double)intervalNew
  95. {
  96. // Director::setAnimationInterval() is called, we should invalidate it first
  97. [self stopMainLoop];
  98. self.interval = 60.0 * intervalNew;
  99. displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
  100. [displayLink setFrameInterval: self.interval];
  101. [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  102. }
  103. -(void) doCaller: (id) sender
  104. {
  105. if (isAppActive) {
  106. cocos2d::Director* director = cocos2d::Director::getInstance();
  107. EAGLContext* cocos2dxContext = [(CCEAGLView*)director->getOpenGLView()->getEAGLView() context];
  108. if (cocos2dxContext != [EAGLContext currentContext])
  109. glFlush();
  110. [EAGLContext setCurrentContext: cocos2dxContext];
  111. director->mainLoop();
  112. }
  113. }
  114. @end
  115. #endif // CC_TARGET_PLATFORM == CC_PLATFORM_IOS