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: faces drawn in assembly, 8 blocks seen at first

Mason363ebcb44ea3c56
diff --git a/games/numblocks/ce/src/render.c b/games/numblocks/ce/src/render.c
index dae50f0d..c855ac82 100644
--- a/games/numblocks/ce/src/render.c
+++ b/games/numblocks/ce/src/render.c
@@ -24,7 +24,7 @@ uint32_t stat_faces, stat_spans, stat_px, stat_cells;
#ifdef HOST
extern unsigned long st_cols, st_list, st_frus, st_cov, st_culled;
#endif
-int view_dist = 10;
+int view_dist = 8;
uint8_t render_stale = 1; /* rbuf no longer the last picture (its memory used for something else, the world changed) */
uint32_t world_time;
int crack_stage = -1;
@@ -426,7 +426,7 @@ static void quad(uint8_t split) {
}
/* the cell's corners, camera space: corner k (bits x 1, y 2, z 4) at ck[3k..3k+2] */
-static int ck[24];
+#define ck rs_ck /* (rs.s: rs_face reads them) */
static void corners(int x, int y, int z) {
ck[0] = x, ck[1] = y, ck[2] = z;
ck[3] = x + rxx, ck[4] = y + uxx, ck[5] = z + fxx;
@@ -493,6 +493,41 @@ static int nb_index(int f) {
static uint16_t blk_tex_off[B_N]; /* b x 6 */
+/* face f of the cell (its corners in ck): whole by rs_face (assembly) when far enough, else cut in parts (quad) */
+static void face(uint8_t f, uint8_t split) {
+ if (split > 1) {
+ /* (cut only as much as its depth varies: a face seen straight on is not bent by the texture's straight lines) */
+ face_corners(f);
+ int z0 = fq[2], z1 = fq[2];
+ if (fq[5] < z0) z0 = fq[5];
+ if (fq[5] > z1) z1 = fq[5];
+ if (fq[8] < z0) z0 = fq[8];
+ if (fq[8] > z1) z1 = fq[8];
+ if (fq[11] < z0) z0 = fq[11];
+ if (fq[11] > z1) z1 = fq[11];
+ int d = z1 - z0, d4 = d + d;
+ d4 += d4;
+ if (z0 > 0 && d4 < z0) split = 1;
+ else if (split == 4 && z0 > 0 && d < z0) split = 2;
+ if (split > 1) {
+ quad(split);
+ return;
+ }
+ }
+ stat_faces++;
+ rs_setup(s_tex, s_cm, s_alpha);
+ uint8_t r = rs_face(f);
+
+ if (r == 2) raster(rs_fs, 4);
+ else if (r == 1) {
+ face_corners(f);
+ static CV q[4] = {{0, 0, 0, 0, 0}, {0, 0, 0, 65535, 0}, {0, 0, 0, 65535, 65535}, {0, 0, 0, 0, 65535}};
+ q[0].x = fq[0], q[0].y = fq[1], q[0].z = fq[2], q[1].x = fq[3], q[1].y = fq[4], q[1].z = fq[5];
+ q[2].x = fq[6], q[2].y = fq[7], q[2].z = fq[8], q[3].x = fq[9], q[3].y = fq[10], q[3].z = fq[11];
+ draw_quad(q, 4, rs_fclip);
+ }
+}
+
__attribute__((noinline)) static void cell(uint8_t b, int X, int Y, int Z, int fog) {
stat_cells++;
corners(X, Y, Z);
@@ -513,10 +548,8 @@ __attribute__((noinline)) static void cell(uint8_t b, int X, int Y, int Z, int f
s_tex = tex_of(tex);
s_cm = shade_of(light_level(wlight[c_idx], 1), fog);
s_alpha = 1;
- face_corners(6);
- quad(split);
- face_corners(7);
- quad(split);
+ face(6, split);
+ face(7, split);
return;
}
s_alpha = (blk_flags[b] & BF_ALPHA) != 0;
@@ -543,8 +576,7 @@ __attribute__((noinline)) static void cell(uint8_t b, int X, int Y, int Z, int f
tex = f == 3 || f == 4 ? bt[3] : bt[2];
s_tex = mined ? crack_of(tex) : tex_of(tex);
s_cm = shade_of(light_level(lb, f), fog);
- face_corners(f);
- quad(split);
+ face(f, split);
}
}
diff --git a/games/numblocks/ce/src/rs.c b/games/numblocks/ce/src/rs.c
index c69f9584..446d69db 100644
--- a/games/numblocks/ce/src/rs.c
+++ b/games/numblocks/ce/src/rs.c
@@ -22,6 +22,7 @@ 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);
+uint8_t RS(rs_face)(uint8_t f);
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) {
@@ -287,6 +288,33 @@ uint8_t RS(rs_cells)(const uint8_t *mask, uint8_t slot, uint8_t up, uint8_t down
return n;
}
+#ifndef __ez80__
+int rs_ck[24];
+SV rs_fs[4] = {{0, 0, 0, 0}, {0, 0, 65535, 0}, {0, 0, 65535, 65535}, {0, 0, 0, 65535}};
+uint8_t rs_fclip;
+#endif
+static const uint8_t FACEC[8][4] = {{4, 5, 1, 0}, {2, 3, 7, 6}, {3, 2, 0, 1}, {6, 7, 5, 4},
+ {2, 6, 4, 0}, {7, 3, 1, 5}, {2, 7, 5, 0}, {6, 3, 1, 4}};
+uint8_t RS(rs_face)(uint8_t f) {
+ uint8_t clip = 0, side = 255;
+ for (int i = 0; i < 4; i++) {
+ const int *c = rs_ck + 3 * FACEC[f][i];
+ int code = RS(rs_code)(c[0], c[1], c[2]);
+ clip |= (uint8_t)code, side &= (uint8_t)(code >> 8);
+ }
+ if (side) return 0;
+ rs_fclip = clip;
+ if (clip) return 1;
+ for (int i = 0; i < 4; i++) {
+ const int *c = rs_ck + 3 * FACEC[f][i];
+ RS(rs_proj)(c[0], c[1], c[2], &rs_fs[i].x);
+ }
+ for (int i = 0; i < 4; i++)
+ if ((unsigned)rs_fs[i].x > RW * 16 || (unsigned)rs_fs[i].y > RH * 16) return 2;
+ RS(rs_poly)(rs_fs, 4);
+ return 0;
+}
+
#ifndef __ez80__
const uint8_t *rs_flash;
int rs_jY[65], rs_jZ[65], rs_jV[65];
diff --git a/games/numblocks/ce/src/rs.h b/games/numblocks/ce/src/rs.h
index 21c23c9c..7e1ccedb 100644
--- a/games/numblocks/ce/src/rs.h
+++ b/games/numblocks/ce/src/rs.h
@@ -25,6 +25,14 @@ int rs_code(int x, int y, int z);
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);
+/* a cell's corners in camera space (corner k: x, y, z at rs_ck[3k..], bits of k x 1, y 2, z 4), and face f of it
+ * (0..5 -Y +Y -Z +Z -X +X, 6 and 7 a plant's quads) with rs_setup's texture: passed by when past a side of the view,
+ * drawn when all on the screen (0); 1 when to be cut by planes (rs_fclip: rs_code's), 2 when projected into rs_fs but
+ * not all on the screen */
+extern int rs_ck[24];
+extern SV rs_fs[4];
+extern uint8_t rs_fclip;
+uint8_t rs_face(uint8_t f);
/* 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 e05b2810..bf11b551 100644
--- a/games/numblocks/ce/src/rs.s
+++ b/games/numblocks/ce/src/rs.s
@@ -62,6 +62,8 @@ _rs_build: .d24 RSF_BUILD
.set _rs_shade, t__rs_shade
.global _rs_clip2
.set _rs_clip2, t__rs_clip2
+ .global _rs_face
+ .set _rs_face, t__rs_face
.section .data._rs_vars
.global r_tex
@@ -159,6 +161,22 @@ r_ku: .ds 48
r_kv: .ds 48
.global kt_base
kt_base: .d24 0
+ .global _rs_ck
+_rs_ck: .ds 72
+ .global _rs_fs
+_rs_fs: .d24 0, 0, 0, 0, 0, 0, 65535, 0, 0, 0, 65535, 65535, 0, 0, 0, 65535
+ .global _rs_fclip
+_rs_fclip: .db 0
+ .global fc_p
+fc_p: .d24 0
+ .global fc_clip
+fc_clip: .db 0
+ .global fc_side
+fc_side: .db 0
+ .global pj_out
+pj_out: .d24 0
+ .global rs_facec
+rs_facec: .db 36, 45, 9, 0, 18, 27, 63, 54, 27, 18, 0, 9, 54, 63, 45, 36, 18, 54, 36, 0, 63, 27, 9, 45, 18, 63, 45, 0, 54, 27, 9, 36
.global c2_k
c2_k: .db 0
.global c2_left
diff --git a/games/numblocks/ce/src/rsb.inc b/games/numblocks/ce/src/rsb.inc
index bf9cbd9e..88a2365b 100644
--- a/games/numblocks/ce/src/rsb.inc
+++ b/games/numblocks/ce/src/rsb.inc
@@ -895,6 +895,10 @@ b_clamp20:
b__rs_proj:
ld iy, 0
add iy, sp
+ ld hl, (iy+12)
+ ld (pj_out), hl
+; (iy + 3: x, y, z; pj_out: where to)
+b_proj_iy:
ld hl, (iy+9) ; z
call t_pr_z
ld hl, (iy+3)
@@ -908,7 +912,7 @@ b__rs_proj:
ld hl, 960
or a, a
sbc hl, de
- ld iy, (iy+12)
+ ld iy, (pj_out)
ld (iy+3), hl
pop hl
ld (iy+0), hl
@@ -1765,6 +1769,8 @@ b_Lq_done:
b__rs_code:
ld iy, 0
add iy, sp
+; (iy + 3: x, y, z)
+b_code_iy:
ld bc, 0
ld hl, (iy+9)
ld de, NEAR
@@ -2469,3 +2475,111 @@ b_c2_lerp:
pop de
add hl, de
ret
+
+; ------------------------------------------------------------------------------------------------- rs_face
+; uint8_t rs_face(uint8_t f)
+; face f of the cell whose corners are in rs_ck (corner k's x, y, z at k x 9 bytes; f 0..5 -Y +Y -Z +Z -X +X, 6 and 7
+; a plant's two quads), with the texture rs_setup was given: passed by when all its corners are past one side of the
+; view; 1 when it is to be cut by a plane (rs_fclip the planes); else projected into rs_fs: drawn (0) when all on the
+; screen, else 2 (to be cut to it)
+b__rs_face:
+ ld iy, 0
+ add iy, sp
+ ld l, (iy+3)
+ ld h, 4
+ mlt hl
+ ld de, rs_facec
+ add hl, de
+ ld (fc_p), hl ; its 4 corners' offsets
+ xor a, a
+ ld (fc_clip), a
+ dec a
+ ld (fc_side), a
+ ld b, 4
+.Lfc_code:
+ push bc
+ call t_fc_corner ; iy: the corner's x - 3
+ call t_code_iy
+ ld a, (fc_clip)
+ or a, l
+ ld (fc_clip), a
+ ld a, (fc_side)
+ and a, h
+ ld (fc_side), a
+ pop bc
+ djnz .Lfc_code
+ ld a, (fc_side)
+ or a, a
+ jr nz, .Lfc_none
+ ld a, (fc_clip)
+ ld (_rs_fclip), a
+ or a, a
+ jr z, .Lfc_proj
+ ld a, 1
+ ret
+.Lfc_none:
+ xor a, a
+ ret
+.Lfc_proj:
+ ; each corner projected into rs_fs
+ ld hl, (fc_p)
+ ld de, -4
+ add hl, de
+ ld (fc_p), hl
+ ld hl, _rs_fs
+ ld (pj_out), hl
+ ld b, 4
+.Lfc_p:
+ push bc
+ call t_fc_corner
+ call t_proj_iy
+ ld hl, (pj_out)
+ ld de, 12
+ add hl, de
+ ld (pj_out), hl
+ pop bc
+ djnz .Lfc_p
+ ; all on the screen: 0 <= x <= 2560, 0 <= y <= 1920
+ ld iy, _rs_fs
+ ld b, 4
+.Lfc_on:
+ ld hl, (iy+0)
+ ld de, RW * 16 + 1
+ or a, a
+ sbc hl, de
+ jr nc, .Lfc_off ; (x < 0 too: as unsigned, past)
+ ld hl, (iy+3)
+ ld de, 120 * 16 + 1
+ or a, a
+ sbc hl, de
+ jr nc, .Lfc_off
+ lea iy, iy+12
+ djnz .Lfc_on
+ ld a, 4
+ ld hl, 0
+ ld l, a
+ push hl
+ ld hl, _rs_fs
+ push hl
+ call t__rs_poly
+ pop hl
+ pop hl
+ xor a, a
+ ret
+.Lfc_off:
+ ld a, 2
+ ret
+
+; iy: the next corner of the face (fc_p) as x - 3, for code_iy and proj_iy
+b_fc_corner:
+ ld hl, (fc_p)
+ ld a, (hl)
+ inc hl
+ ld (fc_p), hl
+ ld hl, _rs_ck - 3
+ ld de, 0
+ ld e, a
+ add hl, de
+ push hl
+ pop iy
+ ret
diff --git a/games/numblocks/ce/src/rsb_list.inc b/games/numblocks/ce/src/rsb_list.inc
index 203acfc1..02af1c0a 100644
--- a/games/numblocks/ce/src/rsb_list.inc
+++ b/games/numblocks/ce/src/rsb_list.inc
@@ -59,4 +59,8 @@
\m c2_lerp
\m Lc2_edge
\m Lc2_next
+ \m proj_iy
+ \m code_iy
+ \m _rs_face
+ \m fc_corner
.endm