axmol/cocos/2d/platform/win32/CCApplication.cpp

273 lines
7.9 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 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.
****************************************************************************/
2014-01-31 08:51:43 +08:00
2014-04-27 01:35:57 +08:00
#include "base/CCPlatformConfig.h"
2014-01-31 08:51:43 +08:00
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
#include "CCApplication.h"
2014-01-31 08:51:43 +08:00
#include "CCGLView.h"
#include "2d/CCDirector.h"
#include <algorithm>
#include "2d/platform/CCFileUtils.h"
/**
@brief This function change the PVRFrame show/hide setting in register.
@param bEnable If true show the PVRFrame window, otherwise hide.
*/
static void PVRFrameEnableControlWindow(bool bEnable);
NS_CC_BEGIN
// sharedApplication pointer
Application * Application::sm_pSharedApplication = 0;
Application::Application()
: _instance(nullptr)
, _accelTable(nullptr)
{
_instance = GetModuleHandle(nullptr);
_animationInterval.QuadPart = 0;
CC_ASSERT(! sm_pSharedApplication);
sm_pSharedApplication = this;
}
Application::~Application()
{
CC_ASSERT(this == sm_pSharedApplication);
sm_pSharedApplication = NULL;
}
int Application::run()
{
PVRFrameEnableControlWindow(false);
// Main message loop:
LARGE_INTEGER nFreq;
LARGE_INTEGER nLast;
LARGE_INTEGER nNow;
QueryPerformanceFrequency(&nFreq);
QueryPerformanceCounter(&nLast);
// Initialize instance and cocos2d.
if (!applicationDidFinishLaunching())
{
return 0;
}
2014-01-24 08:04:11 +08:00
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
// Retain glview to avoid glview being released in the while loop
glview->retain();
2013-08-29 13:48:58 +08:00
2014-01-24 08:04:11 +08:00
while(!glview->windowShouldClose())
{
2013-08-20 13:57:15 +08:00
QueryPerformanceCounter(&nNow);
if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart)
{
2013-08-20 13:57:15 +08:00
nLast.QuadPart = nNow.QuadPart;
director->mainLoop();
glview->pollEvents();
}
2013-08-20 13:57:15 +08:00
else
{
2013-08-20 13:57:15 +08:00
Sleep(0);
}
}
// Director should still do a cleanup if the window was closed manually.
if (glview->isOpenGLReady())
{
director->end();
director->mainLoop();
director = nullptr;
}
glview->release();
2013-08-20 13:57:15 +08:00
return true;
}
void Application::setAnimationInterval(double interval)
{
LARGE_INTEGER nFreq;
QueryPerformanceFrequency(&nFreq);
_animationInterval.QuadPart = (LONGLONG)(interval * nFreq.QuadPart);
}
//////////////////////////////////////////////////////////////////////////
// static member function
//////////////////////////////////////////////////////////////////////////
Application* Application::getInstance()
{
CC_ASSERT(sm_pSharedApplication);
return sm_pSharedApplication;
}
// @deprecated Use getInstance() instead
Application* Application::sharedApplication()
{
return Application::getInstance();
}
LanguageType Application::getCurrentLanguage()
{
LanguageType ret = LanguageType::ENGLISH;
LCID localeID = GetUserDefaultLCID();
unsigned short primaryLanguageID = localeID & 0xFF;
switch (primaryLanguageID)
{
case LANG_CHINESE:
ret = LanguageType::CHINESE;
break;
case LANG_ENGLISH:
ret = LanguageType::ENGLISH;
break;
case LANG_FRENCH:
ret = LanguageType::FRENCH;
break;
case LANG_ITALIAN:
ret = LanguageType::ITALIAN;
break;
case LANG_GERMAN:
ret = LanguageType::GERMAN;
break;
case LANG_SPANISH:
ret = LanguageType::SPANISH;
break;
2014-01-26 04:38:15 +08:00
case LANG_DUTCH:
ret = LanguageType::DUTCH;
break;
case LANG_RUSSIAN:
ret = LanguageType::RUSSIAN;
break;
case LANG_KOREAN:
ret = LanguageType::KOREAN;
break;
2012-11-28 16:53:10 +08:00
case LANG_JAPANESE:
ret = LanguageType::JAPANESE;
2012-11-28 16:53:10 +08:00
break;
2012-12-05 18:31:05 +08:00
case LANG_HUNGARIAN:
ret = LanguageType::HUNGARIAN;
2012-12-05 18:31:05 +08:00
break;
case LANG_PORTUGUESE:
ret = LanguageType::PORTUGUESE;
break;
case LANG_ARABIC:
ret = LanguageType::ARABIC;
break;
2013-06-11 22:43:35 +08:00
case LANG_NORWEGIAN:
ret = LanguageType::NORWEGIAN;
2013-05-29 16:16:22 +08:00
break;
2013-06-11 22:46:38 +08:00
case LANG_POLISH:
ret = LanguageType::POLISH;
2013-06-11 22:46:38 +08:00
break;
}
return ret;
}
const char * Application::getCurrentLanguageCode()
{
LANGID lid = GetUserDefaultUILanguage();
const LCID locale_id = MAKELCID(lid, SORT_DEFAULT);
static char code[3] = { 0 };
GetLocaleInfoA(locale_id, LOCALE_SISO639LANGNAME, code, sizeof(code));
code[2] = '\0';
return code;
}
Application::Platform Application::getTargetPlatform()
{
return Platform::OS_WINDOWS;
}
void Application::setResourceRootPath(const std::string& rootResDir)
{
_resourceRootPath = rootResDir;
std::replace(_resourceRootPath.begin(), _resourceRootPath.end(), '\\', '/');
if (_resourceRootPath[_resourceRootPath.length() - 1] != '/')
{
_resourceRootPath += '/';
}
FileUtils* pFileUtils = FileUtils::getInstance();
std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
searchPaths.insert(searchPaths.begin(), _resourceRootPath);
pFileUtils->setSearchPaths(searchPaths);
}
const std::string& Application::getResourceRootPath(void)
{
return _resourceRootPath;
}
void Application::setStartupScriptFilename(const std::string& startupScriptFile)
{
_startupScriptFilename = startupScriptFile;
std::replace(_startupScriptFilename.begin(), _startupScriptFilename.end(), '\\', '/');
}
NS_CC_END
//////////////////////////////////////////////////////////////////////////
// Local function
//////////////////////////////////////////////////////////////////////////
static void PVRFrameEnableControlWindow(bool bEnable)
{
HKEY hKey = 0;
// Open PVRFrame control key, if not exist create it.
if(ERROR_SUCCESS != RegCreateKeyExW(HKEY_CURRENT_USER,
L"Software\\Imagination Technologies\\PVRVFRame\\STARTUP\\",
0,
0,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
0,
&hKey,
NULL))
{
return;
}
const WCHAR* wszValue = L"hide_gui";
const WCHAR* wszNewData = (bEnable) ? L"NO" : L"YES";
WCHAR wszOldData[256] = {0};
DWORD dwSize = sizeof(wszOldData);
LSTATUS status = RegQueryValueExW(hKey, wszValue, 0, NULL, (LPBYTE)wszOldData, &dwSize);
if (ERROR_FILE_NOT_FOUND == status // the key not exist
|| (ERROR_SUCCESS == status // or the hide_gui value is exist
&& 0 != wcscmp(wszNewData, wszOldData))) // but new data and old data not equal
{
dwSize = sizeof(WCHAR) * (wcslen(wszNewData) + 1);
RegSetValueEx(hKey, wszValue, 0, REG_SZ, (const BYTE *)wszNewData, dwSize);
}
RegCloseKey(hKey);
}
2014-01-31 08:51:43 +08:00
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32