zx-kit 0.29.0 → 0.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -1
- package/dist/attrscreen.d.ts +96 -0
- package/dist/attrscreen.d.ts.map +1 -0
- package/dist/attrscreen.js +167 -0
- package/dist/attrscreen.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
> **A Speccy-flavoured fantasy toolkit for tiny TypeScript browser games.**
|
|
4
4
|
> Inspired by the ZX Spectrum — not an emulator, not a hardware clone.
|
|
5
5
|
|
|
6
|
-
Spectrum-palette canvas rendering. ROM bitmap font. AY-3-8912 three-channel audio. Beeper SFX. Tile maps. Free-roaming sprites. Collision detection. Saves. Camera. Scene manager. Particle pool. Dithered lighting. Offscreen layer cache. Zero dependencies. TypeScript-first.
|
|
6
|
+
Spectrum-palette canvas rendering. ROM bitmap font. AY-3-8912 three-channel audio. Beeper SFX. Tile maps. Free-roaming sprites. Collision detection. Saves. Camera. Scene manager. Particle pool. Dithered lighting. Offscreen layer cache. Authentic attribute clash. Zero dependencies. TypeScript-first.
|
|
7
7
|
|
|
8
8
|
[](https://www.npmjs.com/package/zx-kit)
|
|
9
9
|
[](LICENSE)
|
|
@@ -28,6 +28,7 @@ zx-kit captures that aesthetic in TypeScript. You get the Spectrum's palette, RO
|
|
|
28
28
|
- **Canvas renderer** — pixel-perfect scaled rendering, sprite flipping, text drawing, CRT scanline overlay, animated border flashing
|
|
29
29
|
- **Tile map engine** — scrollable maps, O(1) id-index, smart seasonal background swapping, solid-tile collision queries
|
|
30
30
|
- **Offscreen layer cache** — render a static or rarely-changing layer (tile map, CRT overlay) once to an offscreen canvas and blit it each frame; `dirty`-flag invalidation turns thousands of per-pixel `fillRect`s into a single `drawImage`
|
|
31
|
+
- **Authentic attribute clash (opt-in)** — a 32×24 cell ink/paper screen that reproduces the real Spectrum colour bleed when a sprite and the background share an 8×8 cell; resolved to one `putImageData`/frame. Off by default, on when you want it
|
|
31
32
|
- **Free-roaming sprites** — position, velocity, gravity, `flipX` caching, transparent or opaque background
|
|
32
33
|
- **Three-tier collision** — AABB overlap tests, generic rect-vs-tile wall resolution (any sprite size), and pixel-precise mask overlap with O(pixels) sorted-merge intersection — no allocations per frame
|
|
33
34
|
- **Keyboard and gamepad input** — configurable key-repeat, transparent gamepad polling, single-consume action flags, instant state reset on phase transitions
|
|
@@ -591,6 +592,7 @@ requestAnimationFrame(loop)
|
|
|
591
592
|
| [`i18n.ts`](#i18nts--runtime-locale-selection) | Type-safe runtime locale selection for translated string packs |
|
|
592
593
|
| [`lighting.ts`](#lightingts--dithered-cave-darkness) | Dithered cave darkness: pre-baked level tiles + dirty-cell buffer, one blit/frame (no per-frame putImageData) |
|
|
593
594
|
| [`cache.ts`](#cachets--offscreen-layer-cache) | Offscreen layer cache: render a static layer once, blit each frame, `dirty`-flag invalidation |
|
|
595
|
+
| [`attrscreen.ts`](#attrscreents--attribute-clash-opt-in) | Opt-in authentic ZX colour clash: 1-bit pixels + 32×24 per-cell ink/paper, one `putImageData`/frame |
|
|
594
596
|
| [`music.ts`](#musicts--note-name-ay-music) | Write AY music by note name (`A5`, `C#4`) and loop it for background tracks |
|
|
595
597
|
|
|
596
598
|
---
|
|
@@ -2792,6 +2794,65 @@ invalidateLayer(tiles)
|
|
|
2792
2794
|
|
|
2793
2795
|
---
|
|
2794
2796
|
|
|
2797
|
+
## `attrscreen.ts` — Attribute Clash (opt-in)
|
|
2798
|
+
|
|
2799
|
+
The real Spectrum stored the screen as **two planes**: a 1-bit pixel bitmap and a
|
|
2800
|
+
32×24 grid where each 8×8 cell holds exactly *one* ink + *one* paper. Drawing into a
|
|
2801
|
+
cell rewrote that single attribute, so the sprite and the background sharing the
|
|
2802
|
+
cell snapped to the same two colours — the famous **colour clash**.
|
|
2803
|
+
|
|
2804
|
+
zx-kit composites in full colour by default (no clash — see *Spectrum-inspired, not
|
|
2805
|
+
hardware-accurate*). `attrscreen.ts` is the opt-in way to get the authentic bleed,
|
|
2806
|
+
the same shape as `drawScanlines` / `renderDarkness`: route a frame through an
|
|
2807
|
+
`AttrScreen`, then `flushAttrScreen` once.
|
|
2808
|
+
|
|
2809
|
+
`stampMono` writes a monochrome bitmap's *shape* into the pixel plane and
|
|
2810
|
+
re-attributes every cell a lit pixel lands in — and it **never clears** other
|
|
2811
|
+
pixels, so whatever was already in a touched cell renders in the new colour. That
|
|
2812
|
+
is the clash. The flush resolves both planes into one reused `ImageData` and uploads
|
|
2813
|
+
it with a single `putImageData` + `drawImage` — never per-pixel `fillRect`.
|
|
2814
|
+
|
|
2815
|
+
> Not hardware-exact by choice: ink and paper may be any two of the 15 colours (the
|
|
2816
|
+
> real machine forced both to share the BRIGHT bit), and FLASH is not modelled (yet).
|
|
2817
|
+
> Assumes a little-endian platform (every browser). Headless-safe.
|
|
2818
|
+
|
|
2819
|
+
### `createAttrScreen(cols = 32, rows = 24): AttrScreen`
|
|
2820
|
+
|
|
2821
|
+
Allocates a screen-space attribute plane (default 32×24 cells = 256×192). Create
|
|
2822
|
+
once; reuse across frames. Throws on a non-positive size.
|
|
2823
|
+
|
|
2824
|
+
### `clearAttrScreen(scr, paper, ink?)`
|
|
2825
|
+
|
|
2826
|
+
Resets for a new frame: clears all pixels to paper and fills every cell's
|
|
2827
|
+
attributes (`ink` defaults to `paper`). Call before stamping.
|
|
2828
|
+
|
|
2829
|
+
### `stampMono(scr, bitmap, x, y, ink, paper, policy?)`
|
|
2830
|
+
|
|
2831
|
+
Stamps a monochrome `Bitmap` at screen pixel `(x, y)` (rounded; may be sub-cell,
|
|
2832
|
+
negative, or off-screen — clipped). `policy` controls how touched cells recolour:
|
|
2833
|
+
`'both'` (default — ink **and** paper, the most authentic bleed), `'ink-only'`
|
|
2834
|
+
(keep the existing paper), `'paper-only'`.
|
|
2835
|
+
|
|
2836
|
+
### `flushAttrScreen(ctx, scr)`
|
|
2837
|
+
|
|
2838
|
+
Resolves the two planes into RGBA and uploads with one `putImageData` + `drawImage`
|
|
2839
|
+
at `(0, 0)` under the current transform (so `setupCanvas`'s `×scale` fills the
|
|
2840
|
+
canvas). Headless: fills `scr.rgba` and skips the blit.
|
|
2841
|
+
|
|
2842
|
+
```ts
|
|
2843
|
+
const scr = createAttrScreen() // once
|
|
2844
|
+
// each frame, in screen space:
|
|
2845
|
+
clearAttrScreen(scr, C.BLACK)
|
|
2846
|
+
stampMono(scr, caveBitmap, 0, 0, C.B_BLUE, C.BLACK) // background
|
|
2847
|
+
stampMono(scr, rabbitBitmap, rx, ry, C.B_WHITE, C.BLACK) // sprite → its cells clash to white
|
|
2848
|
+
flushAttrScreen(ctx, scr)
|
|
2849
|
+
```
|
|
2850
|
+
|
|
2851
|
+
> A cell clashes only when a lit pixel lands in it (silhouette clash), so a sprite
|
|
2852
|
+
> recolours exactly the cells it visibly occupies.
|
|
2853
|
+
|
|
2854
|
+
---
|
|
2855
|
+
|
|
2795
2856
|
## Architecture
|
|
2796
2857
|
|
|
2797
2858
|
### Module structure
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module attrscreen
|
|
3
|
+
*
|
|
4
|
+
* **Authentic ZX attribute clash — opt-in.** The real Spectrum stored the screen
|
|
5
|
+
* as two separate planes: a 1-bit **pixel** bitmap (256×192) and a 32×24
|
|
6
|
+
* **attribute** grid where each 8×8 cell holds exactly *one* ink + *one* paper.
|
|
7
|
+
* Drawing a sprite into a cell rewrote that cell's attribute, so everything in
|
|
8
|
+
* the cell — sprite *and* background — snapped to the same two colours. That bleed
|
|
9
|
+
* is the famous *colour clash*.
|
|
10
|
+
*
|
|
11
|
+
* zx-kit normally composites in full colour (no clash — by design, see the README).
|
|
12
|
+
* This module is the opt-in way to get the real thing, the same shape as the other
|
|
13
|
+
* effects ({@link "renderer" | drawScanlines}, {@link "lighting" | renderDarkness}):
|
|
14
|
+
* a game that wants clash routes its drawing through an {@link AttrScreen} and
|
|
15
|
+
* calls {@link flushAttrScreen} once per frame.
|
|
16
|
+
*
|
|
17
|
+
* **The dual plane:** {@link stampMono} writes a monochrome bitmap's *shape* into
|
|
18
|
+
* the pixel plane and re-attributes every cell it touches (per {@link AttrPolicy}).
|
|
19
|
+
* Crucially it does **not** clear other pixels — so a leaf already drawn in a cell
|
|
20
|
+
* keeps its pixels but now renders in the *new* cell colour. That is the clash.
|
|
21
|
+
*
|
|
22
|
+
* **Fast flush:** the two planes are resolved into one RGBA buffer and uploaded
|
|
23
|
+
* with a single `putImageData` + `drawImage` — never per-pixel `fillRect`. Assumes
|
|
24
|
+
* a little-endian platform (every browser; common Node).
|
|
25
|
+
*
|
|
26
|
+
* Headless-safe: with no `document` the offscreen canvas is `null`, the resolve
|
|
27
|
+
* still runs (so the logic is testable) and the blit is skipped — nothing throws.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const scr = createAttrScreen() // 32×24 cells = 256×192, once
|
|
32
|
+
* // each frame, in screen space:
|
|
33
|
+
* clearAttrScreen(scr, C.BLACK) // blank paper
|
|
34
|
+
* stampMono(scr, caveBitmap, 0, 0, C.B_BLUE, C.BLACK) // background
|
|
35
|
+
* stampMono(scr, rabbitBitmap, rx, ry, C.B_WHITE, C.BLACK) // sprite → its cells clash to white
|
|
36
|
+
* flushAttrScreen(ctx, scr) // one putImageData + drawImage
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
import { type SpectrumColor } from './palette.js';
|
|
40
|
+
import type { Bitmap } from './renderer.js';
|
|
41
|
+
/**
|
|
42
|
+
* How a {@link stampMono} re-colours each cell it touches:
|
|
43
|
+
* - `'both'` — set ink **and** paper (last writer owns the whole cell — the most
|
|
44
|
+
* authentic clash: the background paper changes too).
|
|
45
|
+
* - `'ink-only'` — set ink, keep the existing paper (gentler bleed).
|
|
46
|
+
* - `'paper-only'` — set paper, keep the existing ink.
|
|
47
|
+
*/
|
|
48
|
+
export type AttrPolicy = 'both' | 'ink-only' | 'paper-only';
|
|
49
|
+
/** A screen-space dual plane: 1-bit pixels + per-cell ink/paper attributes. */
|
|
50
|
+
export interface AttrScreen {
|
|
51
|
+
readonly cols: number;
|
|
52
|
+
readonly rows: number;
|
|
53
|
+
readonly width: number;
|
|
54
|
+
readonly height: number;
|
|
55
|
+
/** One byte per pixel — `1` = ink, `0` = paper. Row-major, `width*height`. */
|
|
56
|
+
readonly pixels: Uint8Array;
|
|
57
|
+
/** Packed RGBA (little-endian) ink colour per cell, `cols*rows`. */
|
|
58
|
+
readonly cellInk: Uint32Array;
|
|
59
|
+
/** Packed RGBA paper colour per cell, `cols*rows`. */
|
|
60
|
+
readonly cellPaper: Uint32Array;
|
|
61
|
+
/** Resolved RGBA frame buffer (`width*height*4`), filled by {@link flushAttrScreen}. */
|
|
62
|
+
readonly rgba: Uint8ClampedArray;
|
|
63
|
+
/** Reusable `ImageData` backing {@link rgba} (`null` when headless). */
|
|
64
|
+
readonly image: ImageData | null;
|
|
65
|
+
/** Offscreen canvas the resolved buffer is uploaded to (`null` when headless). */
|
|
66
|
+
readonly canvas: HTMLCanvasElement | null;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Creates a screen-space attribute plane of `cols`×`rows` 8×8 cells (default
|
|
70
|
+
* 32×24 = 256×192). Allocate once; reuse across frames. Throws on a non-positive
|
|
71
|
+
* size.
|
|
72
|
+
*/
|
|
73
|
+
export declare function createAttrScreen(cols?: number, rows?: number): AttrScreen;
|
|
74
|
+
/**
|
|
75
|
+
* Resets the screen for a new frame: clears all pixels to paper and fills every
|
|
76
|
+
* cell's attributes. `ink` defaults to `paper`, so untouched cells are a flat
|
|
77
|
+
* paper colour. Call at the start of each frame, before stamping.
|
|
78
|
+
*/
|
|
79
|
+
export declare function clearAttrScreen(scr: AttrScreen, paper: SpectrumColor, ink?: SpectrumColor): void;
|
|
80
|
+
/**
|
|
81
|
+
* Stamps a monochrome {@link Bitmap} at screen pixel `(x, y)` (rounded; may be
|
|
82
|
+
* sub-cell, negative, or off-screen — clipped). Sets the bitmap's lit pixels in
|
|
83
|
+
* the pixel plane and re-attributes every cell that receives a lit pixel, per
|
|
84
|
+
* `policy` (default `'both'`). Lit-pixel-only: existing pixels are never cleared,
|
|
85
|
+
* so other sprites/background in a touched cell bleed to the new colour.
|
|
86
|
+
*/
|
|
87
|
+
export declare function stampMono(scr: AttrScreen, bitmap: Bitmap, x: number, y: number, ink: SpectrumColor, paper: SpectrumColor, policy?: AttrPolicy): void;
|
|
88
|
+
/**
|
|
89
|
+
* Resolves the two planes into RGBA (each pixel takes its cell's ink or paper)
|
|
90
|
+
* and uploads the result with one `putImageData` + `drawImage`. The image is
|
|
91
|
+
* drawn at `(0, 0)` under the current transform, so the usual `setupCanvas`
|
|
92
|
+
* `×scale` makes it fill the canvas. Headless: fills {@link AttrScreen.rgba} and
|
|
93
|
+
* skips the blit.
|
|
94
|
+
*/
|
|
95
|
+
export declare function flushAttrScreen(ctx: CanvasRenderingContext2D, scr: AttrScreen): void;
|
|
96
|
+
//# sourceMappingURL=attrscreen.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attrscreen.d.ts","sourceRoot":"","sources":["../src/attrscreen.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,OAAO,EAAQ,KAAK,aAAa,EAAE,MAAM,cAAc,CAAA;AACvD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AAE3C;;;;;;GAMG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,UAAU,GAAG,YAAY,CAAA;AAE3D,+EAA+E;AAC/E,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,8EAA8E;IAC9E,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAA;IAC3B,oEAAoE;IACpE,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAA;IAC7B,sDAAsD;IACtD,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAA;IAC/B,wFAAwF;IACxF,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAA;IAChC,wEAAwE;IACxE,QAAQ,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI,CAAA;IAChC,kFAAkF;IAClF,QAAQ,CAAC,MAAM,EAAE,iBAAiB,GAAG,IAAI,CAAA;CAC1C;AAUD;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,SAAK,EAAE,IAAI,SAAK,GAAG,UAAU,CAiCjE;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,aAAa,EAAE,GAAG,GAAE,aAAqB,GAAG,IAAI,CAIvG;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CACvB,GAAG,EAAE,UAAU,EACf,MAAM,EAAE,MAAM,EACd,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,GAAG,EAAE,aAAa,EAClB,KAAK,EAAE,aAAa,EACpB,MAAM,GAAE,UAAmB,GAC1B,IAAI,CA+BN;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,wBAAwB,EAAE,GAAG,EAAE,UAAU,GAAG,IAAI,CAoBpF"}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module attrscreen
|
|
3
|
+
*
|
|
4
|
+
* **Authentic ZX attribute clash — opt-in.** The real Spectrum stored the screen
|
|
5
|
+
* as two separate planes: a 1-bit **pixel** bitmap (256×192) and a 32×24
|
|
6
|
+
* **attribute** grid where each 8×8 cell holds exactly *one* ink + *one* paper.
|
|
7
|
+
* Drawing a sprite into a cell rewrote that cell's attribute, so everything in
|
|
8
|
+
* the cell — sprite *and* background — snapped to the same two colours. That bleed
|
|
9
|
+
* is the famous *colour clash*.
|
|
10
|
+
*
|
|
11
|
+
* zx-kit normally composites in full colour (no clash — by design, see the README).
|
|
12
|
+
* This module is the opt-in way to get the real thing, the same shape as the other
|
|
13
|
+
* effects ({@link "renderer" | drawScanlines}, {@link "lighting" | renderDarkness}):
|
|
14
|
+
* a game that wants clash routes its drawing through an {@link AttrScreen} and
|
|
15
|
+
* calls {@link flushAttrScreen} once per frame.
|
|
16
|
+
*
|
|
17
|
+
* **The dual plane:** {@link stampMono} writes a monochrome bitmap's *shape* into
|
|
18
|
+
* the pixel plane and re-attributes every cell it touches (per {@link AttrPolicy}).
|
|
19
|
+
* Crucially it does **not** clear other pixels — so a leaf already drawn in a cell
|
|
20
|
+
* keeps its pixels but now renders in the *new* cell colour. That is the clash.
|
|
21
|
+
*
|
|
22
|
+
* **Fast flush:** the two planes are resolved into one RGBA buffer and uploaded
|
|
23
|
+
* with a single `putImageData` + `drawImage` — never per-pixel `fillRect`. Assumes
|
|
24
|
+
* a little-endian platform (every browser; common Node).
|
|
25
|
+
*
|
|
26
|
+
* Headless-safe: with no `document` the offscreen canvas is `null`, the resolve
|
|
27
|
+
* still runs (so the logic is testable) and the blit is skipped — nothing throws.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const scr = createAttrScreen() // 32×24 cells = 256×192, once
|
|
32
|
+
* // each frame, in screen space:
|
|
33
|
+
* clearAttrScreen(scr, C.BLACK) // blank paper
|
|
34
|
+
* stampMono(scr, caveBitmap, 0, 0, C.B_BLUE, C.BLACK) // background
|
|
35
|
+
* stampMono(scr, rabbitBitmap, rx, ry, C.B_WHITE, C.BLACK) // sprite → its cells clash to white
|
|
36
|
+
* flushAttrScreen(ctx, scr) // one putImageData + drawImage
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
import { CELL } from './palette.js';
|
|
40
|
+
/** Packs a `#RRGGBB` Spectrum colour into a little-endian RGBA word (opaque). */
|
|
41
|
+
function hexToU32(hex) {
|
|
42
|
+
const r = parseInt(hex.slice(1, 3), 16);
|
|
43
|
+
const g = parseInt(hex.slice(3, 5), 16);
|
|
44
|
+
const b = parseInt(hex.slice(5, 7), 16);
|
|
45
|
+
return ((255 << 24) | (b << 16) | (g << 8) | r) >>> 0;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Creates a screen-space attribute plane of `cols`×`rows` 8×8 cells (default
|
|
49
|
+
* 32×24 = 256×192). Allocate once; reuse across frames. Throws on a non-positive
|
|
50
|
+
* size.
|
|
51
|
+
*/
|
|
52
|
+
export function createAttrScreen(cols = 32, rows = 24) {
|
|
53
|
+
if (!Number.isInteger(cols) || cols <= 0 || !Number.isInteger(rows) || rows <= 0) {
|
|
54
|
+
throw new Error(`createAttrScreen: cols and rows must be positive integers, got ${cols}×${rows}`);
|
|
55
|
+
}
|
|
56
|
+
const width = cols * CELL;
|
|
57
|
+
const height = rows * CELL;
|
|
58
|
+
let canvas = null;
|
|
59
|
+
let image = null;
|
|
60
|
+
if (typeof document !== 'undefined') {
|
|
61
|
+
canvas = document.createElement('canvas');
|
|
62
|
+
canvas.width = width;
|
|
63
|
+
canvas.height = height;
|
|
64
|
+
const ctx = canvas.getContext('2d');
|
|
65
|
+
if (ctx) {
|
|
66
|
+
ctx.imageSmoothingEnabled = false;
|
|
67
|
+
image = ctx.createImageData(width, height);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// In the browser the resolved buffer IS the ImageData's data (no per-frame copy,
|
|
71
|
+
// no constructor); headless it's a standalone buffer so the resolve stays testable.
|
|
72
|
+
const rgba = image ? image.data : new Uint8ClampedArray(width * height * 4);
|
|
73
|
+
return {
|
|
74
|
+
cols,
|
|
75
|
+
rows,
|
|
76
|
+
width,
|
|
77
|
+
height,
|
|
78
|
+
pixels: new Uint8Array(width * height),
|
|
79
|
+
cellInk: new Uint32Array(cols * rows),
|
|
80
|
+
cellPaper: new Uint32Array(cols * rows),
|
|
81
|
+
rgba,
|
|
82
|
+
image,
|
|
83
|
+
canvas,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Resets the screen for a new frame: clears all pixels to paper and fills every
|
|
88
|
+
* cell's attributes. `ink` defaults to `paper`, so untouched cells are a flat
|
|
89
|
+
* paper colour. Call at the start of each frame, before stamping.
|
|
90
|
+
*/
|
|
91
|
+
export function clearAttrScreen(scr, paper, ink = paper) {
|
|
92
|
+
scr.pixels.fill(0);
|
|
93
|
+
scr.cellPaper.fill(hexToU32(paper));
|
|
94
|
+
scr.cellInk.fill(hexToU32(ink));
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Stamps a monochrome {@link Bitmap} at screen pixel `(x, y)` (rounded; may be
|
|
98
|
+
* sub-cell, negative, or off-screen — clipped). Sets the bitmap's lit pixels in
|
|
99
|
+
* the pixel plane and re-attributes every cell that receives a lit pixel, per
|
|
100
|
+
* `policy` (default `'both'`). Lit-pixel-only: existing pixels are never cleared,
|
|
101
|
+
* so other sprites/background in a touched cell bleed to the new colour.
|
|
102
|
+
*/
|
|
103
|
+
export function stampMono(scr, bitmap, x, y, ink, paper, policy = 'both') {
|
|
104
|
+
const inkU32 = hexToU32(ink);
|
|
105
|
+
const paperU32 = hexToU32(paper);
|
|
106
|
+
const setInk = policy !== 'paper-only';
|
|
107
|
+
const setPaper = policy !== 'ink-only';
|
|
108
|
+
const { data, width: bw, height: bh } = bitmap;
|
|
109
|
+
const bytesPerRow = bw >> 3;
|
|
110
|
+
const ox = Math.round(x);
|
|
111
|
+
const oy = Math.round(y);
|
|
112
|
+
for (let row = 0; row < bh; row++) {
|
|
113
|
+
const py = oy + row;
|
|
114
|
+
if (py < 0 || py >= scr.height)
|
|
115
|
+
continue;
|
|
116
|
+
const rowByteBase = row * bytesPerRow;
|
|
117
|
+
const rowPixBase = py * scr.width;
|
|
118
|
+
const cellRowBase = (py >> 3) * scr.cols;
|
|
119
|
+
for (let byteIdx = 0; byteIdx < bytesPerRow; byteIdx++) {
|
|
120
|
+
const byte = data[rowByteBase + byteIdx];
|
|
121
|
+
if (!byte)
|
|
122
|
+
continue;
|
|
123
|
+
const colBase = byteIdx << 3;
|
|
124
|
+
for (let bit = 0; bit < 8; bit++) {
|
|
125
|
+
if (!(byte & (0x80 >> bit)))
|
|
126
|
+
continue;
|
|
127
|
+
const px = ox + colBase + bit;
|
|
128
|
+
if (px < 0 || px >= scr.width)
|
|
129
|
+
continue;
|
|
130
|
+
scr.pixels[rowPixBase + px] = 1;
|
|
131
|
+
const cell = cellRowBase + (px >> 3);
|
|
132
|
+
if (setInk)
|
|
133
|
+
scr.cellInk[cell] = inkU32;
|
|
134
|
+
if (setPaper)
|
|
135
|
+
scr.cellPaper[cell] = paperU32;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Resolves the two planes into RGBA (each pixel takes its cell's ink or paper)
|
|
142
|
+
* and uploads the result with one `putImageData` + `drawImage`. The image is
|
|
143
|
+
* drawn at `(0, 0)` under the current transform, so the usual `setupCanvas`
|
|
144
|
+
* `×scale` makes it fill the canvas. Headless: fills {@link AttrScreen.rgba} and
|
|
145
|
+
* skips the blit.
|
|
146
|
+
*/
|
|
147
|
+
export function flushAttrScreen(ctx, scr) {
|
|
148
|
+
const { pixels, cellInk, cellPaper, width, height, cols } = scr;
|
|
149
|
+
const out = new Uint32Array(scr.rgba.buffer);
|
|
150
|
+
let p = 0;
|
|
151
|
+
for (let yy = 0; yy < height; yy++) {
|
|
152
|
+
const cellRow = (yy >> 3) * cols;
|
|
153
|
+
for (let xx = 0; xx < width; xx++) {
|
|
154
|
+
const cell = cellRow + (xx >> 3);
|
|
155
|
+
out[p] = pixels[p] ? cellInk[cell] : cellPaper[cell];
|
|
156
|
+
p++;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (scr.canvas && scr.image) {
|
|
160
|
+
const offctx = scr.canvas.getContext('2d');
|
|
161
|
+
if (offctx) {
|
|
162
|
+
offctx.putImageData(scr.image, 0, 0);
|
|
163
|
+
ctx.drawImage(scr.canvas, 0, 0);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=attrscreen.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attrscreen.js","sourceRoot":"","sources":["../src/attrscreen.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,OAAO,EAAE,IAAI,EAAsB,MAAM,cAAc,CAAA;AAgCvD,iFAAiF;AACjF,SAAS,QAAQ,CAAC,GAAkB;IAClC,MAAM,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IACvC,MAAM,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IACvC,MAAM,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IACvC,OAAO,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAA;AACvD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAI,GAAG,EAAE,EAAE,IAAI,GAAG,EAAE;IACnD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,I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AA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA;AACvB,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA;AACvB,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,YAAY,CAAA"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA;AACvB,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA;AACvB,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,YAAY,CAAA"}
|