Skip to content

Commit 1ee07ab

Browse files
committed
Update fximg.base.ts, fximg.utils.ts, test.ts
1 parent 05c86c5 commit 1ee07ab

3 files changed

Lines changed: 87 additions & 81 deletions

File tree

fximg.base.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,12 @@ namespace helpers {
154154
x |= 0; h |= 0;
155155
const len = Math.min(src.length, h);
156156
if (len <= 0 || fximgIsOutOfRange(x, fximgWidthOf(fxpic) * fximgLengthOf(fxpic))) return;
157-
157+
158158
const start = fximgStartIndex(fxpic);
159159
const colStartBit = (x * h) & 1; // 0 = even (aligned), 1 = odd (misaligned)
160-
160+
161161
let srcIdx = 0;
162162
let dstByteIdx = start + ((x * h) >>> 1);
163-
164163
if (colStartBit === 0) {
165164
// Fast path: aligned → copy byte-wise ได้เลย
166165
// src[0] ไป nybble สูงของ byte แรก, src[1] ไป nybble ต่ำ, ฯลฯ
@@ -226,13 +225,15 @@ namespace helpers {
226225
if (fximgRoCheck(fxpic)) return;
227226
idx = idx || 0;
228227
if (fximgIsOutOfRange(idx, fximgLengthOf(fxpic))) return;
229-
color &= 0xF;
230-
const h = fximgHeightOf(fxpic);
231-
const rowBuf = pins.createBuffer(h);
232-
rowBuf.fill(color);
233-
const w = fximgWidthOf(fxpic);
234-
idx *= w;
235-
for (let x = 0; x < w; x++) fximgSetRows(fxpic, idx + x, rowBuf, h);
228+
control.runInParallel(() => {
229+
color &= 0xF;
230+
const h = fximgHeightOf(fxpic);
231+
const rowBuf = pins.createBuffer(h);
232+
rowBuf.fill(color);
233+
const w = fximgWidthOf(fxpic);
234+
idx *= w;
235+
for (let x = 0; x < w; x++) fximgSetRows(fxpic, idx + x, rowBuf, h);
236+
})
236237
}
237238

238239
// 5. replace (แทนที่สี)
@@ -241,15 +242,17 @@ namespace helpers {
241242
from &= 0xF; to &= 0xF;
242243
idx = idx || 0;
243244
if (fximgIsOutOfRange(idx, fximgLengthOf(fxpic))) return;
244-
const w = fximgWidthOf(fxpic);
245-
idx *= w;
246-
const h = fximgHeightOf(fxpic);
247-
const rowBuf = pins.createBuffer(h);
248-
for (let x = 0, rowChange = false; x < w; x++, rowChange = false) {
249-
fximgGetRows(fxpic, idx + x, rowBuf, h);
250-
for (let y = 0; y < h; y++) if (rowBuf[y] === from) rowBuf[y] = to, rowChange = true;
251-
if (rowChange) fximgSetRows(fxpic, idx + x, rowBuf, h);
252-
}
245+
control.runInParallel(() => {
246+
const w = fximgWidthOf(fxpic);
247+
idx *= w;
248+
const h = fximgHeightOf(fxpic);
249+
const rowBuf = pins.createBuffer(h);
250+
for (let x = 0, rowChange = false; x < w; x++, rowChange = false) {
251+
fximgGetRows(fxpic, idx + x, rowBuf, h);
252+
for (let y = 0; y < h; y++) if (rowBuf[y] === from) rowBuf[y] = to, rowChange = true;
253+
if (rowChange) fximgSetRows(fxpic, idx + x, rowBuf, h);
254+
}
255+
})
253256
}
254257

255258
export function fximgEqualTo(fxpic: Fximg, otherfxpic: Fximg) {

fximg.utils.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -136,26 +136,26 @@ namespace helpers {
136136
if (sx < 0) continue;
137137
if (sx >= clipWSrc) break;
138138
const dx = x + xDst;
139-
139+
140140
// ดึง column ส่วนที่ต้องการจาก src (offset ySrc)
141141
if (sx !== curSx) fximgGetRows(src, sx, srcRow, safeSrcH), curSx = sx;
142142
fximgGetRows(dst, dx, dstRow, safeDstH);
143-
143+
144144
for (let y = 0; y < clipHDst; y++) {
145145
const sy = ((y * scaleY) | 0) + ySrc;
146146
if (sy < 0) continue;
147147
if (sy >= clipHSrc) break;
148148
const dy = y + yDst;
149149
const newPixel = srcRow[sy]; // pixel จาก src (หลัง shift ySrc แล้ว)
150-
150+
151151
if (transparent && newPixel < 1) continue;
152-
152+
153153
const oldPixel = dstRow[dy];
154154
if (oldPixel === newPixel) continue;
155155
dstRow[dy] = newPixel;
156156
rowChange = true, anyChange = true;
157157
}
158-
158+
159159
if (!rowChange) continue;
160160
fximgSetRows(dst, dx, dstRow, safeDstH);
161161

@@ -269,13 +269,13 @@ namespace helpers {
269269
if (width < 1 || height < 1) return;
270270
color &= 0xF;
271271
idx *= w;
272-
272+
273273
const sx = Math.clamp(0, w - 1, x);
274274
const ex = Math.clamp(0, w - 1, x + width - 1);
275275
const sy = Math.clamp(0, h - 1, y);
276276
const ey = Math.clamp(0, h - 1, y + height - 1);
277277
if (sx > ex || sy > ey) return;
278-
278+
279279
const buf = pins.createBuffer(h);
280280
for (let cx = sx; cx <= ex; cx++) {
281281
fximgGetRows(fxpic, cx + idx, buf, h);
@@ -412,6 +412,7 @@ namespace helpers {
412412
buf.fill(color, Math.max(offset, 0), (dy << 1) + Math.min(offset, 0));
413413
fximg.setRows(fxpic, x, buf, h);
414414
}
415+
415416
}
416417

417418
// 12. drawImage (ไม่ transparent)
@@ -430,26 +431,26 @@ namespace helpers {
430431
const sh = fximgHeightOf(from);
431432
const tw = fximgWidthOf(to)
432433
const th = fximgHeightOf(to);
433-
434+
434435
const rowFrom = pins.createBuffer(sh);
435436
const rowTo = pins.createBuffer(th);
436437
for (let sx = 0; sx < sw; sx++) {
437438
let tx = dx + sx;
438439
if (tx < 0) continue;
439440
if (tx >= tw) break;
440-
441+
441442
fximgGetRows(from, sx, rowFrom, sh);
442443
fximgGetRows(to, tx, rowTo, th);
443-
444+
444445
const dyPos = Math.max(dy, 0);
445446
const dyNeg = Math.min(dy, 0);
446-
447+
447448
const tmpRow = pins.createBuffer(sh + dyNeg);
448449
if (dyNeg < 0) rowFrom.shift(Math.abs(dyNeg))
449450
tmpRow.write(dyPos, rowFrom);
450-
451+
451452
if (transparent) for (let i = 0; i < (sh + dyNeg); i++) if (tmpRow[i] < 1) tmpRow[i] = rowTo[dyPos + i];
452-
453+
453454
rowTo.write(dyPos, tmpRow);
454455
fximgSetRows(to, tx, rowTo, th);
455456
}
@@ -703,27 +704,27 @@ namespace helpers {
703704
let v = [new pt2(x0, y0), new pt2(x1, y1), new pt2(x2, y2)];
704705
if (fximgIsOutOfAreas(v, w, h)) return;
705706
idx *= w;
706-
707+
707708
// ------------------- Sort vertices by y (top -> bottom) -------------------
708-
709+
709710
let tmp = -1;
710711
//Sort points so that x0 <= x1 <= x2
711712
if (x1 < x0) tmp = x0, x0 = x1, x1 = tmp, tmp = y0, y0 = y1, y1 = tmp;//[x0, y0, x1, y1] = [x1, y1, x0, y0];
712713
if (x2 < x0) tmp = x2, x2 = x0, x0 = tmp, tmp = y2, y2 = y0, y0 = tmp;//[x2, y2, x0, y0] = [x0, y0, x2, y2];
713714
if (x2 < x1) tmp = x2, x2 = x1, x1 = tmp, tmp = y2, y2 = y1, y1 = tmp;//[x2, y2, x1, y1] = [x1, y1, x2, y2];
714-
715+
715716
//Compute the x coordinates of the triangles edges
716717
let y012 = interpolate(x0, y0, x1, y1, w, h).concat(interpolate(x1, y1, x2, y2, w, h));
717718
let y02 = interpolate(x0, y0, x2, y2, w, h);
718-
719+
719720
if (y012.length < 1 || y02.length < 1) return;
720-
721+
721722
//Ensure the arrays are the correct length
722723
if (y012.length > y02.length)
723724
y012.pop();
724725
else if (y012.length < y02.length)
725726
y02.pop();
726-
727+
727728
//Determine which is left and which is right
728729
let yts: number[] = [];
729730
let ybs: number[] = [];
@@ -736,9 +737,9 @@ namespace helpers {
736737
else
737738
yts = y012,
738739
ybs = y02;
739-
740+
740741
const buf = pins.createBuffer(h);
741-
742+
742743
//Draw em
743744
//TODO: just write to the buffer with fill; place any bitwise operations for dithering patterns thusly
744745
const drawX = (x: number, xi: number) => {
@@ -750,7 +751,7 @@ namespace helpers {
750751
buf.fill(color, Math.max(yt, 0), fillSize);
751752
fximgSetRows(fxpic, x, buf, h);
752753
}
753-
754+
754755
for (let x = x0, xi = 0; x <= x2; x++, xi++) {
755756
drawX(x, xi);
756757
drawX(++x, ++xi);
@@ -857,29 +858,29 @@ namespace helpers {
857858
const toW = fximgWidthOf(to);
858859
const toH = fximgHeightOf(to);
859860
// ถ้าใช้ dstTotalW แบบเดิม → const toTotalW = toW * fximgLengthOf(to); แต่ polymesh ไม่ใช้ เลยข้าม
860-
861+
861862
const fromRowBuf = pins.createBuffer(h);
862863
//const emptyHash = fromRowBuf.hash(0xffff) & 0xffff;
863-
864+
864865
const wInv = 1 / w;
865866
const hInv = 1 / h;
866-
867+
867868
const pqu = new pt2_2(
868869
(x1 - x0),
869870
(y1 - y0),
870871
(x2 - x3),
871872
(y2 - y3)
872873
);
873-
874+
874875
for (let sx = 0; sx < w; sx++) {
875876
const ix = center ? fximgZigzet(0, w - 1, sx) : sx;
876877
fximgGetRows(from, w - ix - 1, fromRowBuf, h); // ใช้ w - ix -1 เหมือน polymesh (reverse ถ้า center)
877-
878+
878879
//if (fromRowBuf.hash(0xffff) === emptyHash) continue;
879-
880+
880881
const u0 = (ix * wInv);
881882
const u1 = ((ix + 1) * wInv);
882-
883+
883884
const qu = new pt2_4(
884885
x0 + pqu.x0 * u0,
885886
y0 + pqu.y0 * u0,
@@ -890,23 +891,23 @@ namespace helpers {
890891
x3 + pqu.x1 * u1,
891892
y3 + pqu.y1 * u1
892893
);
893-
894+
894895
const pqv = new pt2_2(
895896
(qu.x1 - qu.x0),
896897
(qu.y1 - qu.y0),
897898
(qu.x3 - qu.x2),
898899
(qu.y3 - qu.y2)
899900
);
900-
901+
901902
for (let sy = 0; sy < h; sy++) {
902903
const iy = center ? fximgZigzet(0, h - 1, sy) : sy;
903-
904+
904905
const color = fromRowBuf[iy]; // pixel แถวบนสุดหลัง shift
905906
if (transparent && color < 1) continue; // transparent
906-
907+
907908
const v0 = (iy * hInv);
908909
const v1 = ((iy + 1) * hInv);
909-
910+
910911
const qv = new pt2_4(
911912
(qu.x0 + pqv.x0 * v0) | 0,
912913
(qu.y0 + pqv.y0 * v0) | 0,
@@ -917,16 +918,16 @@ namespace helpers {
917918
(qu.x2 + pqv.x1 * v1) | 0,
918919
(qu.y2 + pqv.y1 * v1) | 0
919920
);
920-
921+
921922
// Skip ถ้าทั้ง quad ออกนอกจอ (เหมือน polymesh)
922923
if (qv.toArr.every(pt =>
923924
pt.x < 0 || pt.x >= toW || pt.y < 0 || pt.y >= toH
924925
)) continue;
925-
926+
926927
// Stamp ด้วย 2 triangle (สไตล์ polymesh)
927928
fximgFillTriangle(to, qv.x1, qv.y1, qv.x0, qv.y0, qv.x3, qv.y3, color);
928929
fximgFillTriangle(to, qv.x2, qv.y2, qv.x0, qv.y0, qv.x3, qv.y3, color);
929-
930+
930931
// หรือถ้าอยากใช้ polygon4 เดิม (อาจแก้ gap ได้ดีกว่าในบางเคส)
931932
// fximgFillPolygon4(to, qv.x1, qv.y1, qv.x0, qv.y0, qv.x2, qv.y2, qv.x3, qv.y3, color);
932933
}

test.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,32 @@ if (1) {
3535

3636

3737
game.onUpdate(() => {
38-
let j = randint(1, 4);
39-
for (let i = 0; i < j; i++) {
40-
if (0) fximg.fillPolygon4(imgfxb,
41-
randint(0, w - 1), randint(10, h - 1),
42-
randint(0, w - 1), randint(10, h - 1),
43-
randint(0, w - 1), randint(10, h - 1),
44-
randint(0, w - 1), randint(10, h - 1),
45-
randint(0x0, 0xf)
46-
);
47-
if (1) fximg.drawTransDistortedImage(imgfxb, imgfxa,
48-
randint(0, w - 1), randint(10, h - 1),
49-
randint(0, w - 1), randint(10, h - 1),
50-
randint(0, w - 1), randint(10, h - 1),
51-
randint(0, w - 1), randint(10, h - 1),
52-
);
53-
if (0) fximg.fillTriangle(imgfxb,
54-
randint(0, w - 1), randint(10, h - 1),
55-
randint(0, w - 1), randint(10, h - 1),
56-
randint(0, w - 1), randint(10, h - 1),
57-
randint(0x0, 0xf),
58-
);
59-
//scene.setBackgroundImage(fximg.toImage(imgfxb));
60-
}
61-
scene.setBackgroundImage(fximg.toImage(imgfxb));
62-
scene.backgroundImage().print(`draw count: ${j}`, 1, 1, 0x1)
38+
control.runInParallel(() => {
39+
let j = 1//randint(1, 4);
40+
for (let i = 0; i < j; i++) {
41+
if (0) fximg.fillPolygon4(imgfxb,
42+
randint(0, w - 1), randint(10, h - 1),
43+
randint(0, w - 1), randint(10, h - 1),
44+
randint(0, w - 1), randint(10, h - 1),
45+
randint(0, w - 1), randint(10, h - 1),
46+
randint(0x0, 0xf)
47+
);
48+
if (1) fximg.drawTransDistortedImage(imgfxb, imgfxa,
49+
randint(0, w - 1), randint(10, h - 1),
50+
randint(0, w - 1), randint(10, h - 1),
51+
randint(0, w - 1), randint(10, h - 1),
52+
randint(0, w - 1), randint(10, h - 1),
53+
);
54+
if (0) fximg.fillTriangle(imgfxb,
55+
randint(0, w - 1), randint(10, h - 1),
56+
randint(0, w - 1), randint(10, h - 1),
57+
randint(0, w - 1), randint(10, h - 1),
58+
randint(0x0, 0xf),
59+
);
60+
//scene.setBackgroundImage(fximg.toImage(imgfxb));
61+
}
62+
scene.setBackgroundImage(fximg.toImage(imgfxb));
63+
scene.backgroundImage().print(`draw count: ${j}`, 1, 1, 0x1)
64+
})
6365
})
6466

0 commit comments

Comments
 (0)