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.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
Rider: memory kept safe: Box2D's step stack, the arena's end guarded, a lost world never a crash
Mason3637287bbb14c65
diff --git a/games/rider/Makefile b/games/rider/Makefileindex a32eda48..8d71a01d 100644--- a/games/rider/Makefile+++ b/games/rider/Makefile@@ -26,7 +26,7 @@ BOX2D = $(wildcard src/Box2D/*/*.cpp src/Box2D/*/*/*.cpp)BOX2D_H = $(wildcard src/Box2D/*/*.h src/Box2D/*/*/*.h)PHYS = src/phys.cpp $(BOX2D)# the calculator's memory: Box2D's arena, its tree made once, small chunks and stack-B2 = -DB2_TREE_NODES=320 -DB2_STACK_SIZE=4096 -DB2_CHUNK_SIZE=1024 -DB2_CHUNK_ARRAY_INCREMENT=64+B2 = -DB2_TREE_NODES=320 -DB2_STACK_SIZE=16384 -DB2_CHUNK_SIZE=1024 -DB2_CHUNK_ARRAY_INCREMENT=64# (a computer's pointers are twice as big: room for that)DEVICE_ARENA = -DPH_ARENA=102400HOST_ARENA = -DPH_ARENA=163840 -DPH_DEVICE_ARENA=102400 -DB2_BLOCK_HOOK@@ -70,7 +70,7 @@ OBJ = $(patsubst %.c,$(BUILD_DIR)/obj/%.o,$(notdir $(SRC)))PHYS_OBJ = $(patsubst src/%.cpp,$(BUILD_DIR)/phys/%.o,$(PHYS))$(BUILD_DIR)/rider.nwa: $(OBJ) $(PHYS_OBJ) $(BUILD_DIR)/icon.o@echo "LD $@"- $(Q) $(CC) $(CFLAGS) $(LDFLAGS) $^ -lm -o $@+ $(Q) $(CC) $(CFLAGS) $(LDFLAGS) $(LDEXTRA) $^ -lm -o $@$(Q) $(STRIP) $@$(BUILD_DIR)/icon.o: src/icon.png | $(BUILD_DIR)diff --git a/games/rider/src/game.c b/games/rider/src/game.cindex 0e4d3808..f5533268 100644--- a/games/rider/src/game.c+++ b/games/rider/src/game.c@@ -767,7 +767,8 @@ static void endless_more(rd_run *r) {}/* ---------------------------------------------------------------- the near track: in and out of the physics world */-#define RESERVE 16000 /* arena bytes kept free: for the step, and the picture between steps */+#define RESERVE 20000 /* arena bytes kept free in Box2D's blocks and the arena's end */+#define TAIL 14000 /* the arena's untouched end kept: new chunks, the step past its stack, the picture */#define EMERGENCY 6000 /* short of memory: what is right round the bike still comes in above this */#define PROXY_BUDGET 140 /* fixtures in the physics world, at most (the tree holds B2_TREE_NODES / 2) *//* the bike goes 18 pixels a frame at most: what is this near comes in some frames before it can touch it; short of@@ -781,7 +782,7 @@ static bool near_box(float x0, float y0, float x1, float y1, const box_t *w, flo}static void stream(rd_run *r, bool all) {rd_bikestate *b = &r->b;- tight = ph_free() < RESERVE;+ tight = ph_free() < RESERVE || ph_tail() < TAIL;/* the windows: round the bike, and round anything falling free (awake) */box_t win[MAXDYN + 1];int nw = 0;@@ -811,7 +812,7 @@ static void stream(rd_run *r, bool all) {if (l->dyn != NO_DYN) { /* a moving object: all of it */if (all) in = keep;if (!(l->st & ST_IN) && in) {- if (ph_proxies() >= PROXY_BUDGET || ph_free() < (tight ? EMERGENCY : RESERVE)) continue;+ if (ph_proxies() >= PROXY_BUDGET || (tight ? ph_tail() < EMERGENCY : ph_free() < RESERVE)) continue;dyn_in(r, i);l->st |= ST_IN;} else if ((l->st & ST_IN) && !keep) {@@ -838,7 +839,7 @@ static void stream(rd_run *r, bool all) {if (all) pin = pkeep;uint64_t bit = (uint64_t)1 << k;if (!(r->pmask[i] & bit) && pin) {- if (ph_proxies() >= PROXY_BUDGET || ph_free() < (tight ? EMERGENCY : RESERVE)) continue;+ if (ph_proxies() >= PROXY_BUDGET || (tight ? ph_tail() < EMERGENCY : ph_free() < RESERVE)) continue;static_in(r, i, k, xy, n);r->pmask[i] |= bit;} else if ((r->pmask[i] & bit) && !pkeep) {@@ -1006,6 +1007,9 @@ static void first_touches(rd_run *r, int seg) {}}if (touch) d->pivot_ms = r->ms + (o->delay >= 0 ? o->delay : 0);+#ifdef HOST+ if (getenv("TOUCHES")) printf("pivoted object %d at %.0f %.0f: %s\n", i, o->cx, o->cy, touch ? "touches, lets go" : "held");+#endif}}@@ -1055,7 +1059,29 @@ static void timers(rd_run *r) {b->accel_fresh = false;}+static void bits_fall(rd_run *r) {+ r->end_ms += DT_MS;+ for (int i = 0; i < r->nbits; i++) {+ rd_bit *p = &r->bits[i];+ p->vy += 294 * 1.5f * (1.0f / 60), p->x += p->vx / 60, p->y += p->vy / 60, p->a += p->w / 60;+ }+ if (r->shake > 0) r->shake -= 1.0f / 60;+}+static void step_world(rd_run *r, bool held);void run_step(rd_run *r, bool held) {+ if (r->broken) { bits_fall(r); return; }+ static ph_jmp full;+ if (PH_SETJMP(full)) { /* the calculator's memory ran out in Box2D: this world is given up, the bike breaks */+ ph_on_full(0), ph_listen(0);+ r->broken = true;+ crash_why = "memory", crash(r);+ return;+ }+ ph_on_full(&full);+ step_world(r, held);+ ph_on_full(0);+}+static void step_world(rd_run *r, bool held) {R = r;rd_bikestate *b = &r->b;ph_listen(&listener);@@ -1086,14 +1112,7 @@ void run_step(rd_run *r, bool held) {/* enterFrame: the timers (the accelerate timer's tick among them) */timers(r);r->frame++;- if (r->ended) {- r->end_ms += DT_MS;- for (int i = 0; i < r->nbits; i++) {- rd_bit *p = &r->bits[i];- p->vy += 294 * 1.5f * (1.0f / 60), p->x += p->vx / 60, p->y += p->vy / 60, p->a += p->w / 60;- }- if (r->shake > 0) r->shake -= 1.0f / 60;- }+ if (r->ended) bits_fall(r);if (!r->ended) {/* GameCharacter's enterFrame: the button's step by the way it goes, the wheels' damping */float vx, vy;diff --git a/games/rider/src/phys.cpp b/games/rider/src/phys.cppindex a4b2333b..24b18bdf 100644--- a/games/rider/src/phys.cpp+++ b/games/rider/src/phys.cpp@@ -14,6 +14,10 @@#include <Box2D/Dynamics/b2World.h>#include <new>#include <string.h>+#ifdef HOST+#include <stdio.h>+#include <stdlib.h>+#endif#ifndef PH_ARENA#define PH_ARENA (72 * 1024)@@ -22,6 +26,8 @@#define DEG 0.0174532925f/* ---------------------------------------------------------------- the arena: first fit, blocks behind 8 bytes (their size) */+static ph_jmp *oom;+void ph_on_full(ph_jmp *j) { oom = j; }typedef struct {uint32_t top, used, peak, pad;#ifdef B2_BLOCK_HOOK@@ -54,7 +60,17 @@ static void *arena_alloc(int32 size, void *) {}at += sz;}- if (HEAD->top + need > PH_ARENA) return 0;+ if (HEAD->top + need > PH_ARENA) {+ if (oom) PH_LONGJMP(*oom); /* (the game gives up this world) */+#ifdef HOST+ extern b2World *ph_world_for_debug;+ fprintf(stderr, "ph: arena full (%u bytes asked, %u in use; %d bodies, %d contacts, %d proxies)\n", (unsigned)size,+ (unsigned)HEAD->used, ph_world_for_debug->GetBodyCount(), ph_world_for_debug->GetContactCount(),+ ph_world_for_debug->GetProxyCount());+ abort();+#endif+ return 0;+ }uint32_t *h = (uint32_t *)(arena.b + HEAD->top);*h = need, HEAD->top += need, HEAD->used += need;if (HEAD->used > HEAD->peak) HEAD->peak = HEAD->used;@@ -114,13 +130,19 @@ static int dev_size(int32 host) {default: return host;}}+extern "C" size_t ph_device_peak(void);static void block_hook(int32 size, int32 d) {static const int cls[] = {16, 32, 64, 96, 128, 160, 192, 224, 256, 320, 384, 448, 512, 640};int dv = dev_size(size), k = 0;while (cls[k] < dv) k++;dev_live[k] += d;int per = B2_CHUNK_SIZE / cls[k], need = (dev_live[k] + per - 1) / per;- if (need > dev_chunks[k]) dev_chunks[k] = need;+ if (need > dev_chunks[k]) {+ dev_chunks[k] = need;+#ifdef PH_DEVICE_ARENA+ if (ph_device_peak() > PH_DEVICE_ARENA && oom) PH_LONGJMP(*oom); /* (the calculator would be out of memory here) */+#endif+ }}extern "C" void ph_device_classes(int *chunks) { for (int k = 0; k < 14; k++) chunks[k] = dev_chunks[k]; }extern "C" size_t ph_device_used(void) { /* (the blocks in use, not the chunks kept) */@@ -137,6 +159,9 @@ extern "C" size_t ph_device_peak(void) {#endifstatic b2World *world;+#ifdef HOST+b2World *ph_world_for_debug;+#endifstatic b2Body *ground;static Listener *listener;static union {@@ -157,6 +182,9 @@ void ph_world_new(void) {/* (no destructor: the arena is simply started again) */world = new (arena_alloc(sizeof(b2World), 0)) b2World(b2Vec2(0.0f, 9.8f));world->SetAllowSleeping(true);+#ifdef HOST+ ph_world_for_debug = world;+#endiflistener = new (listener_mem.b) Listener();world->SetContactListener(listener);b2BodyDef bd;@@ -164,6 +192,16 @@ void ph_world_new(void) {cbs = 0;}size_t ph_used(void) { return HEAD->used; }+/* the arena's end never handed out yet: Box2D keeps its chunks for good, so this only goes down */+#if defined(B2_BLOCK_HOOK) && defined(PH_DEVICE_ARENA)+extern "C" size_t ph_device_peak(void);+size_t ph_tail(void) {+ size_t p = ph_device_peak();+ return p < PH_DEVICE_ARENA ? PH_DEVICE_ARENA - p : 0;+}+#else+size_t ph_tail(void) { return PH_ARENA - HEAD->top; }+#endif/* what is free: the arena's end, its free blocks, and the free blocks in Box2D's chunks */#if defined(B2_BLOCK_HOOK) && defined(PH_DEVICE_ARENA)extern "C" size_t ph_device_used(void);diff --git a/games/rider/src/phys.h b/games/rider/src/phys.hindex def89b1c..30143bd6 100644--- a/games/rider/src/phys.h+++ b/games/rider/src/phys.h@@ -5,6 +5,7 @@* All of Box2D's memory is one arena (ph_arena), so a whole world can be copied away and back (tests). */#ifndef PHYS_H#define PHYS_H+#include <setjmp.h>#include <stdbool.h>#include <stddef.h>#include <stdint.h>@@ -35,6 +36,19 @@ typedef struct {void ph_world_new(void);size_t ph_used(void);size_t ph_free(void);+size_t ph_tail(void); /* the end of the arena never used yet */+/* where to go if the arena runs out (NULL: nowhere); the world is then lost. (GCC's own setjmp on the calculator:+ * the C library's would bring in the C++ unwinder) */+#ifdef HOST+typedef jmp_buf ph_jmp;+#define PH_SETJMP(b) setjmp(b)+#define PH_LONGJMP(b) longjmp(b, 1)+#else+typedef void *ph_jmp[5];+#define PH_SETJMP(b) __builtin_setjmp(b)+#define PH_LONGJMP(b) __builtin_longjmp(b, 1)+#endif+void ph_on_full(ph_jmp *j);size_t ph_peak(void);size_t ph_arena_size(void);size_t ph_top(void); /* the arena in use is [0, ph_top()) */diff --git a/games/rider/src/rider.h b/games/rider/src/rider.hindex 2996929f..ac2d516b 100644--- a/games/rider/src/rider.h+++ b/games/rider/src/rider.h@@ -185,6 +185,7 @@ typedef struct {int flips, perfect, doubles, gems;int best_flips_jump;int ended; /* END_ */+ bool broken; /* (out of memory: the physics world is given up) */float end_ms;float finish_x;float bottom; /* below this is out of the world */