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: 17 KB less RAM, for calculator software that gives apps less (Mason363/NumPlay#48)

Mason Chend5228d5636a8
diff --git a/games/numblocks/src/gen.c b/games/numblocks/src/gen.c
index 55d8f2a8..a31e81cd 100644
--- a/games/numblocks/src/gen.c
+++ b/games/numblocks/src/gen.c
@@ -3150,7 +3150,14 @@ typedef struct {
uint8_t b00; /* biome of block (0, 0): the biome of the population of chunk (cx-1, cz-1) */
ColSum s[256];
} SumEntry;
-static SumEntry sumc[NSUM];
+_Static_assert(sizeof(SumEntry) * NSUM == GEN_CACHE_BYTES, "nb.h's GEN_CACHE_BYTES: the summary cache's size");
+#ifdef HOST
+static SumEntry sumc_mem[NSUM];
+static SumEntry *sumc = sumc_mem;
+#else
+static SumEntry *sumc; /* on main's stack (gen_set_cache): calculator software gives apps 32 KB of it apart */
+void gen_set_cache(void *mem) { sumc = mem; }
+#endif
static uint32_t sum_clock;
static SumEntry *sum_find(int cx, int cz) {
@@ -5405,7 +5412,7 @@ void gen_init(int64_t seed) {
cave_mul_x = jr_long(&r);
cave_mul_z = jr_long(&r);
/* caches */
- memset(sumc, 0, sizeof sumc);
+ memset(sumc, 0, NSUM * sizeof *sumc);
memset(pmemo, 0, sizeof pmemo);
memset(sum_pin, 0, sizeof sum_pin);
last_cx = bc_cx = 0x7fffffff;
@@ -5616,5 +5623,5 @@ unsigned gen_ram_bytes(void) {
sizeof pmemo + sizeof tstack + sizeof biome_index + sizeof c_b16 + sizeof c_sl +
sizeof c_top + sizeof cb16 + sizeof col_tmp + sizeof perm_grass + sizeof perm_temp +
sizeof bgrid + sizeof lay_wseed + sizeof bflags + sizeof tov + sizeof crec + sizeof U +
- sizeof sumc);
+ NSUM * sizeof *sumc);
}
diff --git a/games/numblocks/src/gen.h b/games/numblocks/src/gen.h
index a9643e4b..8bfc0335 100644
--- a/games/numblocks/src/gen.h
+++ b/games/numblocks/src/gen.h
@@ -8,6 +8,8 @@
/* Starts a world: seed as a Java long (what /seed prints). Call before anything else. */
void gen_init(int64_t seed);
+#define GEN_CACHE_BYTES (25 * 528) /* the summary cache: on main's stack on the calculator (gen_set_cache) */
+void gen_set_cache(void *mem);
/* The world type: 0 Minecraft's default, 1 superflat (FlatGeneratorInfo's default preset
* "2;7,2x3,2;1": bedrock, two dirt, grass, all plains, nothing else). Kept across gen_init. */
diff --git a/games/numblocks/src/main.c b/games/numblocks/src/main.c
index 2240f139..5e5612e6 100644
--- a/games/numblocks/src/main.c
+++ b/games/numblocks/src/main.c
@@ -3,6 +3,7 @@
* placed between the last two ticks so it moves smoothly. */
#include <math.h>
#include "nb.h"
+#include "gen.h"
#pragma GCC optimize("Os") /* (not where the time goes: small) */
#ifndef HOST
@@ -250,6 +251,9 @@ bool game_frame(void) {
#ifndef HOST
int main(void) {
+ /* the world generator's summary cache, here on the stack: some calculator software gives apps less RAM */
+ uint32_t gen_cache[GEN_CACHE_BYTES / 4];
+ gen_set_cache(gen_cache);
plat_begin();
game_init();
while (game_frame()) {}
diff --git a/games/numblocks/src/render.c b/games/numblocks/src/render.c
index b5072097..c913aee4 100644
--- a/games/numblocks/src/render.c
+++ b/games/numblocks/src/render.c
@@ -17,8 +17,6 @@
/* the strip sent to the screen: on render_frame's stack (the calculator gives apps 32 KB of it, the
* game uses a few), not in the RAM that calculator software since 23.2 gives apps (148928 bytes) */
static uint16_t *strip;
-static uint16_t cbuf[SR][RW]; /* a strip of the picture */
-static float zbuf[SR][RW]; /* and how far each pixel's ray went (entities are hidden behind) */
/* per frame */
static float ox, oy, oz; /* camera, cache coordinates */
@@ -1916,6 +1914,8 @@ static void strips_sent(bool still) { shown_ok = still; }
void render_frame(const Camera *c, uint32_t tod) {
uint16_t buf[STRIP_N] __attribute__((aligned(4)));
strip = buf;
+ uint16_t cbuf[SR][RW]; /* a strip of the picture */
+ float zbuf[SR][RW]; /* and how far each pixel's ray went (entities are hidden behind) */
bool still = still_screen();
static int last_gui;
bool opening_pause = gui == GUI_PAUSE && last_gui == GUI_NONE && c;