CCApplication.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /****************************************************************************
  2. Copyright (c) 2010-2013 cocos2d-x.org
  3. Copyright (c) Microsoft Open 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_WINRT
  23. #include "platform/winrt/CCGLViewImpl-winrt.h"
  24. using namespace Windows::UI::Core;
  25. using namespace Windows::Foundation;
  26. #else
  27. #include "platform/wp8/CCGLViewImpl-wp8.h"
  28. #endif
  29. #include "base/CCDirector.h"
  30. #include <algorithm>
  31. #include "platform/CCFileUtils.h"
  32. #include "platform/winrt/CCWinRTUtils.h"
  33. #include "platform/CCApplication.h"
  34. #include "tinyxml2/tinyxml2.h"
  35. /**
  36. @brief This function change the PVRFrame show/hide setting in register.
  37. @param bEnable If true show the PVRFrame window, otherwise hide.
  38. */
  39. NS_CC_BEGIN
  40. // sharedApplication pointer
  41. Application * Application::sm_pSharedApplication = nullptr;
  42. ////////////////////////////////////////////////////////////////////////////////
  43. // implement Application
  44. ////////////////////////////////////////////////////////////////////////////////
  45. // sharedApplication pointer
  46. Application * s_pSharedApplication = nullptr;
  47. Application::Application() :
  48. m_openURLDelegate(nullptr)
  49. {
  50. m_nAnimationInterval.QuadPart = 0;
  51. CC_ASSERT(! sm_pSharedApplication);
  52. sm_pSharedApplication = this;
  53. }
  54. Application::~Application()
  55. {
  56. CC_ASSERT(this == sm_pSharedApplication);
  57. sm_pSharedApplication = nullptr;
  58. }
  59. int Application::run()
  60. {
  61. // Initialize instance and cocos2d.
  62. if (!applicationDidFinishLaunching())
  63. {
  64. return 0;
  65. }
  66. GLViewImpl::sharedOpenGLView()->Run();
  67. return 0;
  68. }
  69. void Application::setAnimationInterval(float interval)
  70. {
  71. LARGE_INTEGER nFreq;
  72. QueryPerformanceFrequency(&nFreq);
  73. m_nAnimationInterval.QuadPart = (LONGLONG)(interval * nFreq.QuadPart);
  74. }
  75. void Application::setAnimationInterval(float interval, SetIntervalReason reason)
  76. {
  77. setAnimationInterval(interval);
  78. }
  79. //////////////////////////////////////////////////////////////////////////
  80. // static member function
  81. //////////////////////////////////////////////////////////////////////////
  82. Application* Application::getInstance()
  83. {
  84. CC_ASSERT(sm_pSharedApplication);
  85. return sm_pSharedApplication;
  86. }
  87. const char * Application::getCurrentLanguageCode()
  88. {
  89. static std::string code = "en";
  90. #if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
  91. auto languages = Windows::System::UserProfile::GlobalizationPreferences::Languages;
  92. code = PlatformStringToString(languages->GetAt(0));
  93. #else
  94. ULONG numLanguages = 0;
  95. DWORD cchLanguagesBuffer = 0;
  96. BOOL result = GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numLanguages, NULL, &cchLanguagesBuffer);
  97. if (result) {
  98. WCHAR* pwszLanguagesBuffer = new WCHAR[cchLanguagesBuffer];
  99. result = GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numLanguages, pwszLanguagesBuffer, &cchLanguagesBuffer);
  100. if (result) {
  101. code = StringWideCharToUtf8(pwszLanguagesBuffer);
  102. }
  103. if (pwszLanguagesBuffer)
  104. {
  105. delete [] pwszLanguagesBuffer;
  106. }
  107. }
  108. #endif
  109. return code.c_str();
  110. }
  111. LanguageType Application::getCurrentLanguage()
  112. {
  113. LanguageType ret = LanguageType::ENGLISH;
  114. const char* code = getCurrentLanguageCode();
  115. if (strncmp(code, "zh", 2) == 0)
  116. {
  117. ret = LanguageType::CHINESE;
  118. }
  119. else if (strncmp(code, "ja", 2) == 0)
  120. {
  121. ret = LanguageType::JAPANESE;
  122. }
  123. else if (strncmp(code, "fr", 2) == 0)
  124. {
  125. ret = LanguageType::FRENCH;
  126. }
  127. else if (strncmp(code, "it", 2) == 0)
  128. {
  129. ret = LanguageType::ITALIAN;
  130. }
  131. else if (strncmp(code, "de", 2) == 0)
  132. {
  133. ret = LanguageType::GERMAN;
  134. }
  135. else if (strncmp(code, "es", 2) == 0)
  136. {
  137. ret = LanguageType::SPANISH;
  138. }
  139. else if (strncmp(code, "nl", 2) == 0)
  140. {
  141. ret = LanguageType::DUTCH;
  142. }
  143. else if (strncmp(code, "ru", 2) == 0)
  144. {
  145. ret = LanguageType::RUSSIAN;
  146. }
  147. else if (strncmp(code, "hu", 2) == 0)
  148. {
  149. ret = LanguageType::HUNGARIAN;
  150. }
  151. else if (strncmp(code, "pt", 2) == 0)
  152. {
  153. ret = LanguageType::PORTUGUESE;
  154. }
  155. else if (strncmp(code, "ko", 2) == 0)
  156. {
  157. ret = LanguageType::KOREAN;
  158. }
  159. else if (strncmp(code, "ar", 2) == 0)
  160. {
  161. ret = LanguageType::ARABIC;
  162. }
  163. else if (strncmp(code, "nb", 2) == 0)
  164. {
  165. ret = LanguageType::NORWEGIAN;
  166. }
  167. else if (strncmp(code, "pl", 2) == 0)
  168. {
  169. ret = LanguageType::POLISH;
  170. }
  171. else if (strncmp(code, "tr", 2) == 0)
  172. {
  173. ret = LanguageType::TURKISH;
  174. }
  175. else if (strncmp(code, "uk", 2) == 0)
  176. {
  177. ret = LanguageType::UKRAINIAN;
  178. }
  179. else if (strncmp(code, "ro", 2) == 0)
  180. {
  181. ret = LanguageType::ROMANIAN;
  182. }
  183. else if (strncmp(code, "bg", 2) == 0)
  184. {
  185. ret = LanguageType::BULGARIAN;
  186. }
  187. return ret;
  188. }
  189. Application::Platform Application::getTargetPlatform()
  190. {
  191. if (isWindowsPhone())
  192. {
  193. return Platform::OS_WP8;
  194. }
  195. else
  196. {
  197. return Platform::OS_WINRT;
  198. }
  199. }
  200. std::string Application::getVersion()
  201. {
  202. std::string r("");
  203. std::string s = FileUtils::getInstance()->getStringFromFile("WMAppManifest.xml");
  204. if (!s.empty()) {
  205. tinyxml2::XMLDocument doc;
  206. if (!doc.Parse(s.c_str())) {
  207. tinyxml2::XMLElement *app = doc.RootElement()->FirstChildElement("App");
  208. if (app) {
  209. const char* version = app->Attribute("Version");
  210. if (version) {
  211. r = version;
  212. }
  213. }
  214. }
  215. }
  216. return r;
  217. }
  218. bool Application::openURL(const std::string &url)
  219. {
  220. #if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
  221. auto dispatcher = cocos2d::GLViewImpl::sharedOpenGLView()->getDispatcher();
  222. dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([url]() {
  223. auto uri = ref new Windows::Foundation::Uri(PlatformStringFromString(url));
  224. concurrency::task<bool> launchUriOperation(Windows::System::Launcher::LaunchUriAsync(uri));
  225. }));
  226. return true;
  227. #else
  228. if (m_openURLDelegate)
  229. {
  230. m_openURLDelegate(PlatformStringFromString(url));
  231. return true;
  232. }
  233. else
  234. {
  235. return false;
  236. }
  237. #endif
  238. }
  239. void Application::setResourceRootPath(const std::string& rootResDir)
  240. {
  241. m_resourceRootPath = rootResDir;
  242. std::replace(m_resourceRootPath.begin(), m_resourceRootPath.end(), '\\', '/');
  243. if (m_resourceRootPath[m_resourceRootPath.length() - 1] != '/')
  244. {
  245. m_resourceRootPath += '/';
  246. }
  247. FileUtils* pFileUtils = FileUtils::getInstance();
  248. std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
  249. searchPaths.insert(searchPaths.begin(), m_resourceRootPath);
  250. pFileUtils->setSearchPaths(searchPaths);
  251. }
  252. const std::string& Application::getResourceRootPath(void)
  253. {
  254. return m_resourceRootPath;
  255. }
  256. void Application::setStartupScriptFilename(const std::string& startupScriptFile)
  257. {
  258. m_startupScriptFilename = startupScriptFile;
  259. std::replace(m_startupScriptFilename.begin(), m_startupScriptFilename.end(), '\\', '/');
  260. }
  261. NS_CC_END