From f3f9988314d0d8056a81b53098fe57c4d840bea2 Mon Sep 17 00:00:00 2001 From: John Doty Date: Fri, 30 Jun 2023 06:29:21 -0700 Subject: [PATCH] [oden] Assets becomes async load in terms of IO/GFX --- src/assets.ts | 8 +++++--- src/main.ts | 14 +++++++++----- types/asset-core.d.ts | 2 ++ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/assets.ts b/src/assets.ts index d611c934..fe1dd156 100644 --- a/src/assets.ts +++ b/src/assets.ts @@ -1,5 +1,7 @@ -import * as core from "asset-core"; +import * as io from "./io.ts"; +import * as gfx from "./graphics.ts"; -export function load_texture(path: string): number { - return core.load_texture(path); +export async function load_texture(path: string): Promise { + const buffer = await io.load(path); + return gfx.create_texture(buffer, path); } diff --git a/src/main.ts b/src/main.ts index 01e8041a..b1f49aba 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,18 +1,22 @@ import { cls, print, spr, use_texture } from "./graphics.ts"; import { load_texture } from "./assets.ts"; -let the_texture = 0; +let the_texture: number | undefined = undefined; export function init() { print("Hello world!"); - // TODO: Async IO - the_texture = load_texture("./src/happy-tree.png"); + + // Start this load, but then... + load_texture("./src/happy-tree.png").then((n) => (the_texture = n)); } export function update() {} export function draw() { cls(0.1, 0.2, 0.3); - use_texture(the_texture); - spr((320 - 256) / 2, 0, 256, 240, 0, 0); + if (the_texture != undefined) { + // ...it gets resolved here? + use_texture(the_texture); + spr((320 - 256) / 2, 0, 256, 240, 0, 0); + } } diff --git a/types/asset-core.d.ts b/types/asset-core.d.ts index dfcee30d..87680547 100644 --- a/types/asset-core.d.ts +++ b/types/asset-core.d.ts @@ -1 +1,3 @@ +// These are the functions exposed by the native assets module. +// export function load_texture(path: string): number;