Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

NumBlocks CE: polygons cut to the screen in assembly

Mason363c17ebbdd4046
diff --git a/games/numblocks/ce/src/main.c b/games/numblocks/ce/src/main.c
index 19c85fc6..2f0d3dec 100644
--- a/games/numblocks/ce/src/main.c
+++ b/games/numblocks/ce/src/main.c
@@ -268,6 +268,24 @@ int main(void) {
plat_exit();
}
#endif
+#if NB_BENCH == 10
+ {
+ /* rs_clip2 against its C twin */
+ extern uint8_t rsc_rs_clip2(const SV *in, uint8_t n, SV *out, uint8_t k);
+ static SV in[4] = {{-300, 100, 0, 0}, {900, -50, 65535, 0}, {1200, 700, 65535, 65535}, {-40, 900, 0, 65535}};
+ static SV oa[16], ob[16];
+ for (uint8_t k = 0; k < 4; k++) {
+ uint8_t na = rs_clip2(in, 4, oa, k), nb = rsc_rs_clip2(in, 4, ob, k);
+ n += sprintf(out + n, "k%d n %d %d\n", k, na, nb);
+ for (int i = 0; i < na && i < nb; i++)
+ if (memcmp(&oa[i], &ob[i], sizeof(SV)))
+ n += sprintf(out + n, " %d: %d %d %d %d / %d %d %d %d\n", i, oa[i].x, oa[i].y, oa[i].u, oa[i].v, ob[i].x,
+ ob[i].y, ob[i].u, ob[i].v);
+ }
+ plat_save("NBBENCH", out, (unsigned)n, 0, 0);
+ plat_exit();
+ }
+#endif
#if NB_BENCH == 9
{
/* the picture with the archive's loops and without: the same? */
diff --git a/games/numblocks/ce/src/render.c b/games/numblocks/ce/src/render.c
index faa97540..dae50f0d 100644
--- a/games/numblocks/ce/src/render.c
+++ b/games/numblocks/ce/src/render.c
@@ -241,33 +241,6 @@ typedef struct { int x, y, z, u, v; } CV; /* camera space, Q14; u, v the texel
static int lerp12(int a, int b, int t) { return a + (int)((int32_t)(b - a) * t >> 12); }
-/* clipping in 2D against x >= 0 (k 0), x <= W (1), y >= 0 (2), y <= H (3) */
-static int clip2(SV *in, int n, SV *out, int k) {
- int m = 0;
- for (int i = 0; i < n; i++) {
- SV *a = &in[i], *b = &in[i + 1 == n ? 0 : i + 1];
- int da, db;
- switch (k) {
- case 0: da = a->x, db = b->x; break;
- case 1: da = RW * 16 - a->x, db = RW * 16 - b->x; break;
- case 2: da = a->y, db = b->y; break;
- default: da = RH * 16 - a->y, db = RH * 16 - b->y;
- }
- if (da >= 0) out[m++] = *a;
- if ((da >= 0) != (db >= 0)) {
-#ifdef HOST
- { extern unsigned long st_c2; st_c2++; }
-#endif
- int t = (int)(((int32_t)da << 12) / (da - db)); /* 0..4096 */
- SV *o = &out[m++];
- o->u = lerp12(a->u, b->u, t), o->v = lerp12(a->v, b->v, t);
- if (k < 2) o->x = k ? RW * 16 : 0, o->y = lerp12(a->y, b->y, t);
- else o->y = k == 2 ? 0 : RH * 16, o->x = lerp12(a->x, b->x, t);
- }
- }
- return m;
-}
-
static void raster(SV *p, int n) {
static SV buf_a[16], buf_b[16];
int xmin = p[0].x, xmax = p[0].x, ymin = p[0].y, ymax = p[0].y;
@@ -280,10 +253,10 @@ static void raster(SV *p, int n) {
if (xmax <= 0 || ymax <= 0 || xmin >= RW * 16 || ymin >= RH * 16) return;
if (xmin < 0 || ymin < 0 || xmax > RW * 16 || ymax > RH * 16) {
SV *a = p, *b = buf_a;
- if (xmin < 0) n = clip2(a, n, b, 0), a = b, b = a == buf_a ? buf_b : buf_a;
- if (xmax > RW * 16 && n) n = clip2(a, n, b, 1), a = b, b = a == buf_a ? buf_b : buf_a;
- if (ymin < 0 && n) n = clip2(a, n, b, 2), a = b, b = a == buf_a ? buf_b : buf_a;
- if (ymax > RH * 16 && n) n = clip2(a, n, b, 3), a = b, b = a == buf_a ? buf_b : buf_a;
+ if (xmin < 0) n = rs_clip2(a, (uint8_t)n, b, 0), a = b, b = a == buf_a ? buf_b : buf_a;
+ if (xmax > RW * 16 && n) n = rs_clip2(a, (uint8_t)n, b, 1), a = b, b = a == buf_a ? buf_b : buf_a;
+ if (ymin < 0 && n) n = rs_clip2(a, (uint8_t)n, b, 2), a = b, b = a == buf_a ? buf_b : buf_a;
+ if (ymax > RH * 16 && n) n = rs_clip2(a, (uint8_t)n, b, 3), a = b, b = a == buf_a ? buf_b : buf_a;
if (n < 3) return;
p = a;
}
diff --git a/games/numblocks/ce/src/rs.c b/games/numblocks/ce/src/rs.c
index 76d86f8e..c69f9584 100644
--- a/games/numblocks/ce/src/rs.c
+++ b/games/numblocks/ce/src/rs.c
@@ -21,6 +21,7 @@ uint8_t RS(rs_hidden)(int x, int y, int z, int r);
int RS(rs_code)(int x, int y, int z);
void RS(rs_compose)(uint8_t *dst, const uint8_t *a, const uint8_t *f);
int RS(rs_lerp)(int a, int b, int f);
+uint8_t RS(rs_clip2)(const SV *in, uint8_t n, SV *out, uint8_t k);
void RS(rs_shade)(uint8_t *p, const uint8_t *row);
void RS(rs_expand)(const uint8_t *src, uint8_t *dst, const uint8_t *sky, const uint8_t *hud);
void RS(rs_shade)(uint8_t *p, const uint8_t *row) {
@@ -222,6 +223,38 @@ void RS(rs_poly)(const SV *p, uint8_t n) {
}
}
+static int32_t c2_d(const SV *v, uint8_t k) {
+ return k == 0 ? v->x : k == 1 ? RW * 16 - v->x : k == 2 ? v->y : RH * 16 - v->y;
+}
+uint8_t RS(rs_clip2)(const SV *in, uint8_t n, SV *out, uint8_t k) {
+ uint8_t m = 0;
+ for (int i = 0; i < n; i++) {
+ const SV *a = &in[i], *b = &in[i + 1 == n ? 0 : i + 1];
+ int32_t da = c2_d(a, k), db = c2_d(b, k);
+ if (da >= 0) out[m++] = *a;
+ if ((da < 0) != (db < 0)) {
+ /* t: the share before the side, 4096ths, bit by bit; the values as mulhi rounds, the side's exact */
+ int32_t kk = da - db, r = da < 0 ? -da : da;
+ if (kk < 0) kk = -kk;
+ uint32_t t = 0;
+ for (int j = 0; j < 12; j++) {
+ r <<= 1, t <<= 1;
+ if (r >= kk) r -= kk, t |= 1;
+ }
+ uint32_t tt = t << 12;
+ SV o;
+ o.x = W24(a->x + mulhi(W24(b->x - a->x), tt)), o.y = W24(a->y + mulhi(W24(b->y - a->y), tt));
+ o.u = W24(a->u + mulhi(W24(b->u - a->u), tt)), o.v = W24(a->v + mulhi(W24(b->v - a->v), tt));
+ if (k == 0) o.x = 0;
+ else if (k == 1) o.x = RW * 16;
+ else if (k == 2) o.y = 0;
+ else o.y = RH * 16;
+ out[m++] = o;
+ }
+ }
+ return m;
+}
+
/* 1371 << 12 / (256 + i) */
static uint16_t rtab[256];
void RS(rs_proj)(int x, int y, int z, int *out) {
diff --git a/games/numblocks/ce/src/rs.h b/games/numblocks/ce/src/rs.h
index aec9f7c9..21c23c9c 100644
--- a/games/numblocks/ce/src/rs.h
+++ b/games/numblocks/ce/src/rs.h
@@ -23,6 +23,8 @@ uint8_t rs_hidden(int x, int y, int z, int r);
int rs_code(int x, int y, int z);
/* dst[i] = f[a[i]] for the 256 bytes, all three 256-byte aligned */
void rs_compose(uint8_t *dst, const uint8_t *a, const uint8_t *f);
+/* the polygon cut to a side of the screen (k 0: x >= 0, 1: x <= 2560, 2: y >= 0, 3: y <= 1920): into out, how many */
+uint8_t rs_clip2(const SV *in, uint8_t n, SV *out, uint8_t k);
/* p[i] = row[p[i]] for 256 bytes, row 256-byte aligned */
void rs_shade(uint8_t *p, const uint8_t *row);
/* a + floor((b - a) f / 256), a and b 0..255, f 0..256 */
diff --git a/games/numblocks/ce/src/rs.s b/games/numblocks/ce/src/rs.s
index 44bf033a..e05b2810 100644
--- a/games/numblocks/ce/src/rs.s
+++ b/games/numblocks/ce/src/rs.s
@@ -60,6 +60,8 @@ _rs_build: .d24 RSF_BUILD
.set _w_expose, t__w_expose
.global _rs_shade
.set _rs_shade, t__rs_shade
+ .global _rs_clip2
+ .set _rs_clip2, t__rs_clip2
.section .data._rs_vars
.global r_tex
@@ -157,6 +159,28 @@ r_ku: .ds 48
r_kv: .ds 48
.global kt_base
kt_base: .d24 0
+ .global c2_k
+c2_k: .db 0
+ .global c2_left
+c2_left: .db 0
+ .global c2_m
+c2_m: .db 0
+ .global c2_in
+c2_in: .d24 0
+ .global c2_out
+c2_out: .d24 0
+ .global c2_a
+c2_a: .d24 0
+ .global c2_b
+c2_b: .d24 0
+ .global c2_da
+c2_da: .d24 0
+ .global c2_db
+c2_db: .d24 0
+ .global c2_tt
+c2_tt: .d24 0
+ .global c2_o
+c2_o: .ds 12
.global h_r
h_r: .d24 0
.global h_c
diff --git a/games/numblocks/ce/src/rsb.inc b/games/numblocks/ce/src/rsb.inc
index 48372f49..bf9cbd9e 100644
--- a/games/numblocks/ce/src/rsb.inc
+++ b/games/numblocks/ce/src/rsb.inc
@@ -2260,3 +2260,212 @@ b__rs_shade:
ret
; ------------------------------------------------------------------------------------------------- data
+
+; ------------------------------------------------------------------------------------------------- rs_clip2
+; uint8_t rs_clip2(const SV *in, uint8_t n, SV *out, uint8_t k)
+; the polygon cut to a side of the screen (k 0: x >= 0, 1: x <= 2560, 2: y >= 0, 3: y <= 1920): its corners into
+; out, how many. Where an edge crosses: t = its share before the side, in 4096ths (bit by bit), the other values
+; a + (b - a) t (mulhi), the side's own exactly
+ .equ SV_X, 0
+ .equ SV_Y, 3
+ .equ SV_U, 6
+ .equ SV_V, 9
+ .equ SV_SIZE, 12
+b__rs_clip2:
+ push ix
+ ld ix, 0
+ add ix, sp
+ ld a, (ix+15)
+ ld (c2_k), a
+ ld hl, (ix+12)
+ ld (c2_out), hl
+ ld hl, (ix+6)
+ ld (c2_in), hl
+ ld (c2_a), hl
+ ld a, (ix+9)
+ ld (c2_left), a
+ xor a, a
+ ld (c2_m), a
+ ld iy, (c2_a)
+ call t_c2_d
+ ld (c2_da), hl
+b_Lc2_edge:
+.Lc2_edge:
+ ; b: the next corner (the first after the last)
+ ld hl, (c2_a)
+ ld de, SV_SIZE
+ add hl, de
+ ld a, (c2_left)
+ dec a
+ ld (c2_left), a
+ jr nz, .Lc2_b
+ ld hl, (c2_in)
+.Lc2_b:
+ ld (c2_b), hl
+ push hl
+ pop iy
+ call t_c2_d
+ ld (c2_db), hl
+ ; a inside: kept
+ ld a, (c2_da+2)
+ bit 7, a
+ jr nz, .Lc2_aout
+ ld hl, (c2_a)
+ call t_c2_put
+.Lc2_aout:
+ ; a and b on either side: where the edge crosses
+ ld a, (c2_da+2)
+ ld c, a
+ ld a, (c2_db+2)
+ xor a, c
+ bit 7, a
+ jp z, t_Lc2_next
+ ; k = |da - db|, r = |da|: t = r 4096 / k, 12 bits
+ ld hl, (c2_da)
+ ld de, (c2_db)
+ or a, a
+ sbc hl, de
+ call t_c2_abs
+ ex de, hl
+ ld hl, (c2_da)
+ call t_c2_abs
+ ld bc, 0
+ ld a, 12
+.Lc2_div:
+ add hl, hl
+ sla c
+ rl b
+ or a, a
+ sbc hl, de
+ jr c, .Lc2_back
+ inc c
+ jr .Lc2_divn
+.Lc2_back:
+ add hl, de
+.Lc2_divn:
+ dec a
+ jr nz, .Lc2_div
+ ; t << 12 for mulhi
+ push bc
+ pop hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld (c2_tt), hl
+ ; each value: a + (b - a) t
+ ld iy, (c2_a)
+ ld ix, (c2_b)
+ ld hl, (ix+SV_X)
+ ld de, (iy+SV_X)
+ call t_c2_lerp
+ ld (c2_o+SV_X), hl
+ ld hl, (ix+SV_Y)
+ ld de, (iy+SV_Y)
+ call t_c2_lerp
+ ld (c2_o+SV_Y), hl
+ ld hl, (ix+SV_U)
+ ld de, (iy+SV_U)
+ call t_c2_lerp
+ ld (c2_o+SV_U), hl
+ ld hl, (ix+SV_V)
+ ld de, (iy+SV_V)
+ call t_c2_lerp
+ ld (c2_o+SV_V), hl
+ ; the side's own value
+ ld a, (c2_k)
+ or a, a
+ sbc hl, hl
+ cp a, 1
+ jr c, .Lc2_x ; (k 0: x = 0)
+ jr nz, .Lc2_y01
+ ld hl, RW * 16
+.Lc2_x:
+ ld (c2_o+SV_X), hl
+ jr .Lc2_put
+.Lc2_y01:
+ cp a, 2
+ jr z, .Lc2_y
+ ld hl, 120 * 16
+.Lc2_y:
+ ld (c2_o+SV_Y), hl
+.Lc2_put:
+ ld hl, c2_o
+ call t_c2_put
+b_Lc2_next:
+.Lc2_next:
+ ld hl, (c2_b)
+ ld (c2_a), hl
+ ld hl, (c2_db)
+ ld (c2_da), hl
+ ld a, (c2_left)
+ or a, a
+ jp nz, t_Lc2_edge
+ ld a, (c2_m)
+ pop ix
+ ret
+
+; hl: how far the corner at iy is inside side c2_k (>= 0 inside)
+b_c2_d:
+ ld a, (c2_k)
+ cp a, 2
+ jr nc, .Lc2d_y
+ ld hl, (iy+SV_X)
+ or a, a
+ ret z
+ ex de, hl
+ ld hl, RW * 16
+ or a, a
+ sbc hl, de
+ ret
+.Lc2d_y:
+ ld hl, (iy+SV_Y)
+ ret z
+ ex de, hl
+ ld hl, 120 * 16
+ or a, a
+ sbc hl, de
+ ret
+
+; the corner at hl to c2_out, on
+b_c2_put:
+ ld de, (c2_out)
+ ld bc, SV_SIZE
+ ldir
+ ld (c2_out), de
+ ld hl, c2_m
+ inc (hl)
+ ret
+
+; hl = |hl| (de kept)
+b_c2_abs:
+ ld (m_t), hl
+ ld a, (m_t+2)
+ bit 7, a
+ ret z
+ push de
+ ex de, hl
+ or a, a
+ sbc hl, hl
+ sbc hl, de
+ pop de
+ ret
+
+; hl = de + (hl - de) t (c2_tt: t << 12), as mulhi rounds
+b_c2_lerp:
+ push de
+ or a, a
+ sbc hl, de
+ ld de, (c2_tt)
+ call t_mulhi
+ pop de
+ add hl, de
+ ret
diff --git a/games/numblocks/ce/src/rsb_list.inc b/games/numblocks/ce/src/rsb_list.inc
index 88d08b95..203acfc1 100644
--- a/games/numblocks/ce/src/rsb_list.inc
+++ b/games/numblocks/ce/src/rsb_list.inc
@@ -52,4 +52,11 @@
\m Lxp_cell
\m Lxp_next
\m Lxp_show
+ \m _rs_clip2
+ \m c2_d
+ \m c2_put
+ \m c2_abs
+ \m c2_lerp
+ \m Lc2_edge
+ \m Lc2_next
.endm