CCApplication-mac.mm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "platform/CCPlatformConfig.h"
  23. #if CC_TARGET_PLATFORM == CC_PLATFORM_MAC
  24. #import <Cocoa/Cocoa.h>
  25. #include <algorithm>
  26. #include "platform/CCApplication.h"
  27. #include "platform/CCFileUtils.h"
  28. #include "math/CCGeometry.h"
  29. #include "base/CCDirector.h"
  30. #include "base/ccUtils.h"
  31. NS_CC_BEGIN
  32. static long getCurrentMillSecond()
  33. {
  34. long lLastTime = 0;
  35. struct timeval stCurrentTime;
  36. gettimeofday(&stCurrentTime,NULL);
  37. lLastTime = stCurrentTime.tv_sec*1000+stCurrentTime.tv_usec*0.001; // milliseconds
  38. return lLastTime;
  39. }
  40. Application* Application::sm_pSharedApplication = nullptr;
  41. Application::Application()
  42. : _animationInterval(1.0f/60.0f*1000.0f)
  43. {
  44. CCASSERT(! sm_pSharedApplication, "sm_pSharedApplication already exist");
  45. sm_pSharedApplication = this;
  46. }
  47. Application::~Application()
  48. {
  49. CCASSERT(this == sm_pSharedApplication, "sm_pSharedApplication != this");
  50. sm_pSharedApplication = 0;
  51. }
  52. int Application::run()
  53. {
  54. initGLContextAttrs();
  55. if(!applicationDidFinishLaunching())
  56. {
  57. return 1;
  58. }
  59. long lastTime = 0L;
  60. long curTime = 0L;
  61. auto director = Director::getInstance();
  62. auto glview = director->getOpenGLView();
  63. // Retain glview to avoid glview being released in the while loop
  64. glview->retain();
  65. while (!glview->windowShouldClose())
  66. {
  67. lastTime = getCurrentMillSecond();
  68. director->mainLoop();
  69. glview->pollEvents();
  70. curTime = getCurrentMillSecond();
  71. if (curTime - lastTime < _animationInterval)
  72. {
  73. usleep(static_cast<useconds_t>((_animationInterval - curTime + lastTime)*1000));
  74. }
  75. }
  76. /* Only work on Desktop
  77. * Director::mainLoop is really one frame logic
  78. * when we want to close the window, we should call Director::end();
  79. * then call Director::mainLoop to do release of internal resources
  80. */
  81. if (glview->isOpenGLReady())
  82. {
  83. director->end();
  84. director->mainLoop();
  85. }
  86. glview->release();
  87. return 0;
  88. }
  89. void Application::setAnimationInterval(float interval)
  90. {
  91. _animationInterval = interval*1000.0f;
  92. }
  93. void Application::setAnimationInterval(float interval, SetIntervalReason reason)
  94. {
  95. setAnimationInterval(interval);
  96. }
  97. Application::Platform Application::getTargetPlatform()
  98. {
  99. return Platform::OS_MAC;
  100. }
  101. std::string Application::getVersion() {
  102. NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
  103. if (version) {
  104. return [version UTF8String];
  105. }
  106. return "";
  107. }
  108. /////////////////////////////////////////////////////////////////////////////////////////////////
  109. // static member function
  110. //////////////////////////////////////////////////////////////////////////////////////////////////
  111. Application* Application::getInstance()
  112. {
  113. CCASSERT(sm_pSharedApplication, "sm_pSharedApplication not set");
  114. return sm_pSharedApplication;
  115. }
  116. // @deprecated Use getInstance() instead
  117. Application* Application::sharedApplication()
  118. {
  119. return Application::getInstance();
  120. }
  121. const char * Application::getCurrentLanguageCode()
  122. {
  123. static char code[3]={0};
  124. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  125. NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
  126. NSString *currentLanguage = [languages objectAtIndex:0];
  127. // get the current language code.(such as English is "en", Chinese is "zh" and so on)
  128. NSDictionary* temp = [NSLocale componentsFromLocaleIdentifier:currentLanguage];
  129. NSString * languageCode = [temp objectForKey:NSLocaleLanguageCode];
  130. [languageCode getCString:code maxLength:3 encoding:NSASCIIStringEncoding];
  131. code[2]='\0';
  132. return code;
  133. }
  134. LanguageType Application::getCurrentLanguage()
  135. {
  136. // get the current language and country config
  137. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  138. NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
  139. NSString *currentLanguage = [languages objectAtIndex:0];
  140. // get the current language code.(such as English is "en", Chinese is "zh" and so on)
  141. NSDictionary* temp = [NSLocale componentsFromLocaleIdentifier:currentLanguage];
  142. NSString * languageCode = [temp objectForKey:NSLocaleLanguageCode];
  143. return utils::getLanguageTypeByISO2([languageCode UTF8String]);
  144. }
  145. bool Application::openURL(const std::string &url)
  146. {
  147. NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding];
  148. NSURL* nsUrl = [NSURL URLWithString:msg];
  149. return [[NSWorkspace sharedWorkspace] openURL:nsUrl];
  150. }
  151. void Application::setResourceRootPath(const std::string& rootResDir)
  152. {
  153. _resourceRootPath = rootResDir;
  154. if (_resourceRootPath[_resourceRootPath.length() - 1] != '/')
  155. {
  156. _resourceRootPath += '/';
  157. }
  158. FileUtils* pFileUtils = FileUtils::getInstance();
  159. std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
  160. searchPaths.insert(searchPaths.begin(), _resourceRootPath);
  161. pFileUtils->setSearchPaths(searchPaths);
  162. }
  163. const std::string& Application::getResourceRootPath(void)
  164. {
  165. return _resourceRootPath;
  166. }
  167. void Application::setStartupScriptFilename(const std::string& startupScriptFile)
  168. {
  169. _startupScriptFilename = startupScriptFile;
  170. std::replace(_startupScriptFilename.begin(), _startupScriptFilename.end(), '\\', '/');
  171. }
  172. const std::string& Application::getStartupScriptFilename(void)
  173. {
  174. return _startupScriptFilename;
  175. }
  176. NS_CC_END
  177. #endif // CC_TARGET_PLATFORM == CC_PLATFORM_MAC