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

165 lines
5.0 KiB
Plaintext
Raw Normal View History

2019-11-23 20:27:39 +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.
https://axmol.dev/
2019-11-23 20:27:39 +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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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>
#import "platform/ios/DirectorCaller-ios.h"
2019-11-23 20:27:39 +08:00
#import <Foundation/Foundation.h>
#import <OpenGLES/EAGL.h>
#import "base/Director.h"
#import "platform/ios/EAGLView-ios.h"
2019-11-23 20:27:39 +08:00
static id s_sharedDirectorCaller;
@interface NSObject (CADisplayLink)
+ (id)displayLinkWithTarget:(id)arg1 selector:(SEL)arg2;
- (void)addToRunLoop:(id)arg1 forMode:(id)arg2;
- (void)setFrameInterval:(NSInteger)interval;
- (void)invalidate;
2019-11-23 20:27:39 +08:00
@end
@implementation CCDirectorCaller
@synthesize interval;
+ (id)sharedDirectorCaller
2019-11-23 20:27:39 +08:00
{
if (s_sharedDirectorCaller == nil)
{
s_sharedDirectorCaller = [[CCDirectorCaller alloc] init];
}
2019-11-23 20:27:39 +08:00
return s_sharedDirectorCaller;
}
+ (void)destroy
2019-11-23 20:27:39 +08:00
{
[s_sharedDirectorCaller stopMainLoop];
[s_sharedDirectorCaller release];
s_sharedDirectorCaller = nil;
}
- (instancetype)init
{
if (self = [super init])
{
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];
2019-11-23 20:27:39 +08:00
self.interval = 1;
}
return self;
}
- (void)dealloc
2019-11-23 20:27:39 +08:00
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[displayLink release];
[super dealloc];
}
- (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;
}
- (void)startMainLoop
2019-11-23 20:27:39 +08:00
{
// Director::setAnimationInterval() is called, we should invalidate it first
[self stopMainLoop];
2019-11-23 20:27:39 +08:00
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
[displayLink setFrameInterval:self.interval];
2019-11-23 20:27:39 +08:00
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)stopMainLoop
2019-11-23 20:27:39 +08:00
{
[displayLink invalidate];
displayLink = nil;
}
- (void)setAnimationInterval:(double)intervalNew
2019-11-23 20:27:39 +08:00
{
// Director::setAnimationInterval() is called, we should invalidate it first
[self stopMainLoop];
2019-11-23 20:27:39 +08:00
self.interval = 60.0 * intervalNew;
2019-11-23 20:27:39 +08:00
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
[displayLink setFrameInterval:self.interval];
2019-11-23 20:27:39 +08:00
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)doCaller:(id)sender
2019-11-23 20:27:39 +08:00
{
if (isAppActive)
{
2022-08-08 18:02:17 +08:00
ax::Director* director = ax::Director::getInstance();
#if AX_GLES_PROFILE
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
EAGLContext* context = [(EAGLView*)director->getGLView()->getEAGLView() context];
if (context != [EAGLContext currentContext])
glFlush();
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
[EAGLContext setCurrentContext:context];
#endif
2019-11-23 20:27:39 +08:00
CFTimeInterval dt = ((CADisplayLink*)displayLink).timestamp - lastDisplayTime;
lastDisplayTime = ((CADisplayLink*)displayLink).timestamp;
2019-11-23 20:27:39 +08:00
director->mainLoop(dt);
}
}
- (void)initLastDisplayTime
2019-11-23 20:27:39 +08:00
{
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);
}
@end