javascript_main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**************************************************************************/
  2. /* javascript_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 "core/io/resource_loader.h"
  31. #include "main/main.h"
  32. #include "platform/javascript/os_javascript.h"
  33. #include "godot_js.h"
  34. #include <emscripten/emscripten.h>
  35. #include <stdlib.h>
  36. static OS_JavaScript *os = NULL;
  37. static uint64_t target_ticks = 0;
  38. void exit_callback() {
  39. emscripten_cancel_main_loop(); // After this, we can exit!
  40. Main::cleanup();
  41. int exit_code = OS_JavaScript::get_singleton()->get_exit_code();
  42. memdelete(os);
  43. os = NULL;
  44. emscripten_force_exit(exit_code); // No matter that we call cancel_main_loop, regular "exit" will not work, forcing.
  45. }
  46. void cleanup_after_sync() {
  47. emscripten_set_main_loop(exit_callback, -1, false);
  48. }
  49. void main_loop_callback() {
  50. uint64_t current_ticks = os->get_ticks_usec();
  51. bool force_draw = os->check_size_force_redraw();
  52. if (force_draw) {
  53. Main::force_redraw();
  54. } else if (current_ticks < target_ticks && !force_draw) {
  55. return; // Skip frame.
  56. }
  57. int target_fps = Engine::get_singleton()->get_target_fps();
  58. if (target_fps > 0) {
  59. if (current_ticks - target_ticks > 1000000) {
  60. // When the window loses focus, we stop getting updates and accumulate delay.
  61. // For this reason, if the difference is too big, we reset target ticks to the current ticks.
  62. target_ticks = current_ticks;
  63. }
  64. target_ticks += (uint64_t)(1000000 / target_fps);
  65. }
  66. if (os->main_loop_iterate()) {
  67. emscripten_cancel_main_loop(); // Cancel current loop and wait for finalize_async.
  68. os->get_main_loop()->finish();
  69. godot_js_os_finish_async(cleanup_after_sync);
  70. }
  71. }
  72. extern EMSCRIPTEN_KEEPALIVE int godot_js_main(int argc, char *argv[]) {
  73. // Set locale
  74. char locale_ptr[16];
  75. godot_js_config_locale_get(locale_ptr, sizeof(locale_ptr));
  76. setenv("LANG", locale_ptr, true);
  77. os = new OS_JavaScript();
  78. Main::setup(argv[0], argc - 1, &argv[1]);
  79. // Ease up compatibility.
  80. ResourceLoader::set_abort_on_missing_resources(false);
  81. Main::start();
  82. os->get_main_loop()->init();
  83. #ifdef TOOLS_ENABLED
  84. if (Main::is_project_manager() && FileAccess::exists("/tmp/preload.zip")) {
  85. PoolStringArray ps;
  86. ps.push_back("/tmp/preload.zip");
  87. os->get_main_loop()->emit_signal("files_dropped", ps, -1);
  88. }
  89. #endif
  90. emscripten_set_main_loop(main_loop_callback, -1, false);
  91. // Immediately run the first iteration.
  92. // We are inside an animation frame, we want to immediately draw on the newly setup canvas.
  93. main_loop_callback();
  94. return 0;
  95. }