Add 16x16 test sprites and a size contract that fails CI.
Co-authored-by: Cursor <cursoragent@cursor.com>
The contents of this binary file are not shown.
The contents of this binary file are not shown.
The contents of this binary file are not shown.
The contents of this binary file are not shown.
The contents of this binary file are not shown.
The contents of this binary file are not shown.
@@ -0,0 +1,48 @@
1
import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js";
2
3
export const PLAY_IMAGES = [
4
{ name: "ground-grass", url: "/assets/grounds/ground-grass.png" },
5
{ name: "ground-wood", url: "/assets/grounds/ground-wood.png" },
6
{ name: "block-stone-wall", url: "/assets/blocks/block-stone-wall.png" },
7
{ name: "block-torch", url: "/assets/blocks/block-torch.png" },
8
{ name: "player-body-male", url: "/assets/characters/player-body-male.png" },
9
{ name: "player-body-female", url: "/assets/characters/player-body-female.png" },
10
{ name: "night-shade", url: shadeDataUrl() }
11
];
12
13
export function shadeDataUrl ()
14
{
15
const canvas = document.createElement("canvas");
16
canvas.width = SPRITE_SIZE_PX;
17
canvas.height = SPRITE_SIZE_PX;
18
const context = canvas.getContext("2d");
19
context.fillStyle = "#000000";
20
context.fillRect(0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX);
21
return canvas.toDataURL("image/png");
22
}
23
24
export function assertSpriteSizes (images)
25
{
26
const offenders = [];
27
for (const [name, sprite] of Object.entries(images))
28
{
29
if (sprite.width !== SPRITE_SIZE_PX || sprite.height !== SPRITE_SIZE_PX)
30
{
31
offenders.push(`${name} is ${sprite.width}×${sprite.height}`);
32
}
33
}
34
if (offenders.length)
35
{ throw new Error(`World sprites must be ${SPRITE_SIZE_PX}×${SPRITE_SIZE_PX}. Offenders: ${offenders.join("; ")}`); }
36
}
37
38
export function useNearestNeighbour (scene, images)
39
{
40
const gl = scene.glContext.gl;
41
for (const sprite of Object.values(images))
42
{
43
const texture = scene.glContext.getTexture(sprite.g_image);
44
gl.bindTexture(gl.TEXTURE_2D, texture);
45
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
46
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
47
}
48
}
@@ -0,0 +1,48 @@1
import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js";2
3
export const PLAY_IMAGES = [4
{ name: "ground-grass", url: "/assets/grounds/ground-grass.png" },5
{ name: "ground-wood", url: "/assets/grounds/ground-wood.png" },6
{ name: "block-stone-wall", url: "/assets/blocks/block-stone-wall.png" },7
{ name: "block-torch", url: "/assets/blocks/block-torch.png" },8
{ name: "player-body-male", url: "/assets/characters/player-body-male.png" },9
{ name: "player-body-female", url: "/assets/characters/player-body-female.png" },10
{ name: "night-shade", url: shadeDataUrl() }11
];12
13
export function shadeDataUrl ()14
{15
const canvas = document.createElement("canvas");16
canvas.width = SPRITE_SIZE_PX;17
canvas.height = SPRITE_SIZE_PX;18
const context = canvas.getContext("2d");19
context.fillStyle = "#000000";20
context.fillRect(0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX);21
return canvas.toDataURL("image/png");22
}23
24
export function assertSpriteSizes (images)25
{26
const offenders = [];27
for (const [name, sprite] of Object.entries(images))28
{29
if (sprite.width !== SPRITE_SIZE_PX || sprite.height !== SPRITE_SIZE_PX)30
{31
offenders.push(`${name} is ${sprite.width}×${sprite.height}`);32
}33
}34
if (offenders.length)35
{ throw new Error(`World sprites must be ${SPRITE_SIZE_PX}×${SPRITE_SIZE_PX}. Offenders: ${offenders.join("; ")}`); }36
}37
38
export function useNearestNeighbour (scene, images)39
{40
const gl = scene.glContext.gl;41
for (const sprite of Object.values(images))42
{43
const texture = scene.glContext.getTexture(sprite.g_image);44
gl.bindTexture(gl.TEXTURE_2D, texture);45
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);46
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);47
}48
}@@ -0,0 +1,58 @@
1
import { readdirSync, readFileSync } from "node:fs";
2
import { extname, join } from "node:path";
3
import { fileURLToPath } from "node:url";
4
import test from "node:test";
5
import assert from "node:assert/strict";
6
7
const SPRITE_SIZE = 16;
8
const ASSET_ROOT = join(fileURLToPath(new URL("..", import.meta.url)), "assets");
9
const SPRITE_FOLDERS = ["grounds", "blocks", "characters", "items", "equipment", "mobs"];
10
11
function pngSize (buffer)
12
{
13
if (buffer.length < 24 || buffer.toString("ascii", 1, 4) !== "PNG")
14
{ throw new Error("Not a PNG"); }
15
return {
16
width: buffer.readUInt32BE(16),
17
height: buffer.readUInt32BE(20)
18
};
19
}
20
21
function listPngs (dir)
22
{
23
const files = [];
24
for (const entry of readdirSync(dir, { withFileTypes: true }))
25
{
26
const path = join(dir, entry.name);
27
if (entry.isDirectory())
28
{ files.push(...listPngs(path)); }
29
else if (extname(entry.name).toLowerCase() === ".png")
30
{ files.push(path); }
31
}
32
return files;
33
}
34
35
test("every world sprite is exactly 16×16", () =>
36
{
37
const offenders = [];
38
for (const folder of SPRITE_FOLDERS)
39
{
40
const dir = join(ASSET_ROOT, folder);
41
let files = [];
42
try
43
{ files = listPngs(dir); }
44
catch (error)
45
{
46
if (error.code === "ENOENT")
47
{ continue; }
48
throw error;
49
}
50
for (const file of files)
51
{
52
const { width, height } = pngSize(readFileSync(file));
53
if (width !== SPRITE_SIZE || height !== SPRITE_SIZE)
54
{ offenders.push(`${file} is ${width}×${height}`); }
55
}
56
}
57
assert.equal(offenders.length, 0, offenders.join("\n"));
58
});
@@ -0,0 +1,58 @@1
import { readdirSync, readFileSync } from "node:fs";2
import { extname, join } from "node:path";3
import { fileURLToPath } from "node:url";4
import test from "node:test";5
import assert from "node:assert/strict";6
7
const SPRITE_SIZE = 16;8
const ASSET_ROOT = join(fileURLToPath(new URL("..", import.meta.url)), "assets");9
const SPRITE_FOLDERS = ["grounds", "blocks", "characters", "items", "equipment", "mobs"];10
11
function pngSize (buffer)12
{13
if (buffer.length < 24 || buffer.toString("ascii", 1, 4) !== "PNG")14
{ throw new Error("Not a PNG"); }15
return {16
width: buffer.readUInt32BE(16),17
height: buffer.readUInt32BE(20)18
};19
}20
21
function listPngs (dir)22
{23
const files = [];24
for (const entry of readdirSync(dir, { withFileTypes: true }))25
{26
const path = join(dir, entry.name);27
if (entry.isDirectory())28
{ files.push(...listPngs(path)); }29
else if (extname(entry.name).toLowerCase() === ".png")30
{ files.push(path); }31
}32
return files;33
}34
35
test("every world sprite is exactly 16×16", () =>36
{37
const offenders = [];38
for (const folder of SPRITE_FOLDERS)39
{40
const dir = join(ASSET_ROOT, folder);41
let files = [];42
try43
{ files = listPngs(dir); }44
catch (error)45
{46
if (error.code === "ENOENT")47
{ continue; }48
throw error;49
}50
for (const file of files)51
{52
const { width, height } = pngSize(readFileSync(file));53
if (width !== SPRITE_SIZE || height !== SPRITE_SIZE)54
{ offenders.push(`${file} is ${width}×${height}`); }55
}56
}57
assert.equal(offenders.length, 0, offenders.join("\n"));58
});@@ -135,9 +135,9 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
135
135
136
136
## 16×16 sprite contract
137
137
138
**Status:** not implemented
139
**Spec:** World sprites must be exactly 16×16. Reject at load. CI/unit test lists every offender and fails.
140
**Code:** —
138
**Status:** full
139
**Spec:** World sprites must be exactly 16×16. Reject at client load. The unit test walks `client/assets/{grounds,blocks,characters,...}` and fails listing every offender.
140
**Code:** `client/src/render/sprites.js` (`assertSpriteSizes`), `client/tests/sprite-size.test.js`, art in `client/assets/`
141
141
142
142
## Play viewport and camera
143
143
@@ -135,9 +135,9 @@ Each entry is a short note on behaviour and where the code lives. Update this fi135
135
136
## 16×16 sprite contract136
## 16×16 sprite contract137
137
138
**Status:** not implemented 138
**Status:** full 139
**Spec:** World sprites must be exactly 16×16. Reject at load. CI/unit test lists every offender and fails. 139
**Spec:** World sprites must be exactly 16×16. Reject at client load. The unit test walks `client/assets/{grounds,blocks,characters,...}` and fails listing every offender. 140
**Code:** —140
**Code:** `client/src/render/sprites.js` (`assertSpriteSizes`), `client/tests/sprite-size.test.js`, art in `client/assets/`141
141
142
## Play viewport and camera142
## Play viewport and camera143
143