Start maybe working on a native app
This commit is contained in:
+12
-12
@@ -2,6 +2,8 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { load_texture, Texture } from "./renderer/mod.ts";
|
||||
import { decode_png } from "./png.ts";
|
||||
import { platform } from "./platform.ts";
|
||||
|
||||
type Asset = Texture | string | Record<string, unknown>;
|
||||
|
||||
@@ -18,26 +20,24 @@ export class AssetManager {
|
||||
return;
|
||||
}
|
||||
|
||||
// the client's own files are next to the page, see platform.page
|
||||
const url = new URL(src, platform.page);
|
||||
if (src.endsWith(".png")) {
|
||||
const img = new Image();
|
||||
const promise = new Promise<void>((resolve, reject) => {
|
||||
img.onload = () => {
|
||||
this.assets[key] = load_texture(img);
|
||||
resolve();
|
||||
};
|
||||
img.onerror = () => reject(new Error(`Failed to load image: ${src}`));
|
||||
});
|
||||
img.src = src;
|
||||
this.loading_promises.push(promise);
|
||||
const promise = async () => {
|
||||
const resp = await fetch(url);
|
||||
if (!resp.ok) throw new Error(`Failed to load image: ${src}`);
|
||||
this.assets[key] = load_texture(decode_png(new Uint8Array(await resp.arrayBuffer())));
|
||||
};
|
||||
this.loading_promises.push(promise());
|
||||
} else if (src.endsWith(".txt") || src.endsWith(".md") || src.endsWith(".fnt")) {
|
||||
const promise = async () => {
|
||||
const resp = await fetch(src);
|
||||
const resp = await fetch(url);
|
||||
this.assets[key] = await resp.text();
|
||||
};
|
||||
this.loading_promises.push(promise());
|
||||
} else if (src.endsWith(".json")) {
|
||||
const promise = async () => {
|
||||
const resp = await fetch(src);
|
||||
const resp = await fetch(url);
|
||||
this.assets[key] = await resp.json();
|
||||
};
|
||||
this.loading_promises.push(promise());
|
||||
|
||||
Reference in New Issue
Block a user