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
Temple Run: the game's own intro, near plane, clip speeds and steadier far off
Mason363cff8cd984e6b
diff --git a/games/templerun/src/app.c b/games/templerun/src/app.cindex 80a93cec..191df348 100644--- a/games/templerun/src/app.c+++ b/games/templerun/src/app.c@@ -119,8 +119,11 @@ static void store_buy(void) {say("Bought");}+static void board_step(void);+void app_step(uint32_t keys, uint32_t pressed) {state_t += 1.0f / 60;+ scene_tick();if (note_t > 0) note_t -= 1.0f / 60;if (pressed & K_HOME) {if (state == S_RUN || state == S_PAUSE || state == S_QUIT) end_run(); /* (the results not shown yet) */@@ -169,6 +172,7 @@ void app_step(uint32_t keys, uint32_t pressed) {uint16_t before = (uint16_t)(pg.use[0] + pg.use[1] + pg.use[2]);game_step(in);scene_follow();+ board_step();if (pg.use[0] + pg.use[1] + pg.use[2] != before) save_write();if (game_over()) {end_run();@@ -242,18 +246,17 @@ static void button(int x, int y, int w, int h, const char *s, bool focus) {static int board_dist;static float board_t = -1;-static void board(void) {+static void board_step(void) { /* (a 60th of a second of the run) */int d = (int)run.dist;if (d < board_dist) board_dist = 0;int next = board_dist < 1000 ? board_dist + 250 : board_dist + 500;if (d >= next) board_dist = next, board_t = 0;+ else if (board_t >= 0 && (board_t += 1.0f / 60) >= 2.5f) board_t = -1;+}++static void board(void) {if (board_t < 0) return;- board_t += 1.0f / 30;- float t = board_t, down = t < 0.25f ? t * 4 : t < 2.25f ? 1 : t < 2.5f ? (2.5f - t) * 4 : -1;- if (down < 0) {- board_t = -1;- return;- }+ float t = board_t, down = t < 0.25f ? t * 4 : t < 2.25f ? 1 : (2.5f - t) * 4;const HudPic *bp = &hud_pics[HP_BOARD];int y = -bp->h + (int)(down * (bp->h - 34));d_pic(HP_BOARD, (SCREEN_W - bp->w) / 2, y, 255, 0, 0);@@ -410,8 +413,7 @@ static void quit_overlay(void) {d_dim(150);parchment(50, 64, 220, 110);const char *q = quit_new ? "Start a new run?" : "Quit this run?";- d_text(&font_mid, 160 - d_text_width(&font_mid, q) / 2, 92, q, C(0x3A2208), 255);- d_text(&font_small, 160 - d_text_width(&font_small, "Your coins are kept.") / 2, 112, "Your coins are kept.", C(0x5A3A14), 255);+ d_text(&font_mid, 160 - d_text_width(&font_mid, q) / 2, 100, q, C(0x3A2208), 255);button(70, 128, 80, 28, "No", sel == 0);button(170, 128, 80, 28, "Yes", sel == 1);keys_line("Left Right choose - OK select");diff --git a/games/templerun/src/game.c b/games/templerun/src/game.cindex d393185b..84c99bad 100644--- a/games/templerun/src/game.c+++ b/games/templerun/src/game.c@@ -93,7 +93,7 @@ static void pursue(void) {else if (yaw < -1) c = 0, s = 1;else if (yaw > 2) c = -1, s = 0;p->x = x + side[i] * c, p->z = z + side[i] * s, p->y = y, p->yaw = yaw;- p->phase += DT * (pl.dead ? 2.0f : pl.v / 14.0f);+ p->phase += DT / 0.5667f; /* (their Run clip: 0.5667 s, played as it is) */}}@@ -154,16 +154,18 @@ static void stumble(void) {run.stumbles++;}-static void jump(void) {- if (pl.jumping || pl.stumbling || pl.falling || !pl.on_ground) return;+static bool jump(void) {+ if (pl.jumping || pl.stumbling || pl.falling || !pl.on_ground) return false;pl.vy = pl.v < 80 ? 80 : pl.v > 100 ? 100 : pl.v;pl.jumping = true, pl.sliding = false, pl.on_ground = false;+ return true;}-static void slide(void) {- if (pl.sliding || pl.jumping || pl.falling) return;+static bool slide(void) {+ if (pl.sliding || pl.jumping || pl.falling) return false;pl.sliding = true, pl.t_slide = 0;pl.slide_len = minf(pl.v * 0.007f, 0.75f);+ return true;}/* ResurrectPlayer: the wings bring it back on the next quiet piece ahead (false: none near) */@@ -172,8 +174,8 @@ static bool resurrect(void) {for (int k = 0; k < 10 && i >= 0; k++) {const Tile *t = &tiles[i];if (k && !t->has_obstacles && !path_is_turn(t) && t->mesh >= 0) {- for (int j = 0; j < n; j++) path_advance(chain[j]);- path_advance(i);+ for (int j = 0; j < n; j++) path_advance(chain[j], true);+ path_advance(i, true);pl.x = path_cx(t) - 20 * DX[t->dir], pl.z = path_cz(t) - 20 * DZ[t->dir];pl.y = pl.ground = t->y, pl.vy = 0, pl.dir = t->dir;pl.death_v = pl.v > START_V ? pl.v : 0;@@ -354,12 +356,21 @@ void game_step(uint32_t in) {pursue();return;}- /* keys */+ /* the intro: he holds still, then runs out of the temple; keys count once it is over */+ if (run.time <= INTRO_HOLD) {+ pl.anim += DT * pl.v * (0.65f / 100 / 0.4f);+ pursue();+ return;+ }+ if (run.time <= INTRO) in = 0;+ /* keys (a jump or a slide that cannot be made yet is kept half a second, a turn a second: the game's) */if (in & (IN_LEFT | IN_RIGHT)) pl.turn_wish = in & IN_LEFT ? -1 : 1, pl.t_wish = 0;if (pl.turn_wish && (pl.t_wish += DT) > 1) pl.turn_wish = 0;pl.lean = in & IN_LEAN_L ? -6.0f : in & IN_LEAN_R ? 6.0f : 0.0f;- if (in & IN_JUMP) jump();- if (in & IN_SLIDE) slide();+ if (in & IN_JUMP) pl.jump_wish = 0.5f;+ if (in & IN_SLIDE) pl.slide_wish = 0.5f;+ if (pl.jump_wish > 0) pl.jump_wish = jump() ? 0 : pl.jump_wish - DT;+ if (pl.slide_wish > 0) pl.slide_wish = slide() ? 0 : pl.slide_wish - DT;if ((in & IN_WINGS) && game_wings_ok()) {pg.use[USE_WINGS]--;pl.wings = 30;@@ -405,7 +416,7 @@ void game_step(uint32_t in) {pl.y += pl.vy * DT;/* the piece under it (the root moves on, what is behind and the ways not taken pruned, the path laid on) */int ni = path_at(pl.x, pl.z);- if (ni >= 0 && ni != path_root) path_advance(ni), ni = path_root;+ if (ni >= 0 && ni != path_root) path_advance(ni, run.time > INTRO), ni = path_root;t = ni >= 0 ? &tiles[ni] : NULL;/* over ground: inside the piece's ground, or invisible (then over everything) */bool inv = pl.invis > 0, over = inv || (t && path_ground(t, pl.x, pl.z));@@ -471,7 +482,7 @@ void game_step(uint32_t in) {path_pace(run.dist, pl.since_power, d);}collect();- pl.anim += DT * pl.v / 22.0f;+ pl.anim += DT * pl.v * (0.65f / 100 / 0.4f); /* (its Run clip: 0.4 s, played at 0.65 x speed / 100) */trail_gap += pl.v * DT;if (trail_gap >= 2) trail_gap = 0, trail_add();pursue();diff --git a/games/templerun/src/game.h b/games/templerun/src/game.hindex 59fbd2e3..23bb82bd 100644--- a/games/templerun/src/game.h+++ b/games/templerun/src/game.h@@ -35,7 +35,8 @@ typedef struct {float since_power;int turn_wish; /* -1 left, 1 right: pressed before the corner (kept 1 s) */float t_wish;- float anim; /* the run cycle */+ float jump_wish, slide_wish; /* seconds a jump or a slide pressed too soon is still kept (the game's 0.5) */+ float anim; /* the run cycle (its Run clip: 1 a cycle) */} Runner;typedef struct {@@ -83,14 +84,17 @@ extern Fly fly[NFLY];/* the pursuers: where each one is (feet) and the way it faces (radians), whether they are seen */typedef struct {- float x, y, z, yaw, phase;+ float x, y, z, yaw, phase; /* phase: their Run clip (1 a cycle) */} Pursuer;#define NPURSUERS 3extern Pursuer pursuers[NPURSUERS];extern bool pursuers_on;void game_new_run(void);-/* a 60th of a second: keys held (lean) and pressed (turn, jump, slide) */+/* a 60th of a second: keys held (lean) and pressed (turn, jump, slide). A run starts with the game's intro: the+ * runner holds still for INTRO_HOLD seconds, and keys count from INTRO on */+#define INTRO_HOLD 1.0f+#define INTRO 3.0fenum { IN_LEFT = 1, IN_RIGHT = 2, IN_JUMP = 4, IN_SLIDE = 8, IN_LEAN_L = 16, IN_LEAN_R = 32, IN_WINGS = 64,IN_HEADSTART = 128, IN_MEGA = 256 };void game_step(uint32_t in);diff --git a/games/templerun/src/path.c b/games/templerun/src/path.cindex ab354e12..55ff0e7e 100644--- a/games/templerun/src/path.c+++ b/games/templerun/src/path.c@@ -442,12 +442,12 @@ void path_start(void) {grow(i, DEPTH, root_origin);}-void path_advance(int to) {+void path_advance(int to, bool prune) {int r = path_root, d = -1;for (int k = 0; k < 4; k++)if (tiles[r].next[k] == to) d = k;if (d < 0) return;- for (int k = 0; k < 4; k++)+ for (int k = 0; k < 4 && prune; k++)if (k != d && tiles[r].next[k] >= 0) {destroy(tiles[r].next[k], OPP(k));tiles[r].next[k] = -1;diff --git a/games/templerun/src/path.h b/games/templerun/src/path.hindex b7e6ad8e..642c425c 100644--- a/games/templerun/src/path.h+++ b/games/templerun/src/path.h@@ -48,7 +48,8 @@ extern int path_root; /* the piece under the runner *//* the game's pacing (GameController's HandlePacing): by distance run, the time since the last power-up */void path_pace(float dist, float since_power, float dd);void path_start(void); /* the temple, and the path from it */-void path_advance(int to); /* the runner is on the piece next to the root: the rest pruned, laid on */+/* the runner is on the piece next to the root: the rest pruned (not in the intro: the game keeps it all then), laid on */+void path_advance(int to, bool prune);int path_at(float x, float z); /* the piece at a point: the root or one next to it (-1: none) */bool path_ground(const Tile *t, float x, float z); /* inside the piece's ground */bool path_opens(const Tile *t, int side);diff --git a/games/templerun/src/r3d.c b/games/templerun/src/r3d.cindex d17ddb06..2c5e6bbc 100644--- a/games/templerun/src/r3d.c+++ b/games/templerun/src/r3d.c@@ -1,9 +1,9 @@/* The 3D renderer (r3d.h): polygons in a list, drawn band by band with a depth buffer. */#include "r3d.h"-#define NEAR 0.35f+#define NEAR R_NEAR#define FAR 190.0f-#define ZK 22000.0f /* depths are ZK / z: 62857 at the near plane, larger is nearer */+#define ZK 62000.0f /* depths are ZK / z: 65263 at the near plane, larger is nearer */#define MAXP 1800 /* polygons a frame */#define MAXV 6000 /* their corners */#define MAXT 760 /* textured faces in perspective */@@ -39,10 +39,11 @@ int r_y0, r_y1;Camera cam;float r_fog0 = 19, r_fog1 = 76, r_far = 40;uint32_t r_fog_rgb = 0x325256;-/* the fog by depth: its amount (0..32) for depths d >> 2, below fog_d0 (nearer than r_fog0: none) */-#define FOG_N 640+/* the fog by depth: its amount (0..32) for depths d >> 3, below fog_d0 (nearer than r_fog0: none; farther than r_fog1,+ * below fog_d1: all) */+#define FOG_N 512static uint8_t fogtab[FOG_N];-static uint16_t fog_d0, fog565;+static uint16_t fog_d0, fog_d1, fog565;static float fog_made0, fog_made1;/* the frame's lists: in the RAM after the game's own (r_init) */@@ -633,45 +634,75 @@ static void fog_make(void) {if (fog_made0 == r_fog0 && fog_made1 == r_fog1) return;fog_made0 = r_fog0, fog_made1 = r_fog1;float d0 = ZK / r_fog0;- fog_d0 = d0 >= 4 * FOG_N ? 4 * FOG_N - 1 : (uint16_t)d0;+ fog_d0 = d0 >= 8 * FOG_N ? 8 * FOG_N - 1 : (uint16_t)d0;+ fog_d1 = 0;for (int i = 0; i < FOG_N; i++) {- float z = ZK / ((float)i * 4 + 2), t = (z - r_fog0) / (r_fog1 - r_fog0);+ float z = ZK / ((float)i * 8 + 4), t = (z - r_fog0) / (r_fog1 - r_fog0);fogtab[i] = (uint8_t)(t <= 0 ? 0 : t >= 1 ? 32 : (int)(t * 32 + 0.5f));+ if (fogtab[i] == 32) fog_d1 = (uint16_t)(i * 8 + 8);}}-/* a band's pixels toward the fog's colour, each by its depth (the background's too: the sky, depth 0, is all fog) */-static void fog_band(void) {- uint16_t fc = fog565, d0 = fog_d0;- uint32_t y = (fc | (uint32_t)fc << 16) & 0x07E0F81F; /* (r_mix565 with the fog's colour spread once) */- for (int i = 0; i < SCREEN_W * BAND_H; i++) {- uint16_t d = zbuf[i];- if (d >= d0) continue;- int t = fogtab[d >> 2];- if (t >= 32) r_col[i] = fc;- else if (t) {- uint32_t c = r_col[i], x = (c | c << 16) & 0x07E0F81F;- x = (x + ((y - x) * (uint32_t)t >> 5)) & 0x07E0F81F;- r_col[i] = (uint16_t)(x | x >> 16);- }- }-}--uint16_t r_floor_depth(float z) {+static uint16_t r_floor_depth(float z) {float d = ZK / z;d -= d * (1.0f / 256); /* (a hair farther: a shadow lying on it is drawn, what is under it is not) */return d >= 65535 ? 65535 : d <= 0 ? 0 : (uint16_t)d;}-void r_band_depth(int y, int x0, int x1, uint16_t d) {- uint16_t *zb = zbuf + (y - r_y0) * SCREEN_W;- for (int x = x0; x < x1; x++) zb[x] = d;+/* after a band's polygons: the ground (its picture, fogged as its row's depth has it) and the sky where none lies+ * nearer, and every other pixel toward the fog's colour by its depth; one pass over the band */+static void back_band(const Texture *tex, r_ground_fn ground) {+ uint16_t fc = fog565, d0 = fog_d0, d1 = fog_d1;+ uint32_t fs = (fc | (uint32_t)fc << 16) & 0x07E0F81F; /* (r_mix565 with the fog's colour spread once) */+ const uint32_t *sp = tex ? spread(tex) : NULL;+ for (int y = r_y0; y < r_y1; y++) {+ uint16_t *zb = zbuf + (y - r_y0) * SCREEN_W, *cb = r_col + (y - r_y0) * SCREEN_W;+ RGround g = {0, 0, 0, 0, 0};+ if (ground && sp) ground(y, &g);+ uint16_t dg = g.z > 0 && g.z < r_fog1 ? r_floor_depth(g.z) : 0;+ int tg = dg >= d0 ? 0 : fogtab[dg >> 3];+ if (!dg || tg >= 32) {+ /* the sky (or the ground all in the fog): the fog's colour where nothing is drawn */+ for (int x = 0; x < SCREEN_W; x++) {+ uint16_t d = zb[x];+ if (d >= d0) continue;+ if (d < d1) {+ cb[x] = fc;+ continue;+ }+ uint32_t c = cb[x], k = (c | c << 16) & 0x07E0F81F;+ k = (k + ((fs - k) * (uint32_t)fogtab[d >> 3] >> 5)) & 0x07E0F81F;+ cb[x] = (uint16_t)(k | k >> 16);+ }+ continue;+ }+ /* the ground's texel at x: (v >> vs & hm) | (u >> 16 & wm), as raster_row finds them; fogged by its row */+ const uint8_t *px = tex->px;+ uint32_t wm = (1u << tex->wl) - 1, hm = ((1u << tex->hl) - 1) << tex->wl, keep = (uint32_t)(32 - tg), add = fs * (uint32_t)tg;+ int vs = 16 - tex->wl;+ for (int x = 0; x < SCREEN_W; x++) {+ uint16_t d = zb[x];+ if (d < dg) {+ uint32_t u = (uint32_t)(g.u + g.du * x), v = (uint32_t)(g.v + g.dv * x);+ uint32_t k = ((sp[px[(v >> vs & hm) | (u >> 16 & wm)]] * keep + add) >> 5) & 0x07E0F81F;+ cb[x] = (uint16_t)(k | k >> 16), zb[x] = dg;+ } else if (d < d0) {+ if (d < d1) {+ cb[x] = fc;+ continue;+ }+ uint32_t c = cb[x], k = (c | c << 16) & 0x07E0F81F;+ k = (k + ((fs - k) * (uint32_t)fogtab[d >> 3] >> 5)) & 0x07E0F81F;+ cb[x] = (uint16_t)(k | k >> 16);+ }+ }+ }}-void r_render(r_band_fn bg, r_band_fn overlay) {+void r_render(const Texture *tex, r_ground_fn ground, r_band_fn overlay) {fog_make();fog565 = r_565(r_fog_rgb);- /* the see-through polygons after the others, in the order they came */+ /* the see-through polygons after the others (and after the ground: they are seen over it), in the order they came */if (nalpha) {int k = 0;for (int i = 0; i < npolys; i++)@@ -683,17 +714,16 @@ void r_render(r_band_fn bg, r_band_fn overlay) {r_y0 = band * BAND_H, r_y1 = r_y0 + BAND_H;/* (cleared a word at a time: the C library's is a byte at a time) */for (uint32_t *p = (uint32_t *)(void *)zbuf, *e = p + sizeof zbuf / 4; p < e; p += 4) p[0] = p[1] = p[2] = p[3] = 0;- if (bg) bg();- else- for (int i = 0; i < SCREEN_W * BAND_H; i++) r_col[i] = 0;+ int opaque = npolys - nalpha;for (int i = 0; i < npolys; i++) {+ if (i == opaque) back_band(tex, ground);const Poly *q = &polys[order[i]];if (q->y1 > r_y0 && q->y0 < r_y1) {if (q->n == 3 && (q->kind == K_FLAT || q->kind == K_PERSP)) raster_tri(q);else raster(q);}}- fog_band();+ if (opaque == npolys) back_band(tex, ground);if (overlay) overlay();plat_push(0, r_y0, SCREEN_W, BAND_H, r_col);}diff --git a/games/templerun/src/r3d.h b/games/templerun/src/r3d.hindex dab4b2bd..991acb80 100644--- a/games/templerun/src/r3d.h+++ b/games/templerun/src/r3d.h@@ -44,7 +44,7 @@ static inline V3 r_view(V3 p) {}/* a mesh's corner moved once for all its triangles: camera space, and where it lies on the screen from its middle* (when it is in front of the near plane, R_NEAR) */-#define R_NEAR 0.35f+#define R_NEAR 0.95f /* (the game's camera: 5 units) */typedef struct {float x, y, z, sx, sy;} SV;@@ -120,13 +120,16 @@ __attribute__((always_inline)) static inline uint16_t r_mix565(uint16_t a, uint1return (uint16_t)(x | x >> 16);}-/* draws the frame: bg fills each band's colours (sky, ground), the polygons go over it, then- * overlay draws flat things over the band (both may be NULL) */+/* the ground far below, seen where no polygon lies nearer: for row y, where the row meets it (z metres ahead, 0: it+ * does not, the sky) and its picture's texel there at the row's first pixel and the step a pixel (16.16) */+typedef struct {+ float z;+ int32_t u, v, du, dv;+} RGround;+typedef void (*r_ground_fn)(int y, RGround *g);+/* draws the frame, band by band: the polygons, then the ground (tex, as ground finds it) and the sky where none lies+ * nearer, all fogged by depth, the see-through polygons over that, and overlay draws flat things over the band (ground+ * and overlay may be NULL) */typedef void (*r_band_fn)(void);-void r_render(r_band_fn bg, r_band_fn overlay);-/* for bg: a surface it draws hides what is behind it. Row y of the band, pixels [x0, x1), lies at depth d:- * r_floor_depth(z) for a floor seen at z metres ahead of the camera (a hair farther, so that what lies on it,- * a shadow, is still drawn) */-uint16_t r_floor_depth(float z);-void r_band_depth(int y, int x0, int x1, uint16_t d);+void r_render(const Texture *tex, r_ground_fn ground, r_band_fn overlay);#endifdiff --git a/games/templerun/src/scene.c b/games/templerun/src/scene.cindex 61b4ba1b..3fa1401c 100644--- a/games/templerun/src/scene.c+++ b/games/templerun/src/scene.c@@ -18,14 +18,17 @@#define WATER_TILE (2000.0f / 30)#define SMALL_FAR 350.0f /* rocks, heads and ground patches seen this far */#define TREE_FAR FOG1 /* the rest of the scenery (past it, all fog) */-#define SCENERY_POLYS 1080 /* (of the renderer's 1600 a frame) */+#define SCENERY_POLYS 1080 /* the polygons a frame the scenery's reach is kept to (of the renderer's 1800) */+#define LOD_NEAR 95.0f /* path pieces nearer than this are whole */#define VIEW (FOG1 + 20) /* units seen ahead */#define PI 3.14159265fstatic float cx_, cy_, cz_, cyaw, cc, cs; /* the camera (game units) and its yaw's cosine and sine */static float run_yaw, cam_h; /* the run's way, smoothed; the camera's ground height */static float camx, camy, camz; /* the camera's place in a run (the game's GameCamera) */-static float intro; /* seconds the camera still waits where the title had it */+static float intro_t; /* seconds into the run's intro (INTRO: over) */+static float start_x, start_y, start_z; /* the temple's door, where the intro's places are measured from */+static float look_off; /* the title's turn of the view, gone in the intro's first quarter second */static float t_anim;static uint32_t scenery_seed; /* (the scenery of this run: draw_scenery) */static bool title;@@ -49,9 +52,12 @@ static float atan2_(float y, float x) {static float dir_yaw(int d) { return d == 1 ? PI / 2 : d == 2 ? PI : d == 3 ? -PI / 2 : 0; }+static void reach_make(void);+void scene_init(void) {r_fog0 = FOG0 * S, r_fog1 = FOG1 * S, r_fog_rgb = FOG_RGB;r_far = 40;+ reach_make();}/* the game's camera: 50 behind the runner and 35 above the ground it runs on, looking at 20 above it (sliding under@@ -65,19 +71,36 @@ static void cam_want(float *x, float *y, float *z, float *fy) {*x = pl.x - s * d, *z = pl.z - c * d, *y = cam_h + h, *fy = cam_h + f;}+/* the game's intro (its GameStart): the camera goes in a quarter of a second from where the title had it to the+ * game's menu place, 86 before the temple and 10 up, then in 2.5 s, easing in and out, to 20 left, 45 up and 210+ * before it, looking at the runner all the while (he holds still the first second, then runs out under it) */+static void intro_camera(float t) {+ static const float at[3][3] = {{0, 10, 100}, {0, 10, 86}, {-20, 45, 210}};+ int k = t < 0.25f ? 0 : 1;+ float u = k ? (t - 0.25f) / 2.5f : t / 0.25f, e;+ if (u > 1) u = 1;+ if (k == 0) e = (u - 1) * (u - 1) * (u - 1) + 1; /* (MovingObject's easing: out, then in and out) */+ else e = u < 0.5f ? 4 * u * u * u : 0.5f * ((2 * u - 2) * (2 * u - 2) * (2 * u - 2) + 2);+ camx = start_x + at[k][0] + (at[k + 1][0] - at[k][0]) * e;+ camy = start_y + at[k][1] + (at[k + 1][1] - at[k][1]) * e;+ camz = start_z + at[k][2] + (at[k + 1][2] - at[k][2]) * e;+ look_off = k ? 0 : -0.28f * (1 - e);+}+/* a 60th of a second: the camera goes after the runner (when it's caught or falls, the camera stays) */void scene_follow(void) {float want = dir_yaw(pl.dir);run_yaw += wrap(want - run_yaw) * 0.3f;if (pl.dead) return;- if (intro > 0) {- intro -= 1.0f / 60;- return;- }if (!pl.falling) {float g = pl.ground;cam_h = cam_h < g ? (cam_h + 50.0f / 60 > g ? g : cam_h + 50.0f / 60) : (cam_h - 50.0f / 60 < g ? g : cam_h - 50.0f / 60);}+ if (intro_t < INTRO) {+ intro_t += 1.0f / 60;+ intro_camera(intro_t);+ if (intro_t < INTRO) return;+ }float tx, ty, tz, fy;cam_want(&tx, &ty, &tz, &fy);float dx = tx - camx, dy = ty - camy, dz = tz - camz, m2 = dx * dx + dy * dy + dz * dz, step = pl.v * 1.25f / 60;@@ -92,20 +115,49 @@ void scene_reset(void) {scenery_seed = plat_random();run_yaw = dir_yaw(pl.dir);cam_h = pl.ground;- /* the game's menu camera: 100 before the temple and 10 up, looking at the runner in its door; a run starts with it- * there for 3 seconds (the game's intro scene), the runner running out at it, and then it goes after him */+ /* the title's camera: 100 before the temple and 10 up, looking at the runner in its door (turned a little); a run+ * starts from there (intro_camera) */const Tile *t = &tiles[path_root];- camx = path_cx(t), camz = path_cz(t) + 100, camy = cam_h + 10;- intro = 3;+ start_x = path_cx(t), start_y = cam_h, start_z = path_cz(t);+ intro_t = 0;+ intro_camera(0);}+bool scene_intro(void) { return intro_t < INTRO; }++void scene_tick(void) { t_anim += 1.0f / 60; }+/* ---------------------------------------------------------------- meshes */+/* each mesh's reach across (units, from its middle; its height left out): behind the camera, what lies farther than that+ * is not seen (it never looks down so far that what is behind it shows) */+static uint8_t reach[M_COUNT];++static void reach_make(void) {+ for (int m = 0; m < M_COUNT; m++) {+ const Mesh *me = &meshes[m];+ float r2 = 0;+ for (int k = 0; k < 3 * me->nt; k++) {+ const MVert *v = &me->v[me->tidx[k]];+ float x = v->x * 0.25f, z = v->z * 0.25f;+ if (x * x + z * z > r2) r2 = x * x + z * z;+ }+ for (int i = 0; i < me->nf; i++)+ for (int k = me->f[i].first; k < me->f[i].first + me->f[i].n; k++) {+ const MVert *v = &me->v[me->idx[k]];+ float x = v->x * 0.25f, z = v->z * 0.25f;+ if (x * x + z * z > r2) r2 = x * x + z * z;+ }+ float r = __builtin_sqrtf(r2) + 1;+ reach[m] = (uint8_t)(r > me->r ? me->r : r > 255 ? 255 : r);+ }+}+void scene_mesh(int m, float ox, float oy, float oz, float yaw, int alpha) {float dx = ox - cx_, dy = oy - cy_, dz = oz - cz_;float x0 = dx * cc - dz * cs, z0 = dx * cs + dz * cc;const Mesh *me = &meshes[m];- float r = me->r;- if (z0 + r < 0 || z0 - r > VIEW) return;+ float r = me->r, h = (float)reach[m];+ if (z0 + h < 0 || z0 - h > VIEW) return;if (x0 - r > z0 * 1.6f + 8 || -x0 - r > z0 * 1.6f + 8) return;if (me->far >= 0 && z0 > MESH_FAR) me = &meshes[me->far];float s, c;@@ -141,13 +193,17 @@ void scene_mesh(int m, float ox, float oy, float oz, float yaw, int alpha) {V3 nn = {nx * c + nz * s, ny, -nx * s + nz * c};r_poly_view(p, n, r_shade(f->rgb, nn), flags | (f->flags & 1 ? P_TWOSIDED : 0), alpha);}- /* (a path piece's far version has its faces biggest first: farther off, only those; scenery stays whole, as its- * leaves and roots would be left in the air) */+ /* (a path piece has its faces biggest first: past LOD_NEAR, only the first of them, fewer as it is farther, so that+ * coming nearer it gains them one by one, the biggest first; by its distance, not by the way the camera looks, so a+ * turn of the camera changes none. Scenery stays whole, as its leaves and roots would be left in the air) */int nt = me->nt;if (m < 2 * M_PIECES) {- if (z0 > 300) nt /= 5;- else if (z0 > 160) nt /= 3;- else if (z0 > MESH_FAR) nt /= 2;+ float d2 = dx * dx + dz * dz;+ if (d2 > LOD_NEAR * LOD_NEAR) {+ float q = LOD_NEAR / __builtin_sqrtf(d2); /* (q^1.5 of them: as many a frame as the steps of halves, thirds and+ fifths it replaces) */+ nt = (int)((float)nt * q * __builtin_sqrtf(q));+ }}for (int i = 0; i < nt; i++) {const TFace *t = &me->t[i];@@ -211,6 +267,8 @@ static uint32_t mix(uint32_t h) {return h;}+static float scenery_far = TREE_FAR; /* how far the scenery is drawn: it moves slowly, with how busy frames are */+static void draw_scenery(void) {static const uint8_t trees[4] = {M_treeAlone, M_treeAlone2, M_treeAlone3, M_treeAlone4};static const uint8_t rocks[3] = {M_rock, M_rock2, M_rock3}, cliffs[3] = {M_cliff, M_cliff2, M_cliff3};@@ -223,12 +281,13 @@ static void draw_scenery(void) {int a = tiles[i].gx - x0, b = tiles[i].gz - z0;if (a >= 0 && b >= 0 && a < SGRID && b < SGRID) mask[a][b] = 1;}- /* nearest first, ring by ring round the camera's square, and no more once the frame has SCENERY_POLYS polygons: on a- * busy frame what goes is a tree far off */+ /* nearest first, ring by ring round the camera's square, as far as scenery_far: a piece of scenery is there or not by+ * where it is, never by how many polygons came before it, so none comes and goes from one frame to the next. Past+ * SCENERY_POLYS a frame, scenery_far comes a little nearer each frame, and goes back out when there is room */+ float far2 = scenery_far * scenery_far;for (int ring = 0; ring <= SGRID / 2; ring++)for (int a = SGRID / 2 - ring; a <= SGRID / 2 + ring; a++)for (int b = SGRID / 2 - ring; b <= SGRID / 2 + ring; b += (a == SGRID / 2 - ring || a == SGRID / 2 + ring) ? 1 : 2 * ring) {- if (r_count() >= SCENERY_POLYS) return;int cx = x0 + a, cz = z0 + b;if (mask[a][b] || (cx >= -1 && cx <= 1 && cz >= 0 && cz <= 4)) continue;uint32_t h = mix((uint32_t)cx * 0x9E3779B1u ^ mix((uint32_t)cz + scenery_seed));@@ -236,49 +295,41 @@ static void draw_scenery(void) {uint32_t k = (h >> 10) % 21, v = h >> 15;int m = k <= 10 ? trees[v % 4] : k <= 14 ? rocks[v % 3] : k <= 16 ? cliffs[v % 3] : k <= 18 ? M_overgrowthTrees : k == 19 ? M_templeHead : M_groundPatchTemple;float ox = (float)((int)((h >> 20) % 21) - 10), oz = (float)((int)((h >> 25) % 21) - 10);- float wx = (float)cx * TILE + ox, wz = (float)cz * TILE + oz, dx = wx - cx_, dz = wz - cz_;+ float wx = (float)cx * TILE + ox, wz = (float)cz * TILE + oz, dx = wx - cx_, dz = wz - cz_, d2 = dx * dx + dz * dz;/* (the small ones, a few pixels in the haze past SMALL_FAR, aren't drawn there) */- if (k >= 11 && k <= 14 && dx * dx + dz * dz > SMALL_FAR * SMALL_FAR) continue;- if (k >= 19 && dx * dx + dz * dz > SMALL_FAR * SMALL_FAR) continue;- if (dx * dx + dz * dz > TREE_FAR * TREE_FAR) continue;+ if ((k >= 11 && k <= 14) || k >= 19) {+ if (d2 > SMALL_FAR * SMALL_FAR) continue;+ }+ if (d2 > far2) continue;scene_mesh(m, wx, 0, wz, 0, 255);}+ int n = r_count();+ if (n > SCENERY_POLYS && scenery_far > 200) scenery_far -= 3;+ else if (n < SCENERY_POLYS - 80 && scenery_far < TREE_FAR) scenery_far += 1.5f;}/* ---------------------------------------------------------------- the sky and the depths */-/* the background, row by row: the jungle far below (the game's Water Plane) where the row looks down at it, its- * picture fixed on the ground and its depth there; the fog's colour above (r3d.c fogs both by depth) */-static void bg(void) {+/* the jungle far below (the game's Water Plane), where the row looks down at it: its picture fixed on the ground, its+ * depth there (r3d.c draws it where nothing lies nearer, fogged; the fog's colour above) */+static void ground(int y, RGround *g) {const Texture *t = r_tex[2];- const uint8_t *px = t->px;- const uint16_t *pal = t->pal;- uint16_t fog = r_565(FOG_RGB);float hgt = (WATER_Y - cy_) * S, k = (float)(1 << t->wl) / (WATER_TILE * S); /* (texels a metre) */- uint32_t m = (1u << t->wl) - 1;- for (int y = r_y0; y < r_y1; y++) {- uint16_t *p = r_col + (y - r_y0) * SCREEN_W;- float vs = ((float)y + 0.5f - cam.cy) / cam.f;- float ry = -vs * cam.cp - cam.sp, rz = -vs * cam.sp + cam.cp; /* (a ray down the row's middle, z ahead 1) */- float tz = ry < -1e-4f ? hgt / ry : 1e9f; /* its depth where it meets the jungle */- if (tz > r_fog1) {- for (int x = 0; x < SCREEN_W; x++) p[x] = fog;- continue;- }- /* along the row: the ground's place at x, straight lines in the picture */- float ax = (0.5f - cam.cx) / cam.f * tz, az = rz * tz, sx = tz / cam.f;- float u0 = ((cx_ * S) + ax * cc + az * cs) * k, v0 = ((cz_ * S) - ax * cs + az * cc) * k;- int32_t u = (int32_t)(u0 * 65536.0f), v = (int32_t)(v0 * 65536.0f);- int32_t du = (int32_t)(sx * cc * k * 65536.0f), dv = (int32_t)(-sx * cs * k * 65536.0f);- for (int x = 0; x < SCREEN_W; x++, u += du, v += dv)- p[x] = pal[px[(((uint32_t)v >> 16) & m) << t->wl | (((uint32_t)u >> 16) & m)]];- r_band_depth(y, 0, SCREEN_W, r_floor_depth(tz));- }+ float vs = ((float)y + 0.5f - cam.cy) / cam.f;+ float ry = -vs * cam.cp - cam.sp, rz = -vs * cam.sp + cam.cp; /* (a ray down the row's middle, z ahead 1) */+ if (ry >= -1e-4f) return;+ float tz = hgt / ry; /* its depth where it meets the jungle */+ if (tz > r_fog1) return;+ /* along the row: the ground's place at x, straight lines in the picture */+ float ax = (0.5f - cam.cx) / cam.f * tz, az = rz * tz, sx = tz / cam.f;+ float u0 = ((cx_ * S) + ax * cc + az * cs) * k, v0 = ((cz_ * S) - ax * cs + az * cc) * k;+ g->z = tz;+ g->u = (int32_t)(u0 * 65536.0f), g->v = (int32_t)(v0 * 65536.0f);+ g->du = (int32_t)(sx * cc * k * 65536.0f), g->dv = (int32_t)(-sx * cs * k * 65536.0f);}/* ---------------------------------------------------------------- the frame */void scene_draw(r_band_fn overlay, bool on_title) {title = on_title;- t_anim += 1.0f / 30;r_begin();/* the camera: the game's, behind the runner and above it, looking at it */float pitch;@@ -298,7 +349,7 @@ void scene_draw(r_band_fn overlay, bool on_title) {}cx_ = camx, cy_ = camy, cz_ = camz;float dx = fx - cx_, dz = fz - cz_, hd = __builtin_sqrtf(dx * dx + dz * dz);- cyaw = atan2_(dx, dz), pitch = atan2_(cy_ - fy, hd);+ cyaw = atan2_(dx, dz) + look_off, pitch = atan2_(cy_ - fy, hd);r_sincos(cyaw, &cs, &cc);}r_camera(0, 0, 0, pitch, FOCAL, 160, 120);@@ -339,5 +390,5 @@ void scene_draw(r_band_fn overlay, bool on_title) {}/* the scenery last: on a frame with more than the renderer keeps, what goes is a tree far off, not the runner */draw_scenery();- r_render(bg, overlay);+ r_render(r_tex[2], ground, overlay);}diff --git a/games/templerun/src/scene.h b/games/templerun/src/scene.hindex d67e818e..3709c8a0 100644--- a/games/templerun/src/scene.h+++ b/games/templerun/src/scene.h@@ -7,6 +7,8 @@void scene_init(void);void scene_reset(void); /* the camera straight behind the runner (a new run) */void scene_follow(void); /* a 60th of a second: the camera after the runner */+bool scene_intro(void); /* the run's intro is on (its camera; the path behind is kept) */+void scene_tick(void); /* a 60th of a second: what turns or burns (coins, fire) goes on *//* the frame: the camera after the runner (on the title: in front of it), the world, then overlay over it */void scene_draw(r_band_fn overlay, bool on_title);/* one mesh (data.h) at a point of the world, turned by yaw (0: its front toward +z); alpha < 255 see-through */diff --git a/games/templerun/tests/check.py b/games/templerun/tests/check.pyindex fa119f43..a78e2bdd 100644--- a/games/templerun/tests/check.py+++ b/games/templerun/tests/check.py@@ -135,8 +135,8 @@ def store(d):def wings_and_headstart(d):- # OK in a run: the wings; with no one playing the runner dies, and the wings bring it back- st = play(d, "--use", "3", "--frames", "1500", "--keys", "30:ok,90:ok,200:ok")+ # OK in a run (after its intro): the wings; with no one playing the runner dies, and the wings bring it back+ st = play(d, "--use", "3", "--frames", "1500", "--keys", "30:ok,90:ok,300:ok")assert st["saved"] >= 1, stassert save(d)["wings"] == 2, save(d)# 1 near the start: a head start, far ahead in seconds