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: runs on calculator software 23.2 and newer, and on custom builds (Mason363/NumPlay#48)

Mason Chen213ee4d9db31
diff --git a/games/championisland/src/game.c b/games/championisland/src/game.c
index 07eb5352..01551ff6 100644
--- a/games/championisland/src/game.c
+++ b/games/championisland/src/game.c
@@ -11,8 +11,21 @@ Input in;
Game game;
bool game_running = true;
-/* the big memory: the background layer (if the scene has one) and the sprite cache */
-static uint8_t arena[ARENA_BYTES] __attribute__((aligned(4)));
+/* the big memory: the background layer (if the scene has one) and the sprite cache. On the calculator, the RAM after
+ * the game's own (_heap_start to _heap_end), ARENA_BYTES of it at most: software 25.2 and newer gives apps 153676
+ * bytes of RAM, 23.2 and 24 give 148928 and some builds of it less, and the sprite cache takes what there is */
+#ifdef HOST
+static uint8_t arena_mem[ARENA_BYTES] __attribute__((aligned(4)));
+#define arena arena_mem
+static uint32_t arena_size(void) { return ARENA_BYTES; }
+#else
+extern char _heap_start[], _heap_end[];
+#define arena ((uint8_t *)(((uintptr_t)_heap_start + 7) & ~(uintptr_t)7))
+static uint32_t arena_size(void) {
+ uint32_t n = (uint32_t)((uintptr_t)_heap_end - (uintptr_t)arena);
+ return n < ARENA_BYTES ? n : ARENA_BYTES;
+}
+#endif
/* the arena: the scene's state, then its background layer, then the sprite cache */
static uint32_t state_bytes;
@@ -20,9 +33,10 @@ static struct { int w, h; uint8_t sheet; BgPaint paint; } bg_def;
static void relayout(void) {
uint32_t bg = bg_def.w > 0 && bg_def.h > 0 ? (uint32_t)(bg_def.w * bg_def.h + 3) & ~3u : 0;
- if (state_bytes + bg > ARENA_BYTES - MIN_CACHE) bg = 0;
+ uint32_t size = arena_size();
+ if (state_bytes + bg + MIN_CACHE > size) bg = 0;
if (bg) bg_setup(arena + state_bytes, bg_def.w, bg_def.h, bg_def.sheet, bg_def.paint); else bg_off();
- spr_setup(arena + state_bytes + bg, ARENA_BYTES - state_bytes - bg);
+ spr_setup(arena + state_bytes + bg, size - state_bytes - bg);
}
void mem_layout(int bg_w, int bg_h, uint8_t sheet, BgPaint paint) {
diff --git a/games/championisland/src/main.c b/games/championisland/src/main.c
index 4444888d..5d401536 100644
--- a/games/championisland/src/main.c
+++ b/games/championisland/src/main.c
@@ -45,6 +45,11 @@ uint32_t perf_max __attribute__((used)); /* the slowest frame's work, ms (a
uint32_t perf_slow __attribute__((used)); /* frames (after the first second) over 33 ms of work */
int main(void) {
+ /* the display tree and the sprite decoder's ring, here on the stack: some calculator software gives apps less RAM */
+ Node nodes_mem[NODE_MAX];
+ uint8_t ring_mem[LZMA_DICT] __attribute__((aligned(4)));
+ node_set_memory(nodes_mem);
+ spr_set_ring(ring_mem);
plat_begin();
#ifdef BENCH_STREAMS
/* decode speed of every big image: perf_frames = raw KB, perf_ms = time */
diff --git a/games/championisland/src/node.c b/games/championisland/src/node.c
index dcdeca7e..047c91a0 100644
--- a/games/championisland/src/node.c
+++ b/games/championisland/src/node.c
@@ -4,7 +4,15 @@
#include "spr.h"
#include "font.h"
-Node nodes[NODE_MAX];
+/* on main's stack (node_set_memory): the calculator gives apps 32 KB of it apart from their RAM, and some calculator
+ * software gives apps less RAM than 25.2's 153676 bytes */
+#ifdef HOST
+static Node nodes_mem[NODE_MAX];
+Node *nodes = nodes_mem;
+#else
+Node *nodes;
+#endif
+void node_set_memory(Node *mem) { nodes = mem; }
static NodeId free_head;
static unsigned used_count;
@@ -24,7 +32,7 @@ typedef struct { NodeId n; uint16_t ev; EventFn fn; void *ctx; } Listener;
static Listener evs[EV_MAX];
void node_reset(void) {
- memset(nodes, 0, sizeof nodes);
+ memset(nodes, 0, NODE_MAX * sizeof *nodes);
for (int i = 1; i < NODE_MAX - 1; i++) nodes[i].next = (NodeId)(i + 1);
nodes[NODE_MAX - 1].next = 0;
free_head = 1;
diff --git a/games/championisland/src/node.h b/games/championisland/src/node.h
index f56c51fb..e1b7b23e 100644
--- a/games/championisland/src/node.h
+++ b/games/championisland/src/node.h
@@ -46,7 +46,8 @@ enum { NF2_LAZY = 1, NF2_KEEP = 2, NF2_NOLOOP = 4, NF2_MODE = 24 /* mode << 3 */
static inline uint8_t node_mode(const Node *n) { return (uint8_t)((n->flags2 & NF2_MODE) >> 3); }
static inline bool node_loops(const Node *n) { return !(n->flags2 & NF2_NOLOOP); }
-extern Node nodes[NODE_MAX];
+extern Node *nodes; /* NODE_MAX of them, on main's stack on the calculator */
+void node_set_memory(Node *mem);
static inline Node *N(NodeId id) { return &nodes[id]; }
void node_reset(void);
diff --git a/games/championisland/src/spr.c b/games/championisland/src/spr.c
index 095fc19e..6df76b71 100644
--- a/games/championisland/src/spr.c
+++ b/games/championisland/src/spr.c
@@ -13,7 +13,13 @@
/* ---------------------------------------------------------------- LZMA */
static CLzmaProb probs[1984 + 0x300];
-static uint8_t ring[LZMA_DICT] __attribute__((aligned(4)));
+#ifdef HOST
+static uint8_t ring_mem[LZMA_DICT] __attribute__((aligned(4)));
+static uint8_t *ring = ring_mem;
+#else
+static uint8_t *ring; /* on main's stack (spr_set_ring), like the nodes */
+#endif
+void spr_set_ring(uint8_t *mem) { ring = mem; }
static CLzmaDec dec;
static const uint8_t *z_src;
static uint32_t z_in, z_len, z_out, z_total;
@@ -98,7 +104,7 @@ bool z_get(uint8_t *dst, uint32_t n) {
}
/* the decoder's ring when no stream is being read: scratch memory for others */
-uint8_t *z_scratch(uint32_t *size) { *size = sizeof ring; return ring; }
+uint8_t *z_scratch(uint32_t *size) { *size = LZMA_DICT; return ring; }
/* ---------------------------------------------------------------- the cache */
#define ENTRIES 160
diff --git a/games/championisland/src/spr.h b/games/championisland/src/spr.h
index bc279d4f..d3cd7dc6 100644
--- a/games/championisland/src/spr.h
+++ b/games/championisland/src/spr.h
@@ -32,6 +32,7 @@ void spr_release(uint16_t sprite); /* not needed again this f
void spr_setup(uint8_t *mem, uint32_t size); /* where the cache lives (game.c's arena) */
void spr_reset(void);
uint8_t *z_scratch(uint32_t *size); /* the decoder's ring when it is idle */
+void spr_set_ring(uint8_t *mem); /* LZMA_DICT bytes, on main's stack on the calculator */
void spr_tick(void);
uint32_t spr_cache_used(void);
uint32_t spr_capacity(void);