CCDirectorCaller-ios.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include <mach/mach_time.h>
  24. #import "platform/ios/CCDirectorCaller-ios.h"
  25. #import <Foundation/Foundation.h>
  26. #import <OpenGLES/EAGL.h>
  27. #import "base/CCDirector.h"
  28. #import "platform/ios/CCEAGLView-ios.h"
  29. static id s_sharedDirectorCaller;
  30. @interface NSObject(CADisplayLink)
  31. +(id) displayLinkWithTarget: (id)arg1 selector:(SEL)arg2;
  32. -(void) addToRunLoop: (id)arg1 forMode: (id)arg2;
  33. -(void) setFrameInterval: (NSInteger)interval;
  34. -(void) invalidate;
  35. @end
  36. @implementation CCDirectorCaller
  37. @synthesize interval;
  38. +(id) sharedDirectorCaller
  39. {
  40. if (s_sharedDirectorCaller == nil)
  41. {
  42. s_sharedDirectorCaller = [[CCDirectorCaller alloc] init];
  43. }
  44. return s_sharedDirectorCaller;
  45. }
  46. +(void) destroy
  47. {
  48. [s_sharedDirectorCaller stopMainLoop];
  49. [s_sharedDirectorCaller release];
  50. s_sharedDirectorCaller = nil;
  51. }
  52. - (instancetype)init
  53. {
  54. if (self = [super init])
  55. {
  56. isAppActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
  57. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  58. [nc addObserver:self selector:@selector(appDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
  59. [nc addObserver:self selector:@selector(appDidBecomeInactive) name:UIApplicationWillResignActiveNotification object:nil];
  60. self.interval = 1;
  61. }
  62. return self;
  63. }
  64. -(void) dealloc
  65. {
  66. [[NSNotificationCenter defaultCenter] removeObserver:self];
  67. [displayLink release];
  68. [super dealloc];
  69. }
  70. - (void)appDidBecomeActive
  71. {
  72. // initialize initLastDisplayTime, or the dt will be invalid when
  73. // - the app is lauched
  74. // - the app resumes from background
  75. [self initLastDisplayTime];
  76. isAppActive = YES;
  77. }
  78. - (void)appDidBecomeInactive
  79. {
  80. isAppActive = NO;
  81. }
  82. -(void) startMainLoop
  83. {
  84. // Director::setAnimationInterval() is called, we should invalidate it first
  85. [self stopMainLoop];
  86. displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
  87. [displayLink setFrameInterval: self.interval];
  88. [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  89. }
  90. -(void) stopMainLoop
  91. {
  92. [displayLink invalidate];
  93. displayLink = nil;
  94. }
  95. -(void) setAnimationInterval:(double)intervalNew
  96. {
  97. // Director::setAnimationInterval() is called, we should invalidate it first
  98. [self stopMainLoop];
  99. self.interval = 60.0 * intervalNew;
  100. displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
  101. [displayLink setFrameInterval: self.interval];
  102. [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  103. }
  104. -(void) doCaller: (id) sender
  105. {
  106. if (isAppActive) {
  107. cocos2d::Director* director = cocos2d::Director::getInstance();
  108. EAGLContext* cocos2dxContext = [(CCEAGLView*)director->getOpenGLView()->getEAGLView() context];
  109. if (cocos2dxContext != [EAGLContext currentContext])
  110. glFlush();
  111. [EAGLContext setCurrentContext: cocos2dxContext];
  112. CFTimeInterval dt = ((CADisplayLink*)displayLink).timestamp - lastDisplayTime;
  113. lastDisplayTime = ((CADisplayLink*)displayLink).timestamp;
  114. director->mainLoop(dt);
  115. }
  116. }
  117. -(void)initLastDisplayTime
  118. {
  119. struct mach_timebase_info timeBaseInfo;
  120. mach_timebase_info(&timeBaseInfo);
  121. CGFloat clockFrequency = (CGFloat)timeBaseInfo.denom / (CGFloat)timeBaseInfo.numer;
  122. clockFrequency *= 1000000000.0;
  123. // convert absolute time to seconds and should minus one frame time interval
  124. lastDisplayTime = (mach_absolute_time() / clockFrequency) - ((1.0 / 60) * self.interval);
  125. }
  126. @end
  127. #endif // CC_TARGET_PLATFORM == CC_PLATFORM_IOS