Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/web/web_main.cpp
10277 views
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
31
#include "display_server_web.h"
32
#include "godot_js.h"
33
#include "os_web.h"
34
35
#include "core/config/engine.h"
36
#include "core/io/resource_loader.h"
37
#include "main/main.h"
38
#include "scene/main/scene_tree.h"
39
#include "scene/main/window.h" // SceneTree only forward declares it.
40
41
#ifdef TOOLS_ENABLED
42
#include "editor/web_tools_editor_plugin.h"
43
#endif
44
45
#include <emscripten/emscripten.h>
46
#include <cstdlib>
47
48
static OS_Web *os = nullptr;
49
#ifndef PROXY_TO_PTHREAD_ENABLED
50
static uint64_t target_ticks = 0;
51
#endif
52
53
static bool main_started = false;
54
static bool shutdown_complete = false;
55
56
void exit_callback() {
57
if (!shutdown_complete) {
58
return; // Still waiting.
59
}
60
if (main_started) {
61
Main::cleanup();
62
main_started = false;
63
}
64
int exit_code = OS_Web::get_singleton()->get_exit_code();
65
memdelete(os);
66
os = nullptr;
67
emscripten_force_exit(exit_code); // Exit runtime.
68
}
69
70
void cleanup_after_sync() {
71
shutdown_complete = true;
72
}
73
74
void main_loop_callback() {
75
#ifndef PROXY_TO_PTHREAD_ENABLED
76
uint64_t current_ticks = os->get_ticks_usec();
77
#endif
78
79
bool force_draw = DisplayServerWeb::get_singleton()->check_size_force_redraw();
80
if (force_draw) {
81
Main::force_redraw();
82
#ifndef PROXY_TO_PTHREAD_ENABLED
83
} else if (current_ticks < target_ticks) {
84
return; // Skip frame.
85
#endif
86
}
87
88
#ifndef PROXY_TO_PTHREAD_ENABLED
89
int max_fps = Engine::get_singleton()->get_max_fps();
90
if (max_fps > 0) {
91
if (current_ticks - target_ticks > 1000000) {
92
// When the window loses focus, we stop getting updates and accumulate delay.
93
// For this reason, if the difference is too big, we reset target ticks to the current ticks.
94
target_ticks = current_ticks;
95
}
96
target_ticks += (uint64_t)(1000000 / max_fps);
97
}
98
#endif
99
100
if (os->main_loop_iterate()) {
101
emscripten_cancel_main_loop(); // Cancel current loop and set the cleanup one.
102
emscripten_set_main_loop(exit_callback, -1, false);
103
godot_js_os_finish_async(cleanup_after_sync);
104
}
105
}
106
107
void print_web_header() {
108
// Emscripten.
109
char *emscripten_version_char = godot_js_emscripten_get_version();
110
String emscripten_version = vformat("Emscripten %s", emscripten_version_char);
111
// `free()` is used here because it's not memory that was allocated by Godot.
112
free(emscripten_version_char);
113
114
// Build features.
115
String thread_support = OS::get_singleton()->has_feature("threads")
116
? "multi-threaded"
117
: "single-threaded";
118
String extensions_support = OS::get_singleton()->has_feature("web_extensions")
119
? "GDExtension support"
120
: "no GDExtension support";
121
122
Vector<String> build_configuration = { emscripten_version, thread_support, extensions_support };
123
print_line(vformat("Build configuration: %s.", String(", ").join(build_configuration)));
124
}
125
126
/// When calling main, it is assumed FS is setup and synced.
127
extern EMSCRIPTEN_KEEPALIVE int godot_web_main(int argc, char *argv[]) {
128
os = new OS_Web();
129
130
#ifdef TOOLS_ENABLED
131
WebToolsEditorPlugin::initialize();
132
#endif
133
134
// We must override main when testing is enabled
135
TEST_MAIN_OVERRIDE
136
137
Error err = Main::setup(argv[0], argc - 1, &argv[1]);
138
139
// Proper shutdown in case of setup failure.
140
if (err != OK) {
141
// Will only exit after sync.
142
emscripten_set_main_loop(exit_callback, -1, false);
143
godot_js_os_finish_async(cleanup_after_sync);
144
if (err == ERR_HELP) { // Returned by --help and --version, so success.
145
return EXIT_SUCCESS;
146
}
147
return EXIT_FAILURE;
148
}
149
150
print_web_header();
151
152
main_started = true;
153
154
// Ease up compatibility.
155
ResourceLoader::set_abort_on_missing_resources(false);
156
157
int ret = Main::start();
158
os->set_exit_code(ret);
159
os->get_main_loop()->initialize();
160
#ifdef TOOLS_ENABLED
161
if (Engine::get_singleton()->is_project_manager_hint() && FileAccess::exists("/tmp/preload.zip")) {
162
PackedStringArray ps;
163
ps.push_back("/tmp/preload.zip");
164
SceneTree::get_singleton()->get_root()->emit_signal(SNAME("files_dropped"), ps);
165
}
166
#endif
167
emscripten_set_main_loop(main_loop_callback, -1, false);
168
// Immediately run the first iteration.
169
// We are inside an animation frame, we want to immediately draw on the newly setup canvas.
170
main_loop_callback();
171
172
return os->get_exit_code();
173
}
174
175