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: no more flashing in 1A's busy rooms

Mason Chen85b5c34a077a
diff --git a/games/celeste/src/celeste.h b/games/celeste/src/celeste.h
index c319e288..e32b4aba 100644
--- a/games/celeste/src/celeste.h
+++ b/games/celeste/src/celeste.h
@@ -207,6 +207,8 @@ void gfx_circle(float x, float y, float r, uint16_t c, uint8_t alpha, int resolu
/* layers drawn by callbacks, once per strip: y0 and y1 are view rows */
typedef void (*StripFn)(uint16_t *strip, int y0, int y1, void *ctx);
void gfx_custom(StripFn fn, void *ctx, int y0, int y1);
+void gfx_keep(bool on); /* the layers that must be drawn (tiles, stylegrounds in front): commands kept for them */
+void blit_line(uint16_t *strip, int sy0, int sy1, float x0, float y0, float x1, float y1, uint16_t col, uint8_t alpha);
void gfx_camera(float x, float y); /* world position of the view's top left */
extern int g_camx, g_camy; /* floored */
void gfx_hud(bool on); /* following draws ignore the camera */
diff --git a/games/celeste/src/entities.c b/games/celeste/src/entities.c
index 0245eb6a..c18902d6 100644
--- a/games/celeste/src/entities.c
+++ b/games/celeste/src/entities.c
@@ -806,20 +806,32 @@ static void zip_render(Ent *e) {
/* the path between the two places (ZipMoverPathRenderer, depth 5000) */
typedef struct { Ent *zip; } ZipPath;
-static void zip_cogs(Zip *z, Ent *zip, V2 off, bool black) {
+/* the two ropes and their teeth (Draw.Line each): drawn into the strips by one command (a command a line filled the
+ * frame's in 1A's rooms of zip movers, and what came after them was not drawn) */
+static void zip_ropes(uint16_t *strip, int y0, int y1, Ent *zip, bool black) {
+ Zip *z = ST(zip, Zip);
+ V2 off = v2(0, black ? 1 : 0);
V2 from = v2(z->start.x + zip->cw / 2, z->start.y + zip->ch / 2), to = v2(z->target.x + zip->cw / 2, z->target.y + zip->ch / 2);
V2 d = v2norm(v2sub(to, from)), perp = v2(d.y, -d.x);
V2 v1 = v2mul(perp, 3), v2_ = v2mul(perp, -4);
uint16_t rope = black ? 0 : rgb(0x663931), light = black ? 0 : rgb(0x9B6157);
- gfx_line(from.x + v1.x + off.x, from.y + v1.y + off.y, to.x + v1.x + off.x, to.y + v1.y + off.y, rope, 255);
- gfx_line(from.x + v2_.x + off.x, from.y + v2_.y + off.y, to.x + v2_.x + off.x, to.y + v2_.y + off.y, rope, 255);
+ blit_line(strip, y0, y1, from.x + v1.x + off.x, from.y + v1.y + off.y, to.x + v1.x + off.x, to.y + v1.y + off.y, rope, 255);
+ blit_line(strip, y0, y1, from.x + v2_.x + off.x, from.y + v2_.y + off.y, to.x + v2_.x + off.x, to.y + v2_.y + off.y, rope, 255);
float len = v2len(v2sub(to, from));
for (float n = 4 - fmodf(z->percent * PI_F * 8, 4); n < len; n += 4) {
V2 a = v2add(v2add(v2add(from, v1), perp), v2mul(d, n));
V2 b = v2sub(v2add(to, v2_), v2mul(d, n));
- gfx_line(a.x + off.x, a.y + off.y, a.x + d.x * 2 + off.x, a.y + d.y * 2 + off.y, light, 255);
- gfx_line(b.x + off.x, b.y + off.y, b.x - d.x * 2 + off.x, b.y - d.y * 2 + off.y, light, 255);
+ blit_line(strip, y0, y1, a.x + off.x, a.y + off.y, a.x + d.x * 2 + off.x, a.y + d.y * 2 + off.y, light, 255);
+ blit_line(strip, y0, y1, b.x + off.x, b.y + off.y, b.x - d.x * 2 + off.x, b.y - d.y * 2 + off.y, light, 255);
}
+}
+static void zip_ropes_black(uint16_t *strip, int y0, int y1, void *ctx) { zip_ropes(strip, y0, y1, ctx, true); }
+static void zip_ropes_color(uint16_t *strip, int y0, int y1, void *ctx) { zip_ropes(strip, y0, y1, ctx, false); }
+static void zip_cogs(Zip *z, Ent *zip, V2 off, bool black) {
+ V2 from = v2(z->start.x + zip->cw / 2, z->start.y + zip->ch / 2), to = v2(z->target.x + zip->cw / 2, z->target.y + zip->ch / 2);
+ float top = fminf(from.y, to.y) + off.y - 8, bot = fmaxf(from.y, to.y) + off.y + 8; /* the ropes' rows, teeth and all */
+ if (fmaxf(from.x, to.x) + 8 >= g_camx && fminf(from.x, to.x) - 8 < g_camx + VIEW_W)
+ gfx_custom(black ? zip_ropes_black : zip_ropes_color, zip, (int)floorf(top) - g_camy, (int)ceilf(bot) - g_camy + 1);
float rot = z->percent * PI_F * 2;
uint8_t f = black ? GF_SILHOUETTE : 0;
gfx_tex_ex(z->cog, from.x + off.x, from.y + off.y, 6, 6, 1, 1, rot, black ? 0 : 0xFFFF, 255, f);
diff --git a/games/celeste/src/gfx.c b/games/celeste/src/gfx.c
index 7d2c346c..2c6b9b29 100644
--- a/games/celeste/src/gfx.c
+++ b/games/celeste/src/gfx.c
@@ -45,7 +45,8 @@ static Cmd cmds[MAX_CMDS];
static int ncmds;
static Affine affs[MAX_AFFINE];
static int naffs;
-static Custom customs[20];
+#define MAX_CUSTOMS 40
+static Custom customs[MAX_CUSTOMS];
static int ncustoms;
void gfx_camera(float x, float y) {
@@ -86,9 +87,15 @@ void gfx_begin(void) {
/* the interface keeps the last HUD_CMDS commands and HUD_AFFS affine ones: a busy room's world can't take them */
#define HUD_CMDS 48
#define HUD_AFFS 8
+/* and the layers that must be drawn (the level's tiles, the stylegrounds in front: 1A's sunset, the snow) the last
+ * KEEP_CMDS of the world's commands; they and the interface, the last KEEP_CUSTOMS custom layers */
+#define KEEP_CMDS 12
+#define KEEP_CUSTOMS 8
+static bool keep;
+void gfx_keep(bool on) { keep = on; }
static Cmd *add(uint8_t op, int y0, int y1) {
if (y1 <= 0 || y0 >= VIEW_H) return NULL;
- if (ncmds >= (ui ? MAX_CMDS : MAX_CMDS - HUD_CMDS)) {
+ if (ncmds >= (ui ? MAX_CMDS : MAX_CMDS - HUD_CMDS - (keep ? 0 : KEEP_CMDS))) {
#ifdef HOST
dropped_cmds++;
#endif
@@ -188,7 +195,11 @@ static void prep(uint16_t pal, uint16_t tint, uint8_t alpha, uint8_t flags) {
int aa = a[i] + (a[i] >> 7);
if (ga != 256) aa = (aa * ga) >> 8;
epal[i + 1] = (uint16_t)(lr[col >> 11] << 11 | lg[col >> 5 & 63] << 5 | lb[col & 31]);
- if (flags & GF_ADDALPHA) epal[i + 1] = scale565(epal[i + 1], aa);
+ if (flags & GF_ADDALPHA) { /* the tinted color times ga * aa, rounded once (truncated twice, half of it was lost) */
+ uint32_t k = (uint32_t)ga * (uint32_t)aa;
+ epal[i + 1] = (uint16_t)(((((col >> 11) * (tr + 1)) >> 5) * k + 32768) >> 16 << 11 |
+ ((((col >> 5 & 63) * (tg + 1)) >> 6) * k + 32768) >> 16 << 5 | ((((col & 31) * (tb + 1)) >> 5) * k + 32768) >> 16);
+ }
ealpha[i + 1] = (uint16_t)aa;
if (aa != 256) ep_opaque = false;
}
@@ -711,6 +722,14 @@ static void line(uint16_t *strip, int sy0, int sy1, int x0, int y0, int x1, int
}
}
+/* a line between world points (as gfx_line places them) drawn into the strip: for layers of many lines, one command */
+void blit_line(uint16_t *strip, int sy0, int sy1, float x0, float y0, float x1, float y1, uint16_t col, uint8_t alpha) {
+ int X0 = (int)floorf(x0 + 0.5f) - g_camx, Y0 = (int)floorf(y0 + 0.5f) - g_camy;
+ int X1 = (int)floorf(x1 + 0.5f) - g_camx, Y1 = (int)floorf(y1 + 0.5f) - g_camy;
+ if ((Y0 < Y1 ? Y1 : Y0) < sy0 || (Y0 < Y1 ? Y0 : Y1) >= sy1) return;
+ line(strip, sy0, sy1, X0, Y0, X1, Y1, col, alpha);
+}
+
/* ---------------------------------------------------------------- recording */
void gfx_tex(uint16_t tex, float x, float y, uint8_t flags, uint16_t tint, uint8_t alpha) {
Tex t;
@@ -866,7 +885,12 @@ static void blit_mask(uint16_t *strip, int sy0, int sy1, const uint8_t *bits, in
}
void gfx_custom(StripFn fn, void *ctx, int y0, int y1) {
- if (ncustoms >= 20) return;
+ if (ncustoms >= (ui || keep ? MAX_CUSTOMS : MAX_CUSTOMS - KEEP_CUSTOMS)) {
+#ifdef HOST
+ dropped_cmds++;
+#endif
+ return;
+ }
Cmd *c = add(OP_CUSTOM, y0, y1);
if (!c) return;
customs[ncustoms] = (Custom){fn, ctx};
diff --git a/games/celeste/src/npc.c b/games/celeste/src/npc.c
index 57056ef3..50c06c16 100644
--- a/games/celeste/src/npc.c
+++ b/games/celeste/src/npc.c
@@ -18,19 +18,29 @@ static const EntClass LAMP = {.size = sizeof(Lamp), .name = "lamp", .render = la
/* ---------------------------------------------------------------- Wire */
typedef struct { V2 from, to; float sine_x, sine_y; } WireS;
-static void wire_render(Ent *e) {
- WireS *w = ST(e, WireS);
+static V2 wire_control(const WireS *w) { /* Curve.Control, swaying in the wind */
float k = 8 * level_visual_wind();
V2 sway = v2(sinf(w->sine_x + g_level.wind_sine_timer * 2) * k, sinf(w->sine_y + g_level.wind_sine_timer * 2.8f) * k);
- V2 c = v2add(v2add(v2mul(v2add(w->from, w->to), 0.5f), v2(0, 24)), sway);
- V2 prev = w->from;
+ return v2add(v2add(v2mul(v2add(w->from, w->to), 0.5f), v2(0, 24)), sway);
+}
+/* its 16 lines drawn into the strips by one command */
+static void wire_strip(uint16_t *strip, int y0, int y1, void *ctx) {
+ const WireS *w = ST((Ent *)ctx, WireS);
+ V2 c = wire_control(w), prev = w->from;
for (int i = 1; i <= 16; i++) {
float t = i / 16.f, u = 1 - t;
V2 p = v2(u * u * w->from.x + 2 * u * t * c.x + t * t * w->to.x, u * u * w->from.y + 2 * u * t * c.y + t * t * w->to.y);
- gfx_line(prev.x, prev.y, p.x, p.y, 0x5ACC /* 595866 */, 255);
+ blit_line(strip, y0, y1, prev.x, prev.y, p.x, p.y, 0x5ACC /* 595866 */, 255);
prev = p;
}
}
+static void wire_render(Ent *e) {
+ WireS *w = ST(e, WireS);
+ V2 c = wire_control(w); /* the curve stays within its three points */
+ float top = fminf(fminf(w->from.y, w->to.y), c.y), bot = fmaxf(fmaxf(w->from.y, w->to.y), c.y);
+ if (fmaxf(fmaxf(w->from.x, w->to.x), c.x) + 1 < g_camx || fminf(fminf(w->from.x, w->to.x), c.x) - 1 >= g_camx + VIEW_W) return;
+ gfx_custom(wire_strip, e, (int)floorf(top) - g_camy - 1, (int)ceilf(bot) - g_camy + 2);
+}
static const EntClass WIRE = {.size = sizeof(WireS), .name = "wire", .render = wire_render};
V2 *wire_curve_begin(Ent *e) { return e && e->cls == &WIRE ? &ST(e, WireS)->from : NULL; } /* Wire.Curve.Begin */
diff --git a/games/celeste/src/style.c b/games/celeste/src/style.c
index 120308eb..c73d8841 100644
--- a/games/celeste/src/style.c
+++ b/games/celeste/src/style.c
@@ -130,6 +130,7 @@ static void parallax(int i) {
void style_render(bool fg) {
gfx_screen(true);
+ gfx_keep(fg);
int from = fg ? nbg : 0, to = fg ? nbg + nfg : nbg;
for (int i = from; i < to && i < MAX_SG; i++) {
const uint8_t *r = rec(i);
@@ -141,5 +142,6 @@ void style_render(bool fg) {
default: break;
}
}
+ gfx_keep(false);
gfx_screen(false);
}
diff --git a/games/celeste/src/tiles.c b/games/celeste/src/tiles.c
index 054dde19..763f42fe 100644
--- a/games/celeste/src/tiles.c
+++ b/games/celeste/src/tiles.c
@@ -273,7 +273,9 @@ void tiles_render_layer(int layer, int depth) {
for (int k = 0; k < 30; k++)
if (terrains >> k & 1) tex_get(terrain_tex(k + layer_base(layer)), &t);
if (terrains >> TERR_SCENERY & 1) tex_get(T_tilesets_scenery, &t);
+ gfx_keep(true);
gfx_custom(layer_strip, (void *)(intptr_t)layer, 0, VIEW_H);
+ gfx_keep(false);
/* animated overlays (grass) */
const uint8_t *at = section(SEC_ANIMTILES);
int n = rd16(at);