433 lines
13 KiB
Plaintext
433 lines
13 KiB
Plaintext
/****************************************************************************
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
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.
|
|
****************************************************************************/
|
|
|
|
#import "CCApplication.h"
|
|
#import <UIKit/UIKit.h>
|
|
#include "base/CCScheduler.h"
|
|
#include "base/CCAutoreleasePool.h"
|
|
#include "base/CCGLUtils.h"
|
|
#include "base/CCConfiguration.h"
|
|
#include "scripting/js-bindings/event/EventDispatcher.h"
|
|
#include "scripting/js-bindings/jswrapper/SeApi.h"
|
|
#include "base/CCGLUtils.h"
|
|
#include "platform/CCDevice.h"
|
|
|
|
namespace
|
|
{
|
|
}
|
|
|
|
@interface MainLoop : NSObject
|
|
{
|
|
id _displayLink;
|
|
int _fps;
|
|
float _systemVersion;
|
|
BOOL _isAppActive;
|
|
cocos2d::Device::Rotation _lastRotation;
|
|
cocos2d::Application* _application;
|
|
std::shared_ptr<cocos2d::Scheduler> _scheduler;
|
|
}
|
|
-(void) startMainLoop;
|
|
-(void) stopMainLoop;
|
|
-(void) doCaller: (id) sender;
|
|
-(void) setPreferredFPS:(int)fps;
|
|
-(void) firstStart:(id) view;
|
|
@end
|
|
|
|
@implementation MainLoop
|
|
|
|
- (instancetype)initWithApplication:(cocos2d::Application*) application
|
|
{
|
|
self = [super init];
|
|
if (self)
|
|
{
|
|
|
|
_application = application;
|
|
_scheduler = _application->getScheduler();
|
|
|
|
// _isAppActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void) statusBarOrientationChanged:(NSNotification *)note
|
|
{
|
|
cocos2d::Device::Rotation rotation = cocos2d::Device::Rotation::_0;
|
|
UIDevice * device = [UIDevice currentDevice];
|
|
|
|
// NOTE: https://developer.apple.com/documentation/uikit/uideviceorientation
|
|
// when the device rotates to LandscapeLeft, device.orientation returns UIDeviceOrientationLandscapeRight
|
|
// when the device rotates to LandscapeRight, device.orientation returns UIDeviceOrientationLandscapeLeft
|
|
switch(device.orientation)
|
|
{
|
|
case UIDeviceOrientationPortrait:
|
|
rotation = cocos2d::Device::Rotation::_0;
|
|
break;
|
|
case UIDeviceOrientationLandscapeLeft:
|
|
rotation = cocos2d::Device::Rotation::_90;
|
|
break;
|
|
case UIDeviceOrientationPortraitUpsideDown:
|
|
rotation = cocos2d::Device::Rotation::_180;
|
|
break;
|
|
case UIDeviceOrientationLandscapeRight:
|
|
rotation = cocos2d::Device::Rotation::_270;
|
|
break;
|
|
default:
|
|
break;
|
|
};
|
|
if(_lastRotation != rotation){
|
|
cocos2d::EventDispatcher::dispatchOrientationChangeEvent((int) rotation);
|
|
_lastRotation = rotation;
|
|
}
|
|
|
|
CGRect bounds = [UIScreen mainScreen].bounds;
|
|
float scale = [[UIScreen mainScreen] scale];
|
|
float width = bounds.size.width * scale;
|
|
float height = bounds.size.height * scale;
|
|
cocos2d::Application::getInstance()->updateViewSize(width, height);
|
|
}
|
|
|
|
-(void) dealloc
|
|
{
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
[_displayLink release];
|
|
|
|
[super dealloc];
|
|
}
|
|
|
|
- (void)appDidBecomeActive
|
|
{
|
|
_isAppActive = YES;
|
|
}
|
|
|
|
- (void)appDidBecomeInactive
|
|
{
|
|
_isAppActive = NO;
|
|
}
|
|
|
|
-(void) firstStart:(id) view
|
|
{
|
|
// 这里直接开始
|
|
if(!_application->applicationDidFinishLaunching())
|
|
return;
|
|
}
|
|
|
|
-(void) startMainLoop
|
|
{
|
|
[self stopMainLoop];
|
|
|
|
_displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
|
|
if (_systemVersion >= 10.0f)
|
|
[_displayLink setPreferredFramesPerSecond: _fps];
|
|
else
|
|
[_displayLink setFrameInterval: 60 / _fps];
|
|
[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
|
}
|
|
|
|
-(void) stopMainLoop
|
|
{
|
|
if (_displayLink != nil)
|
|
{
|
|
[_displayLink invalidate];
|
|
_displayLink = nil;
|
|
}
|
|
}
|
|
|
|
-(void) setPreferredFPS:(int)fps
|
|
{
|
|
_fps = fps;
|
|
[self startMainLoop];
|
|
}
|
|
|
|
-(void) doCaller: (id) sender
|
|
{
|
|
static std::chrono::steady_clock::time_point prevTime;
|
|
static std::chrono::steady_clock::time_point now;
|
|
}
|
|
|
|
@end
|
|
|
|
NS_CC_BEGIN
|
|
|
|
Application* Application::_instance = nullptr;
|
|
std::shared_ptr<Scheduler> Application::_scheduler = nullptr;
|
|
|
|
Application::Application(const std::string& name, int width, int height)
|
|
{
|
|
Application::_instance = this;
|
|
_scheduler = std::make_shared<Scheduler>();
|
|
|
|
se::ScriptEngine::getInstance();
|
|
EventDispatcher::init();
|
|
|
|
_delegate = [[MainLoop alloc] initWithApplication:this];
|
|
|
|
// updateViewSize(width, height);
|
|
}
|
|
|
|
Application::~Application()
|
|
{
|
|
|
|
|
|
EventDispatcher::destroy();
|
|
se::ScriptEngine::destroyInstance();
|
|
|
|
// stop main loop
|
|
[(MainLoop*)_delegate stopMainLoop];
|
|
[(MainLoop*)_delegate release];
|
|
_delegate = nullptr;
|
|
|
|
_view = nullptr;
|
|
|
|
delete _renderTexture;
|
|
_renderTexture = nullptr;
|
|
|
|
Application::_instance = nullptr;
|
|
}
|
|
|
|
const cocos2d::Vec2& Application::getViewSize() const
|
|
{
|
|
return _viewSize;
|
|
}
|
|
|
|
void Application::updateViewSize(int width, int height)
|
|
{
|
|
_viewSize.x = width;
|
|
_viewSize.y = height;
|
|
cocos2d::EventDispatcher::dispatchResizeEvent(width, height);
|
|
}
|
|
|
|
void Application::start()
|
|
{
|
|
if (_delegate)
|
|
[(MainLoop*)_delegate firstStart:nullptr];
|
|
}
|
|
|
|
void Application::restart()
|
|
{
|
|
if (_delegate) {
|
|
[(MainLoop*)_delegate stopMainLoop];
|
|
[(MainLoop*)_delegate firstStart:nullptr];
|
|
}
|
|
}
|
|
|
|
void Application::end()
|
|
{
|
|
delete this;
|
|
|
|
exit(0);
|
|
}
|
|
|
|
void Application::setPreferredFramesPerSecond(int fps)
|
|
{
|
|
[(MainLoop*)_delegate setPreferredFPS: fps];
|
|
}
|
|
|
|
std::string Application::getCurrentLanguageCode() const
|
|
{
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
|
|
NSString *currentLanguage = [languages objectAtIndex:0];
|
|
return [currentLanguage UTF8String];
|
|
}
|
|
|
|
bool Application::isDisplayStats() {
|
|
se::AutoHandleScope hs;
|
|
se::Value ret;
|
|
char commandBuf[100] = "cc.debug.isDisplayStats();";
|
|
se::ScriptEngine::getInstance()->evalString(commandBuf, 100, &ret);
|
|
return ret.toBoolean();
|
|
}
|
|
|
|
void Application::setDisplayStats(bool isShow) {
|
|
se::AutoHandleScope hs;
|
|
char commandBuf[100] = {0};
|
|
sprintf(commandBuf, "cc.debug.setDisplayStats(%s);", isShow ? "true" : "false");
|
|
se::ScriptEngine::getInstance()->evalString(commandBuf);
|
|
}
|
|
|
|
Application::LanguageType Application::getCurrentLanguage() const
|
|
{
|
|
// get the current language and country config
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
|
|
NSString *currentLanguage = [languages objectAtIndex:0];
|
|
|
|
// get the current language code.(such as English is "en", Chinese is "zh" and so on)
|
|
NSDictionary* temp = [NSLocale componentsFromLocaleIdentifier:currentLanguage];
|
|
NSString * languageCode = [temp objectForKey:NSLocaleLanguageCode];
|
|
|
|
if ([languageCode isEqualToString:@"zh"]) return LanguageType::CHINESE;
|
|
if ([languageCode isEqualToString:@"en"]) return LanguageType::ENGLISH;
|
|
if ([languageCode isEqualToString:@"fr"]) return LanguageType::FRENCH;
|
|
if ([languageCode isEqualToString:@"it"]) return LanguageType::ITALIAN;
|
|
if ([languageCode isEqualToString:@"de"]) return LanguageType::GERMAN;
|
|
if ([languageCode isEqualToString:@"es"]) return LanguageType::SPANISH;
|
|
if ([languageCode isEqualToString:@"nl"]) return LanguageType::DUTCH;
|
|
if ([languageCode isEqualToString:@"ru"]) return LanguageType::RUSSIAN;
|
|
if ([languageCode isEqualToString:@"ko"]) return LanguageType::KOREAN;
|
|
if ([languageCode isEqualToString:@"ja"]) return LanguageType::JAPANESE;
|
|
if ([languageCode isEqualToString:@"hu"]) return LanguageType::HUNGARIAN;
|
|
if ([languageCode isEqualToString:@"pt"]) return LanguageType::PORTUGUESE;
|
|
if ([languageCode isEqualToString:@"ar"]) return LanguageType::ARABIC;
|
|
if ([languageCode isEqualToString:@"nb"]) return LanguageType::NORWEGIAN;
|
|
if ([languageCode isEqualToString:@"pl"]) return LanguageType::POLISH;
|
|
if ([languageCode isEqualToString:@"tr"]) return LanguageType::TURKISH;
|
|
if ([languageCode isEqualToString:@"uk"]) return LanguageType::UKRAINIAN;
|
|
if ([languageCode isEqualToString:@"ro"]) return LanguageType::ROMANIAN;
|
|
if ([languageCode isEqualToString:@"bg"]) return LanguageType::BULGARIAN;
|
|
return LanguageType::ENGLISH;
|
|
}
|
|
|
|
Application::Platform Application::getPlatform() const
|
|
{
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) // idiom for iOS <= 3.2, otherwise: [UIDevice userInterfaceIdiom] is faster.
|
|
return Platform::IPAD;
|
|
else
|
|
return Platform::IPHONE;
|
|
}
|
|
|
|
float Application::getScreenScale() const
|
|
{
|
|
return [(UIView*)_view contentScaleFactor];
|
|
}
|
|
|
|
GLint Application::getMainFBO() const
|
|
{
|
|
return _mainFBO;
|
|
}
|
|
|
|
bool Application::openURL(const std::string &url)
|
|
{
|
|
NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding];
|
|
NSURL* nsUrl = [NSURL URLWithString:msg];
|
|
return [[UIApplication sharedApplication] openURL:nsUrl];
|
|
}
|
|
|
|
void Application::copyTextToClipboard(const std::string &text)
|
|
{
|
|
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
|
|
pasteboard.string = [NSString stringWithCString:text.c_str() encoding:NSUTF8StringEncoding];
|
|
}
|
|
|
|
void Application::showQRCode(const std::string &content, const std::string &title, const std::string &val) {
|
|
NSString *ntitle = [NSString stringWithCString:title.c_str() encoding:NSUTF8StringEncoding];
|
|
NSString *ncontent = [NSString stringWithCString:content.c_str() encoding:NSUTF8StringEncoding];
|
|
NSString *noid = [NSString stringWithCString:val.c_str() encoding:NSUTF8StringEncoding];
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
|
|
[window.rootViewController showRestoreQR:ncontent title:ntitle oid: noid];
|
|
});
|
|
|
|
}
|
|
|
|
void Application::saveKeyLocal(const std::string &account, const std::string &key) {
|
|
NSString *naccount = [NSString stringWithCString:account.c_str() encoding:NSUTF8StringEncoding];
|
|
NSString *nkey = [NSString stringWithCString:key.c_str() encoding:NSUTF8StringEncoding];
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
|
|
[window.rootViewController saveKey:naccount key: nkey];
|
|
});
|
|
|
|
}
|
|
|
|
void Application::loadKeyLocal(const std::string &account, std::string *outItem) {
|
|
NSString *naccount = [NSString stringWithCString:account.c_str() encoding:NSUTF8StringEncoding];
|
|
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
|
|
NSString * nkey = [window.rootViewController loadKey: naccount];
|
|
if (nkey != nil) {
|
|
std::string key = std::string([nkey UTF8String]);
|
|
outItem->assign(key.c_str());
|
|
} else {
|
|
outItem->assign("");
|
|
}
|
|
}
|
|
|
|
void Application::toWallet(const std::string &url) {
|
|
UIApplication *app = [UIApplication sharedApplication];
|
|
NSString *uri = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding];
|
|
[app openURL:[NSURL URLWithString:uri] options:@{} completionHandler:^(BOOL success) {
|
|
if (success) {
|
|
NSLog(@"Opened url");
|
|
}
|
|
}];
|
|
}
|
|
|
|
bool Application::applicationDidFinishLaunching()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
void Application::onPause()
|
|
{
|
|
}
|
|
|
|
void Application::onResume()
|
|
{
|
|
}
|
|
|
|
void Application::setMultitouch(bool value)
|
|
{
|
|
|
|
}
|
|
|
|
void Application::onCreateView(PixelFormat& pixelformat, DepthFormat& depthFormat, int& multisamplingCount)
|
|
{
|
|
pixelformat = PixelFormat::RGB565;
|
|
depthFormat = DepthFormat::DEPTH24_STENCIL8;
|
|
|
|
multisamplingCount = 0;
|
|
}
|
|
|
|
namespace
|
|
{
|
|
GLenum depthFormatMap[] =
|
|
{
|
|
0, // NONE: no depth and no stencil
|
|
GL_DEPTH_COMPONENT24_OES, // DEPTH_COMPONENT16: unsupport, convert to GL_DEPTH_COMPONENT24_OES
|
|
GL_DEPTH_COMPONENT24_OES, // DEPTH_COMPONENT24
|
|
GL_DEPTH_COMPONENT24_OES, // DEPTH_COMPONENT32F: unsupport, convert to GL_DEPTH_COMPONENT24_OES
|
|
GL_DEPTH24_STENCIL8_OES, // DEPTH24_STENCIL8
|
|
GL_DEPTH24_STENCIL8_OES, // DEPTH32F_STENCIL8: unsupport, convert to GL_DEPTH24_STENCIL8_OES
|
|
GL_DEPTH_STENCIL_OES // STENCIL_INDEX8
|
|
};
|
|
|
|
GLenum depthFormat2GLDepthFormat(cocos2d::Application::DepthFormat depthFormat)
|
|
{
|
|
return depthFormatMap[(int)depthFormat];
|
|
}
|
|
}
|
|
|
|
void Application::createView(const std::string& /*name*/, int width, int height)
|
|
{
|
|
}
|
|
|
|
std::string Application::getSystemVersion()
|
|
{
|
|
NSString* systemVersion = [UIDevice currentDevice].systemVersion;
|
|
return [systemVersion UTF8String];
|
|
}
|
|
|
|
NS_CC_END
|