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.

Subway Surfers: the tracks beside the runner no longer vanish

Mason36362215241fc6e
diff --git a/games/subway/src/r3d.c b/games/subway/src/r3d.c
index 284ee8f8..383d6231 100644
--- a/games/subway/src/r3d.c
+++ b/games/subway/src/r3d.c
@@ -581,6 +581,17 @@ static void raster(const Poly *q) {
}
}
+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;
+}
+
void r_render(r_band_fn bg, r_band_fn overlay) {
/* the see-through polygons after the others, in the order they came */
if (nalpha) {
diff --git a/games/subway/src/r3d.h b/games/subway/src/r3d.h
index 28f6c000..aac5644d 100644
--- a/games/subway/src/r3d.h
+++ b/games/subway/src/r3d.h
@@ -4,7 +4,8 @@
* things look round) and textured ones (a picture laid on, in perspective for big faces like a train's
* side). Each frame they are moved in front of the camera, cut at the edges of the view, projected and
* kept in a list; then the screen is drawn in bands of BAND_H rows, each one its background (sky and the
- * ground, scene.c), the polygons that cross it with a depth buffer, the see-through ones after them, and
+ * ground, scene.c, which leaves the ground's depth where it hides what is below it), the polygons that
+ * cross it with a depth buffer, the see-through ones after them, and
* what is drawn flat over the picture (ui). The whole screen never has to fit in RAM: a band and its
* depths do. */
#ifndef SS_R3D_H
@@ -117,4 +118,9 @@ __attribute__((always_inline)) static inline uint16_t r_mix565(uint16_t a, uint1
* overlay draws flat things over the band (both 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);
#endif
diff --git a/games/subway/src/scene.c b/games/subway/src/scene.c
index 5545dcf2..4da576cd 100644
--- a/games/subway/src/scene.c
+++ b/games/subway/src/scene.c
@@ -4,7 +4,9 @@
* across the row the ground goes by evenly. Its colours across the yard are the game's own track seen from above
* (data.c: the ballast, the sleepers, the rails), kept in tables, sharp ones near and blurred ones far (each half as
* wide as the one before), and a row only reads its table at a steady step: no flicker on the far rails. The
- * sleepers come one row in so many: rows that see part of one get a table with them mixed in.
+ * sleepers come one row in so many: rows that see part of one get a table with them mixed in. Like the game's track,
+ * the ground hides what is below it: the city's walls and floors go on down under the tracks (the game never shows
+ * that part), so each row leaves its depth where the tracks are, and the polygons are drawn only above it.
*
* The city, the trains and the barriers are the game's models baked into a few flat faces with their detail
* painted on (tools/bake.py): a stretch of city (SEG) at a time, its left and right sides, or a section (the
@@ -19,6 +21,7 @@
#define SLEEPER (15 * U) /* from one sleeper to the next */
#define SLEEPER_Z0 (5.6f * U) /* where one starts in that */
#define SLEEPER_W (4.4f * U)
+#define TRACKS (30 * U) /* the three tracks reach this far to each side (the table's ballast) */
enum { G_NONE, G_SLEEPER, G_MIX, G_KINDS };
static uint16_t ground[G_KINDS][GROUND_W * 2]; /* levels one after the other */
@@ -28,12 +31,14 @@ static uint16_t sky[SCREEN_H];
static float detail = 1;
static int horizon; /* the first row of ground */
-/* each row of ground: its table, where it starts and its step (16.16), its haze */
+/* each row of ground: its table (its level), where it starts and its step (16.16), its haze; the tracks' pixels
+ * and depth */
typedef struct {
const uint16_t *lut;
int32_t u, du;
- uint16_t n;
- uint8_t haze;
+ int16_t x0, x1;
+ uint16_t depth;
+ uint8_t level, haze;
} GroundRow;
static GroundRow rows[SCREEN_H];
@@ -127,11 +132,16 @@ void scene_camera(float x, float y, float z, float pitch, float f, float cy) {
while (step > 1.4f && l < LEVELS - 1) step *= 0.5f, l++;
float scale = (float)(32 >> l);
g->lut = ground[kind] + level_at[l];
- g->n = (uint16_t)(GROUND_W >> l);
+ g->level = (uint8_t)l;
g->u = (int32_t)(((cam.x + (0.5f - cam.cx) * zc / cam.f) - GROUND_X0) * scale * 65536);
g->du = (int32_t)(zc / cam.f * scale * 65536);
float h = (zc - r_haze0) / (r_haze1 - r_haze0);
g->haze = (uint8_t)(h <= 0 ? 0 : h >= 1 ? 240 : (int)(h * 240));
+ /* the pixels whose middles see the tracks */
+ float px = cam.f / zc, xa = cam.cx - 0.5f + (-TRACKS - cam.x) * px, xb = cam.cx - 0.5f + (TRACKS - cam.x) * px;
+ g->x0 = (int16_t)(xa <= 0 ? 0 : xa >= SCREEN_W ? SCREEN_W : (int)xa + ((float)(int)xa < xa));
+ g->x1 = (int16_t)(xb < 0 ? 0 : xb >= SCREEN_W - 1 ? SCREEN_W : (int)xb + 1);
+ g->depth = r_floor_depth(zc);
}
}
@@ -169,7 +179,7 @@ void scene_background(void) {
}
const GroundRow *g = &rows[y];
const uint16_t *lut = g->lut;
- uint32_t n = g->n;
+ uint32_t n = GROUND_W >> g->level;
if (g->haze) {
int t = (g->haze + 4) >> 3;
if (lut != hazed_lut || t != hazed_t) {
@@ -192,6 +202,7 @@ void scene_background(void) {
u += du * xa;
for (; x < xb; x++, u += du) p[x] = lut[(uint32_t)u >> 16];
for (; x < SCREEN_W; x++) p[x] = right;
+ if (g->x0 < g->x1) r_band_depth(y, g->x0, g->x1, g->depth);
}
}