javascript_main.cpp 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*************************************************************************/
  2. /* javascript_main.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "core/io/resource_loader.h"
  31. #include "main/main.h"
  32. #include "platform/javascript/display_server_javascript.h"
  33. #include "platform/javascript/os_javascript.h"
  34. #include <emscripten/emscripten.h>
  35. #include <stdlib.h>
  36. #include "godot_js.h"
  37. static OS_JavaScript *os = nullptr;
  38. static uint64_t target_ticks = 0;
  39. void exit_callback() {
  40. emscripten_cancel_main_loop(); // After this, we can exit!
  41. Main::cleanup();
  42. int exit_code = OS_JavaScript::get_singleton()->get_exit_code();
  43. memdelete(os);
  44. os = nullptr;
  45. emscripten_force_exit(exit_code); // No matter that we call cancel_main_loop, regular "exit" will not work, forcing.
  46. }
  47. void cleanup_after_sync() {
  48. emscripten_set_main_loop(exit_callback, -1, false);
  49. }
  50. void main_loop_callback() {
  51. uint64_t current_ticks = os->get_ticks_usec();
  52. bool force_draw = DisplayServerJavaScript::get_singleton()->check_size_force_redraw();
  53. if (force_draw) {
  54. Main::force_redraw();
  55. } else if (current_ticks < target_ticks) {
  56. return; // Skip frame.
  57. }
  58. int target_fps = Engine::get_singleton()->get_target_fps();
  59. if (target_fps > 0) {
  60. target_ticks += (uint64_t)(1000000 / target_fps);
  61. }
  62. if (os->main_loop_iterate()) {
  63. emscripten_cancel_main_loop(); // Cancel current loop and wait for cleanup_after_sync.
  64. godot_js_os_finish_async(cleanup_after_sync);
  65. }
  66. }
  67. /// When calling main, it is assumed FS is setup and synced.
  68. extern EMSCRIPTEN_KEEPALIVE int godot_js_main(int argc, char *argv[]) {
  69. os = new OS_JavaScript();
  70. // We must override main when testing is enabled
  71. TEST_MAIN_OVERRIDE
  72. Main::setup(argv[0], argc - 1, &argv[1]);
  73. // Ease up compatibility.
  74. ResourceLoader::set_abort_on_missing_resources(false);
  75. Main::start();
  76. os->get_main_loop()->init();
  77. emscripten_set_main_loop(main_loop_callback, -1, false);
  78. // Immediately run the first iteration.
  79. // We are inside an animation frame, we want to immediately draw on the newly setup canvas.
  80. main_loop_callback();
  81. return 0;
  82. }