@amritk/nish-x86_64-linux 0.38.0 → 0.40.1
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/INSTALL.md +74 -129
- package/bin/nish +0 -0
- package/docs/AI.md +455 -65
- package/package.json +1 -1
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +320 -33
- package/runtime/nish.h +84 -10
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +97 -12
- package/runtime/runtime-async.c +43 -10
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-net.c +64 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +94 -10
- package/runtime/runtime-sandbox.c +404 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-wasm.c +437 -0
- package/runtime/runtime.c +5 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +42 -8
- package/std/README.md +78 -91
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1629 -2732
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +151 -13
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +120 -10
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +344 -111
- package/std/json-typed.ts +256 -12
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/handler.ts +130 -16
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +636 -162
- package/std/net/http1-server.ts +569 -42
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +131 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/postgres-async.ts +1304 -86
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +122 -7
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +373 -27
- package/std/net/quic-stream.ts +195 -37
- package/std/net/quic.ts +516 -22
- package/std/net/router.ts +407 -38
- package/std/net/serve.ts +1022 -110
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls/verify.ts +19 -1
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +129 -19
- package/std/net/webtransport.ts +185 -1
- package/std/raster.ts +837 -0
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/release.ts +593 -0
- package/std/secret.ts +26 -59
- package/std/synth.ts +143 -0
- package/std/text.ts +233 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
package/std/raster.ts
ADDED
|
@@ -0,0 +1,837 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nish/raster` — software drawing into a frame of `0xRRGGBB` pixels: colour
|
|
3
|
+
* arithmetic, rectangles, anti-aliased discs, additive glows, a vertical
|
|
4
|
+
* gradient, a 5×7 pixel font, and two fixed pools that move and draw
|
|
5
|
+
* themselves (a particle pool and a trail).
|
|
6
|
+
*
|
|
7
|
+
* **What it draws into.** Every drawing function takes the frame as three
|
|
8
|
+
* arguments, `pixels: Int32Array, width: i32, height: i32`, row-major with
|
|
9
|
+
* pixel `(x, y)` at `y * width + x`. An `Int32Array` parameter accepts both
|
|
10
|
+
* kinds of frame a program has, so neither path copies:
|
|
11
|
+
*
|
|
12
|
+
* - a `nish/term-pixels` `Framebuffer` lends its `pixels` (an `i32[]`, which
|
|
13
|
+
* is the same type to `nish`) with its `width` and `height`, passed through
|
|
14
|
+
* `rasterFrame`, which returns the same array and exists only because `tsc`
|
|
15
|
+
* reads `Int32Array` as the JavaScript view (`runtime/nish.d.ts`);
|
|
16
|
+
* - a wasm program lends an `Int32Array` it declares as module storage, which
|
|
17
|
+
* the page reads in place (`nish/host/game`'s frame is one).
|
|
18
|
+
*
|
|
19
|
+
* The functions only index the frame, which is what lets module storage be
|
|
20
|
+
* lent to them. A frame shorter than `width * height` loses the rows it does
|
|
21
|
+
* not hold, and everything is clipped to the frame, so no coordinate, however
|
|
22
|
+
* wild, reads or writes outside it.
|
|
23
|
+
*
|
|
24
|
+
* import { Framebuffer } from "nish/term-pixels";
|
|
25
|
+
* import { rasterDisc, rasterGradient, rasterRgb } from "nish/raster";
|
|
26
|
+
*
|
|
27
|
+
* const fb = new Framebuffer(320, 180);
|
|
28
|
+
* const px = rasterFrame(fb.pixels);
|
|
29
|
+
* rasterGradient(px, fb.width, fb.height, rasterRgb(10, 10, 40), 0);
|
|
30
|
+
* rasterDisc(px, fb.width, fb.height, 160.0, 90.0, 12.0, 0xffcc00, 1.0);
|
|
31
|
+
*
|
|
32
|
+
* Shapes take pixel coordinates as `f64` where a fraction matters (a disc's
|
|
33
|
+
* centre and radius) and as `i32` where it does not (a rectangle's edges).
|
|
34
|
+
* The pools keep positions in whatever units the caller simulates in and draw
|
|
35
|
+
* through a transform, `pixel = offset + v * scale`, so a game can keep its
|
|
36
|
+
* world apart from the frame's size.
|
|
37
|
+
*
|
|
38
|
+
* Nothing here allocates after a pool is made, and every shape touches each
|
|
39
|
+
* pixel it covers a bounded number of times, so a frame's cost follows the
|
|
40
|
+
* frame's size. Pure: no capability, no I/O and no clock.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/** The weight `rasterRect` and the mixes read as "all of the new colour". */
|
|
44
|
+
export const RASTER_SOLID: i32 = 256
|
|
45
|
+
|
|
46
|
+
/** A glyph's width in font pixels. */
|
|
47
|
+
export const RASTER_GLYPH_W: i32 = 5
|
|
48
|
+
|
|
49
|
+
/** A glyph's height in font pixels. */
|
|
50
|
+
export const RASTER_GLYPH_H: i32 = 7
|
|
51
|
+
|
|
52
|
+
/** The step from one glyph to the next in font pixels: the glyph and a one-pixel gap. */
|
|
53
|
+
export const RASTER_GLYPH_ADVANCE: i32 = 6
|
|
54
|
+
|
|
55
|
+
/** The largest font scale drawn; a larger one draws at this, so pixel arithmetic cannot overflow an `i32`. */
|
|
56
|
+
export const RASTER_MAX_TEXT_SCALE: i32 = 1024
|
|
57
|
+
|
|
58
|
+
/** Pixel coordinates are clamped to this before they become an `i32`. */
|
|
59
|
+
const RASTER_PIXEL_LIMIT: f64 = 1000000.0
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* An `i32[]` frame as the `Int32Array` the drawing functions take: the same
|
|
63
|
+
* array, not a copy, so drawing through the result draws into `pixels`.
|
|
64
|
+
*
|
|
65
|
+
* @param pixels - the frame, such as a `Framebuffer`'s `pixels`
|
|
66
|
+
* @returns `pixels` itself
|
|
67
|
+
*/
|
|
68
|
+
export const rasterFrame = (pixels: i32[]): Int32Array => {
|
|
69
|
+
// @ts-expect-error `Int32Array` is `i32[]` in Nish and a JavaScript view in lib.es5 (runtime/nish.d.ts).
|
|
70
|
+
return pixels
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ---- Colour ----------------------------------------------------------------
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* A pixel coordinate floored to an `i32`, clamped first so neither a huge
|
|
77
|
+
* value nor NaN can overflow the conversion.
|
|
78
|
+
*
|
|
79
|
+
* @param v - the coordinate
|
|
80
|
+
* @returns `floor(v)`, held to ±1000000; NaN is -1000000
|
|
81
|
+
*/
|
|
82
|
+
export const rasterPixel = (v: f64): i32 => {
|
|
83
|
+
if (!(v > -RASTER_PIXEL_LIMIT)) {
|
|
84
|
+
return -1000000
|
|
85
|
+
}
|
|
86
|
+
if (v > RASTER_PIXEL_LIMIT) {
|
|
87
|
+
return 1000000
|
|
88
|
+
}
|
|
89
|
+
return toI32(Math.floor(v))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** `v` clamped to a channel's 0..255. */
|
|
93
|
+
const rasterByte = (v: i32): i32 => Math.min(Math.max(v, 0), 255)
|
|
94
|
+
|
|
95
|
+
/** `x` held to 0..1; NaN becomes 0. */
|
|
96
|
+
const rasterUnit = (x: f64): f64 => {
|
|
97
|
+
if (!(x > 0.0)) {
|
|
98
|
+
return 0.0
|
|
99
|
+
}
|
|
100
|
+
return x > 1.0 ? 1.0 : x
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A colour from three channels.
|
|
105
|
+
*
|
|
106
|
+
* @param r - red, clamped to 0..255
|
|
107
|
+
* @param g - green, clamped to 0..255
|
|
108
|
+
* @param b - blue, clamped to 0..255
|
|
109
|
+
* @returns the colour as `0xRRGGBB`
|
|
110
|
+
*/
|
|
111
|
+
export const rasterRgb = (r: i32, g: i32, b: i32): i32 =>
|
|
112
|
+
(rasterByte(r) << 16) | (rasterByte(g) << 8) | rasterByte(b)
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* A fraction as the 0..256 weight the mixes and `rasterRect` take.
|
|
116
|
+
*
|
|
117
|
+
* @param t - the fraction, clamped to 0..1; NaN is 0
|
|
118
|
+
* @returns the weight, 0 to `RASTER_SOLID`
|
|
119
|
+
*/
|
|
120
|
+
export const rasterWeight = (t: f64): i32 => {
|
|
121
|
+
if (!(t > 0.0)) {
|
|
122
|
+
return 0
|
|
123
|
+
}
|
|
124
|
+
if (t >= 1.0) {
|
|
125
|
+
return RASTER_SOLID
|
|
126
|
+
}
|
|
127
|
+
return toI32(t * 256.0)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Channel `a` moved toward `b` by `w` 256ths. */
|
|
131
|
+
const rasterMixByte = (a: i32, b: i32, w: i32): i32 => a + (((b - a) * w) >> 8)
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* `a` moved toward `b` by a 0..256 weight, which a per-pixel loop computes
|
|
135
|
+
* once with `rasterWeight`.
|
|
136
|
+
*
|
|
137
|
+
* @param a - the colour at weight 0
|
|
138
|
+
* @param b - the colour at weight 256
|
|
139
|
+
* @param w - the weight, 0 to `RASTER_SOLID`
|
|
140
|
+
* @returns the mixed colour
|
|
141
|
+
*/
|
|
142
|
+
export const rasterMixWeight = (a: i32, b: i32, w: i32): i32 =>
|
|
143
|
+
(rasterMixByte((a >> 16) & 255, (b >> 16) & 255, w) << 16) |
|
|
144
|
+
(rasterMixByte((a >> 8) & 255, (b >> 8) & 255, w) << 8) |
|
|
145
|
+
rasterMixByte(a & 255, b & 255, w)
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* `a` moved toward `b` by a fraction.
|
|
149
|
+
*
|
|
150
|
+
* @param a - the colour at 0
|
|
151
|
+
* @param b - the colour at 1
|
|
152
|
+
* @param t - how far, clamped to 0..1
|
|
153
|
+
* @returns the mixed colour
|
|
154
|
+
*/
|
|
155
|
+
export const rasterMix = (a: i32, b: i32, t: f64): i32 => rasterMixWeight(a, b, rasterWeight(t))
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Two colours added channel by channel, each saturating at 255: how light adds.
|
|
159
|
+
*
|
|
160
|
+
* @param a - one colour
|
|
161
|
+
* @param b - the other
|
|
162
|
+
* @returns the sum
|
|
163
|
+
*/
|
|
164
|
+
export const rasterAdd = (a: i32, b: i32): i32 =>
|
|
165
|
+
(Math.min(((a >> 16) & 255) + ((b >> 16) & 255), 255) << 16) |
|
|
166
|
+
(Math.min(((a >> 8) & 255) + ((b >> 8) & 255), 255) << 8) |
|
|
167
|
+
Math.min((a & 255) + (b & 255), 255)
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* A dimmer copy of the same hue.
|
|
171
|
+
*
|
|
172
|
+
* @param c - the colour
|
|
173
|
+
* @param k - the factor every channel is multiplied by, clamped to 0..1
|
|
174
|
+
* @returns the dimmed colour
|
|
175
|
+
*/
|
|
176
|
+
export const rasterDim = (c: i32, k: f64): i32 => rasterMixWeight(0, c, rasterWeight(k))
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The fully saturated colour at a hue, so a rainbow is
|
|
180
|
+
* `rasterHue(phase + i * step)`.
|
|
181
|
+
*
|
|
182
|
+
* @param h - the hue in turns: 0 is red, 1/3 green, 2/3 blue; any value wraps
|
|
183
|
+
* @returns the colour
|
|
184
|
+
*/
|
|
185
|
+
export const rasterHue = (h: f64): i32 => {
|
|
186
|
+
let turn: f64 = h - Math.floor(h)
|
|
187
|
+
if (!(turn >= 0.0 && turn < 1.0)) {
|
|
188
|
+
turn = 0.0
|
|
189
|
+
}
|
|
190
|
+
const h6: f64 = turn * 6.0
|
|
191
|
+
const sector: i32 = toI32(Math.floor(h6))
|
|
192
|
+
const rise: i32 = rasterByte(toI32((h6 - Math.floor(h6)) * 255.0))
|
|
193
|
+
const fall: i32 = 255 - rise
|
|
194
|
+
if (sector === 0) {
|
|
195
|
+
return rasterRgb(255, rise, 0)
|
|
196
|
+
}
|
|
197
|
+
if (sector === 1) {
|
|
198
|
+
return rasterRgb(fall, 255, 0)
|
|
199
|
+
}
|
|
200
|
+
if (sector === 2) {
|
|
201
|
+
return rasterRgb(0, 255, rise)
|
|
202
|
+
}
|
|
203
|
+
if (sector === 3) {
|
|
204
|
+
return rasterRgb(0, fall, 255)
|
|
205
|
+
}
|
|
206
|
+
if (sector === 4) {
|
|
207
|
+
return rasterRgb(rise, 0, 255)
|
|
208
|
+
}
|
|
209
|
+
return rasterRgb(255, 0, fall)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ---- Shapes ----------------------------------------------------------------
|
|
213
|
+
|
|
214
|
+
/** The rows of a `width`-wide frame that `pixels` really holds, at most `height`. */
|
|
215
|
+
const rasterRows = (pixels: Int32Array, width: i32, height: i32): i32 => {
|
|
216
|
+
if (width <= 0 || height <= 0) {
|
|
217
|
+
return 0
|
|
218
|
+
}
|
|
219
|
+
return Math.min(height, toI32(pixels.length) / width)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Pixels `x0..x1` (exclusive) of row `y` set to `colour`, or blended by `w` 256ths below 256, clipped. */
|
|
223
|
+
const rasterSpan = (
|
|
224
|
+
pixels: Int32Array,
|
|
225
|
+
width: i32,
|
|
226
|
+
rows: i32,
|
|
227
|
+
x0: i32,
|
|
228
|
+
x1: i32,
|
|
229
|
+
y: i32,
|
|
230
|
+
colour: i32,
|
|
231
|
+
w: i32
|
|
232
|
+
): void => {
|
|
233
|
+
if (y < 0 || y >= rows) {
|
|
234
|
+
return
|
|
235
|
+
}
|
|
236
|
+
const from: i32 = Math.max(x0, 0)
|
|
237
|
+
const to: i32 = Math.min(x1, width)
|
|
238
|
+
const row: i32 = y * width
|
|
239
|
+
if (w >= RASTER_SOLID) {
|
|
240
|
+
for (let x: i32 = from; x < to; x++) {
|
|
241
|
+
pixels[row + x] = colour
|
|
242
|
+
}
|
|
243
|
+
return
|
|
244
|
+
}
|
|
245
|
+
for (let x: i32 = from; x < to; x++) {
|
|
246
|
+
pixels[row + x] = rasterMixWeight(pixels[row + x], colour, w)
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Every pixel of the frame, as a vertical gradient: a background.
|
|
252
|
+
*
|
|
253
|
+
* @param pixels - the frame, row-major
|
|
254
|
+
* @param width - the frame's width in pixels
|
|
255
|
+
* @param height - the frame's height in pixels
|
|
256
|
+
* @param top - the colour of the first row
|
|
257
|
+
* @param bottom - the colour of the last row
|
|
258
|
+
*/
|
|
259
|
+
export const rasterGradient = (pixels: Int32Array, width: i32, height: i32, top: i32, bottom: i32): void => {
|
|
260
|
+
const rows: i32 = rasterRows(pixels, width, height)
|
|
261
|
+
for (let y: i32 = 0; y < rows; y++) {
|
|
262
|
+
const colour: i32 = rasterMixWeight(top, bottom, height > 1 ? (y * 256) / (height - 1) : 0)
|
|
263
|
+
rasterSpan(pixels, width, rows, 0, width, y, colour, RASTER_SOLID)
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* The pixel rectangle `x0..x1` × `y0..y1` (right and bottom exclusive) set to
|
|
269
|
+
* a colour or blended toward it, clipped to the frame.
|
|
270
|
+
*
|
|
271
|
+
* @param pixels - the frame, row-major
|
|
272
|
+
* @param width - the frame's width in pixels
|
|
273
|
+
* @param height - the frame's height in pixels
|
|
274
|
+
* @param x0 - the first column
|
|
275
|
+
* @param y0 - the first row
|
|
276
|
+
* @param x1 - one past the last column
|
|
277
|
+
* @param y1 - one past the last row
|
|
278
|
+
* @param colour - the colour, `0xRRGGBB`
|
|
279
|
+
* @param weight - how much of `colour`, 0 to `RASTER_SOLID` (see `rasterWeight`); 0 draws nothing
|
|
280
|
+
*/
|
|
281
|
+
export const rasterRect = (
|
|
282
|
+
pixels: Int32Array,
|
|
283
|
+
width: i32,
|
|
284
|
+
height: i32,
|
|
285
|
+
x0: i32,
|
|
286
|
+
y0: i32,
|
|
287
|
+
x1: i32,
|
|
288
|
+
y1: i32,
|
|
289
|
+
colour: i32,
|
|
290
|
+
weight: i32
|
|
291
|
+
): void => {
|
|
292
|
+
if (weight <= 0) {
|
|
293
|
+
return
|
|
294
|
+
}
|
|
295
|
+
const rows: i32 = rasterRows(pixels, width, height)
|
|
296
|
+
const from: i32 = Math.max(y0, 0)
|
|
297
|
+
const to: i32 = Math.min(y1, rows)
|
|
298
|
+
for (let y: i32 = from; y < to; y++) {
|
|
299
|
+
rasterSpan(pixels, width, rows, x0, x1, y, colour, weight)
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* A disc blended over what is there, its edge anti-aliased: each pixel is
|
|
305
|
+
* weighed by how much of it the circle covers, so a disc a few pixels wide
|
|
306
|
+
* still looks round, and one narrower than a pixel fades out rather than stay
|
|
307
|
+
* a grey dot.
|
|
308
|
+
*
|
|
309
|
+
* @param pixels - the frame, row-major
|
|
310
|
+
* @param width - the frame's width in pixels
|
|
311
|
+
* @param height - the frame's height in pixels
|
|
312
|
+
* @param cx - the centre's x in pixels
|
|
313
|
+
* @param cy - the centre's y in pixels
|
|
314
|
+
* @param r - the radius in pixels; 0 or less draws nothing
|
|
315
|
+
* @param colour - the colour, `0xRRGGBB`
|
|
316
|
+
* @param alpha - the opacity, clamped to 0..1
|
|
317
|
+
*/
|
|
318
|
+
export const rasterDisc = (
|
|
319
|
+
pixels: Int32Array,
|
|
320
|
+
width: i32,
|
|
321
|
+
height: i32,
|
|
322
|
+
cx: f64,
|
|
323
|
+
cy: f64,
|
|
324
|
+
r: f64,
|
|
325
|
+
colour: i32,
|
|
326
|
+
alpha: f64
|
|
327
|
+
): void => {
|
|
328
|
+
const a: f64 = rasterUnit(alpha) * rasterUnit(r * 2.0)
|
|
329
|
+
if (!(r > 0.0) || a <= 0.0) {
|
|
330
|
+
return
|
|
331
|
+
}
|
|
332
|
+
const rows: i32 = rasterRows(pixels, width, height)
|
|
333
|
+
const inner: f64 = r > 0.5 ? (r - 0.5) * (r - 0.5) : 0.0
|
|
334
|
+
const outer: f64 = (r + 0.5) * (r + 0.5)
|
|
335
|
+
const fullW: i32 = rasterWeight(a)
|
|
336
|
+
const x0: i32 = Math.max(rasterPixel(cx - r - 1.0), 0)
|
|
337
|
+
const x1: i32 = Math.min(rasterPixel(cx + r + 1.0) + 1, width)
|
|
338
|
+
const y0: i32 = Math.max(rasterPixel(cy - r - 1.0), 0)
|
|
339
|
+
const y1: i32 = Math.min(rasterPixel(cy + r + 1.0) + 1, rows)
|
|
340
|
+
for (let y: i32 = y0; y < y1; y++) {
|
|
341
|
+
const dy: f64 = toF64(y) + 0.5 - cy
|
|
342
|
+
const row: i32 = y * width
|
|
343
|
+
for (let x: i32 = x0; x < x1; x++) {
|
|
344
|
+
const dx: f64 = toF64(x) + 0.5 - cx
|
|
345
|
+
const d2: f64 = dx * dx + dy * dy
|
|
346
|
+
if (d2 >= outer) {
|
|
347
|
+
continue
|
|
348
|
+
}
|
|
349
|
+
const w: i32 = d2 <= inner ? fullW : rasterWeight((r + 0.5 - Math.sqrt(d2)) * a)
|
|
350
|
+
pixels[row + x] = w >= RASTER_SOLID ? colour : rasterMixWeight(pixels[row + x], colour, w)
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Light added around a point, brightest at the centre and falling off with the
|
|
357
|
+
* square of the distance: a halo.
|
|
358
|
+
*
|
|
359
|
+
* @param pixels - the frame, row-major
|
|
360
|
+
* @param width - the frame's width in pixels
|
|
361
|
+
* @param height - the frame's height in pixels
|
|
362
|
+
* @param cx - the centre's x in pixels
|
|
363
|
+
* @param cy - the centre's y in pixels
|
|
364
|
+
* @param r - the radius in pixels the light reaches; 0 or less draws nothing
|
|
365
|
+
* @param colour - the light's colour, `0xRRGGBB`
|
|
366
|
+
* @param strength - how much: 1 adds the full colour at the centre
|
|
367
|
+
*/
|
|
368
|
+
export const rasterGlow = (
|
|
369
|
+
pixels: Int32Array,
|
|
370
|
+
width: i32,
|
|
371
|
+
height: i32,
|
|
372
|
+
cx: f64,
|
|
373
|
+
cy: f64,
|
|
374
|
+
r: f64,
|
|
375
|
+
colour: i32,
|
|
376
|
+
strength: f64
|
|
377
|
+
): void => {
|
|
378
|
+
if (!(r > 0.0) || !(strength > 0.0)) {
|
|
379
|
+
return
|
|
380
|
+
}
|
|
381
|
+
const rows: i32 = rasterRows(pixels, width, height)
|
|
382
|
+
const r2: f64 = r * r
|
|
383
|
+
const x0: i32 = Math.max(rasterPixel(cx - r), 0)
|
|
384
|
+
const x1: i32 = Math.min(rasterPixel(cx + r) + 1, width)
|
|
385
|
+
const y0: i32 = Math.max(rasterPixel(cy - r), 0)
|
|
386
|
+
const y1: i32 = Math.min(rasterPixel(cy + r) + 1, rows)
|
|
387
|
+
for (let y: i32 = y0; y < y1; y++) {
|
|
388
|
+
const dy: f64 = toF64(y) + 0.5 - cy
|
|
389
|
+
const row: i32 = y * width
|
|
390
|
+
for (let x: i32 = x0; x < x1; x++) {
|
|
391
|
+
const dx: f64 = toF64(x) + 0.5 - cx
|
|
392
|
+
const d2: f64 = dx * dx + dy * dy
|
|
393
|
+
if (d2 >= r2) {
|
|
394
|
+
continue
|
|
395
|
+
}
|
|
396
|
+
const near: f64 = d2 / r2
|
|
397
|
+
const fall: f64 = 1.0 - near
|
|
398
|
+
const w: i32 = rasterWeight(fall * fall * strength)
|
|
399
|
+
if (w > 0) {
|
|
400
|
+
pixels[row + x] = rasterAdd(pixels[row + x], rasterMixWeight(0, colour, w))
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// ---- Text ------------------------------------------------------------------
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* The 5×7 font, ASCII 32 to 126: seven characters a glyph, one a row from the
|
|
410
|
+
* top, each the row's five pixels as bits (16 the leftmost) plus 48, so every
|
|
411
|
+
* character is a digit, one of `:;<=>?@` or `A` to `O`. A string is the one
|
|
412
|
+
* table a module constant can be, so the font costs no allocation and no code
|
|
413
|
+
* to build it.
|
|
414
|
+
*/
|
|
415
|
+
const RASTER_FONT: string =
|
|
416
|
+
// space to -
|
|
417
|
+
"00000004444404::00000:O::O:04?D>5N4HI248C3<BD8EB=4480000248884284222480:4O4:0044O44000000<4000O000" +
|
|
418
|
+
// . to ;
|
|
419
|
+
"00000<<01248@0>ACEIA>4<4444>>A168@O>A161A>26:BO22O@N11A>68@NAA>O124888>AA>AA>>AA?12<0<<0<<00<<0<48" +
|
|
420
|
+
// < to I
|
|
421
|
+
"248@84200O0O008421248>A12404>AGEG@>>AAOAAANAANAAN>A@@@A>LBAAABLO@@N@@OO@@N@@@>A@GAA?AAAOAAA>44444>" +
|
|
422
|
+
// J to W
|
|
423
|
+
"72222B<ABDHDBA@@@@@@OAKEEAAAAAIECAA>AAAAA>NAAN@@@>AAAEB=NAANDBA?@@>11NO444444AAAAAA>AAAAA:4AAAEEE:" +
|
|
424
|
+
// X to e
|
|
425
|
+
"AA:4:AAAA:4444O1248@O>88888>0@84210>22222>4:A0000000000O840000000>1?A?@@NAAAN00>@@A>11?AAA?00>AO@>" +
|
|
426
|
+
// f to s
|
|
427
|
+
"698L8880?AA?1>@@FIAAA40<444>20622B<@@BDHDB<44444>00JEEAA00FIAAA00>AAA>00NAN@@00?A?1100FI@@@00?@>1N" +
|
|
428
|
+
// t to ~
|
|
429
|
+
"88L889600AAAC=00AAA:400AAEE:00A:4:A00AA?1>00O248O244844244444448442448008E200"
|
|
430
|
+
|
|
431
|
+
/** The bits of row `row` of the glyph for byte `c`; a byte outside 32..126 draws as `?`. */
|
|
432
|
+
const rasterGlyphRow = (c: i32, row: i32): i32 => {
|
|
433
|
+
const g: i32 = c >= 32 && c <= 126 ? c - 32 : 31
|
|
434
|
+
const at: i32 = g * RASTER_GLYPH_H + row
|
|
435
|
+
if (row < 0 || row >= RASTER_GLYPH_H || at < 0 || at >= toI32(RASTER_FONT.length)) {
|
|
436
|
+
return 0
|
|
437
|
+
}
|
|
438
|
+
return toI32(RASTER_FONT.charCodeAt(at)) - 48
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* The width text takes in the 5×7 font: six font pixels a glyph, less the
|
|
443
|
+
* last gap.
|
|
444
|
+
*
|
|
445
|
+
* @param text - the text, one glyph a byte, so a character outside ASCII takes one per UTF-8 byte
|
|
446
|
+
* @param scale - the font scale, frame pixels per font pixel
|
|
447
|
+
* @returns the width in frame pixels, 0 for empty text or a scale under 1
|
|
448
|
+
*/
|
|
449
|
+
export const rasterTextWidth = (text: string, scale: i32): i32 => {
|
|
450
|
+
const n: i32 = toI32(text.length)
|
|
451
|
+
if (n === 0 || scale < 1) {
|
|
452
|
+
return 0
|
|
453
|
+
}
|
|
454
|
+
return (n * RASTER_GLYPH_ADVANCE - 1) * Math.min(scale, RASTER_MAX_TEXT_SCALE)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Text in the 5×7 font, each font pixel a `scale` × `scale` block of frame
|
|
459
|
+
* pixels. Text is pixel art, so it sits on whole pixels and is never blended.
|
|
460
|
+
* A byte outside ASCII 32..126 draws as `?`, so a character outside ASCII
|
|
461
|
+
* draws as one `?` for each of its UTF-8 bytes.
|
|
462
|
+
*
|
|
463
|
+
* @param pixels - the frame, row-major
|
|
464
|
+
* @param width - the frame's width in pixels
|
|
465
|
+
* @param height - the frame's height in pixels
|
|
466
|
+
* @param x - the left edge in pixels
|
|
467
|
+
* @param y - the top edge in pixels
|
|
468
|
+
* @param scale - the font scale; under 1 draws nothing, over `RASTER_MAX_TEXT_SCALE` draws at that
|
|
469
|
+
* @param text - the text
|
|
470
|
+
* @param colour - the colour, `0xRRGGBB`
|
|
471
|
+
*/
|
|
472
|
+
export const rasterText = (
|
|
473
|
+
pixels: Int32Array,
|
|
474
|
+
width: i32,
|
|
475
|
+
height: i32,
|
|
476
|
+
x: i32,
|
|
477
|
+
y: i32,
|
|
478
|
+
scale: i32,
|
|
479
|
+
text: string,
|
|
480
|
+
colour: i32
|
|
481
|
+
): void => {
|
|
482
|
+
if (scale < 1) {
|
|
483
|
+
return
|
|
484
|
+
}
|
|
485
|
+
const size: i32 = Math.min(scale, RASTER_MAX_TEXT_SCALE)
|
|
486
|
+
const n: i32 = toI32(text.length)
|
|
487
|
+
let gx: i32 = x
|
|
488
|
+
for (let i: i32 = 0; i < n && gx < width; i++) {
|
|
489
|
+
const c: i32 = toI32(text.charCodeAt(i))
|
|
490
|
+
if (gx + RASTER_GLYPH_W * size > 0) {
|
|
491
|
+
for (let row: i32 = 0; row < RASTER_GLYPH_H; row++) {
|
|
492
|
+
const bits: i32 = rasterGlyphRow(c, row)
|
|
493
|
+
const top: i32 = y + row * size
|
|
494
|
+
for (let col: i32 = 0; col < RASTER_GLYPH_W; col++) {
|
|
495
|
+
if (((bits >> (RASTER_GLYPH_W - 1 - col)) & 1) !== 0) {
|
|
496
|
+
const left: i32 = gx + col * size
|
|
497
|
+
rasterRect(pixels, width, height, left, top, left + size, top + size, colour, RASTER_SOLID)
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
gx = gx + RASTER_GLYPH_ADVANCE * size
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// ---- Trail -----------------------------------------------------------------
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* The last positions of something moving, newest last, in a ring made once:
|
|
510
|
+
* what `rasterTrail` draws behind it. `rasterPushTrail` adds a point; a full
|
|
511
|
+
* ring forgets its oldest. Positions are in the caller's units.
|
|
512
|
+
*/
|
|
513
|
+
export class RasterTrail {
|
|
514
|
+
/** The x of each point, in ring order. */
|
|
515
|
+
xs: f64[]
|
|
516
|
+
/** The y of each point, in ring order. */
|
|
517
|
+
ys: f64[]
|
|
518
|
+
/** The slot the next point goes in. */
|
|
519
|
+
head: i32
|
|
520
|
+
/** The points held, up to the capacity. */
|
|
521
|
+
count: i32
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* An empty trail.
|
|
525
|
+
*
|
|
526
|
+
* @param capacity - the points it remembers, at least one
|
|
527
|
+
*/
|
|
528
|
+
constructor(capacity: i32) {
|
|
529
|
+
const n: i32 = Math.max(capacity, 1)
|
|
530
|
+
this.xs = new Array<f64>(n)
|
|
531
|
+
this.xs.fill(0.0)
|
|
532
|
+
this.ys = new Array<f64>(n)
|
|
533
|
+
this.ys.fill(0.0)
|
|
534
|
+
this.head = 0
|
|
535
|
+
this.count = 0
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/** Forget every point. */
|
|
539
|
+
clear(): void {
|
|
540
|
+
this.head = 0
|
|
541
|
+
this.count = 0
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Add the newest point to a trail, forgetting the oldest when it is full.
|
|
547
|
+
*
|
|
548
|
+
* @param trail - the trail
|
|
549
|
+
* @param x - the point's x
|
|
550
|
+
* @param y - the point's y
|
|
551
|
+
*/
|
|
552
|
+
export const rasterPushTrail = (trail: RasterTrail, x: f64, y: f64): void => {
|
|
553
|
+
const n: i32 = toI32(trail.xs.length)
|
|
554
|
+
if (trail.head < 0 || trail.head >= n || n !== toI32(trail.ys.length)) {
|
|
555
|
+
trail.head = 0
|
|
556
|
+
return
|
|
557
|
+
}
|
|
558
|
+
trail.xs[trail.head] = x
|
|
559
|
+
trail.ys[trail.head] = y
|
|
560
|
+
trail.head = trail.head + 1 >= n ? 0 : trail.head + 1
|
|
561
|
+
if (trail.count < n) {
|
|
562
|
+
trail.count = trail.count + 1
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* A trail as a line of discs from oldest to newest, each larger and more
|
|
568
|
+
* opaque than the one before: a streak that fades out behind what made it.
|
|
569
|
+
* A point at `v` is drawn at pixel `offset + v * scale`.
|
|
570
|
+
*
|
|
571
|
+
* @param pixels - the frame, row-major
|
|
572
|
+
* @param width - the frame's width in pixels
|
|
573
|
+
* @param height - the frame's height in pixels
|
|
574
|
+
* @param trail - the trail
|
|
575
|
+
* @param r - the newest disc's radius, in the trail's units
|
|
576
|
+
* @param colour - the colour, `0xRRGGBB`
|
|
577
|
+
* @param alpha - the newest disc's opacity
|
|
578
|
+
* @param scale - frame pixels per trail unit
|
|
579
|
+
* @param offsetX - the pixel x of trail x 0
|
|
580
|
+
* @param offsetY - the pixel y of trail y 0
|
|
581
|
+
*/
|
|
582
|
+
export const rasterTrail = (
|
|
583
|
+
pixels: Int32Array,
|
|
584
|
+
width: i32,
|
|
585
|
+
height: i32,
|
|
586
|
+
trail: RasterTrail,
|
|
587
|
+
r: f64,
|
|
588
|
+
colour: i32,
|
|
589
|
+
alpha: f64,
|
|
590
|
+
scale: f64,
|
|
591
|
+
offsetX: f64,
|
|
592
|
+
offsetY: f64
|
|
593
|
+
): void => {
|
|
594
|
+
const n: i32 = toI32(trail.xs.length)
|
|
595
|
+
const count: i32 = Math.min(trail.count, n)
|
|
596
|
+
for (let i: i32 = 0; i < count; i++) {
|
|
597
|
+
// The oldest point is `count` slots behind the head, wrapping.
|
|
598
|
+
let slot: i32 = trail.head - count + i
|
|
599
|
+
if (slot < 0) {
|
|
600
|
+
slot = slot + n
|
|
601
|
+
}
|
|
602
|
+
if (slot < 0 || slot >= n || slot >= toI32(trail.ys.length)) {
|
|
603
|
+
continue
|
|
604
|
+
}
|
|
605
|
+
const f: f64 = toF64(i + 1) / toF64(count + 1)
|
|
606
|
+
const radius: f64 = r * (f * 0.6 + 0.4)
|
|
607
|
+
const cx: f64 = offsetX + trail.xs[slot] * scale
|
|
608
|
+
const cy: f64 = offsetY + trail.ys[slot] * scale
|
|
609
|
+
rasterDisc(pixels, width, height, cx, cy, radius * scale, colour, alpha * f)
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// ---- Particles -------------------------------------------------------------
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* A fixed pool of particles: sparks, debris, fireworks. Each particle's
|
|
617
|
+
* position, previous position, velocity, colour, size and life sit in parallel
|
|
618
|
+
* arrays made once, so spawning, stepping and drawing allocate nothing. Slots
|
|
619
|
+
* are reused in spawn order, so when every slot is live a new particle
|
|
620
|
+
* replaces the oldest. Positions and sizes are in the caller's units, speeds
|
|
621
|
+
* in those units a second.
|
|
622
|
+
*/
|
|
623
|
+
export class RasterParticles {
|
|
624
|
+
/** x now. */
|
|
625
|
+
xs: f64[]
|
|
626
|
+
/** y now. */
|
|
627
|
+
ys: f64[]
|
|
628
|
+
/** x a step ago, which a draw interpolates from. */
|
|
629
|
+
prevXs: f64[]
|
|
630
|
+
/** y a step ago. */
|
|
631
|
+
prevYs: f64[]
|
|
632
|
+
/** Units a second across. */
|
|
633
|
+
vxs: f64[]
|
|
634
|
+
/** Units a second down. */
|
|
635
|
+
vys: f64[]
|
|
636
|
+
/** Seconds of life left; a particle at or under 0 is dead. */
|
|
637
|
+
lives: f64[]
|
|
638
|
+
/** The seconds of life each particle was spawned with, which its fade is measured against. */
|
|
639
|
+
spans: f64[]
|
|
640
|
+
/** The side of each particle's square. */
|
|
641
|
+
sizes: f64[]
|
|
642
|
+
/** Each particle's colour. */
|
|
643
|
+
colours: i32[]
|
|
644
|
+
/** The slot the next spawn takes: the oldest. */
|
|
645
|
+
next: i32
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* A pool of dead particles.
|
|
649
|
+
*
|
|
650
|
+
* @param capacity - the particles it holds at once, at least one
|
|
651
|
+
*/
|
|
652
|
+
constructor(capacity: i32) {
|
|
653
|
+
const n: i32 = Math.max(capacity, 1)
|
|
654
|
+
this.xs = new Array<f64>(n)
|
|
655
|
+
this.xs.fill(0.0)
|
|
656
|
+
this.ys = new Array<f64>(n)
|
|
657
|
+
this.ys.fill(0.0)
|
|
658
|
+
this.prevXs = new Array<f64>(n)
|
|
659
|
+
this.prevXs.fill(0.0)
|
|
660
|
+
this.prevYs = new Array<f64>(n)
|
|
661
|
+
this.prevYs.fill(0.0)
|
|
662
|
+
this.vxs = new Array<f64>(n)
|
|
663
|
+
this.vxs.fill(0.0)
|
|
664
|
+
this.vys = new Array<f64>(n)
|
|
665
|
+
this.vys.fill(0.0)
|
|
666
|
+
this.lives = new Array<f64>(n)
|
|
667
|
+
this.lives.fill(0.0)
|
|
668
|
+
this.spans = new Array<f64>(n)
|
|
669
|
+
this.spans.fill(1.0)
|
|
670
|
+
this.sizes = new Array<f64>(n)
|
|
671
|
+
this.sizes.fill(1.0)
|
|
672
|
+
this.colours = new Array<i32>(n)
|
|
673
|
+
this.colours.fill(0)
|
|
674
|
+
this.next = 0
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* How many particles the pool holds at once.
|
|
679
|
+
*
|
|
680
|
+
* @returns the capacity
|
|
681
|
+
*/
|
|
682
|
+
capacity(): i32 {
|
|
683
|
+
return toI32(this.lives.length)
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* How many particles are alive.
|
|
688
|
+
*
|
|
689
|
+
* @returns the live count
|
|
690
|
+
*/
|
|
691
|
+
live(): i32 {
|
|
692
|
+
let n: i32 = 0
|
|
693
|
+
for (const life of this.lives) {
|
|
694
|
+
if (life > 0.0) {
|
|
695
|
+
n++
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
return n
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
/** Kill every particle. */
|
|
702
|
+
clear(): void {
|
|
703
|
+
this.lives.fill(0.0)
|
|
704
|
+
this.next = 0
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Whether a slot is in every one of the parallel arrays, which the pool
|
|
709
|
+
* keeps the same length.
|
|
710
|
+
*
|
|
711
|
+
* @param i - the slot
|
|
712
|
+
* @returns true when every array has slot `i`
|
|
713
|
+
*/
|
|
714
|
+
has(i: i32): boolean {
|
|
715
|
+
const n: i32 = this.capacity()
|
|
716
|
+
return (
|
|
717
|
+
i >= 0 &&
|
|
718
|
+
i < n &&
|
|
719
|
+
n === toI32(this.xs.length) &&
|
|
720
|
+
n === toI32(this.ys.length) &&
|
|
721
|
+
n === toI32(this.prevXs.length) &&
|
|
722
|
+
n === toI32(this.prevYs.length) &&
|
|
723
|
+
n === toI32(this.vxs.length) &&
|
|
724
|
+
n === toI32(this.vys.length) &&
|
|
725
|
+
n === toI32(this.spans.length) &&
|
|
726
|
+
n === toI32(this.sizes.length) &&
|
|
727
|
+
n === toI32(this.colours.length)
|
|
728
|
+
)
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* A new particle in the pool's oldest slot, live or not, so a full pool
|
|
734
|
+
* replaces its oldest particle.
|
|
735
|
+
*
|
|
736
|
+
* @param pool - the pool
|
|
737
|
+
* @param x - the centre's x
|
|
738
|
+
* @param y - the centre's y
|
|
739
|
+
* @param vx - units a second across
|
|
740
|
+
* @param vy - units a second down
|
|
741
|
+
* @param life - seconds it lives; 0 or less spawns it dead
|
|
742
|
+
* @param size - the side of its square
|
|
743
|
+
* @param colour - its colour, `0xRRGGBB`
|
|
744
|
+
*/
|
|
745
|
+
export const rasterSpawnParticle = (
|
|
746
|
+
pool: RasterParticles,
|
|
747
|
+
x: f64,
|
|
748
|
+
y: f64,
|
|
749
|
+
vx: f64,
|
|
750
|
+
vy: f64,
|
|
751
|
+
life: f64,
|
|
752
|
+
size: f64,
|
|
753
|
+
colour: i32
|
|
754
|
+
): void => {
|
|
755
|
+
const i: i32 = pool.next
|
|
756
|
+
if (!pool.has(i)) {
|
|
757
|
+
pool.next = 0
|
|
758
|
+
return
|
|
759
|
+
}
|
|
760
|
+
pool.xs[i] = x
|
|
761
|
+
pool.ys[i] = y
|
|
762
|
+
pool.prevXs[i] = x
|
|
763
|
+
pool.prevYs[i] = y
|
|
764
|
+
pool.vxs[i] = vx
|
|
765
|
+
pool.vys[i] = vy
|
|
766
|
+
pool.lives[i] = life > 0.0 ? life : 0.0
|
|
767
|
+
pool.spans[i] = life > 0.0 ? life : 1.0
|
|
768
|
+
pool.sizes[i] = size
|
|
769
|
+
pool.colours[i] = colour
|
|
770
|
+
pool.next = i + 1 >= pool.capacity() ? 0 : i + 1
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/**
|
|
774
|
+
* One step of a pool: every live particle falls, slows, moves and ages.
|
|
775
|
+
*
|
|
776
|
+
* @param pool - the pool
|
|
777
|
+
* @param dt - the step in seconds
|
|
778
|
+
* @param gravity - units a second squared, down
|
|
779
|
+
* @param drag - the fraction of its speed a particle loses a second
|
|
780
|
+
*/
|
|
781
|
+
export const rasterStepParticles = (pool: RasterParticles, dt: f64, gravity: f64, drag: f64): void => {
|
|
782
|
+
const lost: f64 = drag * dt
|
|
783
|
+
const keep: f64 = rasterUnit(1.0 - lost)
|
|
784
|
+
for (let i: i32 = 0; i < pool.capacity(); i++) {
|
|
785
|
+
if (!pool.has(i) || pool.lives[i] <= 0.0) {
|
|
786
|
+
continue
|
|
787
|
+
}
|
|
788
|
+
pool.prevXs[i] = pool.xs[i]
|
|
789
|
+
pool.prevYs[i] = pool.ys[i]
|
|
790
|
+
pool.vxs[i] = pool.vxs[i] * keep
|
|
791
|
+
pool.vys[i] = (pool.vys[i] + gravity * dt) * keep
|
|
792
|
+
pool.xs[i] = pool.xs[i] + pool.vxs[i] * dt
|
|
793
|
+
pool.ys[i] = pool.ys[i] + pool.vys[i] * dt
|
|
794
|
+
pool.lives[i] = pool.lives[i] - dt
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* Every live particle of a pool as a square blended over the frame, at its
|
|
800
|
+
* position `alpha` of the way from the last step to this one and faded by the
|
|
801
|
+
* life it has left. A position `v` is drawn at pixel `offset + v * scale`.
|
|
802
|
+
*
|
|
803
|
+
* @param pixels - the frame, row-major
|
|
804
|
+
* @param width - the frame's width in pixels
|
|
805
|
+
* @param height - the frame's height in pixels
|
|
806
|
+
* @param pool - the pool
|
|
807
|
+
* @param alpha - how far between the last step and this one, 0..1
|
|
808
|
+
* @param scale - frame pixels per pool unit
|
|
809
|
+
* @param offsetX - the pixel x of pool x 0
|
|
810
|
+
* @param offsetY - the pixel y of pool y 0
|
|
811
|
+
*/
|
|
812
|
+
export const rasterParticles = (
|
|
813
|
+
pixels: Int32Array,
|
|
814
|
+
width: i32,
|
|
815
|
+
height: i32,
|
|
816
|
+
pool: RasterParticles,
|
|
817
|
+
alpha: f64,
|
|
818
|
+
scale: f64,
|
|
819
|
+
offsetX: f64,
|
|
820
|
+
offsetY: f64
|
|
821
|
+
): void => {
|
|
822
|
+
for (let i: i32 = 0; i < pool.capacity(); i++) {
|
|
823
|
+
if (!pool.has(i) || pool.lives[i] <= 0.0) {
|
|
824
|
+
continue
|
|
825
|
+
}
|
|
826
|
+
const size: f64 = pool.sizes[i]
|
|
827
|
+
const half: f64 = size / 2.0
|
|
828
|
+
const x: f64 = pool.prevXs[i] + (pool.xs[i] - pool.prevXs[i]) * alpha - half
|
|
829
|
+
const y: f64 = pool.prevYs[i] + (pool.ys[i] - pool.prevYs[i]) * alpha - half
|
|
830
|
+
const x0: i32 = rasterPixel(offsetX + x * scale + 0.5)
|
|
831
|
+
const y0: i32 = rasterPixel(offsetY + y * scale + 0.5)
|
|
832
|
+
const x1: i32 = rasterPixel(offsetX + (x + size) * scale + 0.5)
|
|
833
|
+
const y1: i32 = rasterPixel(offsetY + (y + size) * scale + 0.5)
|
|
834
|
+
const weight: i32 = rasterWeight(pool.lives[i] / pool.spans[i])
|
|
835
|
+
rasterRect(pixels, width, height, x0, y0, x1, y1, pool.colours[i], weight)
|
|
836
|
+
}
|
|
837
|
+
}
|