javascript_main.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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-2017 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 "emscripten.h"
  30. #include "io/resource_loader.h"
  31. #include "main/main.h"
  32. #include "os_javascript.h"
  33. #include <GL/glut.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() {
  113. start_step = 1;
  114. }
  115. }
  116. int main(int argc, char *argv[]) {
  117. /* Initialize the window */
  118. printf("let it go dude!\n");
  119. glutInit(&argc, argv);
  120. os = new OS_JavaScript(argv[0], _gfx_init, NULL, NULL);
  121. Error err = Main::setup(argv[0], argc - 1, &argv[1]);
  122. ResourceLoader::set_abort_on_missing_resources(false); //ease up compatibility
  123. glutMouseFunc(_glut_mouse_button);
  124. glutMotionFunc(_glut_mouse_motion);
  125. glutPassiveMotionFunc(_glut_mouse_motion);
  126. /* Set up glut callback functions */
  127. glutIdleFunc(_gfx_idle);
  128. // glutReshapeFunc(gears_reshape);
  129. glutDisplayFunc(_godot_draw);
  130. //glutSpecialFunc(gears_special);
  131. //mount persistent file system
  132. /* clang-format off */
  133. EM_ASM(
  134. FS.mkdir('/userfs');
  135. FS.mount(IDBFS, {}, '/userfs');
  136. // sync from persistent state into memory and then
  137. // run the 'main_after_fs_sync' function
  138. FS.syncfs(true, function(err) {
  139. if (err) {
  140. Module.setStatus('Failed to load persistent data\nPlease allow (third-party) cookies');
  141. Module.printErr('Failed to populate IDB file system: ' + err.message);
  142. Module.exit();
  143. } else {
  144. Module.print('Successfully populated IDB file system');
  145. ccall('main_after_fs_sync', 'void', []);
  146. }
  147. });
  148. );
  149. /* clang-format on */
  150. glutMainLoop();
  151. return 0;
  152. }
  153. /*
  154. *
  155. *09] <azakai|2__> reduz: yes, define TOTAL_MEMORY on Module. for example var Module = { TOTAL_MEMORY: 12345.. }; before the main
  156. *
  157. */