CCApplication-win32.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "platform/CCPlatformConfig.h"
  22. #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
  23. #include "platform/CCApplication.h"
  24. #include "base/CCDirector.h"
  25. #include <algorithm>
  26. #include "platform/CCFileUtils.h"
  27. #include <shellapi.h>
  28. #include <WinVer.h>
  29. /**
  30. @brief This function change the PVRFrame show/hide setting in register.
  31. @param bEnable If true show the PVRFrame window, otherwise hide.
  32. */
  33. static void PVRFrameEnableControlWindow(bool bEnable);
  34. NS_CC_BEGIN
  35. // sharedApplication pointer
  36. Application * Application::sm_pSharedApplication = nullptr;
  37. Application::Application()
  38. : _instance(nullptr)
  39. , _accelTable(nullptr)
  40. {
  41. _instance = GetModuleHandle(nullptr);
  42. _animationInterval.QuadPart = 0;
  43. CC_ASSERT(! sm_pSharedApplication);
  44. sm_pSharedApplication = this;
  45. }
  46. Application::~Application()
  47. {
  48. CC_ASSERT(this == sm_pSharedApplication);
  49. sm_pSharedApplication = nullptr;
  50. }
  51. int Application::run()
  52. {
  53. PVRFrameEnableControlWindow(false);
  54. ///////////////////////////////////////////////////////////////////////////
  55. /////////////// changing timer resolution
  56. ///////////////////////////////////////////////////////////////////////////
  57. UINT TARGET_RESOLUTION = 1; // 1 millisecond target resolution
  58. TIMECAPS tc;
  59. UINT wTimerRes = 0;
  60. if (TIMERR_NOERROR == timeGetDevCaps(&tc, sizeof(TIMECAPS)))
  61. {
  62. wTimerRes = std::min(std::max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
  63. timeBeginPeriod(wTimerRes);
  64. }
  65. // Main message loop:
  66. LARGE_INTEGER nLast;
  67. LARGE_INTEGER nNow;
  68. QueryPerformanceCounter(&nLast);
  69. initGLContextAttrs();
  70. // Initialize instance and cocos2d.
  71. if (!applicationDidFinishLaunching())
  72. {
  73. return 1;
  74. }
  75. auto director = Director::getInstance();
  76. auto glview = director->getOpenGLView();
  77. // Retain glview to avoid glview being released in the while loop
  78. glview->retain();
  79. LONGLONG interval = 0LL;
  80. LONG waitMS = 0L;
  81. LARGE_INTEGER freq;
  82. QueryPerformanceFrequency(&freq);
  83. while(!glview->windowShouldClose())
  84. {
  85. QueryPerformanceCounter(&nNow);
  86. interval = nNow.QuadPart - nLast.QuadPart;
  87. if (interval >= _animationInterval.QuadPart)
  88. {
  89. nLast.QuadPart = nNow.QuadPart;
  90. director->mainLoop();
  91. glview->pollEvents();
  92. }
  93. else
  94. {
  95. // The precision of timer on Windows is set to highest (1ms) by 'timeBeginPeriod' from above code,
  96. // but it's still not precise enough. For example, if the precision of timer is 1ms,
  97. // Sleep(3) may make a sleep of 2ms or 4ms. Therefore, we subtract 1ms here to make Sleep time shorter.
  98. // If 'waitMS' is equal or less than 1ms, don't sleep and run into next loop to
  99. // boost CPU to next frame accurately.
  100. waitMS = (_animationInterval.QuadPart - interval) * 1000LL / freq.QuadPart - 1L;
  101. if (waitMS > 1L)
  102. Sleep(waitMS);
  103. }
  104. }
  105. // Director should still do a cleanup if the window was closed manually.
  106. if (glview->isOpenGLReady())
  107. {
  108. director->end();
  109. director->mainLoop();
  110. director = nullptr;
  111. }
  112. glview->release();
  113. ///////////////////////////////////////////////////////////////////////////
  114. /////////////// restoring timer resolution
  115. ///////////////////////////////////////////////////////////////////////////
  116. if (wTimerRes != 0)
  117. {
  118. timeEndPeriod(wTimerRes);
  119. }
  120. return 0;
  121. }
  122. void Application::setAnimationInterval(float interval)
  123. {
  124. LARGE_INTEGER freq;
  125. QueryPerformanceFrequency(&freq);
  126. _animationInterval.QuadPart = (LONGLONG)(interval * freq.QuadPart);
  127. }
  128. void Application::setAnimationInterval(float interval, SetIntervalReason reason)
  129. {
  130. setAnimationInterval(interval);
  131. }
  132. //////////////////////////////////////////////////////////////////////////
  133. // static member function
  134. //////////////////////////////////////////////////////////////////////////
  135. Application* Application::getInstance()
  136. {
  137. CC_ASSERT(sm_pSharedApplication);
  138. return sm_pSharedApplication;
  139. }
  140. // @deprecated Use getInstance() instead
  141. Application* Application::sharedApplication()
  142. {
  143. return Application::getInstance();
  144. }
  145. LanguageType Application::getCurrentLanguage()
  146. {
  147. LanguageType ret = LanguageType::ENGLISH;
  148. LCID localeID = GetUserDefaultLCID();
  149. unsigned short primaryLanguageID = localeID & 0xFF;
  150. switch (primaryLanguageID)
  151. {
  152. case LANG_CHINESE:
  153. ret = LanguageType::CHINESE;
  154. break;
  155. case LANG_ENGLISH:
  156. ret = LanguageType::ENGLISH;
  157. break;
  158. case LANG_FRENCH:
  159. ret = LanguageType::FRENCH;
  160. break;
  161. case LANG_ITALIAN:
  162. ret = LanguageType::ITALIAN;
  163. break;
  164. case LANG_GERMAN:
  165. ret = LanguageType::GERMAN;
  166. break;
  167. case LANG_SPANISH:
  168. ret = LanguageType::SPANISH;
  169. break;
  170. case LANG_DUTCH:
  171. ret = LanguageType::DUTCH;
  172. break;
  173. case LANG_RUSSIAN:
  174. ret = LanguageType::RUSSIAN;
  175. break;
  176. case LANG_KOREAN:
  177. ret = LanguageType::KOREAN;
  178. break;
  179. case LANG_JAPANESE:
  180. ret = LanguageType::JAPANESE;
  181. break;
  182. case LANG_HUNGARIAN:
  183. ret = LanguageType::HUNGARIAN;
  184. break;
  185. case LANG_PORTUGUESE:
  186. ret = LanguageType::PORTUGUESE;
  187. break;
  188. case LANG_ARABIC:
  189. ret = LanguageType::ARABIC;
  190. break;
  191. case LANG_NORWEGIAN:
  192. ret = LanguageType::NORWEGIAN;
  193. break;
  194. case LANG_POLISH:
  195. ret = LanguageType::POLISH;
  196. break;
  197. case LANG_TURKISH:
  198. ret = LanguageType::TURKISH;
  199. break;
  200. case LANG_UKRAINIAN:
  201. ret = LanguageType::UKRAINIAN;
  202. break;
  203. case LANG_ROMANIAN:
  204. ret = LanguageType::ROMANIAN;
  205. break;
  206. case LANG_BULGARIAN:
  207. ret = LanguageType::BULGARIAN;
  208. break;
  209. }
  210. return ret;
  211. }
  212. const char * Application::getCurrentLanguageCode()
  213. {
  214. LANGID lid = GetUserDefaultUILanguage();
  215. const LCID locale_id = MAKELCID(lid, SORT_DEFAULT);
  216. static char code[3] = { 0 };
  217. GetLocaleInfoA(locale_id, LOCALE_SISO639LANGNAME, code, sizeof(code));
  218. code[2] = '\0';
  219. return code;
  220. }
  221. Application::Platform Application::getTargetPlatform()
  222. {
  223. return Platform::OS_WINDOWS;
  224. }
  225. std::string Application::getVersion()
  226. {
  227. char verString[256] = { 0 };
  228. TCHAR szVersionFile[MAX_PATH];
  229. GetModuleFileName(NULL, szVersionFile, MAX_PATH);
  230. DWORD verHandle = NULL;
  231. UINT size = 0;
  232. LPBYTE lpBuffer = NULL;
  233. DWORD verSize = GetFileVersionInfoSize(szVersionFile, &verHandle);
  234. if (verSize != NULL)
  235. {
  236. LPSTR verData = new char[verSize];
  237. if (GetFileVersionInfo(szVersionFile, verHandle, verSize, verData))
  238. {
  239. if (VerQueryValue(verData, L"\\", (VOID FAR* FAR*)&lpBuffer, &size))
  240. {
  241. if (size)
  242. {
  243. VS_FIXEDFILEINFO *verInfo = (VS_FIXEDFILEINFO *)lpBuffer;
  244. if (verInfo->dwSignature == 0xfeef04bd)
  245. {
  246. // Doesn't matter if you are on 32 bit or 64 bit,
  247. // DWORD is always 32 bits, so first two revision numbers
  248. // come from dwFileVersionMS, last two come from dwFileVersionLS
  249. sprintf(verString, "%d.%d.%d.%d", (verInfo->dwFileVersionMS >> 16) & 0xffff,
  250. (verInfo->dwFileVersionMS >> 0) & 0xffff,
  251. (verInfo->dwFileVersionLS >> 16) & 0xffff,
  252. (verInfo->dwFileVersionLS >> 0) & 0xffff
  253. );
  254. }
  255. }
  256. }
  257. }
  258. delete[] verData;
  259. }
  260. return verString;
  261. }
  262. bool Application::openURL(const std::string &url)
  263. {
  264. WCHAR *temp = new WCHAR[url.size() + 1];
  265. int wchars_num = MultiByteToWideChar(CP_UTF8, 0, url.c_str(), url.size() + 1, temp, url.size() + 1);
  266. HINSTANCE r = ShellExecuteW(NULL, L"open", temp, NULL, NULL, SW_SHOWNORMAL);
  267. delete[] temp;
  268. return (size_t)r>32;
  269. }
  270. void Application::setResourceRootPath(const std::string& rootResDir)
  271. {
  272. _resourceRootPath = rootResDir;
  273. std::replace(_resourceRootPath.begin(), _resourceRootPath.end(), '\\', '/');
  274. if (_resourceRootPath[_resourceRootPath.length() - 1] != '/')
  275. {
  276. _resourceRootPath += '/';
  277. }
  278. FileUtils* pFileUtils = FileUtils::getInstance();
  279. std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
  280. searchPaths.insert(searchPaths.begin(), _resourceRootPath);
  281. pFileUtils->setSearchPaths(searchPaths);
  282. }
  283. const std::string& Application::getResourceRootPath(void)
  284. {
  285. return _resourceRootPath;
  286. }
  287. void Application::setStartupScriptFilename(const std::string& startupScriptFile)
  288. {
  289. _startupScriptFilename = startupScriptFile;
  290. std::replace(_startupScriptFilename.begin(), _startupScriptFilename.end(), '\\', '/');
  291. }
  292. NS_CC_END
  293. //////////////////////////////////////////////////////////////////////////
  294. // Local function
  295. //////////////////////////////////////////////////////////////////////////
  296. static void PVRFrameEnableControlWindow(bool bEnable)
  297. {
  298. HKEY hKey = 0;
  299. // Open PVRFrame control key, if not exist create it.
  300. if(ERROR_SUCCESS != RegCreateKeyExW(HKEY_CURRENT_USER,
  301. L"Software\\Imagination Technologies\\PVRVFRame\\STARTUP\\",
  302. 0,
  303. 0,
  304. REG_OPTION_NON_VOLATILE,
  305. KEY_ALL_ACCESS,
  306. 0,
  307. &hKey,
  308. nullptr))
  309. {
  310. return;
  311. }
  312. const WCHAR* wszValue = L"hide_gui";
  313. const WCHAR* wszNewData = (bEnable) ? L"NO" : L"YES";
  314. WCHAR wszOldData[256] = {0};
  315. DWORD dwSize = sizeof(wszOldData);
  316. LSTATUS status = RegQueryValueExW(hKey, wszValue, 0, nullptr, (LPBYTE)wszOldData, &dwSize);
  317. if (ERROR_FILE_NOT_FOUND == status // the key not exist
  318. || (ERROR_SUCCESS == status // or the hide_gui value is exist
  319. && 0 != wcscmp(wszNewData, wszOldData))) // but new data and old data not equal
  320. {
  321. dwSize = sizeof(WCHAR) * (wcslen(wszNewData) + 1);
  322. RegSetValueEx(hKey, wszValue, 0, REG_SZ, (const BYTE *)wszNewData, dwSize);
  323. }
  324. RegCloseKey(hKey);
  325. }
  326. #endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32