2026-08-24 15:54:44 +02:00
|
|
|
import type { Texture } from "engine/render/Texture"
|
|
|
|
|
import type { Textures } from "game/textures"
|
|
|
|
|
import barkUrl from "assets/bark.png"
|
|
|
|
|
import beeUrl from "assets/bee.png"
|
|
|
|
|
import birchUrl from "assets/birch.png"
|
|
|
|
|
import crateUrl from "assets/crate.png"
|
|
|
|
|
import floorUrl from "assets/floor.png"
|
|
|
|
|
import flowerUrl from "assets/flower.png"
|
|
|
|
|
import frogUrl from "assets/frog.png"
|
|
|
|
|
import grassUrl from "assets/grass.png"
|
|
|
|
|
import leafUrl from "assets/leaf.png"
|
|
|
|
|
import needleUrl from "assets/needle.png"
|
|
|
|
|
import npcUrl from "assets/npc.png"
|
|
|
|
|
import robinUrl from "assets/robin.png"
|
|
|
|
|
import rockUrl from "assets/rock.png"
|
|
|
|
|
import skyboxUrl from "assets/rockies.skybox.png"
|
|
|
|
|
import wallUrl from "assets/wall.png"
|
2026-08-04 02:09:54 +02:00
|
|
|
|
|
|
|
|
/** Load every game texture up front. Call once before starting the loop. */
|
|
|
|
|
export async function loadTextures(): Promise<Textures> {
|
2026-08-08 14:15:32 +02:00
|
|
|
const [floor, grass, bark, birch, leaf, needle, rock, flower, wall, crate, npc, frog, bee, robin, skybox] = await Promise.all([
|
2026-08-04 02:09:54 +02:00
|
|
|
loadTexture(floorUrl),
|
2026-08-04 12:51:07 +02:00
|
|
|
loadTexture(grassUrl),
|
2026-08-04 13:53:59 +02:00
|
|
|
loadTexture(barkUrl),
|
2026-08-07 13:01:32 +02:00
|
|
|
loadTexture(birchUrl),
|
2026-08-04 13:53:59 +02:00
|
|
|
loadTexture(leafUrl),
|
|
|
|
|
loadTexture(needleUrl),
|
2026-08-04 14:02:12 +02:00
|
|
|
loadTexture(rockUrl),
|
2026-08-04 23:03:55 +02:00
|
|
|
loadTexture(flowerUrl),
|
2026-08-04 02:09:54 +02:00
|
|
|
loadTexture(wallUrl),
|
|
|
|
|
loadTexture(crateUrl),
|
|
|
|
|
loadTexture(npcUrl),
|
2026-08-07 12:49:13 +02:00
|
|
|
loadTexture(frogUrl),
|
|
|
|
|
loadTexture(beeUrl),
|
2026-08-07 13:37:30 +02:00
|
|
|
loadTexture(robinUrl),
|
2026-08-08 14:15:32 +02:00
|
|
|
loadTexture(skyboxUrl),
|
2026-08-04 02:09:54 +02:00
|
|
|
])
|
2026-08-08 14:15:32 +02:00
|
|
|
return { floor, grass, bark, birch, leaf, needle, rock, flower, wall, crate, npc, frog, bee, robin, skybox }
|
2026-08-04 02:09:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadTexture(url: string): Promise<Texture> {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const image = new Image()
|
|
|
|
|
image.addEventListener("load", () => resolve(toTexture(image)))
|
|
|
|
|
image.addEventListener("error", () => reject(new Error(`failed to load ${url}`)))
|
|
|
|
|
image.src = url
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Draw a loaded image into a canvas and read its pixels back as a Texture.
|
|
|
|
|
* ImageData bytes are RGBA, identical to how Color packs a Uint32, so the
|
|
|
|
|
* buffer is reused directly with no per-pixel conversion. */
|
|
|
|
|
function toTexture(image: HTMLImageElement): Texture {
|
|
|
|
|
const canvas = document.createElement("canvas")
|
|
|
|
|
canvas.width = image.naturalWidth
|
|
|
|
|
canvas.height = image.naturalHeight
|
|
|
|
|
const ctx = canvas.getContext("2d")!
|
|
|
|
|
ctx.drawImage(image, 0, 0)
|
|
|
|
|
const pixels = ctx.getImageData(0, 0, canvas.width, canvas.height)
|
|
|
|
|
return { width: pixels.width, height: pixels.height, data: new Uint32Array(pixels.data.buffer) }
|
|
|
|
|
}
|