javascript_main.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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-2015 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. uint32_t m = glutGetModifiers();
  70. ev.key.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  71. ev.key.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  72. ev.key.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  73. os->push_input(ev);
  74. }
  75. static void _glut_skey_up(int key, int x, int y) {
  76. _glut_skey(false,key);
  77. }
  78. static void _glut_skey_down(int key, int x, int y) {
  79. _glut_skey(true,key);
  80. }
  81. static void _glut_key(bool pressed,unsigned char key) {
  82. InputEvent ev;
  83. ev.type=InputEvent::KEY;
  84. ev.key.pressed=pressed;
  85. switch(key) {
  86. case '\n': ev.key.scancode=KEY_RETURN; break;
  87. case 0x1b: ev.key.scancode=KEY_ESCAPE; break;
  88. case 8: ev.key.scancode=KEY_BACKSPACE; break;
  89. case 0x7f: ev.key.scancode=KEY_DELETE; break;
  90. case 0x20: ev.key.scancode=KEY_SPACE; ev.key.unicode=key; break;
  91. default: {
  92. ev.key.unicode=key;
  93. }
  94. }
  95. uint32_t m = glutGetModifiers();
  96. ev.key.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  97. ev.key.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  98. ev.key.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  99. os->push_input(ev);
  100. }
  101. static void _glut_key_up(unsigned char key, int x, int y) {
  102. _glut_key(false,key);
  103. }
  104. static void _glut_key_down(unsigned char key, int x, int y) {
  105. _glut_key(true,key);
  106. }
  107. static uint32_t _mouse_button_mask=0;
  108. static void _glut_mouse_button(int button, int state, int x, int y) {
  109. InputEvent ev;
  110. ev.type=InputEvent::MOUSE_BUTTON;
  111. switch(button) {
  112. case GLUT_LEFT_BUTTON: ev.mouse_button.button_index=BUTTON_LEFT; break;
  113. case GLUT_MIDDLE_BUTTON: ev.mouse_button.button_index=BUTTON_MIDDLE; break;
  114. case GLUT_RIGHT_BUTTON: ev.mouse_button.button_index=BUTTON_RIGHT; break;
  115. case 3: ev.mouse_button.button_index=BUTTON_WHEEL_UP; break;
  116. case 4: ev.mouse_button.button_index=BUTTON_WHEEL_DOWN; break;
  117. }
  118. ev.mouse_button.pressed=state==GLUT_DOWN;
  119. ev.mouse_button.x=x;
  120. ev.mouse_button.y=y;
  121. ev.mouse_button.global_x=x;
  122. ev.mouse_button.global_y=y;
  123. if (ev.mouse_button.button_index<4) {
  124. if (ev.mouse_button.pressed) {
  125. _mouse_button_mask|=1<<ev.mouse_button.button_index;
  126. } else {
  127. _mouse_button_mask&=~(1<<ev.mouse_button.button_index);
  128. }
  129. }
  130. uint32_t m = glutGetModifiers();
  131. ev.mouse_button.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  132. ev.mouse_button.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  133. ev.mouse_button.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  134. os->push_input(ev);
  135. }
  136. static int _glut_prev_x=0;
  137. static int _glut_prev_y=0;
  138. static void _glut_mouse_motion(int x, int y) {
  139. InputEvent ev;
  140. ev.type=InputEvent::MOUSE_MOTION;
  141. ev.mouse_motion.button_mask=_mouse_button_mask;
  142. ev.mouse_motion.x=x;
  143. ev.mouse_motion.y=y;
  144. ev.mouse_motion.global_x=x;
  145. ev.mouse_motion.global_y=y;
  146. ev.mouse_motion.relative_x=x-_glut_prev_x;
  147. ev.mouse_motion.relative_y=y-_glut_prev_y;
  148. _glut_prev_x=x;
  149. _glut_prev_y=y;
  150. uint32_t m = glutGetModifiers();
  151. ev.mouse_motion.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  152. ev.mouse_motion.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  153. ev.mouse_motion.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  154. os->push_input(ev);
  155. }
  156. static void _gfx_idle() {
  157. glutPostRedisplay();
  158. }
  159. int start_step=0;
  160. static void _godot_draw(void) {
  161. if (start_step==1) {
  162. start_step=2;
  163. Main::start();
  164. os->main_loop_begin();
  165. }
  166. if (start_step==2) {
  167. os->main_loop_iterate();
  168. }
  169. glutSwapBuffers();
  170. }
  171. extern "C" {
  172. void main_after_fs_sync(int value) {
  173. start_step=1;
  174. printf("FS SYNCHED!\n");
  175. }
  176. }
  177. int main(int argc, char *argv[]) {
  178. /* Initialize the window */
  179. printf("let it go!\n");
  180. glutInit(&argc, argv);
  181. os = new OS_JavaScript(_gfx_init,NULL,NULL,NULL,NULL);
  182. #if 0
  183. char *args[]={"-test","gui","-v",NULL};
  184. Error err = Main::setup("apk",3,args);
  185. #else
  186. // char *args[]={"-v",NULL};//
  187. // Error err = Main::setup("",1,args);
  188. Error err = Main::setup("",0,NULL);
  189. #endif
  190. ResourceLoader::set_abort_on_missing_resources(false); //ease up compatibility
  191. glutSpecialUpFunc(_glut_skey_up);
  192. glutSpecialFunc(_glut_skey_down);
  193. glutKeyboardUpFunc(_glut_key_up);
  194. glutKeyboardFunc(_glut_key_down);
  195. glutMouseFunc(_glut_mouse_button);
  196. glutMotionFunc(_glut_mouse_motion);
  197. glutMotionFunc(_glut_mouse_motion);
  198. glutPassiveMotionFunc(_glut_mouse_motion);
  199. /* Set up glut callback functions */
  200. glutIdleFunc (_gfx_idle);
  201. // glutReshapeFunc(gears_reshape);
  202. glutDisplayFunc(_godot_draw);
  203. //glutSpecialFunc(gears_special);
  204. //mount persistent filesystem
  205. EM_ASM(
  206. FS.mkdir('/userfs');
  207. FS.mount(IDBFS, {}, '/userfs');
  208. // sync from persisted state into memory and then
  209. // run the 'test' function
  210. FS.syncfs(true, function (err) {
  211. assert(!err);
  212. console.log("done syncinc!");
  213. _after_sync_cb = Module.cwrap('main_after_fs_sync', 'void',['number']);
  214. _after_sync_cb(0);
  215. });
  216. );
  217. glutMainLoop();
  218. return 0;
  219. }
  220. /*
  221. *
  222. *09] <azakai|2__> reduz: yes, define TOTAL_MEMORY on Module. for example var Module = { TOTAL_MEMORY: 12345.. }; before the main
  223. *
  224. */