Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
234 lines (198 loc) · 7.52 KB
/
Copy pathindex.html
File metadata and controls
234 lines (198 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Friday Night Funkin': Shadow Engine</title>
<meta id="viewport" name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="mobile-web-app-capable" content="yes">
<link rel="icon" type="image/png" href="./favicon.png">
<script>
window.addEventListener ("touchmove", function (event) { event.preventDefault (); }, { passive: false });
</script>
<style>
html,body { margin: 0; padding: 0; height: 100%; overflow: hidden; background: #000000; }
#content { background: #000000; width: 100%; height: 100%; color: #ffffff; font: 20px sans-serif; display: flex; align-items: center; justify-content: center; text-align: center; }
#canvas { position: absolute; top: 0; left: 0; outline: none; }
#status { position: absolute; z-index: 1; display: flex; flex-direction: column; align-items: center; gap: 14px; }
#status[hidden] { display: none; }
#statusBar { width: min(320px, 70vw); height: 6px; border-radius: 3px; background: #333333; overflow: hidden; }
#statusFill { width: 0; height: 100%; border-radius: 3px; background: #ffffff; }
#statusBar.indeterminate #statusFill { width: 35%; animation: statusSlide 1.1s ease-in-out infinite; }
@keyframes statusSlide { from { transform: translateX(-100%); } to { transform: translateX(290%); } }
</style>
</head>
<body>
<div id="content">
<div id="status" hidden>
<div id="statusLabel"></div>
<div id="statusBar"><div id="statusFill"></div></div>
</div>
<canvas id="canvas" tabindex="-1" oncontextmenu="event.preventDefault ()"></canvas>
</div>
<script type="text/javascript">
var Module = {};
Module.canvas = document.getElementById ("canvas");
Module.setStatus = function (msg) { console.log (msg); };
var container = document.getElementById ("content");
var loadingBox = document.getElementById ("status");
var loadingLabel = document.getElementById ("statusLabel");
var loadingBar = document.getElementById ("statusBar");
var loadingFill = document.getElementById ("statusFill");
// progress is 0 to 1, or left out for a bar that just keeps moving
function setLoading (text, progress)
{
loadingBox.hidden = !text;
loadingLabel.textContent = text || "";
if (progress == null)
{
loadingBar.className = "indeterminate";
loadingFill.style.width = "";
}
else
{
loadingBar.className = "";
loadingFill.style.width = Math.min (1, progress) * 100 + "%";
}
}
// Downloads the .wasm ourselves so there is something to show while it comes in. A file the browser has cached
// finishes before SHOW_AFTER, so the text only appears when there is really something to wait for.
var SHOW_AFTER = 250;
function megabytes (bytes)
{
return (bytes / 1048576).toFixed (1) + " MB";
}
Module.instantiateWasm = function (imports, onInstance)
{
var url = "./ShadowEngine.wasm";
var loaded = 0, total = 0, showing = false, finished = false;
function update ()
{
if (!showing) return;
// The full size is left out when it is not known, as with a Content-Encoding, where the length is the
// compressed size and the bytes read here are not
setLoading ("Downloading WASM... " + megabytes (loaded) + (total > 0 ? " / " + megabytes (total) : ""), total > 0 ? loaded / total : null);
}
var timer = setTimeout (function ()
{
if (finished) return;
showing = true;
update ();
}, SHOW_AFTER);
fetch (url).then (function (response)
{
if (!response.ok) throw new Error ("HTTP " + response.status);
if (!response.body)
{
return response.arrayBuffer ().then (function (bytes) { return WebAssembly.instantiate (bytes, imports); });
}
if (!response.headers.get ("Content-Encoding")) total = +response.headers.get ("Content-Length") || 0;
var reader = response.body.getReader ();
var stream = new ReadableStream ({
pull: function (controller)
{
return reader.read ().then (function (result)
{
if (result.done)
{
if (showing) setLoading ("Compiling WASM...", null);
controller.close ();
return;
}
loaded += result.value.length;
update ();
controller.enqueue (result.value);
});
},
cancel: function (reason) { return reader.cancel (reason); }
});
return WebAssembly.instantiateStreaming (new Response (stream, { headers: { "Content-Type": "application/wasm" } }), imports);
}).then (function (result)
{
finished = true;
clearTimeout (timer);
setLoading ("");
onInstance (result.instance, result.module);
}).catch (function (error)
{
finished = true;
clearTimeout (timer);
console.error (error);
unsupported ("Could not download " + url.substr (2) + " (" + error.message + ").");
});
return {};
};
function unsupported (message)
{
Module.canvas.remove ();
container.textContent = message;
}
// bgfx renders with WebGPU or WebGL 2. The renderer comes from SDL_LIME_BGFX_RENDERER, or from this page:
// "?renderer=webgl", or Module.limeRenderer = "webgl" above, a comma separated list is tried in order.
// Left out, WebGPU is tried first and WebGL 2 after it.
var requested = (new URLSearchParams (window.location.search).get ("renderer") || "").toLowerCase ();
if (requested) Module.limeRenderer = requested;
var names = (Module.limeRenderer || "").split (/[,;\s]+/).filter (function (name) { return name && name !== "auto"; });
var wantsWebGPU = names.length === 0 || names.indexOf ("webgpu") !== -1;
function start ()
{
var script = document.createElement ("script");
script.src = "./ShadowEngine.js";
document.body.appendChild (script);
}
// WebGPU: bgfx takes this device instead of requesting one itself, which would need Asyncify.
// Without one, WebGPU does not start and bgfx moves on to the next renderer.
function requestDevice ()
{
if (!navigator.gpu) return Promise.reject (new Error ("This browser does not support WebGPU."));
return navigator.gpu.requestAdapter ({ powerPreference: "high-performance" }).then (function (adapter)
{
if (!adapter) throw new Error ("No WebGPU adapter");
var limits = {};
for (var key in adapter.limits)
{
if (typeof adapter.limits[key] === "number") limits[key] = adapter.limits[key];
}
return adapter.requestDevice ({ requiredFeatures: Array.from (adapter.features), requiredLimits: limits });
});
}
// Audio mixes on an AudioWorklet, which shares memory with the page, so the page has to be cross-origin isolated.
if (!window.isSecureContext)
{
unsupported ("This page needs to be opened over HTTPS or from localhost.");
}
else if (!window.crossOriginIsolated)
{
if (!("serviceWorker" in navigator) || navigator.serviceWorker.controller)
{
unsupported ("This page could not be cross-origin isolated.");
}
else
{
navigator.serviceWorker.addEventListener ("controllerchange", function () { window.location.reload (); });
navigator.serviceWorker.register ("./isolation.js").then (function (registration)
{
if (registration.active) window.location.reload ();
}).catch (function (error)
{
console.error (error);
unsupported ("This page could not be cross-origin isolated.");
});
}
}
else if (!wantsWebGPU)
{
start ();
}
else
{
requestDevice ().then (function (device)
{
Module.preinitializedWebGPUDevice = device;
}).catch (function (error)
{
console.warn (error);
}).then (start);
}
</script>
</body>
</html>