javascript_main.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*************************************************************************/
  2. /* javascript_main.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include <GL/glut.h>
  30. #include "os_javascript.h"
  31. #include "main/main.h"
  32. #include "io/resource_loader.h"
  33. #include "os/keyboard.h"
  34. #include "emscripten.h"
  35. #include <string.h>
  36. OS_JavaScript *os=NULL;
  37. static void _gfx_init(void *ud,bool gl2,int w, int h,bool fs) {
  38. glutInitWindowSize(w, h);
  39. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  40. glutCreateWindow("godot");
  41. }
  42. static void _glut_skey(bool pressed,int key) {
  43. InputEvent ev;
  44. ev.type=InputEvent::KEY;
  45. ev.key.pressed=pressed;
  46. switch(key) {
  47. case GLUT_KEY_F1: ev.key.scancode=KEY_F1; break;
  48. case GLUT_KEY_F2: ev.key.scancode=KEY_F2; break;
  49. case GLUT_KEY_F3: ev.key.scancode=KEY_F3; break;
  50. case GLUT_KEY_F4: ev.key.scancode=KEY_F4; break;
  51. case GLUT_KEY_F5: ev.key.scancode=KEY_F5; break;
  52. case GLUT_KEY_F6: ev.key.scancode=KEY_F6; break;
  53. case GLUT_KEY_F7: ev.key.scancode=KEY_F7; break;
  54. case GLUT_KEY_F8: ev.key.scancode=KEY_F8; break;
  55. case GLUT_KEY_F9: ev.key.scancode=KEY_F9; break;
  56. case GLUT_KEY_F10: ev.key.scancode=KEY_F10; break;
  57. case GLUT_KEY_F11: ev.key.scancode=KEY_F11; break;
  58. case GLUT_KEY_F12: ev.key.scancode=KEY_F12; break;
  59. case GLUT_KEY_LEFT: ev.key.scancode=KEY_LEFT; break;
  60. case GLUT_KEY_UP: ev.key.scancode=KEY_UP; break;
  61. case GLUT_KEY_RIGHT: ev.key.scancode=KEY_RIGHT; break;
  62. case GLUT_KEY_DOWN: ev.key.scancode=KEY_DOWN; break;
  63. case GLUT_KEY_PAGE_UP: ev.key.scancode=KEY_PAGEUP; break;
  64. case GLUT_KEY_PAGE_DOWN: ev.key.scancode=KEY_PAGEDOWN; break;
  65. case GLUT_KEY_HOME: ev.key.scancode=KEY_HOME; break;
  66. case GLUT_KEY_END: ev.key.scancode=KEY_END; break;
  67. case GLUT_KEY_INSERT: ev.key.scancode=KEY_INSERT; break;
  68. }
  69. if (pressed) {
  70. if (os->skey_pressed[key])
  71. ev.key.echo = true;
  72. else
  73. os->skey_pressed[key] = true;
  74. }
  75. else {
  76. os->skey_pressed[key] = false;
  77. }
  78. uint32_t m = glutGetModifiers();
  79. ev.key.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  80. ev.key.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  81. ev.key.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  82. os->push_input(ev);
  83. }
  84. static void _glut_skey_up(int key, int x, int y) {
  85. _glut_skey(false,key);
  86. }
  87. static void _glut_skey_down(int key, int x, int y) {
  88. _glut_skey(true,key);
  89. }
  90. static void _glut_key(bool pressed,unsigned char key) {
  91. InputEvent ev;
  92. ev.type=InputEvent::KEY;
  93. ev.key.pressed=pressed;
  94. switch(key) {
  95. case '\n': ev.key.scancode=KEY_RETURN; break;
  96. case 0x1b: ev.key.scancode=KEY_ESCAPE; break;
  97. case 8: ev.key.scancode=KEY_BACKSPACE; break;
  98. case 0x7f: ev.key.scancode=KEY_DELETE; break;
  99. case 0x20: ev.key.scancode=KEY_SPACE; ev.key.unicode=key; break;
  100. default: {
  101. ev.key.unicode=key;
  102. }
  103. if (pressed) {
  104. if (os->key_pressed[key])
  105. ev.key.echo = true;
  106. else
  107. os->key_pressed[key] = true;
  108. }
  109. else {
  110. os->key_pressed[key] = false;
  111. }
  112. }
  113. uint32_t m = glutGetModifiers();
  114. ev.key.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  115. ev.key.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  116. ev.key.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  117. os->push_input(ev);
  118. }
  119. static void _glut_key_up(unsigned char key, int x, int y) {
  120. _glut_key(false,key);
  121. }
  122. static void _glut_key_down(unsigned char key, int x, int y) {
  123. _glut_key(true,key);
  124. }
  125. static uint32_t _mouse_button_mask=0;
  126. static void _glut_mouse_button(int button, int state, int x, int y) {
  127. InputEvent ev;
  128. ev.type=InputEvent::MOUSE_BUTTON;
  129. switch(button) {
  130. case GLUT_LEFT_BUTTON: ev.mouse_button.button_index=BUTTON_LEFT; break;
  131. case GLUT_MIDDLE_BUTTON: ev.mouse_button.button_index=BUTTON_MIDDLE; break;
  132. case GLUT_RIGHT_BUTTON: ev.mouse_button.button_index=BUTTON_RIGHT; break;
  133. case 3: ev.mouse_button.button_index=BUTTON_WHEEL_UP; break;
  134. case 4: ev.mouse_button.button_index=BUTTON_WHEEL_DOWN; break;
  135. }
  136. ev.mouse_button.pressed=state==GLUT_DOWN;
  137. ev.mouse_button.x=x;
  138. ev.mouse_button.y=y;
  139. ev.mouse_button.global_x=x;
  140. ev.mouse_button.global_y=y;
  141. if (ev.mouse_button.button_index<4) {
  142. if (ev.mouse_button.pressed) {
  143. _mouse_button_mask|=1<<ev.mouse_button.button_index;
  144. } else {
  145. _mouse_button_mask&=~(1<<ev.mouse_button.button_index);
  146. }
  147. }
  148. uint32_t m = glutGetModifiers();
  149. ev.mouse_button.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  150. ev.mouse_button.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  151. ev.mouse_button.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  152. os->push_input(ev);
  153. }
  154. static int _glut_prev_x=0;
  155. static int _glut_prev_y=0;
  156. static void _glut_mouse_motion(int x, int y) {
  157. InputEvent ev;
  158. ev.type=InputEvent::MOUSE_MOTION;
  159. ev.mouse_motion.button_mask=_mouse_button_mask;
  160. ev.mouse_motion.x=x;
  161. ev.mouse_motion.y=y;
  162. ev.mouse_motion.global_x=x;
  163. ev.mouse_motion.global_y=y;
  164. ev.mouse_motion.relative_x=x-_glut_prev_x;
  165. ev.mouse_motion.relative_y=y-_glut_prev_y;
  166. _glut_prev_x=x;
  167. _glut_prev_y=y;
  168. uint32_t m = glutGetModifiers();
  169. ev.mouse_motion.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  170. ev.mouse_motion.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  171. ev.mouse_motion.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  172. os->push_input(ev);
  173. }
  174. static void _gfx_idle() {
  175. glutPostRedisplay();
  176. }
  177. int start_step=0;
  178. static void _godot_draw(void) {
  179. if (start_step==1) {
  180. start_step=2;
  181. Main::start();
  182. os->main_loop_begin();
  183. }
  184. if (start_step==2) {
  185. os->main_loop_iterate();
  186. }
  187. glutSwapBuffers();
  188. }
  189. extern "C" {
  190. void main_after_fs_sync(int value) {
  191. start_step=1;
  192. printf("FS SYNCHED!\n");
  193. }
  194. }
  195. int main(int argc, char *argv[]) {
  196. /* Initialize the window */
  197. printf("let it go!\n");
  198. glutInit(&argc, argv);
  199. os = new OS_JavaScript(_gfx_init,NULL,NULL,NULL,NULL);
  200. #if 0
  201. char *args[]={"-test","gui","-v",NULL};
  202. Error err = Main::setup("apk",3,args);
  203. #else
  204. // char *args[]={"-v",NULL};//
  205. // Error err = Main::setup("",1,args);
  206. Error err = Main::setup("",0,NULL);
  207. #endif
  208. ResourceLoader::set_abort_on_missing_resources(false); //ease up compatibility
  209. glutSpecialUpFunc(_glut_skey_up);
  210. glutSpecialFunc(_glut_skey_down);
  211. glutKeyboardUpFunc(_glut_key_up);
  212. glutKeyboardFunc(_glut_key_down);
  213. glutMouseFunc(_glut_mouse_button);
  214. glutMotionFunc(_glut_mouse_motion);
  215. glutMotionFunc(_glut_mouse_motion);
  216. glutPassiveMotionFunc(_glut_mouse_motion);
  217. /* Set up glut callback functions */
  218. glutIdleFunc (_gfx_idle);
  219. // glutReshapeFunc(gears_reshape);
  220. glutDisplayFunc(_godot_draw);
  221. //glutSpecialFunc(gears_special);
  222. //mount persistent filesystem
  223. EM_ASM(
  224. FS.mkdir('/userfs');
  225. FS.mount(IDBFS, {}, '/userfs');
  226. // sync from persisted state into memory and then
  227. // run the 'test' function
  228. FS.syncfs(true, function (err) {
  229. assert(!err);
  230. console.log("done syncinc!");
  231. _after_sync_cb = Module.cwrap('main_after_fs_sync', 'void',['number']);
  232. _after_sync_cb(0);
  233. });
  234. );
  235. glutMainLoop();
  236. return 0;
  237. }
  238. /*
  239. *
  240. *09] <azakai|2__> reduz: yes, define TOTAL_MEMORY on Module. for example var Module = { TOTAL_MEMORY: 12345.. }; before the main
  241. *
  242. */