axmol/cocos/platform/winrt/CCApplication.cpp

266 lines
7.2 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2013 cocos2d-x.org
Copyright (c) Microsoft Open 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.
****************************************************************************/
2014-09-10 09:15:16 +08:00
#include "platform/CCPlatformConfig.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
2014-10-15 05:16:24 +08:00
#include "platform/winrt/CCGLViewImpl-winrt.h"
using namespace Windows::UI::Core;
using namespace Windows::Foundation;
#else
2014-09-10 09:22:11 +08:00
#include "platform/wp8/CCGLViewImpl-wp8.h"
#endif
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "base/CCDirector.h"
#include <algorithm>
#include "platform/CCFileUtils.h"
#include "CCWinRTUtils.h"
/**
@brief This function change the PVRFrame show/hide setting in register.
@param bEnable If true show the PVRFrame window, otherwise hide.
*/
NS_CC_BEGIN
// sharedApplication pointer
Application * Application::sm_pSharedApplication = 0;
////////////////////////////////////////////////////////////////////////////////
// implement Application
////////////////////////////////////////////////////////////////////////////////
// sharedApplication pointer
Application * s_pSharedApplication = 0;
2014-09-22 20:47:58 +08:00
Application::Application() :
m_openURLDelegate(nullptr)
{
m_nAnimationInterval.QuadPart = 0;
CC_ASSERT(! sm_pSharedApplication);
sm_pSharedApplication = this;
}
Application::~Application()
{
CC_ASSERT(this == sm_pSharedApplication);
sm_pSharedApplication = NULL;
}
int Application::run()
{
// Initialize instance and cocos2d.
if (!applicationDidFinishLaunching())
{
return 0;
}
2014-08-04 14:17:14 +08:00
GLViewImpl::sharedOpenGLView()->Run();
return 0;
}
void Application::setAnimationInterval(double interval)
{
LARGE_INTEGER nFreq;
QueryPerformanceFrequency(&nFreq);
m_nAnimationInterval.QuadPart = (LONGLONG)(interval * nFreq.QuadPart);
}
//////////////////////////////////////////////////////////////////////////
// static member function
//////////////////////////////////////////////////////////////////////////
Application* Application::getInstance()
{
CC_ASSERT(sm_pSharedApplication);
return sm_pSharedApplication;
}
const char * Application::getCurrentLanguageCode()
{
static std::string code = "en";
#if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
auto languages = Windows::System::UserProfile::GlobalizationPreferences::Languages;
code = PlatformStringToString(languages->GetAt(0));
#else
ULONG numLanguages = 0;
DWORD cchLanguagesBuffer = 0;
BOOL result = GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numLanguages, NULL, &cchLanguagesBuffer);
if (result) {
WCHAR* pwszLanguagesBuffer = new WCHAR[cchLanguagesBuffer];
result = GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numLanguages, pwszLanguagesBuffer, &cchLanguagesBuffer);
if (result) {
code = CCUnicodeToUtf8(pwszLanguagesBuffer);
}
if (pwszLanguagesBuffer)
{
delete pwszLanguagesBuffer;
}
}
#endif
return code.c_str();
}
LanguageType Application::getCurrentLanguage()
{
LanguageType ret = LanguageType::ENGLISH;
const char* code = getCurrentLanguageCode();
if (strncmp(code, "zh", 2) == 0)
{
ret = LanguageType::CHINESE;
}
else if (strncmp(code, "ja", 2) == 0)
{
ret = LanguageType::JAPANESE;
}
else if (strncmp(code, "fr", 2) == 0)
{
ret = LanguageType::FRENCH;
}
else if (strncmp(code, "it", 2) == 0)
{
ret = LanguageType::ITALIAN;
}
else if (strncmp(code, "de", 2) == 0)
{
ret = LanguageType::GERMAN;
}
else if (strncmp(code, "es", 2) == 0)
{
ret = LanguageType::SPANISH;
}
else if (strncmp(code, "nl", 2) == 0)
{
ret = LanguageType::DUTCH;
}
else if (strncmp(code, "ru", 2) == 0)
{
ret = LanguageType::RUSSIAN;
}
else if (strncmp(code, "hu", 2) == 0)
{
ret = LanguageType::HUNGARIAN;
}
else if (strncmp(code, "pt", 2) == 0)
{
ret = LanguageType::PORTUGUESE;
}
else if (strncmp(code, "ko", 2) == 0)
{
ret = LanguageType::KOREAN;
}
else if (strncmp(code, "ar", 2) == 0)
{
ret = LanguageType::ARABIC;
}
else if (strncmp(code, "nb", 2) == 0)
{
ret = LanguageType::NORWEGIAN;
}
else if (strncmp(code, "pl", 2) == 0)
{
ret = LanguageType::POLISH;
}
else if (strncmp(code, "tr", 2) == 0)
{
ret = LanguageType::TURKISH;
}
else if (strncmp(code, "uk", 2) == 0)
{
ret = LanguageType::UKRAINIAN;
}
2015-02-25 23:20:48 +08:00
else if (strncmp(code, "ro", 2) == 0)
{
ret = LanguageType::ROMANIAN;
}
return ret;
}
Application::Platform Application::getTargetPlatform()
{
return Platform::OS_WP8;
}
bool Application::openURL(const std::string &url)
{
#if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
auto dispatcher = cocos2d::GLViewImpl::sharedOpenGLView()->getDispatcher();
dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([url]() {
auto uri = ref new Windows::Foundation::Uri(PlatformStringFromString(url));
concurrency::task<bool> launchUriOperation(Windows::System::Launcher::LaunchUriAsync(uri));
}));
return true;
#else
2014-09-22 20:47:58 +08:00
if (m_openURLDelegate)
{
m_openURLDelegate(PlatformStringFromString(url));
return true;
}
else
{
return false;
}
#endif
}
void Application::setResourceRootPath(const std::string& rootResDir)
{
m_resourceRootPath = rootResDir;
std::replace(m_resourceRootPath.begin(), m_resourceRootPath.end(), '\\', '/');
if (m_resourceRootPath[m_resourceRootPath.length() - 1] != '/')
{
m_resourceRootPath += '/';
}
FileUtils* pFileUtils = FileUtils::getInstance();
std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
searchPaths.insert(searchPaths.begin(), m_resourceRootPath);
pFileUtils->setSearchPaths(searchPaths);
}
const std::string& Application::getResourceRootPath(void)
{
return m_resourceRootPath;
}
void Application::setStartupScriptFilename(const std::string& startupScriptFile)
{
m_startupScriptFilename = startupScriptFile;
std::replace(m_startupScriptFilename.begin(), m_startupScriptFilename.end(), '\\', '/');
}
NS_CC_END