2012-07-21 12:23:40 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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 <Cocoa/Cocoa.h>
|
2012-09-05 16:15:09 +08:00
|
|
|
#include <algorithm>
|
2013-01-28 10:36:37 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2012-09-05 16:15:09 +08:00
|
|
|
#include "CCGeometry.h"
|
2012-07-22 02:46:42 +08:00
|
|
|
#include "CCDirector.h"
|
2012-07-21 12:23:40 +08:00
|
|
|
#import "CCDirectorCaller.h"
|
2013-08-21 12:01:56 +08:00
|
|
|
#include "CCEGLView.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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Application* Application::sm_pSharedApplication = 0;
|
2012-07-21 12:23:40 +08:00
|
|
|
|
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(! 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
|
|
|
{
|
2013-08-21 12:01:56 +08:00
|
|
|
if(!applicationDidFinishLaunching())
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-08-21 12:01:56 +08:00
|
|
|
return 0;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
2013-08-21 12:01:56 +08:00
|
|
|
EGLView* pMainWnd = EGLView::getInstance();
|
|
|
|
|
|
|
|
while (!pMainWnd->windowShouldClose())
|
|
|
|
{
|
|
|
|
Director::getInstance()->mainLoop();
|
|
|
|
pMainWnd->pollEvents();
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
Director::getInstance()->end();
|
|
|
|
Director::getInstance()->mainLoop();
|
2013-08-21 12:01:56 +08:00
|
|
|
return true;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Application::setAnimationInterval(double interval)
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-06-21 08:46:22 +08:00
|
|
|
[[CCDirectorCaller sharedDirectorCaller] setAnimationInterval: interval ];
|
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
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
|
2013-07-26 17:29:06 +08:00
|
|
|
LanguageType ret = LanguageType::ENGLISH;
|
2012-07-21 12:23:40 +08:00
|
|
|
if ([languageCode isEqualToString:@"zh"])
|
|
|
|
{
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::CHINESE;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
else if ([languageCode isEqualToString:@"en"])
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret = LanguageType::ENGLISH;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
2012-10-18 15:57:18 +08:00
|
|
|
else if ([languageCode isEqualToString:@"fr"]){
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::FRENCH;
|
2012-10-18 15:57:18 +08:00
|
|
|
}
|
|
|
|
else if ([languageCode isEqualToString:@"it"]){
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::ITALIAN;
|
2012-10-18 15:57:18 +08:00
|
|
|
}
|
|
|
|
else if ([languageCode isEqualToString:@"de"]){
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::GERMAN;
|
2012-10-18 15:57:18 +08:00
|
|
|
}
|
|
|
|
else if ([languageCode isEqualToString:@"es"]){
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::SPANISH;
|
2012-10-18 15:57:18 +08:00
|
|
|
}
|
|
|
|
else if ([languageCode isEqualToString:@"ru"]){
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::RUSSIAN;
|
2012-10-18 15:57:18 +08:00
|
|
|
}
|
|
|
|
else if ([languageCode isEqualToString:@"ko"]){
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::KOREAN;
|
2012-10-18 15:57:18 +08:00
|
|
|
}
|
2012-11-28 16:53:10 +08:00
|
|
|
else if ([languageCode isEqualToString:@"ja"]){
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::JAPANESE;
|
2012-11-28 16:53:10 +08:00
|
|
|
}
|
2012-12-05 18:31:05 +08:00
|
|
|
else if ([languageCode isEqualToString:@"hu"]){
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::HUNGARIAN;
|
2012-12-05 18:31:05 +08:00
|
|
|
}
|
2013-02-11 23:29:56 +08:00
|
|
|
else if ([languageCode isEqualToString:@"pt"])
|
|
|
|
{
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::PORTUGUESE;
|
2013-02-11 23:29:56 +08:00
|
|
|
}
|
|
|
|
else if ([languageCode isEqualToString:@"ar"])
|
|
|
|
{
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::ARABIC;
|
2013-02-11 23:29:56 +08:00
|
|
|
}
|
2013-05-29 16:16:22 +08:00
|
|
|
else if ([languageCode isEqualToString:@"nb"]){
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::NORWEGIAN;
|
2013-06-11 22:46:38 +08:00
|
|
|
}
|
|
|
|
else if ([languageCode isEqualToString:@"pl"]){
|
2013-07-26 17:29:06 +08:00
|
|
|
ret = LanguageType::POLISH;
|
2013-06-11 22:46:38 +08:00
|
|
|
}
|
2012-07-21 12:23:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|