os_android.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include "os_android.h"
  2. #include "java_glue.h"
  3. #include "drivers/gles2/rasterizer_gles2.h"
  4. #include "servers/visual/visual_server_raster.h"
  5. #include "file_access_jandroid.h"
  6. #include "dir_access_jandroid.h"
  7. #include "core/io/file_access_buffered_fa.h"
  8. #include "main/main.h"
  9. int OS_Android::get_video_driver_count() const {
  10. return 1;
  11. }
  12. const char * OS_Android::get_video_driver_name(int p_driver) const {
  13. return "GLES2";
  14. }
  15. OS::VideoMode OS_Android::get_default_video_mode() const {
  16. return OS::VideoMode();
  17. }
  18. int OS_Android::get_audio_driver_count() const {
  19. return 1;
  20. }
  21. const char * OS_Android::get_audio_driver_name(int p_driver) const {
  22. return "Android";
  23. }
  24. void OS_Android::initialize_core() {
  25. OS_Unix::initialize_core();
  26. //FileAccessJAndroid::make_default();
  27. DirAccessJAndroid::make_default();
  28. FileAccessBufferedFA<FileAccessJAndroid>::make_default();
  29. }
  30. void OS_Android::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver) {
  31. AudioDriverManagerSW::add_driver(&audio_driver_android);
  32. rasterizer = memnew( RasterizerGLES2 );
  33. visual_server = memnew( VisualServerRaster(rasterizer) );
  34. visual_server->init();
  35. visual_server->cursor_set_visible(false, 0);
  36. AudioDriverManagerSW::get_driver(p_audio_driver)->set_singleton();
  37. if (AudioDriverManagerSW::get_driver(p_audio_driver)->init()!=OK) {
  38. ERR_PRINT("Initializing audio failed.");
  39. }
  40. sample_manager = memnew( SampleManagerMallocSW );
  41. audio_server = memnew( AudioServerSW(sample_manager) );
  42. audio_server->set_mixer_params(AudioMixerSW::INTERPOLATION_LINEAR,false);
  43. audio_server->init();
  44. spatial_sound_server = memnew( SpatialSoundServerSW );
  45. spatial_sound_server->init();
  46. spatial_sound_2d_server = memnew( SpatialSound2DServerSW );
  47. spatial_sound_2d_server->init();
  48. //
  49. physics_server = memnew( PhysicsServerSW );
  50. physics_server->init();
  51. physics_2d_server = memnew( Physics2DServerSW );
  52. physics_2d_server->init();
  53. input = memnew( InputDefault );
  54. }
  55. void OS_Android::set_main_loop( MainLoop * p_main_loop ) {
  56. main_loop=p_main_loop;
  57. }
  58. void OS_Android::delete_main_loop() {
  59. memdelete( main_loop );
  60. }
  61. void OS_Android::finalize() {
  62. memdelete(input);
  63. }
  64. void OS_Android::vprint(const char* p_format, va_list p_list, bool p_stderr) {
  65. __android_log_vprint(p_stderr?ANDROID_LOG_ERROR:ANDROID_LOG_INFO,"godot",p_format,p_list);
  66. }
  67. void OS_Android::print(const char *p_format, ... ) {
  68. va_list argp;
  69. va_start(argp, p_format);
  70. __android_log_vprint(ANDROID_LOG_INFO,"godot",p_format,argp);
  71. va_end(argp);
  72. }
  73. void OS_Android::alert(const String& p_alert) {
  74. print("ALERT: %s\n",p_alert.utf8().get_data());
  75. }
  76. void OS_Android::set_mouse_show(bool p_show) {
  77. //android has no mouse...
  78. }
  79. void OS_Android::set_mouse_grab(bool p_grab) {
  80. //it really has no mouse...!
  81. }
  82. bool OS_Android::is_mouse_grab_enabled() const {
  83. //*sigh* technology has evolved so much since i was a kid..
  84. return false;
  85. }
  86. Point2 OS_Android::get_mouse_pos() const {
  87. return Point2();
  88. }
  89. int OS_Android::get_mouse_button_state() const {
  90. return 0;
  91. }
  92. void OS_Android::set_window_title(const String& p_title) {
  93. }
  94. //interesting byt not yet
  95. //void set_clipboard(const String& p_text);
  96. //String get_clipboard() const;
  97. void OS_Android::set_video_mode(const VideoMode& p_video_mode,int p_screen) {
  98. }
  99. OS::VideoMode OS_Android::get_video_mode(int p_screen) const {
  100. return default_videomode;
  101. }
  102. void OS_Android::get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen) const {
  103. p_list->push_back(default_videomode);
  104. }
  105. String OS_Android::get_name() {
  106. return "Android";
  107. }
  108. MainLoop *OS_Android::get_main_loop() const {
  109. return main_loop;
  110. }
  111. bool OS_Android::can_draw() const {
  112. return true; //always?
  113. }
  114. void OS_Android::set_cursor_shape(CursorShape p_shape) {
  115. //android really really really has no mouse.. how amazing..
  116. }
  117. void OS_Android::main_loop_begin() {
  118. if (main_loop)
  119. main_loop->init();
  120. }
  121. bool OS_Android::main_loop_iterate() {
  122. if (!main_loop)
  123. return false;
  124. return Main::iteration();
  125. }
  126. void OS_Android::main_loop_end() {
  127. if (main_loop)
  128. main_loop->finish();
  129. }
  130. void OS_Android::process_touch(int p_what,int p_pointer, const Vector<TouchPos>& p_points) {
  131. switch(p_what) {
  132. case 0: { //gesture begin
  133. if (touch.size()) {
  134. //end all if exist
  135. InputEvent ev;
  136. ev.type=InputEvent::MOUSE_BUTTON;
  137. ev.ID=++last_id;
  138. ev.mouse_button.button_index=BUTTON_LEFT;
  139. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  140. ev.mouse_button.pressed=false;
  141. ev.mouse_button.x=touch[0].pos.x;
  142. ev.mouse_button.y=touch[0].pos.y;
  143. ev.mouse_button.global_x=touch[0].pos.x;
  144. ev.mouse_button.global_y=touch[0].pos.y;
  145. input->set_mouse_pos(Point2(ev.mouse_button.x,ev.mouse_button.y));
  146. main_loop->input_event(ev);
  147. for(int i=0;i<touch.size();i++) {
  148. InputEvent ev;
  149. ev.type=InputEvent::SCREEN_TOUCH;
  150. ev.ID=++last_id;
  151. ev.screen_touch.index=touch[i].id;
  152. ev.screen_touch.pressed=false;
  153. ev.screen_touch.x=touch[i].pos.x;
  154. ev.screen_touch.y=touch[i].pos.y;
  155. main_loop->input_event(ev);
  156. }
  157. }
  158. touch.resize(p_points.size());
  159. for(int i=0;i<p_points.size();i++) {
  160. touch[i].id=p_points[i].id;
  161. touch[i].pos=p_points[i].pos;
  162. }
  163. {
  164. //send mouse
  165. InputEvent ev;
  166. ev.type=InputEvent::MOUSE_BUTTON;
  167. ev.ID=++last_id;
  168. ev.mouse_button.button_index=BUTTON_LEFT;
  169. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  170. ev.mouse_button.pressed=true;
  171. ev.mouse_button.x=touch[0].pos.x;
  172. ev.mouse_button.y=touch[0].pos.y;
  173. ev.mouse_button.global_x=touch[0].pos.x;
  174. ev.mouse_button.global_y=touch[0].pos.y;
  175. last_mouse=touch[0].pos;
  176. input->set_mouse_pos(Point2(ev.mouse_button.x,ev.mouse_button.y));
  177. main_loop->input_event(ev);
  178. }
  179. //send touch
  180. for(int i=0;i<touch.size();i++) {
  181. InputEvent ev;
  182. ev.type=InputEvent::SCREEN_TOUCH;
  183. ev.ID=++last_id;
  184. ev.screen_touch.index=touch[i].id;
  185. ev.screen_touch.pressed=true;
  186. ev.screen_touch.x=touch[i].pos.x;
  187. ev.screen_touch.y=touch[i].pos.y;
  188. main_loop->input_event(ev);
  189. }
  190. } break;
  191. case 1: { //motion
  192. if (p_points.size()) {
  193. //send mouse, should look for point 0?
  194. InputEvent ev;
  195. ev.type=InputEvent::MOUSE_MOTION;
  196. ev.ID=++last_id;
  197. ev.mouse_motion.button_mask=BUTTON_MASK_LEFT;
  198. ev.mouse_motion.x=p_points[0].pos.x;
  199. ev.mouse_motion.y=p_points[0].pos.y;
  200. input->set_mouse_pos(Point2(ev.mouse_motion.x,ev.mouse_motion.y));
  201. ev.mouse_motion.speed_x=input->get_mouse_speed().x;
  202. ev.mouse_motion.speed_y=input->get_mouse_speed().y;
  203. ev.mouse_motion.relative_x=p_points[0].pos.x-last_mouse.x;
  204. ev.mouse_motion.relative_y=p_points[0].pos.y-last_mouse.y;
  205. last_mouse=p_points[0].pos;
  206. main_loop->input_event(ev);
  207. }
  208. ERR_FAIL_COND(touch.size()!=p_points.size());
  209. for(int i=0;i<touch.size();i++) {
  210. int idx=-1;
  211. for(int j=0;j<p_points.size();j++) {
  212. if (touch[i].id==p_points[j].id) {
  213. idx=j;
  214. break;
  215. }
  216. }
  217. ERR_CONTINUE(idx==-1);
  218. if (touch[i].pos==p_points[idx].pos)
  219. continue; //no move unncesearily
  220. InputEvent ev;
  221. ev.type=InputEvent::SCREEN_DRAG;
  222. ev.ID=++last_id;
  223. ev.screen_drag.index=touch[i].id;
  224. ev.screen_drag.x=p_points[idx].pos.x;
  225. ev.screen_drag.y=p_points[idx].pos.y;
  226. ev.screen_drag.x=p_points[idx].pos.x - touch[i].pos.x;
  227. ev.screen_drag.y=p_points[idx].pos.y - touch[i].pos.y;
  228. main_loop->input_event(ev);
  229. touch[i].pos=p_points[idx].pos;
  230. }
  231. } break;
  232. case 2: { //release
  233. if (touch.size()) {
  234. //end all if exist
  235. InputEvent ev;
  236. ev.type=InputEvent::MOUSE_BUTTON;
  237. ev.ID=++last_id;
  238. ev.mouse_button.button_index=BUTTON_LEFT;
  239. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  240. ev.mouse_button.pressed=false;
  241. ev.mouse_button.x=touch[0].pos.x;
  242. ev.mouse_button.y=touch[0].pos.y;
  243. ev.mouse_button.global_x=touch[0].pos.x;
  244. ev.mouse_button.global_y=touch[0].pos.y;
  245. main_loop->input_event(ev);
  246. for(int i=0;i<touch.size();i++) {
  247. InputEvent ev;
  248. ev.type=InputEvent::SCREEN_TOUCH;
  249. ev.ID=++last_id;
  250. ev.screen_touch.index=touch[i].id;
  251. ev.screen_touch.pressed=false;
  252. ev.screen_touch.x=touch[i].pos.x;
  253. ev.screen_touch.y=touch[i].pos.y;
  254. main_loop->input_event(ev);
  255. }
  256. }
  257. } break;
  258. case 3: { // add tuchi
  259. ERR_FAIL_INDEX(p_pointer,p_points.size());
  260. TouchPos tp=p_points[p_pointer];
  261. touch.push_back(tp);
  262. InputEvent ev;
  263. ev.type=InputEvent::SCREEN_TOUCH;
  264. ev.ID=++last_id;
  265. ev.screen_touch.index=tp.id;
  266. ev.screen_touch.pressed=true;
  267. ev.screen_touch.x=tp.pos.x;
  268. ev.screen_touch.y=tp.pos.y;
  269. main_loop->input_event(ev);
  270. } break;
  271. case 4: {
  272. for(int i=0;i<touch.size();i++) {
  273. if (touch[i].id==p_pointer) {
  274. InputEvent ev;
  275. ev.type=InputEvent::SCREEN_TOUCH;
  276. ev.ID=++last_id;
  277. ev.screen_touch.index=touch[i].id;
  278. ev.screen_touch.pressed=false;
  279. ev.screen_touch.x=touch[i].pos.x;
  280. ev.screen_touch.y=touch[i].pos.y;
  281. main_loop->input_event(ev);
  282. touch.remove(i);
  283. i--;
  284. }
  285. }
  286. } break;
  287. }
  288. }
  289. OS_Android::OS_Android(int p_video_width,int p_video_height) {
  290. default_videomode.width=p_video_width;
  291. default_videomode.height=p_video_height;
  292. default_videomode.fullscreen=true;
  293. default_videomode.resizable=false;
  294. main_loop=NULL;
  295. last_id=1;
  296. }
  297. OS_Android::~OS_Android() {
  298. }