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: smoother room changes

Mason Chend8db94f47848
diff --git a/games/celeste/src/level.c b/games/celeste/src/level.c
index 4df1a93e..df5a9af7 100644
--- a/games/celeste/src/level.c
+++ b/games/celeste/src/level.c
@@ -212,7 +212,6 @@ static void need_textures(void) {
}
for (int i = 0; i < nextra_tex; i++) res_need(extra_tex[i]);
res_load_needed();
- tiles_invalidate();
}
void level_extra_textures(const uint16_t *ids, int n) {
extra_tex = ids, nextra_tex = n;
@@ -248,6 +247,45 @@ char level_tile_type(int layer, int cx, int cy) {
return c;
}
+/* the room's tile types of cells [x0, x1) of row cy (as rle_at) into out (out[0]: cell x0) */
+static void rle_span(const Room *rm, int layer, int cy, int x0, int x1, char *out) {
+ int ly = cy - rm->ty, lx = x0 - rm->tx, end = x1 - rm->tx;
+ memset(out, '0', (size_t)(x1 - x0));
+ if (ly < 0 || ly >= rm->th) return;
+ if (end > rm->tw) end = rm->tw;
+ const TileLayer *L = &rm->layer[layer];
+ const uint8_t *p = L->data + L->rows[ly];
+ int mask = (1 << L->bits) - 1;
+ if (lx < 0) lx = 0;
+ for (int x = 0; lx < end; p++) { /* x: where the run starts */
+ int x2 = x + (*p & mask) + 1;
+ char c = (char)L->types[*p >> L->bits];
+ for (; lx < x2 && lx < end; lx++) out[lx - (x0 - rm->tx)] = c;
+ x = x2;
+ }
+}
+
+/* level_tile_type of cells [cx, cx + n) of row cy (n <= 64) */
+void level_tile_row(int layer, int cx, int cy, int n, char *out) {
+ Room *rm = g_level.room;
+ if (!rm) {
+ memset(out, '0', (size_t)n);
+ return;
+ }
+ rle_span(rm, layer, cy, cx, cx + n, out);
+ Room *o = &g_level.rooms[g_level.room_slot ^ 1];
+ int py = cy * 8;
+ if (!g_level.transitioning || o->index < 0 || py < o->y || py >= o->y + o->h) return;
+ bool row_cur = py >= rm->y && py < rm->y + rm->h;
+ char other[64];
+ rle_span(o, layer, cy, cx, cx + n, other);
+ for (int k = 0; k < n; k++) {
+ int px = (cx + k) * 8;
+ bool in_cur = row_cur && px >= rm->x && px < rm->x + rm->w;
+ if (!in_cur && px >= o->x && px < o->x + o->w) out[k] = other[k];
+ }
+}
+
/* any solid tile in the cells [cx0, cx1] x [cy0, cy1] (inclusive) */
bool level_solid_cells(int cx0, int cy0, int cx1, int cy1) {
Room *rm = g_level.room;
@@ -540,6 +578,7 @@ static void transition_to(int index, V2 dir) {
g_level.room_slot = slot;
g_level.room = &g_level.rooms[slot];
need_textures();
+ tiles_invalidate(); /* (the tiles at its edges with the other room) */
ents_awake_new();
cassette_level_start(true);
Room *rm = g_level.room;
@@ -577,6 +616,7 @@ static void transition_update(void) {
int old = g_level.room_slot ^ 1;
ents_remove_room(old);
blocks_free_tiles(old);
+ tiles_invalidate(); /* (its tiles go from the cache, the edges change) */
g_level.rooms[old].index = -1;
V2 sp = player_spawn_near(v2(pe->x, pe->y)); /* Session.RespawnPoint = the closest spawn */
g_session.rx = (int32_t)sp.x, g_session.ry = (int32_t)sp.y;
diff --git a/games/celeste/src/level.h b/games/celeste/src/level.h
index 01a64868..67e47ff0 100644
--- a/games/celeste/src/level.h
+++ b/games/celeste/src/level.h
@@ -262,6 +262,7 @@ int level_player_facing(void);
bool level_solid_cells(int cx0, int cy0, int cx1, int cy1);
bool level_grid_collide(const Ent *grid, float l, float t, float r, float b);
char level_tile_type(int layer, int cx, int cy);
+void level_tile_row(int layer, int cx, int cy, int n, char *out);
bool level_is_dash_block(Ent *e);
void level_break_dash_block(Ent *e, V2 dir);
void level_enforce_bounds(Player *p);
diff --git a/games/celeste/src/res.c b/games/celeste/src/res.c
index 4dc133b4..cf387cd9 100644
--- a/games/celeste/src/res.c
+++ b/games/celeste/src/res.c
@@ -45,8 +45,8 @@ static void pack_info(uint16_t p, const uint8_t **data, uint32_t *comp, uint32_t
*raw = rd32(s + 8);
}
-/* decodes pack p from its start, giving bytes [from, to) to sink */
-static void lz_run(uint16_t p, uint32_t from, uint32_t to, Sink sink, void *ctx) {
+/* decodes pack p from its start, giving bytes [from, to) to sink (until *stop, if given) */
+static void lz_run(uint16_t p, uint32_t from, uint32_t to, Sink sink, void *ctx, const bool *stop) {
const uint8_t *src;
uint32_t comp, raw;
pack_info(p, &src, &comp, &raw);
@@ -61,11 +61,12 @@ static void lz_run(uint16_t p, uint32_t from, uint32_t to, Sink sink, void *ctx)
dec.dicBufSize = DICT_SIZE;
LzmaDec_Init(&dec);
uint32_t pos = 0, in = 0;
- while (pos < to) {
+ while (pos < to && !(stop && *stop)) {
if (dec.dicPos == dec.dicBufSize) dec.dicPos = 0;
SizeT start = dec.dicPos, inlen = comp - in;
ELzmaStatus st;
SizeT limit = dec.dicBufSize;
+ if (limit - start > 1024) limit = start + 1024; /* (a bit at a time: it can stop sooner) */
if (limit - start > to - pos) limit = start + (to - pos);
if (LzmaDec_DecodeToDic(&dec, limit, src + in, &inlen, LZMA_FINISH_ANY, &st) != SZ_OK) return;
in += (uint32_t)inlen;
@@ -79,7 +80,7 @@ static void lz_run(uint16_t p, uint32_t from, uint32_t to, Sink sink, void *ctx)
}
void res_stream(uint16_t pack, uint32_t off, uint32_t size, Sink sink, void *ctx) {
- lz_run(pack, off, off + size, sink, ctx);
+ lz_run(pack, off, off + size, sink, ctx, NULL);
}
/* ---------------------------------------------------------------- the cache */
@@ -326,6 +327,7 @@ typedef struct {
uint8_t mark; /* what the loaded entries are marked */
bool soft; /* all but `hard` only if there is room for them as it is */
uint16_t hard;
+ bool done; /* all read: the rest of the pack is not decoded */
} Reader;
static uint32_t entry_offset(uint16_t id) { return tex_entry(id) & 0x3FFFF; }
@@ -366,7 +368,7 @@ static void finish_tex(Reader *r) {
}
g_res_gen++;
r->e = NULL;
- r->cur++;
+ r->done = ++r->cur == r->n;
r->hdr_n = 0;
r->px_n = 0;
r->row_n = 0;
@@ -483,7 +485,7 @@ static void load_list_ex(uint16_t *ids, int n, uint8_t mark, int hard) {
rd.mark = mark;
rd.soft = hard >= 0, rd.hard = (uint16_t)hard;
uint32_t last = entry_offset(ids[j - 1]);
- lz_run((uint16_t)pack, 0, last + 20 + 4096u * 4096u, reader_sink, &rd);
+ lz_run((uint16_t)pack, 0, last + 20 + 4096u * 4096u, reader_sink, &rd, &rd.done);
i = j;
}
}
diff --git a/games/celeste/src/tiles.c b/games/celeste/src/tiles.c
index f464842f..7d855119 100644
--- a/games/celeste/src/tiles.c
+++ b/games/celeste/src/tiles.c
@@ -120,8 +120,15 @@ static TileCache tcache[2];
void tiles_invalidate(void) { tcache[0].valid = tcache[1].valid = false; }
-typedef struct { int layer; const Room *room; } RoomCtx;
-static char room_get(int x, int y, void *ctx) { return level_tile_type(((RoomCtx *)ctx)->layer, x, y); }
+/* the tile types of the window and 2 cells around it (what the autotiler looks at), read a row at a time */
+#define WIN_W (TC_W + 4)
+#define WIN_H (TC_H + 4)
+typedef struct { int layer; const Room *room; char (*win)[WIN_W]; int wx, wy; } RoomCtx;
+static char room_get(int x, int y, void *ctx) {
+ RoomCtx *rc = ctx;
+ unsigned u = (unsigned)(x - rc->wx), v = (unsigned)(y - rc->wy);
+ return u < WIN_W && v < WIN_H ? rc->win[v][u] : level_tile_type(rc->layer, x, y);
+}
static bool in_room(const Room *rm, int x, int y) {
int px = x * 8, py = y * 8;
return rm && px >= rm->x && px < rm->x + rm->w && py >= rm->y && py < rm->y + rm->h;
@@ -129,16 +136,28 @@ static bool in_room(const Room *rm, int x, int y) {
/* CheckForSameLevel: the other cell is in the room of the cell being tiled */
static bool room_in_level(int x, int y, void *ctx) { return in_room(((RoomCtx *)ctx)->room, x, y); }
+/* the cache's window at cell (cx, cy): the cells already there move along, only the new ones are tiled */
static void fill_cache(int layer, int cx, int cy) {
TileCache *c = &tcache[layer];
+ int dx = cx - c->cx, dy = cy - c->cy, ox = c->cx, oy = c->cy;
+ bool keep = c->valid && dx > -TC_W && dx < TC_W && dy > -TC_H && dy < TC_H;
+ if (keep) {
+ int ady = dy < 0 ? -dy : dy, adx = dx < 0 ? -dx : dx;
+ if (dy) memmove(&c->cell[dy < 0 ? ady : 0], &c->cell[dy > 0 ? ady : 0], sizeof c->cell[0] * (size_t)(TC_H - ady));
+ if (dx)
+ for (int r = 0; r < TC_H; r++) memmove(&c->cell[r][dx < 0 ? adx : 0], &c->cell[r][dx > 0 ? adx : 0], 2 * (size_t)(TC_W - adx));
+ }
c->cx = cx, c->cy = cy, c->valid = true;
- RoomCtx rc = {layer, NULL};
+ char win[WIN_H][WIN_W];
+ for (int r = 0; r < WIN_H; r++) level_tile_row(layer, cx - 2, cy - 2 + r, WIN_W, win[r]);
+ RoomCtx rc = {layer, NULL, win, cx - 2, cy - 2};
/* padding looks only inside the cell's room for the fg (PaddingIgnoreOutOfLevel), anywhere for the bg */
AutoCtx a = {room_get, &rc, layer == 0 ? room_in_level : NULL, false};
for (int r = 0; r < TC_H; r++)
for (int k = 0; k < TC_W; k++) {
int x = cx + k, y = cy + r;
- char t = level_tile_type(layer, x, y);
+ if (keep && x >= ox && x < ox + TC_W && y >= oy && y < oy + TC_H) continue;
+ char t = win[r + 2][k + 2];
c->cell[r][k] = 31;
if (t == '0') continue;
rc.room = in_room(g_level.room, x, y) ? g_level.room : &g_level.rooms[g_level.room_slot ^ 1];