# Assets

Put source files in `assets/`. The CLI builds a manifest from them. Only files the manifest references are served in dev and shipped in builds, and your sources are never modified.

| File | Asset id | Group |
| --- | --- | --- |
| `assets/logo.png` | `logo` | `default` |
| `assets/level1/map.json` | `level1/map` | `level1` |
| `assets/hero@16x24.png` | `hero` (16x24 sprite grid) | `default` |
| `assets/slime.tt` | `slime` (sprite sheet, one cell per frame), drawn by an agent with tenten | `default` |

Folders become **preload groups**, so you can load a level's assets when you need them.

```ts
import { Assets } from "@debuffjp/assets";
import { Sprite } from "@debuffjp/core";

const assets = new Assets(await (await fetch("assets/manifest.json")).json());
await assets.load("default", ({ loaded, total }) => bar(loaded / total));

const hero = new Sprite({ frames: assets.frames("hero"), animations: { run: { frames: [0, 1, 2, 3], fps: 12 } } });
draw.sprite(assets.frame("coin"), x, y); // any image, wherever it lives
```

## Typed asset ids

`debuff dev`, `debuff build` and `npx debuff assets` write `.debuff/assets.d.ts`, which lists every asset id with its type and preload group. With that file in your `tsconfig.json`, a misspelt id, an id of the wrong type (`assets.image("level1")` on a JSON file) or an unknown group fails to compile instead of failing at runtime:

```text
src/main.ts(50,45): error TS2345: Argument of type '"levle1"' is not assignable to parameter of type '"level1"'.
```

New projects are set up for this. In a project made before 0.22, add the file to `include` and run `debuff build` before `tsc`, so the file exists when `tsc` runs:

```json
{ "include": ["src", "tests", ".debuff/assets.d.ts"] }
```

```json
{ "scripts": { "build": "debuff build && tsc --noEmit" } }
```

The file is rewritten whenever `assets/` changes, and `.debuff/` is git-ignored. Without it, asset ids are plain strings, as they were before 0.22. An id built at runtime, such as `` `tile${n}` ``, no longer type-checks on its own: cast it with `as AssetId` (from `@debuffjp/assets`).

## Atlases, automatically

At build time (and in dev), the PNGs in each preload group are packed into one texture atlas. Players download one file instead of dozens, and the renderer binds fewer textures. Packing is lossless and padded against edge bleeding, and `frame()` / `frames()` work the same either way. Images over half the atlas size (backgrounds) stay standalone. Tune or disable it in `debuff.config.ts`:

```ts
export default defineConfig({ assets: { atlas: { maxSize: 2048, padding: 2, exclude: ["ui/"] } } }); // or atlas: false
```

`npx debuff assets` lists every asset, which importer produced it, its size, and anything that looks unused.

## Compressed textures {#compressed-textures}

From 0.35, the build can also write GPU-compressed copies of every PNG (atlases included) as KTX2 files. They stay compressed in video memory, at a quarter of a PNG's memory, which matters for games with many large images. Turn it on in `debuff.config.ts`:

```ts
export default defineConfig({ assets: { compress: true } }); // or { formats: ["bc7", "astc4x4"], exclude: ["ui/"] }
```

Then give the loader the formats the renderer decodes and spryte's parser:

```ts
import { Assets } from "@debuffjp/assets";
import { parseKtx2 } from "@debuffjp/core";

const assets = new Assets(manifest, "", { compressed: { formats: game.draw.compressedFormats, parse: parseKtx2 } });
```

- Each image gets a BC7 copy (desktop), an ASTC 4x4 copy and an ETC2 copy (phones), all from one encode with mip levels. The loader picks the one the GPU decodes and loads the PNG when none fits, as on the Canvas 2D fallback.
- `frame()` and `frames()` work as before. `image()` throws for an image that loaded compressed, as it does for an atlased one.
- Images with compressed copies aren't preloaded from `index.html`, since the build can't know which file the player's GPU wants.
- Encoding takes about a quarter of a second for a 960x540 image the first time; results are cached in `.debuff/cache/ktx2`.
- The copies hold premultiplied colour, which is what the renderer draws. Leave out images a shader reads as data (`asData`) with `exclude`.

## New formats

Asset importers are plugins and can be written in any language. See [Plugins](./plugins#asset-importers).

Two importers ship with the CLI. `.tt` files, drawn by an agent with [tenten](https://github.com/debuffjp/tenten), become sheets with no configuration; debuff fetches tenten with npx the first time it needs it, and `tenten({ command })` in `debuff.config.ts` changes how it is run. `aseprite()` runs Aseprite over `.aseprite` files and is opt-in:

```ts
import { aseprite, defineConfig } from "@debuffjp/cli";

export default defineConfig({ plugins: [aseprite()] });
```