2012-07-21 12:23:40 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2012-07-21 12:23:40 +08:00
|
|
|
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
2014-01-31 08:51:43 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_MAC
|
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
#import <Cocoa/Cocoa.h>
|
2012-09-05 16:15:09 +08:00
|
|
|
#include <algorithm>
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2014-09-12 08:36:49 +08:00
|
|
|
#import "platform/CCApplication.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "math/CCGeometry.h"
|
|
|
|
#include "base/CCDirector.h"
|
2012-07-21 12:23:40 +08:00
|
|
|
|
2012-09-05 16:15:09 +08:00
|
|
|
NS_CC_BEGIN
|
2012-07-21 12:23:40 +08:00
|
|
|
|
2014-02-21 11:55:37 +08:00
|
|
|
static long getCurrentMillSecond()
|
|
|
|
{
|
|
|
|
long lLastTime = 0;
|
|
|
|
struct timeval stCurrentTime;
|
|
|
|
|
|
|
|
gettimeofday(&stCurrentTime,NULL);
|
2016-10-17 10:12:54 +08:00
|
|
|
lLastTime = stCurrentTime.tv_sec*1000+stCurrentTime.tv_usec*0.001; // milliseconds
|
2014-02-21 11:55:37 +08:00
|
|
|
return lLastTime;
|
|
|
|
}
|
|
|
|
|
2016-06-23 11:39:23 +08:00
|
|
|
Application* Application::sm_pSharedApplication = nullptr;
|
2012-07-21 12:23:40 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Application::Application()
|
2014-02-21 11:55:37 +08:00
|
|
|
: _animationInterval(1.0f/60.0f*1000.0f)
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(! sm_pSharedApplication, "sm_pSharedApplication already exist");
|
2012-07-21 12:23:40 +08:00
|
|
|
sm_pSharedApplication = this;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Application::~Application()
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(this == sm_pSharedApplication, "sm_pSharedApplication != this");
|
2012-07-21 12:23:40 +08:00
|
|
|
sm_pSharedApplication = 0;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int Application::run()
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2014-08-22 16:22:16 +08:00
|
|
|
initGLContextAttrs();
|
2013-08-21 12:01:56 +08:00
|
|
|
if(!applicationDidFinishLaunching())
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2014-04-25 17:01:10 +08:00
|
|
|
return 1;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
2014-03-14 18:40:04 +08:00
|
|
|
|
|
|
|
long lastTime = 0L;
|
|
|
|
long curTime = 0L;
|
|
|
|
|
|
|
|
auto director = Director::getInstance();
|
|
|
|
auto glview = director->getOpenGLView();
|
|
|
|
|
|
|
|
// Retain glview to avoid glview being released in the while loop
|
2014-03-14 17:21:27 +08:00
|
|
|
glview->retain();
|
2013-08-21 12:01:56 +08:00
|
|
|
|
2014-01-24 07:36:55 +08:00
|
|
|
while (!glview->windowShouldClose())
|
2013-08-21 12:01:56 +08:00
|
|
|
{
|
2014-03-14 18:40:04 +08:00
|
|
|
lastTime = getCurrentMillSecond();
|
|
|
|
|
|
|
|
director->mainLoop();
|
2014-03-15 00:10:39 +08:00
|
|
|
glview->pollEvents();
|
|
|
|
|
2014-03-14 18:40:04 +08:00
|
|
|
curTime = getCurrentMillSecond();
|
|
|
|
if (curTime - lastTime < _animationInterval)
|
|
|
|
{
|
|
|
|
usleep(static_cast<useconds_t>((_animationInterval - curTime + lastTime)*1000));
|
2014-02-21 11:55:37 +08:00
|
|
|
}
|
2013-08-21 12:01:56 +08:00
|
|
|
}
|
2013-09-18 17:16:31 +08:00
|
|
|
|
|
|
|
/* Only work on Desktop
|
|
|
|
* Director::mainLoop is really one frame logic
|
|
|
|
* when we want to close the window, we should call Director::end();
|
|
|
|
* then call Director::mainLoop to do release of internal resources
|
|
|
|
*/
|
2014-03-14 17:21:27 +08:00
|
|
|
if (glview->isOpenGLReady())
|
|
|
|
{
|
2014-03-14 18:40:04 +08:00
|
|
|
director->end();
|
|
|
|
director->mainLoop();
|
2014-03-14 17:21:27 +08:00
|
|
|
}
|
2014-03-14 18:40:04 +08:00
|
|
|
|
2014-03-14 17:21:27 +08:00
|
|
|
glview->release();
|
2014-03-14 18:40:04 +08:00
|
|
|
|
2014-04-25 17:01:10 +08:00
|
|
|
return 0;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
2015-07-06 14:57:42 +08:00
|
|
|
void Application::setAnimationInterval(float interval)
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2014-02-21 11:55:37 +08:00
|
|
|
_animationInterval = interval*1000.0f;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 17:29:06 +08:00
|
|
|
Application::Platform Application::getTargetPlatform()
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-07-26 20:24:33 +08:00
|
|
|
return Platform::OS_MAC;
|
2012-08-09 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
2015-10-29 01:57:57 +08:00
|
|
|
std::string Application::getVersion() {
|
|
|
|
NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
|
|
|
|
if (version) {
|
|
|
|
return [version UTF8String];
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// static member function
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-07-15 16:24:42 +08:00
|
|
|
Application* Application::getInstance()
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(sm_pSharedApplication, "sm_pSharedApplication not set");
|
2012-08-21 15:28:43 +08:00
|
|
|
return sm_pSharedApplication;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
2013-07-15 16:37:38 +08:00
|
|
|
// @deprecated Use getInstance() instead
|
|
|
|
Application* Application::sharedApplication()
|
|
|
|
{
|
|
|
|
return Application::getInstance();
|
|
|
|
}
|
|
|
|
|
2014-03-18 02:34:21 +08:00
|
|
|
const char * Application::getCurrentLanguageCode()
|
|
|
|
{
|
|
|
|
static char code[3]={0};
|
|
|
|
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];
|
2014-10-11 15:19:20 +08:00
|
|
|
[languageCode getCString:code maxLength:3 encoding:NSASCIIStringEncoding];
|
2014-03-18 02:34:21 +08:00
|
|
|
code[2]='\0';
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2013-07-26 17:29:06 +08:00
|
|
|
LanguageType Application::getCurrentLanguage()
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
|
|
|
// get the current language and country config
|
|
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
|
|
|
|
NSString *currentLanguage = [languages objectAtIndex:0];
|
2014-12-25 18:02:03 +08:00
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
// 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];
|
2014-12-25 18:02:03 +08:00
|
|
|
|
2015-04-03 15:43:39 +08:00
|
|
|
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;
|
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
2014-09-09 16:07:33 +08:00
|
|
|
bool Application::openURL(const std::string &url)
|
|
|
|
{
|
|
|
|
NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding];
|
|
|
|
NSURL* nsUrl = [NSURL URLWithString:msg];
|
|
|
|
return [[NSWorkspace sharedWorkspace] openURL:nsUrl];
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Application::setResourceRootPath(const std::string& rootResDir)
|
2012-09-05 16:15:09 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_resourceRootPath = rootResDir;
|
|
|
|
if (_resourceRootPath[_resourceRootPath.length() - 1] != '/')
|
2012-09-05 16:15:09 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_resourceRootPath += '/';
|
2012-09-05 16:15:09 +08:00
|
|
|
}
|
2013-07-12 06:24:23 +08:00
|
|
|
FileUtils* pFileUtils = FileUtils::getInstance();
|
2013-01-29 09:56:38 +08:00
|
|
|
std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
|
2013-06-15 14:03:30 +08:00
|
|
|
searchPaths.insert(searchPaths.begin(), _resourceRootPath);
|
2013-01-29 09:56:38 +08:00
|
|
|
pFileUtils->setSearchPaths(searchPaths);
|
2012-09-05 16:15:09 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const std::string& Application::getResourceRootPath(void)
|
2012-09-05 16:15:09 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _resourceRootPath;
|
2012-09-05 16:15:09 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Application::setStartupScriptFilename(const std::string& startupScriptFile)
|
2012-09-05 16:15:09 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_startupScriptFilename = startupScriptFile;
|
|
|
|
std::replace(_startupScriptFilename.begin(), _startupScriptFilename.end(), '\\', '/');
|
2012-09-05 16:15:09 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const std::string& Application::getStartupScriptFilename(void)
|
2012-09-05 16:15:09 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _startupScriptFilename;
|
2012-09-05 16:15:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
2014-01-31 08:51:43 +08:00
|
|
|
|
|
|
|
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_MAC
|