diff --git a/client/benchmark/benchmark.ts b/client/benchmark/benchmark.ts index eb52a15..a6fb86a 100644 --- a/client/benchmark/benchmark.ts +++ b/client/benchmark/benchmark.ts @@ -13,6 +13,7 @@ import type { GameRenderer } from "@/client/rendering/game_renderer.ts"; import { block_visuals } from "@/client/rendering/visuals.ts"; import { draw_item } from "@/client/rendering/render_utils.ts"; import { InputManager } from "@/client/input_manager.ts"; +import { platform } from "@/client/platform.ts"; import { type FrameSample, profiler } from "@/client/profiler.ts"; import { RemotePlayer } from "@/client/entity/remote_player.ts"; import { RemoteEntity } from "@/client/entity/remote_entity.ts"; @@ -129,8 +130,9 @@ export class RenderBenchmark { // a line of names for each row of the gui scene's slots #gui_lines: string[]; #system: SystemInfo; - // the page was hidden, which stops frames + // the game was hidden, which held frames back #hidden = false; + #stop_watching_shown: () => void; #saved_debug = { ...render_debug }; #saved_draw: Record; @@ -157,7 +159,7 @@ export class RenderBenchmark { for (const key of Object.keys(draw) as (keyof typeof draw)[]) draw[key] = true; Object.assign(render_debug, { msaa: false, split_passes: false, uncapped: false }); set_canvas_size(RESOLUTION); - document.addEventListener("visibilitychange", this.#on_visibility); + this.#stop_watching_shown = platform.on_shown(() => this.#hidden = true); this.#flow = this.#run(); } @@ -167,7 +169,7 @@ export class RenderBenchmark { if (InputManager.is_key_pressed("Escape")) { this.#end("The benchmark was stopped"); } else if (this.#hidden) { - this.#end("The benchmark stopped since the page was hidden, which holds frames back"); + this.#end("The benchmark stopped since the game was hidden, which holds frames back"); } else { this.#flow.next(); } @@ -216,14 +218,10 @@ export class RenderBenchmark { set_canvas_size(undefined); profiler.samples = undefined; if (gpu_timer) gpu_timer.samples = undefined; - document.removeEventListener("visibilitychange", this.#on_visibility); + this.#stop_watching_shown(); this.#on_end(result); } - #on_visibility = () => { - if (document.hidden) this.#hidden = true; - }; - // the whole benchmark, a frame at each yield *#run(): Generator { yield* this.#build(); @@ -559,8 +557,9 @@ function system_info(workers: number): SystemInfo { gpu_features: [...device.features].sort(), timestamps: gpu_timer?.supported ?? false, canvas_format: navigator.gpu.getPreferredCanvasFormat(), - device_pixel_ratio: devicePixelRatio, - screen: `${screen.width}x${screen.height}`, + // the desktop app's window is drawn a pixel to a pixel + device_pixel_ratio: globalThis.devicePixelRatio ?? 1, + screen: globalThis.screen && `${screen.width}x${screen.height}`, workers, }; const hints = (navigator as { userAgentData?: UserAgentData }).userAgentData; diff --git a/client/benchmark/report.ts b/client/benchmark/report.ts index e556871..2fc05d6 100644 --- a/client/benchmark/report.ts +++ b/client/benchmark/report.ts @@ -62,7 +62,8 @@ export interface SystemInfo { // the median fps where nothing is drawn, which is the display's refresh when the browser holds frames to it refresh_hz?: number; device_pixel_ratio: number; - screen: string; + // the browser's, the desktop app doesn't know it + screen?: string; // meshing workers, see ChunkWorkerPool workers: number; } diff --git a/client/browser_platform.ts b/client/browser_platform.ts index a7b3849..9970703 100644 --- a/client/browser_platform.ts +++ b/client/browser_platform.ts @@ -92,9 +92,11 @@ export const browser_platform: Platform = { return localStorage; }, on_shown(shown) { - document.addEventListener("visibilitychange", () => { + const listener = () => { if (!document.hidden) shown(); - }); + }; + document.addEventListener("visibilitychange", listener); + return () => document.removeEventListener("visibilitychange", listener); }, // 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) diff --git a/client/native/main.ts b/client/native/main.ts index 513c43b..be4ba5f 100644 --- a/client/native/main.ts +++ b/client/native/main.ts @@ -30,7 +30,7 @@ let lost_mouse = false; let shown = true; let next_frame: ((time: number) => void) | undefined; let frame_waiting = false; -const shown_listeners: (() => void)[] = []; +const shown_listeners = new Set<() => void>(); function run_frame() { frame_waiting = false; @@ -78,7 +78,8 @@ function native_platform(page: URL, stored: Record, credits: str }, request_frame, on_shown(listener) { - shown_listeners.push(listener); + shown_listeners.add(listener); + return () => shown_listeners.delete(listener); }, // kept by the window, which saves it with the app's data storage: { diff --git a/client/platform.ts b/client/platform.ts index da055f3..e117e92 100644 --- a/client/platform.ts +++ b/client/platform.ts @@ -14,8 +14,9 @@ export interface Platform { restart(url: URL): void; // calls frame once it's time for the next frame, with the time like performance.now() request_frame(frame: (time: number) => void): void; - // calls shown when the game can be seen again after being hidden, there are no frames meanwhile - on_shown(shown: () => void): void; + // calls shown when the game can be seen again after being hidden, there are no frames meanwhile. returns what + // stops calling it + on_shown(shown: () => void): () => void; // what's remembered from one time to the next, like the servers whose mods the player trusts. it can throw, like a // browser's in a private window storage: Pick;