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