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
PolyTrack: the frame's physics counted in its time, a simpler picture after heavy physics, quicker contact tests
Mason363ad7769ec44c3
diff --git a/games/polytrack/Makefile b/games/polytrack/Makefileindex 698a7dfe..d1fc90ba 100644--- a/games/polytrack/Makefile+++ b/games/polytrack/Makefile@@ -73,7 +73,7 @@ $(BUILD_DIR)/armcheck.nwa: tests/armcheck.c tests/routes.h src/car.c src/world.carmcheck: $(BUILD_DIR)/armcheck.nwa$(PYTHON) tools/armcheck.py $<$(BUILD_DIR)/armperf.nwa: tests/armperf.c tests/routes.h $(HOT) $(COLD_OBJ) $(HEADERS) $(BUILD_DIR)/icon.o $(BUILD_DIR)/data.o- $(Q) $(CC) $(CFLAGS) -O2 $(LDFLAGS) tests/armperf.c $(HOT) $(filter-out %/main.o,$(COLD_OBJ)) $(BUILD_DIR)/data.o \+ $(Q) $(CC) $(CFLAGS) -O2 $(PERF_CFLAGS) $(LDFLAGS) tests/armperf.c $(HOT) $(filter-out %/main.o,$(COLD_OBJ)) $(BUILD_DIR)/data.o \$(BUILD_DIR)/icon.o -lm -o $@armperf: $(BUILD_DIR)/armperf.nwa$(PYTHON) tools/armperf.py $<diff --git a/games/polytrack/src/car.c b/games/polytrack/src/car.cindex 82baaa84..9dd2a24d 100644--- a/games/polytrack/src/car.c+++ b/games/polytrack/src/car.c@@ -34,6 +34,51 @@ static v3 hull_lo, hull_hi;static float hull_plane[16][4]; /* the hull's face planes, outwards */static int nhull_plane;static wtri cache[PT_CACHE];+/* the cached triangles near enough to touch the car this frame (a sphere round it), in the cache's order: the+ contacts and the wheels' rays look at these only, and find what they would among all */+static uint8_t near_tri[PT_CACHE];+static int nnear;+/* the cached triangles' boxes in bytes (12 m round where the cache was filled, rounded outwards): a quick test+ before the exact one, never turning away a triangle the exact one would keep */+#define BOX_K (255.0f / 12.0f)+static uint8_t box8[PT_CACHE][6];+static v3 box_o;+static int qlo(float v) {+ float f = v * BOX_K;+ int i = (int)f;+ if ((float)i > f) i--;+ return i < 0 ? 0 : i > 255 ? 255 : i;+}+static int qhi(float v) {+ float f = v * BOX_K;+ int i = (int)f;+ if ((float)i < f) i++;+ return i < 0 ? 0 : i > 255 ? 255 : i;+}+static void box8_fill(const car_t *c) {+ box_o = vsub(c->cache_at, V3(6.0f, 6.0f, 6.0f));+ for (int i = 0; i < c->ncache; i++) {+ const wtri *t = &cache[i];+ v3 a = vsub(t->a, box_o), b = vsub(t->b, box_o), d = vsub(t->c, box_o);+ box8[i][0] = (uint8_t)qlo(pt_minf(a.x, pt_minf(b.x, d.x))), box8[i][1] = (uint8_t)qhi(pt_maxf(a.x, pt_maxf(b.x, d.x)));+ box8[i][2] = (uint8_t)qlo(pt_minf(a.y, pt_minf(b.y, d.y))), box8[i][3] = (uint8_t)qhi(pt_maxf(a.y, pt_maxf(b.y, d.y)));+ box8[i][4] = (uint8_t)qlo(pt_minf(a.z, pt_minf(b.z, d.z))), box8[i][5] = (uint8_t)qhi(pt_maxf(a.z, pt_maxf(b.z, d.z)));+ }+}+/* a query box in bytes, and whether triangle i may meet it */+typedef struct { uint8_t v[6]; } qbox;+static qbox qbox_of(v3 lo, v3 hi) {+ lo = vsub(lo, box_o), hi = vsub(hi, box_o);+ qbox q = {{(uint8_t)qlo(lo.x), (uint8_t)qhi(hi.x), (uint8_t)qlo(lo.y), (uint8_t)qhi(hi.y), (uint8_t)qlo(lo.z), (uint8_t)qhi(hi.z)}};+ return q;+}+static bool may_meet(int i, const qbox *q) {+ const uint8_t *b = box8[i];+ return !(b[1] < q->v[0] || b[0] > q->v[1] || b[3] < q->v[2] || b[2] > q->v[3] || b[5] < q->v[4] || b[4] > q->v[5]);+}+static float reach; /* the farthest the hull or a wheel's ray gets from the car's origin */+static v3 hull_c; /* the hull's middle (body space), and the farthest corner from it */+static float hull_r;/* the checkpoints of the track */#define MAX_DET 64@@ -51,6 +96,11 @@ static void hull_setup(void) {hull_lo = V3(pt_minf(hull_lo.x, hull[i].x), pt_minf(hull_lo.y, hull[i].y), pt_minf(hull_lo.z, hull[i].z));hull_hi = V3(pt_maxf(hull_hi.x, hull[i].x), pt_maxf(hull_hi.y, hull[i].y), pt_maxf(hull_hi.z, hull[i].z));}+ reach = 0.0f;+ for (int i = 0; i < PT_HULL_N; i++) reach = pt_maxf(reach, vlen(hull[i]));+ hull_c = vmul(vadd(hull_lo, hull_hi), 0.5f), hull_r = 0.0f;+ for (int i = 0; i < PT_HULL_N; i++) hull_r = pt_maxf(hull_r, vlen(vsub(hull[i], hull_c)));+ for (int i = 0; i < 4; i++) reach = pt_maxf(reach, vlen(car_wheel_pos[i]) + REST + RADIUS);/* inertia: Bullet's box from the cached AABB (support points +- 0.04), its margin 0.02 added twice */float lx = 2.0f * ((hull_hi.x - hull_lo.x) * 0.5f + 0.04f + 0.02f + 0.02f);float ly = 2.0f * ((hull_hi.y - hull_lo.y) * 0.5f + 0.04f + 0.02f + 0.02f);@@ -151,6 +201,7 @@ static void cache_refresh(car_t *c) {v3 h = V3(5.0f, 5.0f, 5.0f);c->ncache = world_gather(vsub(c->pos, h), vadd(c->pos, h), cache, PT_CACHE);c->cache_ok = 1;+ box8_fill(c);}void car_cache_rebuild(car_t *c) {@@ -158,6 +209,15 @@ void car_cache_rebuild(car_t *c) {if (!c->cache_ok) return;v3 h = V3(5.0f, 5.0f, 5.0f);c->ncache = world_gather(vsub(c->cache_at, h), vadd(c->cache_at, h), cache, PT_CACHE);+ box8_fill(c);+}++static void near_refresh(const car_t *c) {+ float r = reach + 0.35f; /* (and what the car moves in a frame, and the contact margins) */+ qbox q = qbox_of(vsub(c->pos, V3(r, r, r)), vadd(c->pos, V3(r, r, r)));+ nnear = 0;+ for (int i = 0; i < c->ncache; i++)+ if (may_meet(i, &q)) near_tri[nnear++] = (uint8_t)i;}/* Bullet's btTriangleRaycastCallback, closest hit: fraction, normal facing the ray's start */@@ -187,8 +247,10 @@ static bool cast_ray(car_t *c, v3 from, v3 to, float *frac, v3 *normal) {float ly = pt_minf(from.y, to.y), hy = pt_maxf(from.y, to.y);float lz = pt_minf(from.z, to.z), hz = pt_maxf(from.z, to.z);*frac = 1.0f;- for (int i = 0; i < c->ncache; i++) {- const wtri *t = &cache[i];+ qbox q = qbox_of(V3(lx, ly, lz), V3(hx, hy, hz));+ for (int j = 0; j < nnear; j++) {+ if (!may_meet(near_tri[j], &q)) continue;+ const wtri *t = &cache[near_tri[j]];if (pt_maxf(t->a.x, pt_maxf(t->b.x, t->c.x)) < lx || pt_minf(t->a.x, pt_minf(t->b.x, t->c.x)) > hx) continue;if (pt_maxf(t->a.y, pt_maxf(t->b.y, t->c.y)) < ly || pt_minf(t->a.y, pt_minf(t->b.y, t->c.y)) > hy) continue;if (pt_maxf(t->a.z, pt_maxf(t->b.z, t->c.z)) < lz || pt_minf(t->a.z, pt_minf(t->b.z, t->c.z)) > hz) continue;@@ -230,8 +292,16 @@ static int find_contacts(car_t *c, contact_t *out) {lo = vsub(lo, V3(g, g, g)), hi = vadd(hi, V3(g, g, g));contact_t cand[48];int nc = 0;- for (int i = 0; i < c->ncache && nc < 48; i++) {- const wtri *t = &cache[i];+ /* (a triangle whose plane passes further than this from the hull's middle can't give a contact: every corner+ is more than 0.5 m from it, and none of its corners is in the hull) */+ v3 hc = vadd(c->pos, mrow(&c->basis, hull_c));+ float far = hull_r + 0.6f;+ qbox q = qbox_of(lo, hi);+ for (int j = 0; j < nnear && nc < 48; j++) {+ if (!may_meet(near_tri[j], &q)) continue;+ const wtri *t = &cache[near_tri[j]];+ float dc = vdot(t->n, hc) - t->d;+ if (dc > far || dc < -far) continue;if (pt_maxf(t->a.x, pt_maxf(t->b.x, t->c.x)) < lo.x || pt_minf(t->a.x, pt_minf(t->b.x, t->c.x)) > hi.x) continue;if (pt_maxf(t->a.y, pt_maxf(t->b.y, t->c.y)) < lo.y || pt_minf(t->a.y, pt_minf(t->b.y, t->c.y)) > hi.y) continue;if (pt_maxf(t->a.z, pt_maxf(t->b.z, t->c.z)) < lo.z || pt_minf(t->a.z, pt_minf(t->b.z, t->c.z)) > hi.z) continue;@@ -632,6 +702,7 @@ static void checkpoints(car_t *c) {void car_step(car_t *c, unsigned in) {if (c->frames > PT_MAX_FRAMES) return;cache_refresh(c);+ near_refresh(c);c->impulse = 0.0f;/* the downforce: -up |v . forward| 0.05 */{@@ -719,6 +790,7 @@ void car_step(car_t *c, unsigned in) {c->next_cp = c->respawn.next;c->cache_ok = 0;cache_refresh(c);+ near_refresh(c);}c->reset_held = reset;/* Bullet's step */diff --git a/games/polytrack/src/main.c b/games/polytrack/src/main.cindex d97cb670..13d9b769 100644--- a/games/polytrack/src/main.c+++ b/games/polytrack/src/main.c@@ -263,6 +263,7 @@ int main(void) {uint8_t shown = 0xFF;bool dirty = true;uint32_t min_frame = frame_gap ? frame_gap : plat_slow() ? 33 : 25;+ uint32_t work_phys = 0; /* ms of physics since the last picture */for (;;) {now = plat_millis();uint32_t dt = now - last;@@ -288,13 +289,17 @@ int main(void) {race_begin();} else {if (hit & PK_VIEW) sv.p.view ^= 1;+ uint32_t p0 = plat_millis();step_race(live, hit, dt);+ work_phys += plat_millis() - p0;if (car.finish && ui.finish_at && ui.clock - ui.finish_at > 2000 && ui.screen == V_RACE)ui.screen = V_RESULT, ui.sel = RES_AGAIN;}} else if (ui.screen == V_RESULT) {/* (the car rolls on behind the results) */+ uint32_t p0 = plat_millis();step_race(0, 0, dt);+ work_phys += plat_millis() - p0;quit = keys_menu(hit);} else {quit = keys_menu(hit);@@ -309,6 +314,7 @@ int main(void) {ui.screen == V_CAR;if (dirty || (moving && now - frame_at >= min_frame)) {uint32_t t0 = plat_millis();+ rd_ease_for(work_phys);float fdt = (float)(now - frame_at) / 1000.0f;if (fdt > 0.1f) fdt = 0.1f;look = car_look_of(&car);@@ -329,7 +335,8 @@ int main(void) {perf_frames++;if (spent > perf_max) perf_max = spent;if (spent > 33) perf_slow++;- if (ui.screen == V_RACE) rd_adapt(spent);+ if (moving) rd_adapt(spent + work_phys); /* (the screens in 3D: the frame's physics and picture) */+ work_phys = 0, rd_ease = 0;frame_at = now;dirty = false;} else {diff --git a/games/polytrack/src/render.c b/games/polytrack/src/render.cindex aa0ceaca..de0a1af9 100644--- a/games/polytrack/src/render.c+++ b/games/polytrack/src/render.c@@ -28,7 +28,8 @@ typedef struct {static tri tris[MAX_TRIS];static int ntris;int rd_tris, rd_dropped, rd_mtris, rd_chunks;-int rd_detail = RD_LEVELS - 1;+int rd_detail = RD_LEVELS - 1, rd_ease;+static int qcur = RD_LEVELS - 1; /* the quality of the frame being made */bool rd_half, rd_halfx;/* the picture's quality, lowest first: how far it draws, how near the parts keep their whole shape, half the rows,triangles at most */@@ -262,7 +263,9 @@ void rd_begin(const camera_t *cam, int env, int sun_byte, bool sky_only) {ntris = 0, rd_dropped = 0;float half = C.fov * 0.5f * (PT_PI / 180.0f);focal = CY * pt_cosf(half) / pt_sinf(half);- int q = rd_detail < 0 ? 0 : rd_detail >= RD_LEVELS ? RD_LEVELS - 1 : rd_detail;+ int q = rd_detail - rd_ease;+ q = q < 0 ? 0 : q >= RD_LEVELS ? RD_LEVELS - 1 : q;+ qcur = q;far_d = DRAW[q];rd_half = HALF[q] & 1, rd_halfx = HALF[q] >> 1;cap = CAP[q];@@ -341,7 +344,7 @@ static void collect(int i, void *ctx) {v = &vis[far];}v->inst = (uint16_t)i, v->chunk = (uint8_t)k, v->dist = d;- v->lod = d < NEAR_DETAIL[rd_detail < 0 ? 0 : rd_detail >= RD_LEVELS ? RD_LEVELS - 1 : rd_detail] ? 0 : 1;+ v->lod = d < NEAR_DETAIL[qcur] ? 0 : 1;}}@@ -424,7 +427,7 @@ static void mountains(void) {float rad[8];for (int k = 0; k < 8; k++) rad[k] = W.mr + 100.0f * (float)k;/* (at the lowest qualities: every other segment, the nearer rings) */- int de = rd_detail <= 2 ? 2 : 1, k0 = rd_detail <= 2 ? 3 : 6;+ int de = qcur <= 2 ? 2 : 1, k0 = qcur <= 2 ? 3 : 6;for (int k = k0; k >= 0; k--)for (int e = 0; e < s; e += de) {int e1 = (e + de) % s;@@ -549,7 +552,7 @@ void rd_car(const car_look *c) {v3 t = to_cam(vadd(c->pos, mrow(&R, V3(0.0f, CAR_MASS_OFFSET, 0.0f))));if (t.z < -6.0f || t.z > far_d) return;car_mesh(pt_body_v, PT_BODY_NV, pt_body_t, PT_BODY_NT, M, t, &R, mat, c);- if (!c->ghost && rd_detail > 2) car_mesh(pt_extra_v, PT_EXTRA_NV, pt_extra_t, PT_EXTRA_NT, M, t, &R, mat, c);+ if (!c->ghost && qcur > 2) car_mesh(pt_extra_v, PT_EXTRA_NV, pt_extra_t, PT_EXTRA_NT, M, t, &R, mat, c);/* the wheels: at their suspension's end, turned by the steering (front) and rolling */for (int i = 0; i < 4; i++) {v3 wp = vadd(c->pos, mrow(&R, vadd(car_wheel_pos[i], V3(0.0f, -c->susp[i], 0.0f))));@@ -594,14 +597,16 @@ void rd_shadow(const car_look *c, const car_t *phys) {for (int i = 1; i + 1 < 6; i++) cam_tri(q[0], q[i], q[i + 1], 0, RK_SHADOW);}-/* the picture's quality follows the time a race frame takes: down at once when slow (or out of room for- triangles), up slowly */+/* the picture's quality follows the time a frame's work takes (the physics and the picture): down at once when+ slow (or out of room for triangles), up slowly. rd_ease_for: a frame whose physics took long is drawn simpler */+void rd_ease_for(uint32_t physics_ms) { rd_ease = physics_ms > 10 ? 2 : physics_ms > 6 ? 1 : 0; }void rd_adapt(uint32_t spent_ms) {static uint32_t slow, quick;+ int q = rd_detail;+ /* (up from the lower resolutions on time alone; above them, only with triangles to spare) */if (spent_ms > 26 || rd_dropped) slow++, quick = 0;- else if (spent_ms < 16 && rd_tris < cap * 3 / 4) quick++, slow = 0;+ else if (q < 3 ? spent_ms < 20 : spent_ms < 16 && rd_tris < cap * 3 / 4) quick++, slow = 0;else slow = quick = 0;- int q = rd_detail;/* (a frame near 33 ms: down at once, two steps when past it) */if (spent_ms > 30) q -= spent_ms > 36 ? 2 : 1, slow = 0;/* (out of triangles but in time, at the nearest distance: not a reason for half the rows) */diff --git a/games/polytrack/src/render.h b/games/polytrack/src/render.hindex 89b40433..88596571 100644--- a/games/polytrack/src/render.h+++ b/games/polytrack/src/render.h@@ -33,7 +33,9 @@ extern const uint32_t rd_paints[8]; /* the car colours */#define RD_LEVELS 7extern int rd_detail; /* 0..RD_LEVELS-1: the picture's quality; lowered when a frame is slow */extern bool rd_half, rd_halfx; /* half the rows, each drawn twice (the lowest qualities); and of the columns */-void rd_adapt(uint32_t spent_ms); /* after each race frame: the quality for the next */+void rd_adapt(uint32_t spent_ms); /* after each frame in 3D, its work in ms: the quality for the next */+extern int rd_ease; /* this frame drawn this many levels lower (rd_ease_for) */+void rd_ease_for(uint32_t physics_ms); /* before drawing: the physics took this long */#define RD_MAX_TRIS 1700 /* triangles in a frame, at most */extern int rd_tris, rd_dropped, rd_mtris, rd_chunks; /* the last frame: triangles kept, some left out, of the mountains; chunks */diff --git a/games/polytrack/tests/armperf.c b/games/polytrack/tests/armperf.cindex ee3de65f..35ea984e 100644--- a/games/polytrack/tests/armperf.c+++ b/games/polytrack/tests/armperf.c@@ -17,6 +17,9 @@ const uint32_t eadk_api_level __attribute__((section(".rodata.eadk_api_level")))frames over 33 ms, frames at qualities 2..5, the finish, at 1, the triangles and the chunks of the slow frames in all, at 0 */__attribute__((used, externally_visible)) volatile uint32_t perf_track, perf_go;__attribute__((used, externally_visible)) uint32_t perf_out[16], perf_done;+/* (for a profile of the slow frames: the frame going on, and each frame's work in ms) */+__attribute__((used, externally_visible)) volatile uint32_t perf_frame;+__attribute__((used, externally_visible)) uint8_t perf_ms[4096];void plat_push(int x, int y, int w, int h, const uint16_t *px) {eadk_display_push_rect((eadk_rect_t){(uint16_t)x, (uint16_t)y, (uint16_t)w, (uint16_t)h}, (const eadk_color_t *)px);@@ -57,20 +60,33 @@ int main(void) {ui.screen = V_RACE, ui.started = 1;rd_detail = RD_LEVELS - 1;uint32_t dt = 33, f = 0;+ /* (the race waits at the start for a driving key: the picture settles meanwhile) */+ for (int i = 0; i < 30; i++) {+ uint32_t t0 = (uint32_t)eadk_timing_millis();+ car_look look = car_look_of(&car);+ chase_update(&chase, &cam, 0.033f, look.pos, look.rot, car.speed);+ pt_render(&sv, &car, &cam, &look, NULL, &ui);+ rd_adapt((uint32_t)eadk_timing_millis() - t0);+ }while (!car.finish && f < 600000) {uint32_t t0 = (uint32_t)eadk_timing_millis();for (uint32_t i = 0; i < dt && !car.finish; i++, f++) car_step(&car, next_keys());+ uint32_t t1 = (uint32_t)eadk_timing_millis();+ rd_ease_for(t1 - t0);car_look look = car_look_of(&car);chase_update(&chase, &cam, (float)dt / 1000.0f, look.pos, look.rot, car.speed);ui.clock += dt;pt_render(&sv, &car, &cam, &look, NULL, &ui);uint32_t spent = (uint32_t)eadk_timing_millis() - t0;+ if (perf_frame < sizeof perf_ms) perf_ms[perf_frame] = (uint8_t)(spent < 255 ? spent : 255);+ perf_frame++;perf_out[0]++, perf_out[1] += spent;if (spent > perf_out[2]) perf_out[2] = spent;- if (spent > 33) perf_out[3]++, perf_out[10] += (uint32_t)rd_tris, perf_out[11] += (uint32_t)rd_chunks;+ if (spent > 33) perf_out[3]++, perf_out[10] += (uint32_t)rd_tris, perf_out[11] += (uint32_t)rd_chunks, perf_out[14] += t1 - t0, perf_out[15] += (uint32_t)car.ncache;if (rd_detail < 3) perf_out[rd_detail == 2 ? 9 : rd_detail == 1 ? 12 : 13]++;else perf_out[4 + rd_detail - 3]++;rd_adapt(spent);+ rd_ease = 0;dt = spent > 33 ? spent : 33; /* (the game's frames: 33 ms at least on these calculators) */}perf_out[8] = car.finish;diff --git a/games/polytrack/tools/armperf.py b/games/polytrack/tools/armperf.pyindex 809cac2e..ac0dece4 100644--- a/games/polytrack/tools/armperf.py+++ b/games/polytrack/tools/armperf.py@@ -38,7 +38,7 @@ def main():det = "/".join(str(x) for x in [o[13], o[12], o[9]] + list(o[4:8]))print(f"{NAMES[t]:9}: {frames:5} frames, {work / max(frames, 1):5.1f} ms a frame, slowest {worst:3} ms, "f"{over:3} over 33 ms (quality 0-6: {det}), finish {o[8]} ms" +- (f", slow ones: {o[10] // over} triangles, {o[11] // over} chunks" if over else "") + ("" if done else ", NOT done"))+ (f", slow ones: {o[10] // over} triangles, {o[11] // over} chunks, {o[14] / over:.1f} ms of physics, {o[15] // over} cached triangles" if over else "") + ("" if done else ", NOT done"))bad += over > 0 or not donesys.exit(1 if bad else 0)