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.

Celeste: the Celestial Resort's busiest rooms at the game's speed

Mason363112a50e07032
diff --git a/games/celeste/src/ch3.c b/games/celeste/src/ch3.c
index f384f42d..5b813033 100644
--- a/games/celeste/src/ch3.c
+++ b/games/celeste/src/ch3.c
@@ -140,12 +140,16 @@ static void sp_rect(uint16_t *strip, int sy0, int sy1, int x, int y, int w, int
int y0 = y < sy0 ? sy0 : y, y1 = y + h > sy1 ? sy1 : y + h;
if (x0 >= x1 || y0 >= y1 || a <= 0) return;
uint16_t pc = a >= 256 ? c : scale565(c, a);
+ uint32_t ia = 256 - (uint32_t)a, pr = pc >> 11, pg = (pc >> 5) & 63, pb = pc & 31;
for (int r = y0; r < y1; r++) {
uint16_t *d = strip + (r - sy0) * VIEW_W + x0;
if (a >= 256)
for (int i = x0; i < x1; i++) *d++ = c;
- else
- for (int i = x0; i < x1; i++, d++) *d = blend565(*d, pc, a);
+ else /* blend565, its color's channels worked out once (the clutter's backing covers most of 3A's busiest room) */
+ for (int i = x0; i < x1; i++, d++) {
+ uint32_t v = *d, cr = pr + (((v >> 11) * ia) >> 8), cg = pg + ((((v >> 5) & 63) * ia) >> 8), cb = pb + (((v & 31) * ia) >> 8);
+ *d = (uint16_t)((cr > 31 ? 31 : cr) << 11 | (cg > 63 ? 63 : cg) << 5 | (cb > 31 ? 31 : cb));
+ }
}
}
static void sp_line(uint16_t *strip, int sy0, int sy1, int x0, int y0, int x1, int y1, uint16_t c, int a) {
@@ -466,25 +470,43 @@ static void clutter_awake(Ent *e) {
}
static void clutter_removed(Ent *e) { clut_drop_slot(ST(e, Clutter)->slot); }
/* ClutterBlockBase.Render: a dark rectangle (deeper), then the blocks in front of the player */
+/* (the room's place and the camera are whole pixels: what is in the strip is found with integers first, a block's
+ * float, at most 2 pixels up, only for those) */
static void clut_base_layer(uint16_t *strip, int sy0, int sy1, void *ctx) {
Ent *e = ctx;
Clutter *c = ST(e, Clutter);
+ int ox = c->rx - g_camx, oy = c->ry - g_camy;
for (int i = 0; i < nclut_base; i++) {
ClutBase *k = &clut_base[i];
if (((k->f >> 3) & 1) != c->slot) continue;
bool on = k->f & 4;
- sp_rect(strip, sy0, sy1, vx(c->rx + k->x * 8.f), vy(c->ry + k->y * 8.f), k->w * 8, k->h * 8 + (on ? 2 : 0), 0, on ? 179 : 77);
+ int y = oy + k->y * 8, h = k->h * 8 + (on ? 2 : 0);
+ if (y >= sy1 || y + h <= sy0) continue;
+ sp_rect(strip, sy0, sy1, ox + k->x * 8, y, k->w * 8, h, 0, on ? 179 : 77);
}
}
+/* the blocks, drawn as their pictures are (spans of the palette's colors: what sp_tex did a pixel at a time) */
static void clut_blk_layer(uint16_t *strip, int sy0, int sy1, void *ctx) {
Ent *e = ctx;
Clutter *c = ST(e, Clutter);
+ int ox = c->rx - g_camx, oy = c->ry - g_camy;
+ Tex tx[3 * 15];
+ uint64_t got = 0; /* tx[i] filled */
for (int i = 0; i < nclut_blk; i++) {
ClutBlock *b = &clut_blk[i];
if (!clut_mine(c, b) || (b->f & CB_FLY)) continue;
- int x = vx(c->rx + b->x * 8.f), y = vy(c->ry + b->y * 8.f + clut_float(c, b));
- if (y >= sy1 || y + CLUT_H[b->tex] * 8 <= sy0 || x >= VIEW_W || x + CLUT_W[b->tex] * 8 <= 0) continue;
- sp_tex(strip, sy0, sy1, clut_tex[b->f & CB_COLOR][b->tex], x, y, 0xFFFF, 256, false);
+ int y0 = oy + b->y * 8, x = ox + b->x * 8, h = CLUT_H[b->tex] * 8;
+ if (y0 - 2 >= sy1 || y0 + h <= sy0 || x >= VIEW_W || x + CLUT_W[b->tex] * 8 <= 0) continue;
+ int y = vy(c->ry + b->y * 8.f + clut_float(c, b));
+ if (y >= sy1 || y + h <= sy0) continue;
+ int ti = (b->f & CB_COLOR) * 15 + b->tex;
+ if (!(got >> ti & 1)) {
+ if (!tex_get(clut_tex[b->f & CB_COLOR][b->tex], &tx[ti])) continue;
+ got |= (uint64_t)1 << ti;
+ }
+ const Tex *t = &tx[ti];
+ if (t->fmt == TF_RAW4) sp_tex(strip, sy0, sy1, clut_tex[b->f & CB_COLOR][b->tex], x, y, 0xFFFF, 256, false);
+ else blit_tex(strip, sy0, sy1, t, x + t->ox, y + t->oy, 0, 0xFFFF, 255);
}
}
static void clutter_render(Ent *e) { gfx_custom(clut_base_layer, e, 0, VIEW_H); }
@@ -1121,6 +1143,7 @@ static bool ts_riding(Ent *e, Ent *pl) {
float dx = s->dir == TS_LEFT ? 1 : s->dir == TS_RIGHT ? -1 : 0, dy = s->dir == TS_UP ? 1 : s->dir == TS_DOWN ? -1 : 0;
return collide_ent_at(e, e->x + dx, e->y + dy, pl) && !collide_ent_at(e, e->x, e->y, pl); /* CollideCheckOutside */
}
+#define TS_REACH 24
static void ts_layer(uint16_t *strip, int sy0, int sy1, void *ctx) {
(void)ctx;
/* DustStyles: the edges' colors */
@@ -1132,10 +1155,16 @@ static void ts_layer(uint16_t *strip, int sy0, int sy1, void *ctx) {
TSpikes *s = ST(e, TSpikes);
V2 along = (s->dir == TS_UP || s->dir == TS_DOWN) ? v2(1, 0) : v2(0, 1), out = ts_outwards(s->dir);
bool vert = s->dir == TS_UP || s->dir == TS_DOWN;
+ /* only the strip's: a tendril or its dust reaches at most TS_REACH pixels from its place */
+ int ey0 = vy(e->y) - TS_REACH, ey1 = vy(e->y + (vert ? 0 : 2 + s->n * 4.f)) + TS_REACH;
+ int ex0 = vx(e->x) - TS_REACH, ex1 = vx(e->x + (vert ? 2 + s->n * 4.f : 0)) + TS_REACH;
+ if (ey1 < sy0 || ey0 >= sy1 || ex1 < 0 || ex0 >= VIEW_W) continue;
for (int i = 0; i < s->n; i++) {
int k = s->first + i;
- uint32_t h = hash32((uint32_t)e->eid * 104729u + (uint32_t)i); /* its own (Added's randoms): not from where it is, it rides blocks */
V2 at = v2add(v2(e->x, e->y), v2mul(along, 2 + i * 4.f));
+ int ay = vy(at.y), ax = vx(at.x);
+ if (ay + TS_REACH < sy0 || ay - TS_REACH >= sy1 || ax + TS_REACH < 0 || ax - TS_REACH >= VIEW_W) continue;
+ uint32_t h = hash32((uint32_t)e->eid * 104729u + (uint32_t)i); /* its own (Added's randoms): not from where it is, it rides blocks */
if (!(ts_state[k] & 128)) { /* the tendril, waving */
int frame = (int)(fmodf((h % 900) / 100.f + g_level.time_active * 12, 9));
uint16_t t = vert ? ts_tent_v[frame] : ts_tent_h[frame];
diff --git a/games/celeste/src/ent.c b/games/celeste/src/ent.c
index 9a83be3f..a7ef248f 100644
--- a/games/celeste/src/ent.c
+++ b/games/celeste/src/ent.c
@@ -1,3 +1,6 @@
+#if defined(__GNUC__) && !defined(__clang__)
+#pragma GCC optimize("Os") /* smaller over faster: the drawing takes the frame, not this (and the app space is full) */
+#endif
/* Entities: the pool, depth order, collisions, and Monocle's Actor, Solid,
* JumpThru movement (pixel by pixel, pushing and carrying riders). */
#include "ent.h"
diff --git a/games/celeste/src/gfx.c b/games/celeste/src/gfx.c
index fa305a4f..5ef43a0d 100644
--- a/games/celeste/src/gfx.c
+++ b/games/celeste/src/gfx.c
@@ -135,6 +135,18 @@ static inline int sx_(float x) { return (int)floorf(x + 0.5f) - (hud ? 0 : g_cam
static inline int sy_(float y) { return (int)floorf(y + 0.5f) - (hud ? 0 : g_camy); }
/* ---------------------------------------------------------------- color math */
+/* s premultiplied: s + d * (1 - a), a in 0..256 (inlined in the blits' loops, a call a pixel cost a sixth of a busy
+ * room's frame) */
+static inline __attribute__((always_inline)) uint16_t blend565_in(uint16_t d, uint16_t s, int a) {
+ uint32_t ia = 256 - a;
+ uint32_t r = (s >> 11) + (((d >> 11) * ia) >> 8);
+ uint32_t g = ((s >> 5) & 63) + ((((d >> 5) & 63) * ia) >> 8);
+ uint32_t b = (s & 31) + (((d & 31) * ia) >> 8);
+ if (r > 31) r = 31;
+ if (g > 63) g = 63;
+ if (b > 31) b = 31;
+ return (uint16_t)(r << 11 | g << 5 | b);
+}
uint16_t blend565(uint16_t d, uint16_t s, int a) {
/* s premultiplied: s + d * (1 - a), a in 0..256 */
uint32_t ia = 256 - a;
@@ -155,6 +167,16 @@ static inline uint16_t add565(uint16_t d, uint16_t s) {
t &= 0x07E0F81Fu;
return (uint16_t)(t | t >> 16);
}
+/* the same for the two pixels of a word: one half of the fields (B and R of the first, G of the second) as above, the
+ * other half shifted down a bit so that each has a free bit over it too; qa, qb: the color added, spread the same ways */
+static inline uint32_t add565x2(uint32_t p, uint32_t qa, uint32_t qb) {
+ uint32_t a = (p & 0x07E0F81Fu) + qa, b = ((p >> 1) & 0x7C0F83F0u) + qb;
+ uint32_t oa = a & 0x08010020u, ob = b & 0x80100400u;
+ uint32_t la = oa & 0x00010020u, ha = oa & 0x08000000u, lb = ob & 0x00000400u, hb = ob & 0x80100000u;
+ a = (a | (la - (la >> 5)) | (ha - (ha >> 6))) & 0x07E0F81Fu;
+ b = (b | (lb - (lb >> 6)) | (hb - (hb >> 5))) & 0x7C0F83F0u;
+ return a | b << 1;
+}
uint16_t mul565(uint16_t c, uint16_t t) {
uint32_t r = ((c >> 11) * ((t >> 11) + 1)) >> 5;
uint32_t g = (((c >> 5) & 63) * (((t >> 5) & 63) + 1)) >> 6;
@@ -238,7 +260,7 @@ static inline void plot(uint16_t *d, uint8_t idx) {
return;
}
int a = alpha_of(idx);
- *d = a >= 256 ? EC[idx] : blend565(*d, EC[idx], a);
+ *d = a >= 256 ? EC[idx] : blend565_in(*d, EC[idx], a);
}
/* n pixels from d on, one step (+1 or -1) apart: one index (run) or n indices (literals) */
@@ -250,13 +272,23 @@ static inline void span_run(uint16_t *d, int n, int step, uint8_t v) {
else
while (n--) *d-- = c;
} else if (ep_flags & GF_ADD) {
- for (; n--; d += step) *d = add565(*d, c);
+ if (step > 0 && n > 3) { /* two pixels at a time (the Celestial Resort's sunset over the whole view) */
+ if ((uintptr_t)d & 2) *d = add565(*d, c), d++, n--;
+ uint32_t qa = (c | (uint32_t)c << 16) & 0x07E0F81Fu, qb = ((c | (uint32_t)c << 16) >> 1) & 0x7C0F83F0u;
+ for (; n >= 2; n -= 2, d += 2) { /* (memcpy: an aligned word, read and written as such) */
+ uint32_t p;
+ memcpy(&p, d, 4);
+ p = add565x2(p, qa, qb);
+ memcpy(d, &p, 4);
+ }
+ }
+ for (; n > 0; n--, d += step) *d = add565(*d, c);
} else {
int a = alpha_of(v);
if (a >= 256)
for (; n--; d += step) *d = c;
else
- for (; n--; d += step) *d = blend565(*d, c, a);
+ for (; n--; d += step) *d = blend565_in(*d, c, a);
}
}
static inline void span_lit(uint16_t *d, int n, int step, const uint8_t *s) {
@@ -270,7 +302,7 @@ static inline void span_lit(uint16_t *d, int n, int step, const uint8_t *s) {
} else
for (; n--; d += step, s++) {
int a = alpha_of(*s);
- *d = a >= 256 ? EC[*s] : blend565(*d, EC[*s], a);
+ *d = a >= 256 ? EC[*s] : blend565_in(*d, EC[*s], a);
}
}
diff --git a/games/celeste/src/main.c b/games/celeste/src/main.c
index 0a20f37a..e7782b03 100644
--- a/games/celeste/src/main.c
+++ b/games/celeste/src/main.c
@@ -26,10 +26,11 @@ int main(void) {
game_init();
uint32_t next = plat_millis() * 3; /* in thirds of a millisecond: 50 per update */
while (g_running) {
- /* 60 updates a second; draw when caught up (at most 2 updates per frame) */
+ /* 60 updates a second; draw when caught up (at most 3 updates per frame: the busiest rooms keep the game's speed
+ on the N0110) */
int n = 0;
uint32_t now = plat_millis() * 3;
- while ((int32_t)(now - next) >= 0 && n < 2 && g_running) {
+ while ((int32_t)(now - next) >= 0 && n < 3 && g_running) {
game_frame();
perf_updates++;
next += 50;
diff --git a/games/celeste/src/res.c b/games/celeste/src/res.c
index 0df8d79c..b0c9af5a 100644
--- a/games/celeste/src/res.c
+++ b/games/celeste/src/res.c
@@ -118,7 +118,14 @@ static bool load_one(uint16_t id);
static struct { uint16_t id, frame; } failed[8]; /* loads that found no room, lately */
static int nfailed;
-static uint32_t tex_entry(uint16_t id) { return rd32(section(SEC_TEX) + 4 + 4 * id); }
+/* the texture table, found once (tex_get runs for every texture a strip draws) */
+static const uint8_t *sec_tex;
+static uint16_t ntex;
+static const uint8_t *tex_table(void) {
+ if (!sec_tex) sec_tex = section(SEC_TEX), ntex = rd16(sec_tex);
+ return sec_tex;
+}
+static uint32_t tex_entry(uint16_t id) { return rd32(tex_table() + 4 + 4 * id); }
int tex_half_extent(uint16_t id) {
int v = section(SEC_TEXDIM)[id];
return v == 255 ? 4096 : v * 2;
@@ -159,7 +166,8 @@ void res_init(void) {
}
bool tex_get(uint16_t id, Tex *t) {
- if (id >= rd16(section(SEC_TEX))) return false; /* none (0xFFFF) */
+ tex_table();
+ if (id >= ntex) return false; /* none (0xFFFF) */
uint32_t e = tex_entry(id);
if (e == 0xFFFFFFFF) return false; /* not in this data */
if (e >> 30 == 1) {