[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

@ -52,6 +52,16 @@ export function spr(
core.spr(x, y, w, h, sx, sy, sw, sh);
}
export class Texture {
#id: number;
constructor(id: number) {
this.#id = id;
}
id(): number {
return this.#id;
}
}
/**
* Create a texture based on the loaded buffer.
*
@ -61,15 +71,41 @@ export function spr(
export function create_texture(
buffer: ArrayBuffer,
label: string | undefined = undefined
): number {
return core.create_texture(buffer, label);
): Texture {
const id = core.create_texture(buffer, label);
return new Texture(id);
}
/**
* Set the specified texture as the current texture for calls to e.g. spr().
*
* @param id - The identifier of the texture to use.
* @param texture - The texture to use.
*/
export function use_texture(id: number) {
core.use_texture(id);
export function use_texture(id: Texture) {
core.use_texture(id.id());
}
/**
* Create a texture that we can render to.
*/
export function create_writable_texture(
width: number,
height: number,
label: string | undefined = undefined
): Texture {
return new Texture(core.create_writable_texture(width, height, label));
}
/**
* Set the current render target to the screen.
*/
export function write_to_screen() {
core.write_to_screen();
}
/**
* Set the current render target to the specified texture.
*/
export function write_to_texture(texture: Texture) {
core.write_to_texture(texture.id());
}