AndroidDevice.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <jni.h>
  24. #include "Allocator.h"
  25. #include "Device.h"
  26. #include "Log.h"
  27. #include "OsEventQueue.h"
  28. #include "Renderer.h"
  29. #include "Touch.h"
  30. #include "OsWindow.h"
  31. namespace crown
  32. {
  33. class AndroidDevice : public Device
  34. {
  35. public:
  36. //-----------------------------------------------------------------------------
  37. AndroidDevice()
  38. : m_game_thread("game_thread")
  39. {
  40. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  41. m_fileserver = 1;
  42. #endif
  43. }
  44. //-----------------------------------------------------------------------------
  45. int32_t run(int, char**)
  46. {
  47. m_game_thread.start(main_loop, (void*)this);
  48. //while (true) {}
  49. return 0;
  50. }
  51. //-----------------------------------------------------------------------------
  52. int32_t loop()
  53. {
  54. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  55. m_console = CE_NEW(default_allocator(), ConsoleServer)();
  56. m_console->init(m_console_port, false);
  57. #endif
  58. Device::init();
  59. while (is_running() && !process_events())
  60. {
  61. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  62. m_console->update();
  63. #endif
  64. Device::frame();
  65. m_touch->update();
  66. m_keyboard->update();
  67. }
  68. Device::shutdown();
  69. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  70. m_console->shutdown();
  71. CE_DELETE(default_allocator(), m_console);
  72. #endif
  73. exit(0);
  74. return 0; // Just to not get a warning
  75. }
  76. //-----------------------------------------------------------------------------
  77. static int32_t main_loop(void* thiz)
  78. {
  79. return ((AndroidDevice*) thiz)->loop();
  80. }
  81. //-----------------------------------------------------------------------------
  82. bool process_events()
  83. {
  84. OsEvent event;
  85. while (m_queue.pop_event(event))
  86. {
  87. if (event.type == OsEvent::NONE) continue;
  88. switch (event.type)
  89. {
  90. case OsEvent::TOUCH:
  91. {
  92. const OsTouchEvent& ev = event.touch;
  93. switch (ev.type)
  94. {
  95. case OsTouchEvent::POINTER: m_touch->set_pointer_state(ev.x, ev.y, ev.pointer_id, ev.pressed); break;
  96. case OsTouchEvent::MOVE: m_touch->set_position(ev.pointer_id, ev.x, ev.y); break;
  97. default: CE_FATAL("Oops, unknown touch event type"); break;
  98. }
  99. break;
  100. }
  101. case OsEvent::KEYBOARD:
  102. {
  103. const OsKeyboardEvent& ev = event.keyboard;
  104. m_keyboard->set_button_state(ev.button, ev.pressed);
  105. break;
  106. }
  107. case OsEvent::METRICS:
  108. {
  109. const OsMetricsEvent& ev = event.metrics;
  110. m_window->m_x = 0;
  111. m_window->m_y = 0;
  112. m_window->m_width = ev.width;
  113. m_window->m_height = ev.height;
  114. break;
  115. }
  116. case OsEvent::EXIT:
  117. {
  118. return true;
  119. }
  120. case OsEvent::PAUSE:
  121. {
  122. pause();
  123. break;
  124. }
  125. case OsEvent::RESUME:
  126. {
  127. unpause();
  128. break;
  129. }
  130. default:
  131. {
  132. CE_FATAL("Unknown Os Event");
  133. break;
  134. }
  135. }
  136. }
  137. return false;
  138. }
  139. //-----------------------------------------------------------------------------
  140. void push_keyboard_event(uint32_t modifier, KeyboardButton::Enum b, bool pressed)
  141. {
  142. m_queue.push_keyboard_event(modifier, b, pressed);
  143. }
  144. //-----------------------------------------------------------------------------
  145. void push_touch_event(uint16_t x, uint16_t y, uint8_t pointer_id)
  146. {
  147. m_queue.push_touch_event(x, y, pointer_id);
  148. }
  149. //-----------------------------------------------------------------------------
  150. void push_touch_event(uint16_t x, uint16_t y, uint8_t pointer_id, bool pressed)
  151. {
  152. m_queue.push_touch_event(x, y, pointer_id, pressed);
  153. }
  154. void push_metrics_event(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  155. {
  156. m_queue.push_metrics_event(x, y, width, height);
  157. }
  158. //-----------------------------------------------------------------------------
  159. void push_pause_event()
  160. {
  161. m_queue.push_pause_event();
  162. }
  163. //-----------------------------------------------------------------------------
  164. void push_resume_event()
  165. {
  166. m_queue.push_resume_event();
  167. }
  168. //-----------------------------------------------------------------------------
  169. void push_exit_event(int32_t code)
  170. {
  171. m_queue.push_exit_event(code);
  172. }
  173. private:
  174. OsEventQueue m_queue;
  175. OsThread m_game_thread;
  176. };
  177. static AndroidDevice* g_engine;
  178. ANativeWindow* g_android_window;
  179. //-----------------------------------------------------------------------------
  180. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_initCrown(JNIEnv* /*env*/, jobject /*obj*/)
  181. {
  182. memory::init();
  183. os::init_os();
  184. g_engine = CE_NEW(default_allocator(), AndroidDevice);
  185. set_device(g_engine);
  186. }
  187. //-----------------------------------------------------------------------------
  188. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_shutdownCrown(JNIEnv* /*env*/, jobject /*obj*/)
  189. {
  190. CE_DELETE(default_allocator(), g_engine);
  191. memory::shutdown();
  192. }
  193. //-----------------------------------------------------------------------------
  194. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_run(JNIEnv* /*env*/, jobject /*obj*/)
  195. {
  196. g_engine->run(0, NULL);
  197. }
  198. //-----------------------------------------------------------------------------
  199. extern "C" void Java_crown_android_CrownLib_acquireWindow(JNIEnv *env, jclass /*clazz*/, jobject surface)
  200. {
  201. // Obtain a native window from a Java surface
  202. CE_ASSERT(surface != 0, "Unable to get Android window");
  203. g_android_window = ANativeWindow_fromSurface(env, surface);
  204. ANativeWindow_acquire(g_android_window);
  205. }
  206. //-----------------------------------------------------------------------------
  207. extern "C" void Java_crown_android_CrownLib_releaseWindow(JNIEnv *env, jclass /*clazz*/, jobject surface)
  208. {
  209. ANativeWindow_release(g_android_window);
  210. }
  211. //-----------------------------------------------------------------------------
  212. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushKeyboardEvent(JNIEnv * /*env*/, jobject /*obj*/, jint modifier, jint b, jint pressed)
  213. {
  214. g_engine->push_keyboard_event(modifier, (KeyboardButton::Enum) b, pressed);
  215. }
  216. //-----------------------------------------------------------------------------
  217. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushTouchEventMove(JNIEnv * /*env*/, jobject /*obj*/, jint pointer_id, jint x, jint y)
  218. {
  219. g_engine->push_touch_event(x, y, pointer_id);
  220. }
  221. //-----------------------------------------------------------------------------
  222. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushTouchEventPointer(JNIEnv * /*env*/, jobject /*obj*/, jint pointer_id, jint x, jint y, jint pressed)
  223. {
  224. g_engine->push_touch_event(x, y, pointer_id, pressed);
  225. }
  226. //-----------------------------------------------------------------------------
  227. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushAccelerometerEvent(JNIEnv * /*env*/, jobject /*obj*/, jint type, jfloat x, jfloat y, jfloat z)
  228. {
  229. }
  230. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushMetricsEvent(JNIEnv * /*env*/, jobject /*obj*/, jint x, jint y, jint width, jint height)
  231. {
  232. g_engine->push_metrics_event(x, y, width, height);
  233. }
  234. //-----------------------------------------------------------------------------
  235. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushPauseEvent(JNIEnv * /*env*/, jobject /*obj*/)
  236. {
  237. g_engine->push_pause_event();
  238. }
  239. //-----------------------------------------------------------------------------
  240. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushResumeEvent(JNIEnv * /*env*/, jobject /*obj*/)
  241. {
  242. g_engine->push_resume_event();
  243. }
  244. //-----------------------------------------------------------------------------
  245. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushExitEvent(JNIEnv * /*env*/, jobject /*obj*/, jint code)
  246. {
  247. g_engine->push_exit_event(code);
  248. }
  249. } // namespace crown