web_main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**************************************************************************/
  2. /* web_main.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "display_server_web.h"
  31. #include "godot_js.h"
  32. #include "os_web.h"
  33. #include "core/config/engine.h"
  34. #include "core/io/resource_loader.h"
  35. #include "main/main.h"
  36. #include "scene/main/scene_tree.h"
  37. #include "scene/main/window.h" // SceneTree only forward declares it.
  38. #include <emscripten/emscripten.h>
  39. #include <stdlib.h>
  40. static OS_Web *os = nullptr;
  41. #ifndef PROXY_TO_PTHREAD_ENABLED
  42. static uint64_t target_ticks = 0;
  43. #endif
  44. static bool main_started = false;
  45. static bool shutdown_complete = false;
  46. void exit_callback() {
  47. if (!shutdown_complete) {
  48. return; // Still waiting.
  49. }
  50. if (main_started) {
  51. Main::cleanup();
  52. main_started = false;
  53. }
  54. int exit_code = OS_Web::get_singleton()->get_exit_code();
  55. memdelete(os);
  56. os = nullptr;
  57. emscripten_force_exit(exit_code); // Exit runtime.
  58. }
  59. void cleanup_after_sync() {
  60. shutdown_complete = true;
  61. }
  62. void main_loop_callback() {
  63. #ifndef PROXY_TO_PTHREAD_ENABLED
  64. uint64_t current_ticks = os->get_ticks_usec();
  65. #endif
  66. bool force_draw = DisplayServerWeb::get_singleton()->check_size_force_redraw();
  67. if (force_draw) {
  68. Main::force_redraw();
  69. #ifndef PROXY_TO_PTHREAD_ENABLED
  70. } else if (current_ticks < target_ticks) {
  71. return; // Skip frame.
  72. #endif
  73. }
  74. #ifndef PROXY_TO_PTHREAD_ENABLED
  75. int max_fps = Engine::get_singleton()->get_max_fps();
  76. if (max_fps > 0) {
  77. if (current_ticks - target_ticks > 1000000) {
  78. // When the window loses focus, we stop getting updates and accumulate delay.
  79. // For this reason, if the difference is too big, we reset target ticks to the current ticks.
  80. target_ticks = current_ticks;
  81. }
  82. target_ticks += (uint64_t)(1000000 / max_fps);
  83. }
  84. #endif
  85. if (os->main_loop_iterate()) {
  86. emscripten_cancel_main_loop(); // Cancel current loop and set the cleanup one.
  87. emscripten_set_main_loop(exit_callback, -1, false);
  88. godot_js_os_finish_async(cleanup_after_sync);
  89. }
  90. }
  91. /// When calling main, it is assumed FS is setup and synced.
  92. extern EMSCRIPTEN_KEEPALIVE int godot_web_main(int argc, char *argv[]) {
  93. os = new OS_Web();
  94. // We must override main when testing is enabled
  95. TEST_MAIN_OVERRIDE
  96. Error err = Main::setup(argv[0], argc - 1, &argv[1]);
  97. // Proper shutdown in case of setup failure.
  98. if (err != OK) {
  99. // Will only exit after sync.
  100. emscripten_set_main_loop(exit_callback, -1, false);
  101. godot_js_os_finish_async(cleanup_after_sync);
  102. if (err == ERR_HELP) { // Returned by --help and --version, so success.
  103. return EXIT_SUCCESS;
  104. }
  105. return EXIT_FAILURE;
  106. }
  107. main_started = true;
  108. // Ease up compatibility.
  109. ResourceLoader::set_abort_on_missing_resources(false);
  110. int ret = Main::start();
  111. os->set_exit_code(ret);
  112. os->get_main_loop()->initialize();
  113. #ifdef TOOLS_ENABLED
  114. if (Engine::get_singleton()->is_project_manager_hint() && FileAccess::exists("/tmp/preload.zip")) {
  115. PackedStringArray ps;
  116. ps.push_back("/tmp/preload.zip");
  117. SceneTree::get_singleton()->get_root()->emit_signal(SNAME("files_dropped"), ps);
  118. }
  119. #endif
  120. emscripten_set_main_loop(main_loop_callback, -1, false);
  121. // Immediately run the first iteration.
  122. // We are inside an animation frame, we want to immediately draw on the newly setup canvas.
  123. main_loop_callback();
  124. return os->get_exit_code();
  125. }