Audio on native

This commit is contained in:
2026-11-22 18:27:01 -03:00
parent a552883795
commit 0d770c7bc4
9 changed files with 790 additions and 10 deletions
+30 -5
View File
@@ -8,13 +8,24 @@
import { join, toFileUrl } from "@std/path";
import { desktop_app } from "./app.ts";
import { SdlWindow } from "./sdl.ts";
import { OpenAl } from "./openal.ts";
import type { ClientMessage, WindowMessage } from "../client/native/messages.ts";
// how often the window's events are looked at, in milliseconds
const POLL_MS = 2;
// how often sounds are checked for having ended, in polls
const AUDIO_POLLS = 10;
const game_window = new SdlWindow("bworld", 1280, 720);
// the game plays without sound when there's no openal
let audio: OpenAl | undefined;
try {
audio = new OpenAl();
} catch (e) {
console.error(`No sound: ${(e as Error).message}`);
}
const app = desktop_app((message) => {
console.error(message);
game_window.show_error("bworld", `The singleplayer world stopped: ${message}`);
@@ -37,11 +48,13 @@ try {
}
let client: Worker | undefined;
let closing = false;
// a fresh client, like loading the page again. the old one is gone with everything it loaded
async function start_client(page: URL) {
client?.terminate();
game_window.set_grabbed(false);
audio?.reset();
const client_dir = await app.client_dir;
const worker = new Worker(toFileUrl(join(client_dir, "client", "native.js")), {
type: "module",
@@ -61,6 +74,7 @@ async function start_client(page: URL) {
surface: game_window.surface_handles(),
...game_window.pixel_size(),
storage,
audio: audio !== undefined,
});
}
@@ -69,6 +83,8 @@ function send(message: WindowMessage) {
}
function handle(message: ClientMessage) {
// what it asks while the app closes, like sounds, would come after openal and the window are gone
if (closing) return;
switch (message.type) {
case "grab":
game_window.set_grabbed(message.grab);
@@ -102,6 +118,9 @@ function handle(message: ClientMessage) {
case "write_clipboard":
game_window.write_clipboard(message.text);
break;
case "audio":
audio?.run(message.commands);
break;
case "store":
storage[message.key] = message.value;
Deno.writeTextFileSync(STORAGE_FILE, JSON.stringify(storage, null, "\t"));
@@ -109,18 +128,24 @@ function handle(message: ClientMessage) {
}
}
let closing = false;
// the world gets saved before the app closes
// the world gets saved before the app closes. the client isn't terminated first, exiting while a terminated worker is
// still letting go of its webgpu surface can crash deno, exiting stops it as well
async function close() {
if (closing) return;
closing = true;
clearInterval(polling);
client?.terminate();
await app.singleplayer.stop();
audio?.close();
game_window.close();
Deno.exit(0);
}
const polling = setInterval(() => game_window.poll(send, close), POLL_MS);
let polls = 0;
const polling = setInterval(() => {
game_window.poll(send, close);
if (audio && ++polls % AUDIO_POLLS === 0) {
const ended = audio.ended();
if (ended.length) send({ type: "audio_ended", sources: ended });
}
}, POLL_MS);
start_client(start_page);