javascript_main.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "emscripten.h"
  34. #include <string.h>
  35. OS_JavaScript *os=NULL;
  36. static void _gfx_init(void *ud,bool gl2,int w, int h,bool fs) {
  37. glutInitWindowSize(w, h);
  38. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  39. glutCreateWindow("godot");
  40. }
  41. static uint32_t _mouse_button_mask=0;
  42. static void _glut_mouse_button(int button, int state, int x, int y) {
  43. InputEvent ev;
  44. ev.type=InputEvent::MOUSE_BUTTON;
  45. switch(button) {
  46. case GLUT_LEFT_BUTTON: ev.mouse_button.button_index=BUTTON_LEFT; break;
  47. case GLUT_MIDDLE_BUTTON: ev.mouse_button.button_index=BUTTON_MIDDLE; break;
  48. case GLUT_RIGHT_BUTTON: ev.mouse_button.button_index=BUTTON_RIGHT; break;
  49. case 3: ev.mouse_button.button_index=BUTTON_WHEEL_UP; break;
  50. case 4: ev.mouse_button.button_index=BUTTON_WHEEL_DOWN; break;
  51. }
  52. ev.mouse_button.pressed=state==GLUT_DOWN;
  53. ev.mouse_button.x=x;
  54. ev.mouse_button.y=y;
  55. ev.mouse_button.global_x=x;
  56. ev.mouse_button.global_y=y;
  57. if (ev.mouse_button.button_index<4) {
  58. if (ev.mouse_button.pressed) {
  59. _mouse_button_mask |= 1 << (ev.mouse_button.button_index-1);
  60. } else {
  61. _mouse_button_mask &= ~(1 << (ev.mouse_button.button_index-1));
  62. }
  63. }
  64. ev.mouse_button.button_mask=_mouse_button_mask;
  65. uint32_t m = glutGetModifiers();
  66. ev.mouse_button.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  67. ev.mouse_button.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  68. ev.mouse_button.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  69. os->push_input(ev);
  70. if (ev.mouse_button.button_index==BUTTON_WHEEL_UP || ev.mouse_button.button_index==BUTTON_WHEEL_DOWN) {
  71. // GLUT doesn't send release events for mouse wheel, so send manually
  72. ev.mouse_button.pressed=false;
  73. os->push_input(ev);
  74. }
  75. }
  76. static int _glut_prev_x=0;
  77. static int _glut_prev_y=0;
  78. static void _glut_mouse_motion(int x, int y) {
  79. InputEvent ev;
  80. ev.type=InputEvent::MOUSE_MOTION;
  81. ev.mouse_motion.button_mask=_mouse_button_mask;
  82. ev.mouse_motion.x=x;
  83. ev.mouse_motion.y=y;
  84. ev.mouse_motion.global_x=x;
  85. ev.mouse_motion.global_y=y;
  86. ev.mouse_motion.relative_x=x-_glut_prev_x;
  87. ev.mouse_motion.relative_y=y-_glut_prev_y;
  88. _glut_prev_x=x;
  89. _glut_prev_y=y;
  90. uint32_t m = glutGetModifiers();
  91. ev.mouse_motion.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  92. ev.mouse_motion.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  93. ev.mouse_motion.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  94. os->push_input(ev);
  95. }
  96. static void _gfx_idle() {
  97. glutPostRedisplay();
  98. }
  99. int start_step=0;
  100. static void _godot_draw(void) {
  101. if (start_step==1) {
  102. start_step=2;
  103. Main::start();
  104. os->main_loop_begin();
  105. }
  106. if (start_step==2) {
  107. os->main_loop_iterate();
  108. }
  109. glutSwapBuffers();
  110. }
  111. extern "C" {
  112. void main_after_fs_sync(int value) {
  113. start_step=1;
  114. printf("FS SYNCHED!\n");
  115. }
  116. }
  117. int main(int argc, char *argv[]) {
  118. /* Initialize the window */
  119. printf("let it go!\n");
  120. glutInit(&argc, argv);
  121. os = new OS_JavaScript(_gfx_init,NULL,NULL);
  122. #if 0
  123. char *args[]={"-test","gui","-v",NULL};
  124. Error err = Main::setup("apk",3,args);
  125. #else
  126. // char *args[]={"-v",NULL};//
  127. // Error err = Main::setup("",1,args);
  128. Error err = Main::setup("",0,NULL);
  129. #endif
  130. ResourceLoader::set_abort_on_missing_resources(false); //ease up compatibility
  131. glutMouseFunc(_glut_mouse_button);
  132. glutMotionFunc(_glut_mouse_motion);
  133. glutPassiveMotionFunc(_glut_mouse_motion);
  134. /* Set up glut callback functions */
  135. glutIdleFunc (_gfx_idle);
  136. // glutReshapeFunc(gears_reshape);
  137. glutDisplayFunc(_godot_draw);
  138. //glutSpecialFunc(gears_special);
  139. //mount persistent filesystem
  140. EM_ASM(
  141. FS.mkdir('/userfs');
  142. FS.mount(IDBFS, {}, '/userfs');
  143. // sync from persisted state into memory and then
  144. // run the 'test' function
  145. FS.syncfs(true, function (err) {
  146. assert(!err);
  147. console.log("done syncinc!");
  148. _after_sync_cb = Module.cwrap('main_after_fs_sync', 'void',['number']);
  149. _after_sync_cb(0);
  150. });
  151. );
  152. glutMainLoop();
  153. return 0;
  154. }
  155. /*
  156. *
  157. *09] <azakai|2__> reduz: yes, define TOTAL_MEMORY on Module. for example var Module = { TOTAL_MEMORY: 12345.. }; before the main
  158. *
  159. */