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.

Balatro CE: 15 KB less code, 90 KB less archive

Mason363e75a3b4fcd6a
diff --git a/games/balatro/ce/makefile b/games/balatro/ce/makefile
index 2167c6b1..96458120 100644
--- a/games/balatro/ce/makefile
+++ b/games/balatro/ce/makefile
@@ -15,8 +15,8 @@ DBL ?= -Xclang -mdouble=64
SHARED = game score jokers cons ev util save ui draw
CEFLAGS = $(DBL) -DNP_PORT=1 -DNP_CE=1 -DNP_APP_ID=\"balatro\" -DNP_APP_NAME=\"Balatro\" \
-I../src -I../../../platform/include -I$(GEN) -Isrc -Wno-unused-function -Wno-unused-parameter
-# (no inlining: 11 KB smaller)
-CFLAGS = -Wall -Oz -fno-inline-functions $(XFLAGS) $(CEFLAGS)
+# (no inlining: 11 KB smaller; a frame pointer biased per function: 15 KB smaller)
+CFLAGS = -Wall -Oz -fno-inline-functions -mllvm -z80-frame-bias-policy=auto $(XFLAGS) $(CEFLAGS)
EXTRA_C_SOURCES = $(SHARED:%=../src/%.c) $(GEN)/main.c $(GEN)/data.c $(GEN)/ce_art.c
DEPS = $(GEN)/ce_art.h $(GEN)/main.c $(GEN)/data.c
LINKER_SCRIPT = ce.ld
diff --git a/games/balatro/ce/src/archive.S b/games/balatro/ce/src/archive.S
index e2ce6fb9..77ffcaa7 100644
--- a/games/balatro/ce/src/archive.S
+++ b/games/balatro/ce/src/archive.S
@@ -44,7 +44,11 @@ _ce_on_stack:
push de
push ix
ld (.L.sp), sp
- ld sp, bc
+ ld (.L.fn), hl
+ push bc
+ pop hl
+ ld sp, hl
+ ld hl, (.L.fn)
call .L.call
ld sp, (.L.sp)
pop ix
@@ -55,3 +59,5 @@ _ce_on_stack:
.section .bss.ce_on_stack, "aw", @nobits
.L.sp:
.skip 3
+.L.fn:
+ .skip 3
diff --git a/games/balatro/ce/src/bg_ce.c b/games/balatro/ce/src/bg_ce.c
index 67134894..42a30c3a 100644
--- a/games/balatro/ce/src/bg_ce.c
+++ b/games/balatro/ce/src/bg_ce.c
@@ -8,7 +8,7 @@
#include "ce.h"
uint8_t ce_bg_ver;
-static const uint8_t *field[3]; /* the run's (right of the sidebar), the title's top and bottom halves */
+static const uint8_t *field[2]; /* the run's (right of the sidebar), the title's: levels, two pixels a byte */
static int mode;
static float colours[3][3], target[3][3], contrast = 1, contrast_target = 1, spin, spin_target;
static int lut_dirty = 1;
@@ -20,8 +20,7 @@ int ce_bg_init(void) {
size_t n;
field[0] = ce_appvar("BALBG0", &n);
field[1] = ce_appvar("BALBG1", &n);
- field[2] = ce_appvar("BALBG2", &n);
- return field[0] && field[1] && field[2];
+ return field[0] && field[1];
}
static float fabsf_(float x) { return x < 0 ? -x : x; }
@@ -139,14 +138,25 @@ void bg_update(float dt, int rows) {
void bg_row(C *dst, int y, int x0, int x1) { (void)dst, (void)y, (void)x0, (void)x1; }
+/* two pixels a byte into their palette entries, from an even x0 */
+static void unpack(uint8_t *dst, const uint8_t *src, int n) {
+ for (; n > 0; n -= 2, src++) {
+ *dst++ = (uint8_t)(CE_SWIRL0 + (*src & 15));
+ *dst++ = (uint8_t)(CE_SWIRL0 + (*src >> 4));
+ }
+}
+
void ce_bg_row(uint8_t *dst, int y, int x0) {
+ x0 &= ~1; /* (the pixel before is covered) */
if (mode == 0) {
- const uint8_t *row = field[0] + y * BG_RUN_W;
- int x = x0;
- for (; x < BG_RUN_X; x++) dst[x] = row[BG_RUN_X - x < BG_RUN_W ? BG_RUN_X - x : 0]; /* (mirrored: a sidebar covers it) */
- memcpy(dst + x, row + (x - BG_RUN_X), (size_t)(SCR_W - x));
- } else {
- const uint8_t *row = y < SCR_H / 2 ? field[1] + y * SCR_W : field[2] + (y - SCR_H / 2) * SCR_W;
- memcpy(dst + x0, row + x0, (size_t)(SCR_W - x0));
- }
+ const uint8_t *row = field[0] + y * (BG_RUN_W / 2);
+ if (x0 < BG_RUN_X) { /* (mirrored: the sidebar covers it) */
+ static uint8_t tmp[BG_RUN_X];
+ unpack(tmp, row, BG_RUN_X);
+ for (int x = x0; x < BG_RUN_X; x++) dst[x] = tmp[BG_RUN_X - 1 - x];
+ x0 = BG_RUN_X;
+ }
+ unpack(dst + x0, row + (x0 - BG_RUN_X) / 2, SCR_W - x0);
+ } else
+ unpack(dst + x0, field[1] + y * (SCR_W / 2) + x0 / 2, SCR_W - x0);
}
diff --git a/games/balatro/ce/src/ce_main.c b/games/balatro/ce/src/ce_main.c
index ffa2a246..4e82f2f1 100644
--- a/games/balatro/ce/src/ce_main.c
+++ b/games/balatro/ce/src/ce_main.c
@@ -146,10 +146,10 @@ static void game(void) { balatro_main(); }
/* the code's appvars (where each goes), then the others */
#define NCODE 4
-static const char BOOTDATA var_names[][9] = {"BALCV0", "BALCV1", "BALCP", "BALCB", "BALLUT", "BALFIRE",
- "BALBG0", "BALBG1", "BALBG2", "BALED", "BALART0", "BALART1",
- "BALART2", "BALART3", "BALART4", "BALART5"};
-#define NVARS (NCODE + 6 + CE_ART_NVARS)
+static const char BOOTDATA var_names[][9] = {"BALCV0", "BALCV1", "BALCP", "BALCB", "BALLUT", "BALFIRE",
+ "BALBG0", "BALBG1", "BALED", "BALART0", "BALART1", "BALART2",
+ "BALART3", "BALART4", "BALART5"};
+#define NVARS (NCODE + 5 + CE_ART_NVARS)
static const char BOOTDATA save_name[9] = "NPbalato";
static const char BOOTDATA msg_missing[] = "Balatro: a file is missing";
static const char BOOTDATA msg_send[] = "Send every Balatro file:";
diff --git a/games/balatro/ce/src/flame_ce.c b/games/balatro/ce/src/flame_ce.c
index 043e77cb..fcbe87cc 100644
--- a/games/balatro/ce/src/flame_ce.c
+++ b/games/balatro/ce/src/flame_ce.c
@@ -70,7 +70,7 @@ void ce_flame_rows(int k, int x, int y, int w, int h, int top, uint8_t *strip, i
const flame_t *f = &fl[k];
if (f->real < 0.1f || w != FL_W || h != FL_H) return;
int lv = level_of(f->real > 10 ? 10 : f->real);
- const uint8_t *g = ce_fire + (lv * FL_FRAMES + frame_of(k)) * (FL_GR * FL_GC);
+ const uint8_t *g = ce_fire + (lv * FL_FRAMES + frame_of(k)) * (FL_GR * FL_GC / 2);
const uint8_t *cols = ce_lut + LUT_FCOL + ((k * FL_LEVELS + lv) * FL_ROWS) * 17;
int r0 = ya - y, r1 = yb - y;
if (r0 < 0) r0 = 0;
@@ -81,16 +81,18 @@ void ce_flame_rows(int k, int x, int y, int w, int h, int top, uint8_t *strip, i
for (int r = r0; r < r1; r++) {
int j = r >> 1;
if (j + 1 >= FL_GR) break;
- const uint8_t *ra = g + j * FL_GC, *rb = ra + FL_GC, *lut = cols + r * 17;
+ const uint8_t *ra = g + j * (FL_GC / 2), *rb = ra + FL_GC / 2, *lut = cols + r * 17;
int wy = r & 1;
/* the tips thin out in the last 3 pixels under the top limit */
int fade = y + r - top < 3 ? (3 - (y + r - top)) * 3 : 0;
uint8_t *d = strip + (y + r - y0) * SCR_W + x;
+#define FV(row, i) (((row)[(i) >> 1] >> (((i) & 1) * 4)) & 15)
for (int cx = c0; cx < c1; cx++) {
- int i = cx >> 1, a = ra[i], b = rb[i];
- if (cx & 1) a = (a + ra[i + 1]) >> 1, b = (b + rb[i + 1]) >> 1;
+ int i = cx >> 1, a = FV(ra, i), b = FV(rb, i);
+ if (cx & 1) a = (a + FV(ra, i + 1)) >> 1, b = (b + FV(rb, i + 1)) >> 1;
int sm = (wy ? (a + b) >> 1 : a) + fade;
- if (sm < 16) d[cx] = lut[sm];
+ if (sm < 15) d[cx] = lut[sm];
}
+#undef FV
}
}
diff --git a/games/balatro/ce/tools/mkart.py b/games/balatro/ce/tools/mkart.py
index 2326b8d5..630d6457 100644
--- a/games/balatro/ce/tools/mkart.py
+++ b/games/balatro/ce/tools/mkart.py
@@ -141,7 +141,7 @@ BAYER = [[0, 8, 2, 10], [12, 4, 14, 6], [3, 11, 1, 9], [15, 7, 13, 5]]
def field(mode, x0, w, h, cell):
"""The paint as src/bg.c draws it: worked out on a grid every `cell` pixels (each point the mean of 4 samples a
pixel around it, as bg.c's passes average them), interpolated between the points for every pixel, then as levels
- 0..15 (ordered dither), as palette indices."""
+ 0..15 (ordered dither), two pixels a byte (the left one in the low half)."""
gw, gh = (w + cell - 1) // cell + 2, (h + cell - 1) // cell + 2
t = 20.0 if mode else 36.0
jit = ((-1, -1), (1, 1), (1, -1), (-1, 1))
@@ -154,8 +154,11 @@ def field(mode, x0, w, h, cell):
gx, fx = x // cell, (x % cell) / cell
v = (grid[gy][gx] * (1 - fx) + grid[gy][gx + 1] * fx) * (1 - fy) + \
(grid[gy + 1][gx] * (1 - fx) + grid[gy + 1][gx + 1] * fx) * fy
- lv = int((v * (LEVELS - 1) * 16 / 255 + BAYER[y & 3][x & 3]) // 16)
- out.append(SWIRL0 + max(0, min(LEVELS - 1, lv)))
+ lv = max(0, min(LEVELS - 1, int((v * (LEVELS - 1) * 16 / 255 + BAYER[y & 3][x & 3]) // 16)))
+ if x & 1:
+ out[-1] |= lv << 4
+ else:
+ out.append(lv)
return bytes(out)
@@ -366,16 +369,19 @@ COL2 = ((0.36, 0.824, 0.36), (1.0, 0.618, 0.1))
def flame_tables():
- """grids: [level][frame] FL_GR x FL_GC bytes, the smoke in 1/128 units capped at 17 (the flame shows under 16);
- colours: [k][level][row][17] palette indices"""
+ """grids: [level][frame] FL_GR x FL_GC values two a byte (the left one in the low half), the smoke in 1/128 units
+ capped at 15 (the flame shows under 15); colours: [k][level][row][17] palette indices"""
grids = bytearray()
for li, I in enumerate(FL_I):
for f in range(FL_FRAMES):
q = flame_params(1, I, f * 0.11)
for j in range(FL_GR):
for i in range(FL_GC):
- sm = smoke_at(q, i * 2.0 / FL_W - .5, j * 2.0 / FL_H - .5)
- grids.append(min(17, int(sm * 2048) >> 7))
+ v = min(15, int(smoke_at(q, i * 2.0 / FL_W - .5, j * 2.0 / FL_H - .5) * 2048) >> 7)
+ if i & 1:
+ grids[-1] |= v << 4
+ else:
+ grids.append(v)
cols = bytearray()
for k in range(2):
for I in FL_I:
@@ -495,14 +501,13 @@ def main():
lut.append(0) # (the fonts' tables have 16-bit fields)
parts[name] = len(lut)
lut += blob
- # the background: the run's paint right of the sidebar, the title's at half size
+ # the background: the run's paint right of the sidebar, the title's
bg_run = field(0, 102, 218, 240, 4)
bg_title = field(1, 0, 320, 240, 2)
blobs = dict(vars_)
blobs["BALLUT"] = lut
blobs["BALBG0"] = bg_run
- blobs["BALBG1"] = bg_title[:len(bg_title) // 2]
- blobs["BALBG2"] = bg_title[len(bg_title) // 2:]
+ blobs["BALBG1"] = bg_title
blobs["BALFIRE"] = fgrids
blobs["BALED"] = edp
if max(len(b) for b in blobs.values()) > MAXVAR: