[oden] Writable Textures and re-work TS API

Now we return Texture objects to make things a little bit more
type-safe, at the cost of a small allocation (I hope!)
This commit is contained in:
John Doty 2023-07-08 17:54:48 -07:00
parent 12cc715873
commit 89045ccbcc
7 changed files with 287 additions and 24 deletions

View file

@ -1,26 +1,48 @@
import { cls, print, spr, use_texture } from "./graphics";
import { cls, print, spr, use_texture, Texture } from "./graphics";
import { load_texture } from "./assets";
import { since_start } from "./time";
import { btn, Button } from "./input";
// import { load_string } from "./io";
import { new_v2, vadd, vmul, vnorm } from "./vector";
/// TODO: Support reload by saving and restoring state from init/update/restore.
/// A nice looping frame counter.
let clock = 0;
let bot_sprite: number | undefined = undefined;
let bot_sprite: Texture | undefined = undefined;
// Note zelda overworld is 16x8 screens
// zelda screen is 16x11 tiles
// from a feeling point of view this is sufficient, apparently :D
export function init() {
print("Hello world!");
let loaded = false;
async function load_map(path: string) {
// print("Loading map:", path);
// const blob = await load_string(path);
// const map = JSON.parse(blob);
// print("Loaded map:", map);
}
function load_assets() {
// Start this load, but then...
load_texture("./bot.png").then((n) => {
let texture_load = load_texture("./bot.png").then((n) => {
print("Bot loaded at", since_start());
bot_sprite = n;
});
let map_load = load_map("./overworld.ldtk");
Promise.all([texture_load, map_load]).then(() => {
loaded = true;
print("All are loaded.");
});
}
export function init() {
print("Hello world!");
load_assets();
}
const friction = 0.6;
@ -80,6 +102,10 @@ const robo_info = {
export function draw() {
cls(0.1, 0.2, 0.3);
if (!loaded) {
return;
}
if (bot_sprite != undefined) {
// ...it gets resolved here?
use_texture(bot_sprite);