Allow for textures that aren't 16x16

This commit is contained in:
2026-09-28 16:20:37 -03:00
parent 52ef4e5ca6
commit fe149bb9ad
18 changed files with 268 additions and 136 deletions
+42 -11
View File
@@ -2,6 +2,7 @@
import { assert, assertEquals, assertRejects, assertStringIncludes, assertThrows } from "@std/assert";
import { zipSync } from "fflate";
import { atlas_layout } from "$/client/atlas.ts";
import { MAX_TEXTURE_SIZE } from "$/common/constants.ts";
import { read_bmod, write_bmod } from "$/common/bmod.ts";
import { load_and_check } from "$/tools/check_mods.ts";
import { pack_mod } from "$/tools/pack_mod.ts";
@@ -79,16 +80,19 @@ Deno.test("broken .bmod files are turned away, saying why", () => {
Error,
"scripts.server scripts/server.js isn't in the file",
);
// a texture that isn't 16x16
const ui = Deno.readFileSync("assets/sprites/gui.png");
// a texture bigger than MAX_TEXTURE_SIZE. only its header is read
const big = new Uint8Array(24);
big.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
new DataView(big.buffer).setUint32(16, MAX_TEXTURE_SIZE * 2);
new DataView(big.buffer).setUint32(20, 16);
assertThrows(
() =>
read_bmod(
zip({ "manifest.json": text(manifest), "data.json": text({}), "textures/big.png": ui }),
zip({ "manifest.json": text(manifest), "data.json": text({}), "textures/big.png": big }),
"e.bmod",
),
Error,
"textures must be 16×16",
`textures can be at most ${MAX_TEXTURE_SIZE}×${MAX_TEXTURE_SIZE}`,
);
// too much once unzipped, without unzipping it all
const huge = new Uint8Array(65 * 1024 * 1024);
@@ -193,16 +197,43 @@ Deno.test("a mod with its own deno.json is bundled with it", async () => {
Deno.removeSync(sources, { recursive: true });
});
Deno.test("the atlas puts the missing texture first and the rest in order", () => {
const layout = atlas_layout(["b:two", "a:one", "engine:break_0", "a:one"]);
const sizes = (entries: [string, number, number][]) =>
new Map(entries.map(([id, width, height]) => [id, { width, height }]));
Deno.test("the atlas lays out same sized textures by id, with the missing texture among them", () => {
const layout = atlas_layout(sizes([["b:two", 16, 16], ["a:one", 16, 16], ["engine:break_0", 16, 16]]));
assertEquals(layout.size, 32);
assertEquals(layout.regions, {
"engine:missing": { x: 0, y: 0 },
"a:one": { x: 1, y: 0 },
"b:two": { x: 0, y: 1 },
"engine:break_0": { x: 1, y: 1 },
"a:one": { x: 0, y: 0, width: 16, height: 16 },
"b:two": { x: 16, y: 0, width: 16, height: 16 },
"engine:break_0": { x: 0, y: 16, width: 16, height: 16 },
"engine:missing": { x: 16, y: 16, width: 16, height: 16 },
});
assertEquals(atlas_layout(Array.from({ length: 100 }, (_, i) => `m:t${i}`)).size, 256);
const many = atlas_layout(sizes(Array.from({ length: 100 }, (_, i) => [`m:t${i}`, 16, 16])));
assertEquals(many.size, 256);
});
Deno.test("the atlas fits textures of any size, tallest first, without overlapping", () => {
const layout = atlas_layout(
sizes([["a:block", 16, 16], ["a:zombie", 64, 64], ["a:boat", 128, 32], ["a:x", 8, 24]]),
);
assertEquals(layout.size, 128);
assertEquals(layout.regions["a:zombie"], { x: 0, y: 0, width: 64, height: 64 });
assertEquals(layout.regions["a:boat"], { x: 0, y: 64, width: 128, height: 32 });
assertEquals(layout.regions["a:x"], { x: 0, y: 96, width: 8, height: 24 });
const big = atlas_layout(
sizes(Array.from({ length: 40 }, (_, i) => [`m:t${i}`, 16 + (i % 5) * 12, 8 + (i % 7) * 9])),
);
const regions = Object.values(big.regions);
for (const [i, a] of regions.entries()) {
assert(a.x >= 0 && a.y >= 0 && a.x + a.width <= big.size && a.y + a.height <= big.size);
for (const b of regions.slice(i + 1)) {
const apart = a.x + a.width <= b.x || b.x + b.width <= a.x || a.y + a.height <= b.y ||
b.y + b.height <= a.y;
assert(apart, `${JSON.stringify(a)} overlaps ${JSON.stringify(b)}`);
}
}
});
// write_bmod without anything optional still makes a file read_bmod takes