116 lines
4.3 KiB
TypeScript
116 lines
4.3 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// the client in a browser page, see platform.ts
|
|
import type { Platform } from "./platform.ts";
|
|
import { render_debug } from "./renderer/mod.ts";
|
|
import { confirm_mods } from "./confirm_mods.ts";
|
|
import { CreditsScreen } from "./gui/credits_screen.ts";
|
|
import { is_mouse_grabbed, set_mouse_grabbed, take_lost_pointer_lock, toggle_fullscreen } from "./browser_input.ts";
|
|
|
|
// with render_debug.uncapped, the most frames between two of the display's refreshes. it's the highest fps that shows,
|
|
// 960 on a 60 hz display, and what the gpu can be behind by
|
|
const UNCAPPED_FRAMES_PER_REFRESH = 16;
|
|
|
|
// with render_debug.uncapped: frames since the display last refreshed, and the frame waiting for the next one
|
|
let frames_this_refresh = 0;
|
|
let waiting_for_refresh: ((time: number) => void) | undefined;
|
|
let refresh_requested = false;
|
|
let next_task: MessageChannel | undefined;
|
|
let next_task_frame: ((time: number) => void) | undefined;
|
|
|
|
// with the display's refresh, or with render_debug.uncapped right away, so frames aren't held to vsync and show what
|
|
// they cost. up to UNCAPPED_FRAMES_PER_REFRESH, then it waits for the next refresh, which the browser holds back
|
|
// while the gpu is behind, so frames the gpu can't keep up with don't pile up. the browser saying a frame is done
|
|
// can't be waited on instead, firefox only says so every 100 ms when nothing else is sent to the gpu
|
|
function request_frame(frame: (time: number) => void) {
|
|
if (!render_debug.uncapped) {
|
|
requestAnimationFrame(frame);
|
|
return;
|
|
}
|
|
if (!refresh_requested) {
|
|
refresh_requested = true;
|
|
requestAnimationFrame(() => {
|
|
refresh_requested = false;
|
|
frames_this_refresh = 0;
|
|
const waiting = waiting_for_refresh;
|
|
waiting_for_refresh = undefined;
|
|
waiting?.(performance.now());
|
|
});
|
|
}
|
|
frames_this_refresh += 1;
|
|
if (frames_this_refresh < UNCAPPED_FRAMES_PER_REFRESH) {
|
|
// a task of its own, so input and the workers' messages get handled in between
|
|
if (!next_task) {
|
|
next_task = new MessageChannel();
|
|
next_task.port1.onmessage = () => next_task_frame?.(performance.now());
|
|
}
|
|
next_task_frame = frame;
|
|
next_task.port2.postMessage(null);
|
|
} else {
|
|
waiting_for_refresh = frame;
|
|
}
|
|
}
|
|
|
|
// pages that aren't https or localhost have no clipboard api, like a server on another computer, but the old way
|
|
// still copies from a click
|
|
function write_clipboard(text: string): Promise<void> {
|
|
if (navigator.clipboard) return navigator.clipboard.writeText(text);
|
|
const area = document.createElement("textarea");
|
|
area.value = text;
|
|
document.body.append(area);
|
|
area.select();
|
|
const copied = document.execCommand("copy");
|
|
area.remove();
|
|
return copied ? Promise.resolve() : Promise.reject(new Error("Couldn't copy"));
|
|
}
|
|
|
|
function show_fatal_error(message: string) {
|
|
document.exitPointerLock?.();
|
|
|
|
const overlay = document.createElement("div");
|
|
overlay.style.cssText =
|
|
"position:fixed;inset:0;display:flex;flex-direction:column;align-items:center;justify-content:center;" +
|
|
"gap:12px;background:rgba(0,0,0,0.85);color:white;font:20px system-ui,sans-serif;text-align:center;";
|
|
const text = document.createElement("div");
|
|
text.textContent = message;
|
|
const hint = document.createElement("div");
|
|
hint.textContent = "Reload the page to try again.";
|
|
hint.style.cssText = "font-size:14px;opacity:0.7;";
|
|
overlay.append(text, hint);
|
|
document.body.append(overlay);
|
|
}
|
|
|
|
export const browser_platform: Platform = {
|
|
get page() {
|
|
return new URL(location.href);
|
|
},
|
|
restart(url) {
|
|
location.href = url.href;
|
|
},
|
|
request_frame,
|
|
get storage() {
|
|
return localStorage;
|
|
},
|
|
on_shown(shown) {
|
|
document.addEventListener("visibilitychange", () => {
|
|
if (!document.hidden) shown();
|
|
});
|
|
},
|
|
// sprinting is ctrl and walking is w, and ctrl+w closes the tab without the page getting a say. in fullscreen
|
|
// the keyboard lock catches it instead (see browser_input.ts)
|
|
confirm_leaving(confirm) {
|
|
self.onbeforeunload = confirm ? (event) => event.preventDefault() : null;
|
|
},
|
|
show_fatal_error,
|
|
confirm_mods,
|
|
credits_screen: (on_back) => new CreditsScreen(on_back),
|
|
read_clipboard: () =>
|
|
navigator.clipboard ? navigator.clipboard.readText() : Promise.reject(new Error("No clipboard")),
|
|
write_clipboard,
|
|
set_mouse_grabbed,
|
|
is_mouse_grabbed,
|
|
take_lost_mouse: take_lost_pointer_lock,
|
|
toggle_fullscreen,
|
|
};
|