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: the HUD's rows written once from runs of its layer

Mason363041d54983c1f
diff --git a/games/numblocks/ce/src/draw.h b/games/numblocks/ce/src/draw.h
index 9b3f95e9..07d33763 100644
--- a/games/numblocks/ce/src/draw.h
+++ b/games/numblocks/ce/src/draw.h
@@ -36,5 +36,6 @@ void dirt_background(int y0, int y1);
#define HUD_Y 180
#define HUD_H (SCREEN_H - HUD_Y)
extern uint8_t *hud_layer; /* HUD_W x HUD_H (in the CE's screen memory, after the world) */
+extern uint8_t hud_changed; /* set when it is drawn into */
void compose(bool hud, bool crosshair); /* the 3D view (rbuf) onto the screen, with the HUD and the crosshair */
#endif
diff --git a/games/numblocks/ce/src/gui.c b/games/numblocks/ce/src/gui.c
index fa1089c5..eb447223 100644
--- a/games/numblocks/ce/src/gui.c
+++ b/games/numblocks/ce/src/gui.c
@@ -84,7 +84,7 @@ bool hud_draw(void) {
last_id = held()->id, last_slot = pl.slot;
}
if (s == sig && !hud_dirty) return false;
- sig = s, hud_dirty = false;
+ sig = s, hud_dirty = false, hud_changed = 1;
cv = (Canvas){hud_layer, HUD_W, HUD_X, HUD_Y, HUD_W, HUD_H};
memset(hud_layer, 0, HUD_W * HUD_H);
int x0 = SCREEN_W / 2 - 91, yb = SCREEN_H - 22;
@@ -267,6 +267,7 @@ static void layout(void) {
static void strip_keep(void) {
int x = panel_x - 2, w = panel_w + 4, y = panel_y - STRIP_H;
for (int r = 0; r < STRIP_H; r++) memcpy(hud_layer + r * w, scr + (y + r) * SCREEN_W + x, (size_t)w);
+ hud_changed = 1;
}
static void draw_panel(void) {
diff --git a/games/numblocks/ce/src/render.c b/games/numblocks/ce/src/render.c
index fe8713a1..fbab31f6 100644
--- a/games/numblocks/ce/src/render.c
+++ b/games/numblocks/ce/src/render.c
@@ -825,4 +825,19 @@ void render_init(void) {
}
/* the picture to the screen: the sky in what is not drawn, the HUD over it */
-void render_show(bool hud) { rs_expand(rbuf, scr, sky_row, hud ? hud_layer : 0); }
+/* the HUD layer's rows as runs, for rs_expand: pairs (see-through, drawn), each row's adding up to HUD_W (none
+ * when they do not fit: rs_expand then goes over each pixel) */
+uint8_t rs_hruns[1600], rs_hruns_ok, hud_changed = 1;
+static void hud_runs(void) {
+ uint8_t *o = rs_hruns;
+ rs_hruns_ok = 0;
+ for (int r = 0; r < HUD_H; r++) {
+ if (o + HUD_W + 1 > rs_hruns + sizeof rs_hruns) return;
+ o = rs_runs_of(hud_layer + r * HUD_W, o, 1); /* (in assembly) */
+ }
+ rs_hruns_ok = 1;
+}
+void render_show(bool hud) {
+ if (hud && hud_changed) hud_runs(), hud_changed = 0;
+ rs_expand(rbuf, scr, sky_row, hud ? hud_layer : 0);
+}
diff --git a/games/numblocks/ce/src/rs.c b/games/numblocks/ce/src/rs.c
index 788f359a..47158cc4 100644
--- a/games/numblocks/ce/src/rs.c
+++ b/games/numblocks/ce/src/rs.c
@@ -25,11 +25,23 @@ 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, uint16_t n);
void RS(rs_rle)(uint8_t *q, const uint8_t *p, int sw);
+uint8_t *RS(rs_runs_of)(const uint8_t *h, uint8_t *o, uint8_t rows);
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, uint16_t n) {
for (unsigned i = 0; i < n; i++) p[i] = row[p[i]];
}
+uint8_t *RS(rs_runs_of)(const uint8_t *h, uint8_t *o, uint8_t rows) {
+ for (; rows; rows--, h += HUD_W)
+ for (int x = 0; x < HUD_W;) {
+ uint8_t t = 0, n = 0;
+ while (x < HUD_W && !h[x]) x++, t++;
+ while (x < HUD_W && h[x]) x++, n++;
+ *o++ = t, *o++ = n;
+ }
+ return o;
+}
+
void RS(rs_rle)(uint8_t *q, const uint8_t *p, int sw) {
while (sw > 0) {
uint8_t n = *p++;
diff --git a/games/numblocks/ce/src/rs.h b/games/numblocks/ce/src/rs.h
index 082b0404..88bd5f68 100644
--- a/games/numblocks/ce/src/rs.h
+++ b/games/numblocks/ce/src/rs.h
@@ -37,6 +37,9 @@ uint8_t rs_face(uint8_t f);
void rs_shade(uint8_t *p, const uint8_t *row, uint16_t n);
/* a picture's row of runs (tools/pack.py rle) to q (its column 0), sw pixels; 0 see-through */
void rs_rle(uint8_t *q, const uint8_t *p, int sw);
+/* the HUD layer's rows (h, HUD_W each) as runs into o: pairs (see-through, drawn), each row's adding up to HUD_W
+ * (at most HUD_W + 1 bytes a row); after them */
+uint8_t *rs_runs_of(const uint8_t *h, uint8_t *o, uint8_t rows);
/* a + floor((b - a) f / 256), a and b 0..255, f 0..256 */
int rs_lerp(int a, int b, int f);
/* the 160 x 120 picture to the screen at 2x, 0s as the row's sky colour (sky[y]); from screen row 180 on, the HUD's
diff --git a/games/numblocks/ce/src/rs.s b/games/numblocks/ce/src/rs.s
index c37628ca..a8deebf7 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_rle
.set _rs_rle, t__rs_rle
+ .global _rs_runs_of
+ .set _rs_runs_of, t__rs_runs_of
.global _rs_clip2
.set _rs_clip2, t__rs_clip2
.global _rs_face
@@ -159,6 +161,16 @@ r_hud: .d24 0
r_rows: .db 0
.global r_hudlim
r_hudlim: .db 0
+ .global r_srow
+r_srow: .d24 0
+ .global r_hrun
+r_hrun: .d24 0
+ .global r_odd
+r_odd: .db 0
+ .global r_sky
+r_sky: .db 0
+ .global r_left
+r_left: .db 0
.global r_line
r_line: .ds 320
.global r_line2
@@ -452,6 +464,8 @@ r_rtab:
.extern _rs_cell
.extern _plat_poll
.extern _wexp
+ .extern _rs_hruns
+ .extern _rs_hruns_ok
.extern _wblk
.extern _wlight
.extern _blk_light
diff --git a/games/numblocks/ce/src/rsb.inc b/games/numblocks/ce/src/rsb.inc
index e35ff451..ac49844a 100644
--- a/games/numblocks/ce/src/rsb.inc
+++ b/games/numblocks/ce/src/rsb.inc
@@ -1127,6 +1127,8 @@ b__rs_expand:
add iy, sp
ld hl, (iy+15)
ld (r_hud), hl
+ ld hl, _rs_hruns
+ ld (r_hrun), hl
ld a, 31 ; (rows left under 31: under the HUD; none without one)
add hl, de
or a, a
@@ -1154,6 +1156,7 @@ b__rs_expand:
ld bc, 320
ldir
pop hl
+b_Lex_next:
.Lex_next:
inc ix
ld a, (r_rows)
@@ -1164,6 +1167,22 @@ b__rs_expand:
ret
.Lex_hudrow:
+ ; (each screen row its own HUD row: from the runs, each pixel written once)
+ ld a, (_rs_hruns_ok)
+ or a, a
+ jr z, .Lex_hudold
+ ld (r_srow), hl
+ ld a, (ix+0)
+ ld (r_sky), a
+ call t_r_hudrow
+ ld hl, (r_srow)
+ call t_r_hudrow
+ ld hl, (r_srow)
+ ld bc, RW
+ add hl, bc
+ ld iy, r_hudlim
+ jr .Lex_next
+.Lex_hudold:
push de
ld de, r_line
ld c, (ix+0)
@@ -1189,7 +1208,7 @@ b__rs_expand:
ld bc, 640
ldir
pop hl
- jr .Lex_next
+ jp t_Lex_next
; b pairs of pixels from hl doubled to de (0: c, the row's sky); hl and de after them (flash/rsf.s has it too)
b_r_double:
@@ -3116,3 +3135,179 @@ b__rs_rle:
jr .Lrl_run
.Lrl_done:
ret
+
+; ------------------------------------------------------------------------------------------------- the HUD's rows
+; (rs_expand: a screen row under the HUD written once, from the HUD layer's runs (rs_hruns: pairs see-through,
+; drawn, each row's adding up to HUD_W) and the world's row doubled; r_odd: the column at the second half of hl's
+; pixel; r_sky the row's sky)
+
+; a screen row of the HUD's: the world's row (hl, in rbuf) doubled where the HUD (r_hud, its runs at r_hrun) lets
+; it through, the HUD's pixels elsewhere; de the screen row, after it at the end; r_hud and r_hrun on to the next
+b_r_hudrow:
+ xor a, a
+ ld (r_odd), a
+ ld a, HUD_X
+ call t_r_cols
+ ld iy, (r_hrun)
+ ld a, HUD_W
+ ld (r_left), a
+.Lhr_run:
+ ld a, (iy+0)
+ call t_r_cols ; the see-through: the world
+ push hl
+ ld hl, (r_hud)
+ ld bc, 0
+ ld c, (iy+0)
+ add hl, bc
+ ld c, (iy+1) ; the HUD's own
+ inc c
+ dec c
+ jr z, .Lhr_none
+ ldir
+.Lhr_none:
+ ld (r_hud), hl
+ pop hl
+ ld a, (iy+1)
+ call t_r_skip ; (the world under them passed by)
+ ld a, (r_left)
+ sub a, (iy+0)
+ sub a, (iy+1)
+ ld (r_left), a
+ lea iy, iy+2
+ jr nz, .Lhr_run
+ ld (r_hrun), iy
+ ld a, 320 - HUD_X - HUD_W
+ jp t_r_cols
+
+; a columns of the world (0..255) to de, doubled from hl (0: the sky); hl and de after them
+b_r_cols:
+ or a, a
+ ret z
+ ld b, a
+ ld a, (r_sky)
+ ld c, a
+ ld a, (r_odd)
+ or a, a
+ jr z, .Lrc_even
+ xor a, a
+ ld (r_odd), a
+ ld a, (hl) ; the second half of hl's pixel
+ inc hl
+ or a, a
+ jr nz, .Lrc_a
+ ld a, c
+.Lrc_a:
+ ld (de), a
+ inc de
+ dec b
+ ret z
+.Lrc_even:
+ srl b ; whole pixels; carry: the first half of one after them
+ jr nc, .Lrc_whole
+ ld a, 1
+ ld (r_odd), a
+ inc b
+ dec b
+ call nz, t_r_px
+ ld a, (hl) ; (hl stays on that pixel)
+ or a, a
+ jr nz, .Lrc_b
+ ld a, c
+.Lrc_b:
+ ld (de), a
+ inc de
+ ret
+.Lrc_whole:
+ inc b
+ dec b
+ ret z
+ jp t_r_px
+
+; b pixels from hl doubled to de (0: c, the sky)
+b_r_px:
+ ld a, (hl)
+ inc hl
+ or a, a
+ jr nz, .Lrp_a
+ ld a, c
+.Lrp_a:
+ ld (de), a
+ inc de
+ ld (de), a
+ inc de
+ djnz b_r_px
+ ret
+
+; a columns of the world passed by (hl on, r_odd), none written
+b_r_skip:
+ ld b, a
+ ld a, (r_odd)
+ add a, b ; columns from the start of hl's pixel
+ srl a ; whole pixels; carry: half into the next
+ ld b, a
+ ld a, 0
+ rla
+ ld (r_odd), a
+ push de
+ ld de, 0
+ ld e, b
+ add hl, de
+ pop de
+ ret
+
+; uint8_t *rs_runs_of(const uint8_t *h, uint8_t *o, uint8_t rows): the HUD layer's rows (h, HUD_W each) as runs
+; into o: pairs (see-through, drawn), each row's adding up to HUD_W; after them (render.c makes sure they fit)
+b__rs_runs_of:
+ ld iy, 0
+ add iy, sp
+ ld hl, (iy+3)
+ ld de, (iy+6)
+ ld a, (iy+9)
+ ld (r_left), a ; rows left
+.Lru_row:
+ ld b, HUD_W ; pixels left in the row
+.Lru_pair:
+ ld c, 0
+.Lru_t:
+ ld a, (hl)
+ or a, a
+ jr nz, .Lru_tend
+ inc hl
+ inc c
+ djnz .Lru_t
+ ; (the row ends see-through)
+ ld a, c
+ ld (de), a
+ inc de
+ xor a, a
+ ld (de), a
+ inc de
+ jr .Lru_next
+.Lru_tend:
+ ld a, c
+ ld (de), a
+ inc de
+ ld c, 0
+.Lru_n:
+ ld a, (hl)
+ or a, a
+ jr z, .Lru_nend
+ inc hl
+ inc c
+ djnz .Lru_n
+ ld a, c
+ ld (de), a
+ inc de
+ jr .Lru_next
+.Lru_nend:
+ ld a, c
+ ld (de), a
+ inc de
+ jr .Lru_pair
+.Lru_next:
+ ld a, (r_left)
+ dec a
+ ld (r_left), a
+ jr nz, .Lru_row
+ ex de, hl
+ ret
diff --git a/games/numblocks/ce/src/rsb_list.inc b/games/numblocks/ce/src/rsb_list.inc
index 8656b083..64c0ba7d 100644
--- a/games/numblocks/ce/src/rsb_list.inc
+++ b/games/numblocks/ce/src/rsb_list.inc
@@ -77,4 +77,10 @@
\m Lws_done
\m Lwsb
\m _rs_rle
+ \m r_hudrow
+ \m r_cols
+ \m r_px
+ \m r_skip
+ \m Lex_next
+ \m _rs_runs_of
.endm