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.
|
|
|
|
|
|
|
|
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 <Cocoa/Cocoa.h>
|
|
|
|
#import <Metal/Metal.h>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "platform/CCApplication.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2021-10-24 11:07:54 +08:00
|
|
|
#include "math/CCMath.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/ccUtils.h"
|
|
|
|
#include "renderer/backend/metal/DeviceMTL.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-09-05 20:45:32 +08:00
|
|
|
static int32_t getCurrentMillSecond()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-09-05 20:45:32 +08:00
|
|
|
int32_t lLastTime = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
struct timeval stCurrentTime;
|
2021-12-28 11:00:34 +08:00
|
|
|
|
|
|
|
gettimeofday(&stCurrentTime, NULL);
|
|
|
|
lLastTime = stCurrentTime.tv_sec * 1000 + stCurrentTime.tv_usec * 0.001; // milliseconds
|
2019-11-23 20:27:39 +08:00
|
|
|
return lLastTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
Application* Application::sm_pSharedApplication = nullptr;
|
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
Application::Application() : _animationInterval(1.0f / 60.0f * 1000.0f)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 11:00:34 +08:00
|
|
|
CCASSERT(!sm_pSharedApplication, "sm_pSharedApplication already exist");
|
2019-11-23 20:27:39 +08:00
|
|
|
sm_pSharedApplication = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Application::~Application()
|
|
|
|
{
|
|
|
|
CCASSERT(this == sm_pSharedApplication, "sm_pSharedApplication != this");
|
|
|
|
sm_pSharedApplication = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Application::run()
|
|
|
|
{
|
|
|
|
initGLContextAttrs();
|
2021-12-28 11:00:34 +08:00
|
|
|
if (!applicationDidFinishLaunching())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2021-09-05 20:45:32 +08:00
|
|
|
int32_t lastTime = 0L;
|
2021-12-28 11:00:34 +08:00
|
|
|
int32_t curTime = 0L;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto director = Director::getInstance();
|
2021-12-28 11:00:34 +08:00
|
|
|
auto glview = director->getOpenGLView();
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Retain glview to avoid glview being released in the while loop
|
|
|
|
glview->retain();
|
|
|
|
|
|
|
|
while (!glview->windowShouldClose())
|
|
|
|
{
|
|
|
|
lastTime = getCurrentMillSecond();
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
director->mainLoop();
|
|
|
|
glview->pollEvents();
|
|
|
|
|
|
|
|
curTime = getCurrentMillSecond();
|
|
|
|
if (curTime - lastTime < _animationInterval)
|
|
|
|
{
|
2021-12-28 11:00:34 +08:00
|
|
|
usleep(static_cast<useconds_t>((_animationInterval - curTime + lastTime) * 1000));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only work on Desktop
|
2021-12-28 11:00:34 +08:00
|
|
|
* 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
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
if (glview->isOpenGLReady())
|
|
|
|
{
|
|
|
|
director->end();
|
|
|
|
director->mainLoop();
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
glview->release();
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::setAnimationInterval(float interval)
|
|
|
|
{
|
2021-12-28 11:00:34 +08:00
|
|
|
_animationInterval = interval * 1000.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Application::Platform Application::getTargetPlatform()
|
|
|
|
{
|
|
|
|
return Platform::OS_MAC;
|
|
|
|
}
|
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
std::string Application::getVersion()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
|
2021-12-28 11:00:34 +08:00
|
|
|
if (version)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return [version UTF8String];
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// static member function
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
Application* Application::getInstance()
|
|
|
|
{
|
|
|
|
CCASSERT(sm_pSharedApplication, "sm_pSharedApplication not set");
|
|
|
|
return sm_pSharedApplication;
|
|
|
|
}
|
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
const char* Application::getCurrentLanguageCode()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 11:00:34 +08:00
|
|
|
static char code[3] = {0};
|
|
|
|
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
NSArray* languages = [defaults objectForKey:@"AppleLanguages"];
|
|
|
|
NSString* currentLanguage = [languages objectAtIndex:0];
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// get the current language code.(such as English is "en", Chinese is "zh" and so on)
|
2021-12-28 11:00:34 +08:00
|
|
|
NSDictionary* temp = [NSLocale componentsFromLocaleIdentifier:currentLanguage];
|
|
|
|
NSString* languageCode = [temp objectForKey:NSLocaleLanguageCode];
|
2019-11-23 20:27:39 +08:00
|
|
|
[languageCode getCString:code maxLength:3 encoding:NSASCIIStringEncoding];
|
2021-12-28 11:00:34 +08:00
|
|
|
code[2] = '\0';
|
2019-11-23 20:27:39 +08:00
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
LanguageType Application::getCurrentLanguage()
|
|
|
|
{
|
|
|
|
// get the current language and country config
|
2021-12-28 11:00:34 +08:00
|
|
|
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
NSArray* languages = [defaults objectForKey:@"AppleLanguages"];
|
|
|
|
NSString* currentLanguage = [languages objectAtIndex:0];
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// get the current language code.(such as English is "en", Chinese is "zh" and so on)
|
2021-12-28 11:00:34 +08:00
|
|
|
NSDictionary* temp = [NSLocale componentsFromLocaleIdentifier:currentLanguage];
|
|
|
|
NSString* languageCode = [temp objectForKey:NSLocaleLanguageCode];
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return utils::getLanguageTypeByISO2([languageCode UTF8String]);
|
|
|
|
}
|
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
bool Application::openURL(const std::string& url)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding];
|
2021-12-28 11:00:34 +08:00
|
|
|
NSURL* nsUrl = [NSURL URLWithString:msg];
|
2019-11-23 20:27:39 +08:00
|
|
|
return [[NSWorkspace sharedWorkspace] openURL:nsUrl];
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void Application::setStartupScriptFilename(std::string_view startupScriptFile)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_startupScriptFilename = startupScriptFile;
|
|
|
|
std::replace(_startupScriptFilename.begin(), _startupScriptFilename.end(), '\\', '/');
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view Application::getStartupScriptFilename()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _startupScriptFilename;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|