CCApplication-android.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/CCApplication.h"
  23. #include <EGL/egl.h>
  24. #include <cstring>
  25. #include <jni.h>
  26. #include "platform/android/jni/JniImp.h"
  27. #include "platform/android/CCGL-android.h"
  28. #include "base/CCScheduler.h"
  29. #include "base/CCConfiguration.h"
  30. #include "audio/include/AudioEngine.h"
  31. #include "scripting/js-bindings/jswrapper/SeApi.h"
  32. #include "scripting/js-bindings/event/EventDispatcher.h"
  33. #define LOG_APP_TAG "CCApplication_android Debug"
  34. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_APP_TAG,__VA_ARGS__)
  35. // IDEA: using ndk-r10c will cause the next function could not be found. It may be a bug of ndk-r10c.
  36. // Here is the workaround method to fix the problem.
  37. #ifdef __aarch64__
  38. extern "C" size_t __ctype_get_mb_cur_max(void)
  39. {
  40. return (size_t) sizeof(wchar_t);
  41. }
  42. #endif
  43. PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT = 0;
  44. PFNGLBINDVERTEXARRAYOESPROC glBindVertexArrayOESEXT = 0;
  45. PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT = 0;
  46. NS_CC_BEGIN
  47. void Application::updateViewSize(int width, int height)
  48. {
  49. _viewSize.x = width;
  50. _viewSize.y = height;
  51. // handle resize event
  52. Application::getInstance()->getScheduler()->performFunctionInCocosThread([=]() {
  53. EventDispatcher::dispatchResizeEvent(width, height);
  54. });
  55. }
  56. extern "C" {
  57. void Java_org_cocos2dx_lib_Cocos2dxGLSurfaceView_nativeOnSizeChanged(JNIEnv * env, jobject obj, jint width, jint height) {
  58. auto inst = Application::getInstance();
  59. // nativeOnSizeChanged is firstly called before Application initiating.
  60. if (inst != nullptr) {
  61. inst->updateViewSize(width, height);
  62. }
  63. }
  64. void Java_org_cocos2dx_lib_Cocos2dxOrientationHelper_nativeOnOrientationChanged(JNIEnv * env, jobject obj, jint rotation) {
  65. auto inst = Application::getInstance();
  66. // nativeOnSizeChanged is firstly called before Application initiating.
  67. if (inst != nullptr) {
  68. // handle orientation change event
  69. inst->getScheduler()->performFunctionInCocosThread([=]() {
  70. EventDispatcher::dispatchOrientationChangeEvent(rotation);
  71. });
  72. }
  73. }
  74. }
  75. Application* Application::_instance = nullptr;
  76. std::shared_ptr<Scheduler> Application::_scheduler = nullptr;
  77. Application::Application(const std::string& name, int width, int height)
  78. {
  79. Application::_instance = this;
  80. Configuration::getInstance();
  81. _scheduler = std::make_shared<Scheduler>();
  82. PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT = (PFNGLGENVERTEXARRAYSOESPROC)eglGetProcAddress("glGenVertexArraysOES");
  83. PFNGLBINDVERTEXARRAYOESPROC glBindVertexArrayOESEXT = (PFNGLBINDVERTEXARRAYOESPROC)eglGetProcAddress("glBindVertexArrayOES");
  84. PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT = (PFNGLDELETEVERTEXARRAYSOESPROC)eglGetProcAddress("glDeleteVertexArraysOES");
  85. _renderTexture = new RenderTexture(width, height);
  86. updateViewSize(width, height);
  87. }
  88. Application::~Application()
  89. {
  90. #if USE_AUDIO
  91. AudioEngine::end();
  92. #endif
  93. EventDispatcher::destroy();
  94. se::ScriptEngine::destroyInstance();
  95. delete _renderTexture;
  96. _renderTexture = nullptr;
  97. Application::_instance = nullptr;
  98. }
  99. void Application::start()
  100. {
  101. if(!applicationDidFinishLaunching())
  102. return;
  103. }
  104. void Application::restart()
  105. {
  106. restartJSVM();
  107. }
  108. void Application::end()
  109. {
  110. exitApplication();
  111. }
  112. void Application::setMultitouch(bool /*value*/)
  113. {
  114. }
  115. bool Application::applicationDidFinishLaunching()
  116. {
  117. return true;
  118. }
  119. void Application::onPause()
  120. {
  121. }
  122. void Application::onResume()
  123. {
  124. }
  125. void Application::setPreferredFramesPerSecond(int fps)
  126. {
  127. _fps = fps;
  128. setPreferredFramesPerSecondJNI(_fps);
  129. }
  130. std::string Application::getCurrentLanguageCode() const
  131. {
  132. return getCurrentLanguageCodeJNI();
  133. }
  134. bool Application::isDisplayStats() {
  135. se::AutoHandleScope hs;
  136. se::Value ret;
  137. char commandBuf[100] = "cc.debug.isDisplayStats();";
  138. se::ScriptEngine::getInstance()->evalString(commandBuf, 100, &ret);
  139. return ret.toBoolean();
  140. }
  141. void Application::setDisplayStats(bool isShow) {
  142. se::AutoHandleScope hs;
  143. char commandBuf[100] = {0};
  144. sprintf(commandBuf, "cc.debug.setDisplayStats(%s);", isShow ? "true" : "false");
  145. se::ScriptEngine::getInstance()->evalString(commandBuf);
  146. }
  147. Application::LanguageType Application::getCurrentLanguage() const
  148. {
  149. std::string languageName = getCurrentLanguageJNI();
  150. const char* pLanguageName = languageName.c_str();
  151. LanguageType ret = LanguageType::ENGLISH;
  152. if (0 == strcmp("zh", pLanguageName))
  153. {
  154. ret = LanguageType::CHINESE;
  155. }
  156. else if (0 == strcmp("en", pLanguageName))
  157. {
  158. ret = LanguageType::ENGLISH;
  159. }
  160. else if (0 == strcmp("fr", pLanguageName))
  161. {
  162. ret = LanguageType::FRENCH;
  163. }
  164. else if (0 == strcmp("it", pLanguageName))
  165. {
  166. ret = LanguageType::ITALIAN;
  167. }
  168. else if (0 == strcmp("de", pLanguageName))
  169. {
  170. ret = LanguageType::GERMAN;
  171. }
  172. else if (0 == strcmp("es", pLanguageName))
  173. {
  174. ret = LanguageType::SPANISH;
  175. }
  176. else if (0 == strcmp("ru", pLanguageName))
  177. {
  178. ret = LanguageType::RUSSIAN;
  179. }
  180. else if (0 == strcmp("nl", pLanguageName))
  181. {
  182. ret = LanguageType::DUTCH;
  183. }
  184. else if (0 == strcmp("ko", pLanguageName))
  185. {
  186. ret = LanguageType::KOREAN;
  187. }
  188. else if (0 == strcmp("ja", pLanguageName))
  189. {
  190. ret = LanguageType::JAPANESE;
  191. }
  192. else if (0 == strcmp("hu", pLanguageName))
  193. {
  194. ret = LanguageType::HUNGARIAN;
  195. }
  196. else if (0 == strcmp("pt", pLanguageName))
  197. {
  198. ret = LanguageType::PORTUGUESE;
  199. }
  200. else if (0 == strcmp("ar", pLanguageName))
  201. {
  202. ret = LanguageType::ARABIC;
  203. }
  204. else if (0 == strcmp("nb", pLanguageName))
  205. {
  206. ret = LanguageType::NORWEGIAN;
  207. }
  208. else if (0 == strcmp("pl", pLanguageName))
  209. {
  210. ret = LanguageType::POLISH;
  211. }
  212. else if (0 == strcmp("tr", pLanguageName))
  213. {
  214. ret = LanguageType::TURKISH;
  215. }
  216. else if (0 == strcmp("uk", pLanguageName))
  217. {
  218. ret = LanguageType::UKRAINIAN;
  219. }
  220. else if (0 == strcmp("ro", pLanguageName))
  221. {
  222. ret = LanguageType::ROMANIAN;
  223. }
  224. else if (0 == strcmp("bg", pLanguageName))
  225. {
  226. ret = LanguageType::BULGARIAN;
  227. }
  228. return ret;
  229. }
  230. Application::Platform Application::getPlatform() const
  231. {
  232. return Platform::ANDROIDOS;
  233. }
  234. float Application::getScreenScale() const
  235. {
  236. return 1.f;
  237. }
  238. GLint Application::getMainFBO() const
  239. {
  240. return _mainFBO;
  241. }
  242. void Application::onCreateView(PixelFormat& /*pixelformat*/, DepthFormat& /*depthFormat*/, int& /*multisamplingCount*/)
  243. {
  244. }
  245. bool Application::openURL(const std::string &url)
  246. {
  247. return openURLJNI(url);
  248. }
  249. void Application::copyTextToClipboard(const std::string &text)
  250. {
  251. copyTextToClipboardJNI(text);
  252. }
  253. std::string Application::getSystemVersion()
  254. {
  255. return getSystemVersionJNI();
  256. }
  257. const cocos2d::Vec2& Application::getViewSize() const
  258. {
  259. return _viewSize;
  260. }
  261. NS_CC_END