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.

Champion Island: the busiest street stays smooth when standing still

Mason Chen46c8592d87c2
diff --git a/games/championisland/src/gfx.c b/games/championisland/src/gfx.c
index f110d893..113f89c4 100644
--- a/games/championisland/src/gfx.c
+++ b/games/championisland/src/gfx.c
@@ -648,6 +648,8 @@ static void draw_rle_row(const uint8_t *p, int x, int w, bool flipx, uint16_t *d
/* the one big sprite of the frame decoded as the bands go down */
static const Item *streaming;
static int stream_row;
+static uint32_t stream_epoch; /* z_epoch as the stream last read: changed, the decoder read another sprite since */
+static bool stream_shared; /* the others are decoded as their bands come, the stream reading on after them */
/* affine sprites sample the one source row they last decoded (into stream_buf) */
static const uint8_t *line_spr;
@@ -679,6 +681,7 @@ static void stream_begin(const Item *it) {
sprite_info(it->ref, &s);
streaming = spr_rows_open(it->ref, &s) ? it : NULL;
stream_row = 0;
+ stream_epoch = z_epoch;
}
static void draw_stream(const Item *it, int y0, int y1, int by, unsigned ga) {
@@ -687,12 +690,16 @@ static void draw_stream(const Item *it, int y0, int y1, int by, unsigned ga) {
sprite_info(it->ref, &s);
const uint16_t *pal = pal565(s.sheet);
const uint8_t *al = palalpha(s.sheet);
+ /* another sprite was decoded since (stream_shared): back to this one's row */
+ if (z_epoch != stream_epoch && !spr_rows_at(it->ref, &s, stream_row)) return;
+ stream_epoch = z_epoch;
for (int y = y0; y < y1; y++) {
int row = y - it->y0;
if (row < stream_row) continue;
if (row > stream_row) z_get(NULL, (uint32_t)(row - stream_row) * s.w);
if (!z_get(stream_buf, s.w)) return;
stream_row = row + 1;
+ stream_epoch = z_epoch; /* (its own next strip may have been opened) */
uint16_t *d = band + (y - by) * VIEW_W;
bool fx = it->flags & DF_FLIPX;
for (int i = 0; i < s.w; i++) {
@@ -715,7 +722,7 @@ static bool scene_later; /* a menu's frame too big for the cache: the scene's
static bool band_short;
static const uint8_t *band_sprite(uint16_t sp, uint8_t flags) {
const uint8_t *r = spr_peek(sp);
- if (r || streaming || (scene_later && !(flags & DF_OVERLAY))) return r;
+ if (r || (streaming && !stream_shared) || (scene_later && !(flags & DF_OVERLAY))) return r;
r = spr_get(sp);
line_spr = NULL; /* the cache may have moved */
return r;
@@ -854,7 +861,7 @@ static __attribute__((noinline)) void draw_shape_item(const Item *it, int y0, in
static __attribute__((noinline)) void draw_mask_item(const Item *it, int y0, int y1, int by) {
const uint8_t *m = spr_peek_mask(it->ref);
- if (!m && !streaming) { m = spr_mask(it->ref); line_spr = NULL; }
+ if (!m && (!streaming || stream_shared)) { m = spr_mask(it->ref); line_spr = NULL; }
if (!m) band_short |= !(it->flags & DF_MISSING);
if (!m || !bgmem) return;
const uint16_t *pal = pal565(bgsheet);
@@ -1019,6 +1026,34 @@ static void release_done(int y) {
}
}
+/* the cache the dirty bands need at most, each for what it shows, the
+ * streamed sprite aside (masks are counted with a bit 15) */
+static uint32_t band_need(uint16_t stream_sp, const bool *dirty) {
+ uint32_t most = 0;
+ for (int by = view_top; by < view_bottom; by += BAND) {
+ if (!dirty[(by - view_top) / BAND]) continue;
+ uint16_t got[64];
+ int n = 0;
+ uint32_t sum = 0;
+ for (int i = 0; i < nitems; i++) {
+ const Item *it = &items[i];
+ if (it->y1 <= by || it->y0 >= by + BAND) continue;
+ uint16_t sp = it->kind == DI_SPRITE || it->kind == DI_STREAM ? it->ref : it->kind == DI_AFFINE ? it->color
+ : it->kind == DI_MASK ? (uint16_t)(it->ref | 0x8000) : NONE16;
+ if (sp == NONE16 || sp == stream_sp || (!(sp & 0x8000) && spr_pinned(sp))) continue;
+ bool dup = false;
+ for (int k = 0; k < n && !dup; k++) dup = got[k] == sp;
+ if (dup) continue;
+ if (n < 64) got[n++] = sp;
+ Sprite t;
+ sprite_info(sp & 0x7FFF, &t);
+ sum += sp & 0x8000 ? spr_mask_bytes(sp & 0x7FFF) : (t.rle + 3) & ~3u;
+ }
+ if (sum > most) most = sum;
+ }
+ return most;
+}
+
#ifdef HOST
int gfx_peak_items, gfx_peak_affs; /* tests: the most a frame used */
int gfx_missing; /* tests: items the last frame left for the next (not cached) */
@@ -1059,6 +1094,7 @@ void gfx_end(void) {
* street) is decoded band by band instead, from the top, each sprite let go
* once its last band is drawn (release_done) */
streaming = NULL;
+ stream_shared = false;
line_spr = NULL;
/* what a menu's opaque panel hides is not drawn (nor decoded) */
for (int j = 0; j < nitems; j++) {
@@ -1161,7 +1197,7 @@ void gfx_end(void) {
for (int i = 0; big >= 0 && i < nitems; i++) {
const Item *a = &items[i];
if (i == big || (a->kind == DI_SPRITE ? a->ref : a->kind == DI_AFFINE ? a->color : NONE16) != items[big].ref) continue;
- if (a->kind != DI_SPRITE || (a->flags & (DF_FLIPY | DF_OPAQUE))) big = -1;
+ if (a->kind != DI_SPRITE || (a->flags & (DF_FLIPY | DF_OPAQUE)) || a->y1 - a->y0 != items[big].y1 - items[big].y0) big = -1; /* (nor a stretched copy) */
for (int j = 0; big >= 0 && j < nitems; j++)
if (j != i && items[j].kind == DI_SPRITE && items[j].ref == a->ref && a->y0 < items[j].y1 + BAND && items[j].y0 < a->y1 + BAND) big = -1;
}
@@ -1176,12 +1212,21 @@ void gfx_end(void) {
}
over = false;
scene_later = menu;
+ } else if (!menu && bs && band_need(items[big].ref, dirty) + 1024 <= spr_capacity()) {
+ /* still too much for the whole frame, but each band's share fits:
+ * the big one streams while the bands go down, the others are
+ * decoded as their bands come (band by band, never one sprite per
+ * band again and again), the stream reading on from its row after */
+ for (int i = 0; i < nitems; i++)
+ if (items[i].kind == DI_SPRITE && items[i].ref == items[big].ref) items[i].kind = DI_STREAM;
+ spr_release(items[big].ref);
+ stream_shared = true;
}
}
}
#ifdef HOST
- if (getenv("CI_NEED")) fprintf(stderr, "need %u of %u%s%s\n", (unsigned)need, (unsigned)spr_capacity(), over ? " (by band)" : "",
- scene_later ? " (scene later)" : "");
+ if (getenv("CI_NEED")) fprintf(stderr, "need %u of %u%s%s\n", (unsigned)need, (unsigned)spr_capacity(),
+ stream_shared ? " (by band, streaming)" : over ? " (by band)" : "", scene_later ? " (scene later)" : "");
#endif
/* the menus' first; then the scene's, as long as the menus' can stay (too
* many: some of the scene's come next frame) */
@@ -1196,6 +1241,29 @@ void gfx_end(void) {
/* then the masks, which may take the place of what this frame does not draw */
for (int i = 0; i < nitems && !over; i++)
if (items[i].kind == DI_MASK && shown[i]) spr_mask(items[i].ref);
+ /* with a shared stream, what starts above its end first, as long as it
+ * fits: decoded now, it does not take the decoder from the stream later */
+ if (stream_shared) {
+ int end = 0;
+ for (int i = 0; i < nitems; i++)
+ if (items[i].kind == DI_STREAM && items[i].y1 > end) end = items[i].y1;
+ uint32_t got = 0;
+ for (int i = 0; i < nitems; i++) {
+ const Item *it = &items[i];
+ if (!shown[i] || it->y0 >= end) continue;
+ uint16_t sp = it->kind == DI_SPRITE ? it->ref : it->kind == DI_AFFINE ? it->color : it->kind == DI_MASK ? it->ref : NONE16;
+ if (sp == NONE16 || (it->kind != DI_MASK && spr_pinned(sp))) continue;
+ Sprite t;
+ sprite_info(sp, &t);
+ uint32_t sz = it->kind == DI_MASK ? spr_mask_bytes(sp) : (t.rle + 3) & ~3u;
+ bool have = it->kind == DI_MASK ? spr_peek_mask(sp) != NULL : spr_peek(sp) != NULL; /* (kept: used this frame) */
+ if (!have && got + sz + 1024 > spr_capacity()) continue;
+ got += sz;
+ if (have) continue;
+ if (it->kind == DI_MASK) spr_mask(sp);
+ else spr_get(sp);
+ }
+ }
/* big sprites are cached too while everything fits; otherwise the first
* one keeps the decoder while the bands go down (from its topmost item) */
for (int i = 0; i < nitems; i++) {
@@ -1211,7 +1279,7 @@ void gfx_end(void) {
}
/* with a stream holding the decoder nothing is decoded while the bands go
* down: what is not cached is drawn next frame */
- for (int i = 0; i < nitems && (streaming || !over); i++) {
+ for (int i = 0; i < nitems && ((streaming && !stream_shared) || !over); i++) {
Item *it = &items[i];
if (!shown[i]) continue;
if ((it->kind == DI_SPRITE || it->kind == DI_AFFINE || (it->kind == DI_STREAM && (!streaming || it->ref != streaming->ref))) &&
diff --git a/games/championisland/src/spr.c b/games/championisland/src/spr.c
index 1ff2c6aa..095fc19e 100644
--- a/games/championisland/src/spr.c
+++ b/games/championisland/src/spr.c
@@ -18,11 +18,13 @@ static CLzmaDec dec;
static const uint8_t *z_src;
static uint32_t z_in, z_len, z_out, z_total;
static const uint8_t *z_next; /* a stream's next strip (its record), read on when this one ends */
+uint32_t z_epoch; /* counts z_open: whoever reads on knows if someone else used the decoder */
#ifdef HOST
uint64_t z_bytes, z_opens; /* tests: how much the decoder did */
#endif
void z_open(uint32_t off, uint32_t clen, uint32_t rawlen) {
+ z_epoch++;
memset(&dec, 0, sizeof dec);
dec.prop.lc = 0;
dec.prop.lp = 0;
diff --git a/games/championisland/src/spr.h b/games/championisland/src/spr.h
index 228de017..bc279d4f 100644
--- a/games/championisland/src/spr.h
+++ b/games/championisland/src/spr.h
@@ -7,6 +7,7 @@
void z_open(uint32_t off, uint32_t clen, uint32_t rawlen);
void z_open_stream(const uint8_t *st); /* a stream from its record (spr_stream), all its strips */
uint32_t z_read(uint32_t n, const uint8_t **at);
+extern uint32_t z_epoch; /* changes when the decoder is opened again */
bool z_get(uint8_t *dst, uint32_t n);
/* Run-length rows of a sprite, decoded on demand, or NULL:
diff --git a/games/championisland/src/world.c b/games/championisland/src/world.c
index 45c8fe8d..97795480 100644
--- a/games/championisland/src/world.c
+++ b/games/championisland/src/world.c
@@ -318,9 +318,18 @@ static void translate_tree(NodeId n) {
for (NodeId c = nodes[n].first; c; c = nodes[c].next) translate_tree(c);
}
+void storage_sprite(NodeId n);
+/* a storageSprite shows its frame from the start, not after its turn comes
+ * (Lucky's statues flashed on the pedestals of champions not yet beaten) */
+static void storage_tree(NodeId n) {
+ storage_sprite(n);
+ for (NodeId c = nodes[n].first; c; c = nodes[c].next) storage_tree(c);
+}
+
static void streamed_in(NodeId c) {
ent_register_tree(c);
translate_tree(c);
+ storage_tree(c);
uint16_t T = nodes[c].T;
if (T == NONE16) return;
/* sr: places are markers for the designers */
diff --git a/games/championisland/tests/check.py b/games/championisland/tests/check.py
index 0b21ea06..cb2af5c2 100644
--- a/games/championisland/tests/check.py
+++ b/games/championisland/tests/check.py
@@ -296,6 +296,17 @@ _, err, _ = run("overworld@hub", 420, env={"CI_PATH": "4;-1240,660;-1380,660;-11
held = [line for line in err.splitlines() if line.startswith("held ")]
check("the busiest streets are whole once the view is still", len(held) == 5 and all(line.endswith(" left 0") for line in held), "\n".join(held))
+# standing still in that street (an animated sign and someone walking redraw
+# a few bands), the two big buildings and the scenery in front need more than
+# the cache: the bigger one is read as the bands go down, the others decoded
+# once each (they used to push each other out band after band, 600 KB and
+# more decoded in one frame, a frame of 150 ms on the calculator)
+_, err, _ = run("overworld@conveniencestore2", 420, "200-215:left", env={"CI_TUTORIAL_DONE": "1", "CI_ZFRAME": "1"})
+kb = {int(m[1]): float(m[2]) for m in re.finditer(r"^f(\d+) decoded ([\d.]+) KB", err, re.M)}
+still = [kb.get(f, 0) for f in range(240, 415)]
+check("standing still in the busiest street decodes little", sum(still) < 6500 and max(still) < 450,
+ f"{sum(still):.0f} KB in all, at most {max(still):.0f} KB in a frame")
+
# random keys where a crash once was (the trophy room's entities outnumbered
# their state records), and in every kind of scene
for scene in ("interior:trophyRoom", "overworld@hub", "overworld@pingponghouse1", "skate:park2", "rugby", "archery"):