javascript_main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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;
  60. } else {
  61. _mouse_button_mask&=~(1<<ev.mouse_button.button_index);
  62. }
  63. }
  64. uint32_t m = glutGetModifiers();
  65. ev.mouse_button.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  66. ev.mouse_button.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  67. ev.mouse_button.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  68. os->push_input(ev);
  69. }
  70. static int _glut_prev_x=0;
  71. static int _glut_prev_y=0;
  72. static void _glut_mouse_motion(int x, int y) {
  73. InputEvent ev;
  74. ev.type=InputEvent::MOUSE_MOTION;
  75. ev.mouse_motion.button_mask=_mouse_button_mask;
  76. ev.mouse_motion.x=x;
  77. ev.mouse_motion.y=y;
  78. ev.mouse_motion.global_x=x;
  79. ev.mouse_motion.global_y=y;
  80. ev.mouse_motion.relative_x=x-_glut_prev_x;
  81. ev.mouse_motion.relative_y=y-_glut_prev_y;
  82. _glut_prev_x=x;
  83. _glut_prev_y=y;
  84. uint32_t m = glutGetModifiers();
  85. ev.mouse_motion.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  86. ev.mouse_motion.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  87. ev.mouse_motion.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  88. os->push_input(ev);
  89. }
  90. static void _gfx_idle() {
  91. glutPostRedisplay();
  92. }
  93. int start_step=0;
  94. static void _godot_draw(void) {
  95. if (start_step==1) {
  96. start_step=2;
  97. Main::start();
  98. os->main_loop_begin();
  99. }
  100. if (start_step==2) {
  101. os->main_loop_iterate();
  102. }
  103. glutSwapBuffers();
  104. }
  105. extern "C" {
  106. void main_after_fs_sync(int value) {
  107. start_step=1;
  108. printf("FS SYNCHED!\n");
  109. }
  110. }
  111. int main(int argc, char *argv[]) {
  112. /* Initialize the window */
  113. printf("let it go!\n");
  114. glutInit(&argc, argv);
  115. os = new OS_JavaScript(_gfx_init,NULL,NULL,NULL,NULL);
  116. #if 0
  117. char *args[]={"-test","gui","-v",NULL};
  118. Error err = Main::setup("apk",3,args);
  119. #else
  120. // char *args[]={"-v",NULL};//
  121. // Error err = Main::setup("",1,args);
  122. Error err = Main::setup("",0,NULL);
  123. #endif
  124. ResourceLoader::set_abort_on_missing_resources(false); //ease up compatibility
  125. glutMouseFunc(_glut_mouse_button);
  126. glutMotionFunc(_glut_mouse_motion);
  127. glutMotionFunc(_glut_mouse_motion);
  128. glutPassiveMotionFunc(_glut_mouse_motion);
  129. /* Set up glut callback functions */
  130. glutIdleFunc (_gfx_idle);
  131. // glutReshapeFunc(gears_reshape);
  132. glutDisplayFunc(_godot_draw);
  133. //glutSpecialFunc(gears_special);
  134. //mount persistent filesystem
  135. EM_ASM(
  136. FS.mkdir('/userfs');
  137. FS.mount(IDBFS, {}, '/userfs');
  138. // sync from persisted state into memory and then
  139. // run the 'test' function
  140. FS.syncfs(true, function (err) {
  141. assert(!err);
  142. console.log("done syncinc!");
  143. _after_sync_cb = Module.cwrap('main_after_fs_sync', 'void',['number']);
  144. _after_sync_cb(0);
  145. });
  146. );
  147. glutMainLoop();
  148. return 0;
  149. }
  150. /*
  151. *
  152. *09] <azakai|2__> reduz: yes, define TOTAL_MEMORY on Module. for example var Module = { TOTAL_MEMORY: 12345.. }; before the main
  153. *
  154. */