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
Celeste: the CE version draws faster
Mason3633bd385c87fef
diff --git a/games/celeste/ce/host/blit.c b/games/celeste/ce/host/blit.cindex e26836a2..a6e2eb76 100644--- a/games/celeste/ce/host/blit.c+++ b/games/celeste/ce/host/blit.c@@ -1,14 +1,21 @@/* src/blit.S's loops in C, for the computer build. */#include "../src/ce.h"+/* (CEL_COUNT: what the frame drew, host/plat_host.c prints it) */+long g_cnt_cells, g_cnt_opq, g_cnt_msk, g_cnt_mskpx, g_cnt_runs, g_cnt_rect;static void tile_rows(const Cell *c, uint8_t *d, unsigned a, unsigned b, unsigned rows) {const uint8_t *s = c->tile;+ g_cnt_cells++;for (int r = 0; r < 8; r++, s += 8, d += 320) {if (!(rows >> r & 1)) continue;- if (c->opaque >> r & 1) memcpy(d + a, s + a, b - a);- else if (c->drawn >> r & 1)- for (unsigned i = a; i < b; i++)- if (s[i]) d[i] = s[i];+ if (c->opaque >> r & 1) memcpy(d + a, s + a, b - a), g_cnt_opq++;+ else if (c->drawn >> r & 1) {+ g_cnt_msk++;+ for (unsigned i = a; i < b; i++) {+ if (s[i]) d[i] = s[i], g_cnt_mskpx++;+ if (s[i] && (i == a || !s[i - 1])) g_cnt_runs++;+ }+ }}}void blit_row(const Cell *cells, unsigned n, uint8_t *dst, unsigned rows) {@@ -40,6 +47,7 @@ void build_cells(const uint8_t *fg, const uint8_t *bg, const TEnt *table, Cell *}}void blit_rect(uint8_t *dst, const uint8_t *src, unsigned w, unsigned h, unsigned src_step) {+ g_cnt_rect += w * h;for (unsigned r = 0; r < h; r++, dst += 320, src += src_step) memcpy(dst, src, w);}void blit_rle_flip(uint8_t *dst, const uint8_t *rows, unsigned nrows) {@@ -63,3 +71,20 @@ int isqrt24(int v) {}return (int)root;}+void blit_rle_clip(uint8_t *dst, const uint8_t *rows, unsigned skip, unsigned nrows, unsigned xa, unsigned xb) {+ for (; skip; skip--) {+ int runs = *rows++;+ while (runs--) rows += 2 + rows[1];+ }+ for (; nrows; nrows--, dst += 320) {+ int runs = *rows++;+ unsigned at = 0;+ while (runs--) {+ at += rows[0];+ unsigned n = rows[1], a0 = at > xa ? at : xa, a1 = at + n < xb ? at + n : xb;+ if (a0 < a1) memcpy(dst + a0, rows + 2 + (a0 - at), a1 - a0);+ at += n;+ rows += 2 + n;+ }+ }+}diff --git a/games/celeste/ce/host/plat_host.c b/games/celeste/ce/host/plat_host.cindex 4fa2f972..9964ae38 100644--- a/games/celeste/ce/host/plat_host.c+++ b/games/celeste/ce/host/plat_host.c@@ -95,6 +95,12 @@ void png(const char *path, const uint8_t *fb) {}void plat_frame(void) {+ if (getenv("CEL_COUNT")) {+ extern long g_cnt_cells, g_cnt_opq, g_cnt_msk, g_cnt_mskpx, g_cnt_runs, g_cnt_rect;+ printf("cells %ld opaque rows %ld masked rows %ld (%ld px, %ld runs) background %ld px\n", g_cnt_cells, g_cnt_opq, g_cnt_msk,+ g_cnt_mskpx, g_cnt_runs, g_cnt_rect);+ g_cnt_cells = g_cnt_opq = g_cnt_msk = g_cnt_mskpx = g_cnt_runs = g_cnt_rect = 0;+ }g_shown = g_fb;cur ^= 1;g_fb = fbs[cur ^ 1];@@ -126,6 +132,8 @@ unsigned plat_keys(void) {int nf = 0, nb = 0, nt = 0;for (int i = 0; i < rm->tw * rm->th; i++) nf += rm->grid[i] != 0, nb += rm->grid[rm->tw * rm->th + i] != 0;for (int i = 0; i < 320 * 180; i++) nt += g_bg8[i] != 0;+ { int cnt[256] = {0}; for (int i = 0; i < g_nents; i++) if (g_ents[i].type != 255) cnt[g_ents[i].type]++;+ printf("ents:"); for (int t = 0; t < 256; t++) if (cnt[t]) printf(" %d:%d", t, cnt[t]); printf("\n"); }printf("room %s tw %d th %d ntiles %d fg %d bg %d bg4 %d hasbg %d map %d %d %d\n", level_room_name(), rm->tw, rm->th,rm->ntiles, nf, nb, nt, g_has_bg, g_bg_map[0], g_bg_map[1], g_bg_map[2]);}diff --git a/games/celeste/ce/src/blit.S b/games/celeste/ce/src/blit.Sindex 33852ad3..1e2e17c9 100644--- a/games/celeste/ce/src/blit.S+++ b/games/celeste/ce/src/blit.S@@ -31,9 +31,11 @@ _blit_row:and a, dld e, a ; opaque rows (E')ld h, 8 ; rows left (H')+ inc a ; (0: all 8 rows shown, all opaque)exxpush de ; (the cell's dst)ld hl, (ix + 2)+ jp z, .Lw_full.Lw_row:exxsrl e@@ -87,6 +89,53 @@ _blit_row:add hl, bcld bc, 320jr .Lw_adv+.Lw_full: ; a whole opaque tile: its rows copied one after another+ ld bc, 8+ ldir+ ld bc, 312+ ex de, hl+ add hl, bc+ ex de, hl+ ld bc, 8+ ldir+ ld bc, 312+ ex de, hl+ add hl, bc+ ex de, hl+ ld bc, 8+ ldir+ ld bc, 312+ ex de, hl+ add hl, bc+ ex de, hl+ ld bc, 8+ ldir+ ld bc, 312+ ex de, hl+ add hl, bc+ ex de, hl+ ld bc, 8+ ldir+ ld bc, 312+ ex de, hl+ add hl, bc+ ex de, hl+ ld bc, 8+ ldir+ ld bc, 312+ ex de, hl+ add hl, bc+ ex de, hl+ ld bc, 8+ ldir+ ld bc, 312+ ex de, hl+ add hl, bc+ ex de, hl+ ld bc, 8+ ldir+ pop de+ jp .Lw_next/* void blit_clip(const Cell *cell, uint8_t *dst, unsigned a, unsigned b, unsigned rows): a tile's columns [a, b) at* dst (its column 0's place), its rows as in blit_row; 0 <= a < b <= 8 */@@ -402,3 +451,110 @@ _isqrt24:djnz .Ls_loopex de, hlret++/* void blit_rle_clip(uint8_t *dst, const uint8_t *rows, unsigned skip, unsigned nrows, unsigned xa, unsigned xb): as+ * blit_rle, the first `skip` stored rows left out, then nrows of them drawn, each only in its columns [xa, xb) (from+ * the stored part's left; dst is where that left is on the first row drawn); 0 <= xa < xb <= 255 */+ .global _blit_rle_clip+ .type _blit_rle_clip, @function+_blit_rle_clip:+ push ix+ ld ix, 0+ add ix, sp+ ld hl, (ix + 9) ; the rows+ ld a, (ix + 12) ; rows to skip+ or a, a+ jr z, .Lk_draw+.Lk_skiprow:+ ld b, (hl) ; runs+ inc hl+ inc b+ jr .Lk_skipnext+.Lk_skiprun:+ inc hl+ ld de, 0+ ld e, (hl)+ inc hl+ add hl, de+.Lk_skipnext:+ djnz .Lk_skiprun+ dec a+ jr nz, .Lk_skiprow+.Lk_draw:+ ld iy, (ix + 6) ; the row's left+ ld a, (ix + 15) ; rows to draw+ or a, a+ jr z, .Lk_done+.Lk_row:+ ex af, af' ; (rows left)+ ld c, (hl) ; runs+ inc hl+ ld b, 0 ; at: the run's first column+ inc c+ jr .Lk_next+.Lk_run:+ ld a, b+ add a, (hl) ; + skip+ inc hl+ ld b, a ; at+ ld e, (hl) ; n+ inc hl ; (hl: its pixels)+ add a, e+ ld d, a ; a1 = at + n ...+ ld a, (ix + 21)+ cp a, d+ jr nc, .Lk_hi+ ld d, a ; ... or xb if less+.Lk_hi:+ ld a, (ix + 18) ; a0 = xa ...+ cp a, b+ jr nc, .Lk_lo+ ld a, b ; ... or at if more+.Lk_lo:+ cp a, d+ jr nc, .Lk_past ; nothing in [xa, xb)+ push hl+ push bc+ push de+ ld c, a ; a0+ ld a, d+ sub a, c+ push af ; a1 - a0 pixels+ ld a, c+ sub a, b+ ld de, 0+ ld e, a+ add hl, de ; from pixel a0+ ld e, c+ push hl+ lea hl, iy + 0+ add hl, de+ ex de, hl ; to the row's left + a0+ pop hl+ pop af+ ld bc, 0+ ld c, a+ ldir+ pop de+ pop bc+ pop hl+.Lk_past:+ ld a, b ; past the run+ add a, e+ ld b, a+ push bc+ ld bc, 0+ ld c, e+ add hl, bc+ pop bc+.Lk_next:+ dec c+ jr nz, .Lk_run+ ld de, 320+ add iy, de+ ex af, af'+ dec a+ jr nz, .Lk_row+.Lk_done:+ pop ix+ retdiff --git a/games/celeste/ce/src/ce.h b/games/celeste/ce/src/ce.hindex 3cddd024..12504c39 100644--- a/games/celeste/ce/src/ce.h+++ b/games/celeste/ce/src/ce.h@@ -96,14 +96,14 @@ extern int g_camx, g_camy; /* the view's top left in the world *//* drawing happens inside the clip only (view pixels); while g_dry, nothing is drawn: the box of what would be (g_bb,* view pixels) and a sum of it (g_bb_sum) grow instead (level.c: what changed since a buffer was drawn) */extern int g_clip_x0, g_clip_y0, g_clip_x1, g_clip_y1;-extern bool g_dry;+extern bool g_dry, g_rec;extern int g_bb[4];extern unsigned g_bb_sum;void bb_add(int x0, int y0, int x1, int y1, unsigned what);static inline bool dry_box(int x0, int y0, int x1, int y1, unsigned what) { /* true: measured, not to be drawn */- if (!g_dry) return false;+ if (!g_rec) return false;bb_add(x0, y0, x1, y1, what);- return true;+ return g_dry;}extern uint8_t g_draw_color;void draw_sprite(const uint8_t *spr, int x, int y, uint8_t flags); /* (x, y): the frame's top left, world */@@ -120,6 +120,7 @@ void draw_cells_update(void); /* the view's cells, for the camera now *extern int g_offx, g_offy; /* the camera in its cell */void draw_layers_back(int ca, int cb, int ra, int rb); /* background, bg tiles: the view's cells [ca, cb] x [ra, rb] */void draw_layer_fg(int ca, int cb, int ra, int rb); /* fg tiles */+void decals_prepare(void); /* those on the view, for this frame */void draw_decals(int fg);void decals_mark(void (*mark)(int x0, int y0, int x1, int y1)); /* the animated ones' boxes (view) */void draw_clear_screen(uint8_t c);@@ -136,7 +137,8 @@ void blit_clip(const Cell *cell, uint8_t *dst, unsigned a, unsigned b, unsignedvoid blit_copy(uint8_t *dst, const uint8_t *src, unsigned n);void blit_rect(uint8_t *dst, const uint8_t *src, unsigned w, unsigned h, unsigned src_step);void blit_rle(uint8_t *dst, const uint8_t *rows, unsigned nrows);-void blit_rle_flip(uint8_t *dst, const uint8_t *rows, unsigned nrows); /* mirrored: dst the top row's rightmost */+void blit_rle_flip(uint8_t *dst, const uint8_t *rows, unsigned nrows);+void blit_rle_clip(uint8_t *dst, const uint8_t *rows, unsigned skip, unsigned nrows, unsigned xa, unsigned xb); /* mirrored: dst the top row's rightmost */void build_cells(const uint8_t *fg, const uint8_t *bg, const TEnt *table, Cell *ofg, Cell *obg, uint8_t *vis, unsigned n);/* the view's cells (draw.c), kept while the camera stays in the same ones: 41 x 24 */#define COLS 41@@ -227,8 +229,9 @@ void ents_update(void);void ents_order(void); /* the order they draw in (by depth), for this frame */void ents_render(int dmax, int dmin); /* depths in (dmin, dmax] */extern int g_cull_buf; /* >= 0: ents_render skips those whose box there misses the clip */+extern int g_rec_buf; /* >= 0: ents_render records what each one draws there */void ents_measure(int buf, void (*changed)(const int16_t *was, const int *now)); /* each one's box against buf's */-void ents_record(int buf); /* what each one draws now, for buf */+void ents_forget(int buf); /* none drawn there (before it is drawn whole) */void ents_remove_room(int slot);void ents_clear(void);bool rect_hits(int l, int t, int r, int b, const Ent *o);diff --git a/games/celeste/ce/src/draw.c b/games/celeste/ce/src/draw.cindex 167e59b3..d4da3f94 100644--- a/games/celeste/ce/src/draw.c+++ b/games/celeste/ce/src/draw.c@@ -5,7 +5,7 @@int g_camx, g_camy;uint8_t g_draw_color;int g_clip_x0, g_clip_y0, g_clip_x1 = VIEW_W, g_clip_y1 = VIEW_H;-bool g_dry;+bool g_dry, g_rec;int g_bb[4];unsigned g_bb_sum;void bb_add(int x0, int y0, int x1, int y1, unsigned what) {@@ -64,17 +64,24 @@ static void blit_sprite(const uint8_t *s, int x, int y, uint8_t flags) {void draw_sprite(const uint8_t *s, int x, int y, uint8_t flags) {x -= g_camx, y -= g_camy;- if (g_dry) {+ if (g_rec) {int l = (flags & DF_FLIPX) ? x + s[0] - s[2] - s[4] : x + s[2], t = (flags & DF_FLIPY) ? y + s[1] - s[3] - s[5] : y + s[3];bb_add(l, t, l + s[4], t + s[5], (unsigned)(uintptr_t)s + flags + g_draw_color + (g_remap ? 77u : 0u));- return;+ if (g_dry) return;}- if (!(flags & ~DF_FLIPX) && !g_remap && s[5]) { /* all of it in the clip: at once */- int l = (flags & DF_FLIPX) ? x + s[0] - s[2] - s[4] : x + s[2], t = y + s[3];- if (l >= g_clip_x0 && t >= g_clip_y0 && l + s[4] <= g_clip_x1 && t + s[5] <= g_clip_y1) {- uint8_t *d = g_fb + (VIEW_Y + t) * 320 + l;- if (flags) blit_rle_flip(d + s[4] - 1, s + 6, s[5]);- else blit_rle(d, s + 6, s[5]);+ if (!(flags & ~DF_FLIPX) && !g_remap) {+ int l = (flags & DF_FLIPX) ? x + s[0] - s[2] - s[4] : x + s[2], t = y + s[3], w = s[4], h = s[5];+ if (l >= g_clip_x1 || t >= g_clip_y1 || l + w <= g_clip_x0 || t + h <= g_clip_y0 || !h) return; /* (none of it) */+ uint8_t *d = g_fb + (VIEW_Y + t) * 320 + l;+ if (l >= g_clip_x0 && t >= g_clip_y0 && l + w <= g_clip_x1 && t + h <= g_clip_y1) { /* all of it: at once */+ if (flags) blit_rle_flip(d + w - 1, s + 6, (unsigned)h);+ else blit_rle(d, s + 6, (unsigned)h);+ return;+ }+ if (!flags) { /* cut by the clip */+ int skip = t < g_clip_y0 ? g_clip_y0 - t : 0, xa = l < g_clip_x0 ? g_clip_x0 - l : 0;+ blit_rle_clip(d + skip * 320, s + 6, (unsigned)skip, (unsigned)(imin(t + h, g_clip_y1) - t - skip), (unsigned)xa,+ (unsigned)imin(w, g_clip_x1 - l));return;}}@@ -146,7 +153,6 @@ void draw_sprite_scaled(const uint8_t *s, int x, int y, int sx, int sy, int ox,/* a tile at view (x, y), clipped */static void blit_tile(const uint8_t *t, int x, int y, bool opaque) {int a = 0, b = 8, r0 = 0, r1 = 8;- if (dry_box(x, y, x + 8, y + 8, (unsigned)(uintptr_t)t)) return;if (x < g_clip_x0) a = g_clip_x0 - x;if (x + 8 > g_clip_x1) b = g_clip_x1 - x;if (y < g_clip_y0) r0 = g_clip_y0 - y;@@ -163,7 +169,16 @@ static void blit_tile(const uint8_t *t, int x, int y, bool opaque) {}void draw_tile(int tile, int x, int y) {- if (tile) blit_tile(tile_data(tile), x - g_camx, y - g_camy, g_tile_flags[2 * tile] == 0xFF);+ if (!tile) return;+ x -= g_camx, y -= g_camy;+ if (dry_box(x, y, x + 8, y + 8, (unsigned)tile)) return;+ if (x >= g_clip_x0 && y >= g_clip_y0 && x + 8 <= g_clip_x1 && y + 8 <= g_clip_y1) { /* (all of it) */+ Cell c;+ c.opaque = g_tile_flags[2 * tile], c.drawn = g_tile_flags[2 * tile + 1], c.tile = tile_data(tile);+ blit_row(&c, 1, g_fb + (VIEW_Y + y) * 320 + x, 0xFF);+ return;+ }+ blit_tile(tile_data(tile), x, y, g_tile_flags[2 * tile] == 0xFF);}void draw_rect_screen(int x, int y, int w, int h, uint8_t c) {@@ -209,38 +224,48 @@ void draw_clear_screen(uint8_t c) { memset(g_fb, c, 320 * 240); }/* ---------------------------------------------------------------- the tile layers *//* the view's cells: each one's fg and bg tiles (the current room's grid over the other's), and where the background- * shows; made again when the camera goes into other cells or the rooms change (g_cells_gen) */+ * shows. A ring both ways: when the camera goes into other cells only the new ones are made (all of them when the+ * rooms change: g_cells_gen); view cell (c, r) is kept at (c + coff, r + roff), each modulo the size. */extern uint8_t *g_cell_cache;-static int cache_c0 = 1 << 20, cache_r0;+static int cache_c0 = 1 << 20, cache_r0, coff, roff;static uint8_t cache_gen = 255;-static CellRow *cells_row(int r) { return (CellRow *)(void *)(g_cell_cache + r * sizeof(CellRow)); }-static void cells_build(int c0, int r0) {- for (int r = 0; r < CROWS; r++) {- CellRow *row = cells_row(r);- memset(row, 0, sizeof *row);- memset(row->vis, 1, COLS);+static int ph(int c) { return c + coff >= COLS ? c + coff - COLS : c + coff; }+static CellRow *cells_row(int r) {+ r += roff;+ if (r >= CROWS) r -= CROWS;+ return (CellRow *)(void *)(g_cell_cache + r * sizeof(CellRow));+}+static void build_span(int r, int ca, int cb) { /* view cells [ca, cb) of row r */+ CellRow *row = cells_row(r);+ while (ca < cb) {+ int p = ph(ca), n = imin(cb - ca, COLS - p); /* (kept side by side) */+ memset(row->fg + p, 0, (size_t)n * sizeof(Cell)), memset(row->bg + p, 0, (size_t)n * sizeof(Cell));+ memset(row->vis + p, 1, (size_t)n);for (int k = 0; k < 2; k++) {Room *rm = &g_level.rooms[k ? g_level.slot : g_level.slot ^ 1]; /* (the current one last: it wins) */if (rm->index < 0) continue;- int ry = r0 + r - (rm->gy >> 3), gx = rm->gx >> 3;+ int ry = cache_r0 + r - (rm->gy >> 3), gx = rm->gx >> 3;if ((unsigned)ry >= (unsigned)rm->th) continue;- int a = imax(c0, gx), b = imin(c0 + COLS, gx + rm->tw);+ int a = imax(cache_c0 + ca, gx), b = imin(cache_c0 + ca + n, gx + rm->tw);if (a >= b) continue;const uint8_t *gf = rm->grid + ry * rm->tw + (a - gx);- build_cells(gf, gf + rm->tw * rm->th, rm->tab, row->fg + (a - c0), row->bg + (a - c0), row->vis + (a - c0),- (unsigned)(b - a));+ int at = p + a - cache_c0 - ca;+ build_cells(gf, gf + rm->tw * rm->th, rm->tab, row->fg + at, row->bg + at, row->vis + at, (unsigned)(b - a));}+ ca += n;}- cache_c0 = c0, cache_r0 = r0, cache_gen = g_cells_gen;}/* cells [ca, cb] of a row of a layer: those inside the view at once, those at its sides clipped */int g_offx, g_offy;static void layer_row(const Cell *cells, uint8_t *line, int ca, int cb, unsigned rows) {- int a = imax(ca, 1), b = imin(cb, COLS - 2);- if (a <= b) blit_row(cells + a, (unsigned)(b - a + 1), line + a * 8 - g_offx, rows);- if (ca == 0) blit_clip(cells, line - g_offx, (unsigned)g_offx, 8, rows);- if (cb == COLS - 1 && g_offx) blit_clip(cells + COLS - 1, line + 320 - g_offx, 0, (unsigned)g_offx, rows);+ for (int a = imax(ca, 1), b = imin(cb, COLS - 2); a <= b;) {+ int p = ph(a), n = imin(b - a + 1, COLS - p);+ blit_row(cells + p, (unsigned)n, line + a * 8 - g_offx, rows);+ a += n;+ }+ if (ca == 0) blit_clip(cells + ph(0), line - g_offx, (unsigned)g_offx, 8, rows);+ if (cb == COLS - 1 && g_offx) blit_clip(cells + ph(COLS - 1), line + 320 - g_offx, 0, (unsigned)g_offx, rows);}static unsigned rows_mask(int r, uint8_t **line) { /* row r of cells: its rows in the view, and its top's place */@@ -251,11 +276,44 @@ static unsigned rows_mask(int r, uint8_t **line) { /* row r of cells: its rows}void draw_cells_update(void) {- int c0 = g_camx >> 3, r0 = g_camy >> 3;+ int c0 = g_camx >> 3, r0 = g_camy >> 3, dc = c0 - cache_c0, dr = r0 - cache_r0;g_offx = g_camx & 7, g_offy = g_camy & 7;- if (c0 != cache_c0 || r0 != cache_r0 || g_cells_gen != cache_gen) cells_build(c0, r0);+ if (!dc && !dr && g_cells_gen == cache_gen) return;+ if (g_cells_gen != cache_gen || dc <= -COLS || dc >= COLS || dr <= -CROWS || dr >= CROWS) { /* all of them */+ cache_c0 = c0, cache_r0 = r0, cache_gen = g_cells_gen, coff = roff = 0;+ for (int r = 0; r < CROWS; r++) build_span(r, 0, COLS);+ return;+ }+ coff = (coff + dc + COLS) % COLS, roff = (roff + dr + CROWS) % CROWS;+ cache_c0 = c0, cache_r0 = r0;+ for (int r = 0; r < CROWS; r++) {+ if ((dr > 0 && r >= CROWS - dr) || (dr < 0 && r < -dr)) build_span(r, 0, COLS); /* a new row */+ else if (dc > 0) build_span(r, COLS - dc, COLS); /* new columns */+ else if (dc < 0) build_span(r, 0, -dc);+ }}+#ifndef __TICE__+/* CEL_VERIFY: the ring against all the cells made again (the number that differ) */+int draw_cells_check(void) {+ static uint8_t keep[CELL_CACHE];+ uint8_t *cache = g_cell_cache;+ int c0 = cache_c0, r0 = cache_r0, co = coff, ro = roff, bad = 0;+ g_cell_cache = keep, coff = roff = 0;+ for (int r = 0; r < CROWS; r++) build_span(r, 0, COLS);+ for (int r = 0; r < CROWS; r++)+ for (int c = 0; c < COLS; c++) {+ CellRow *a = (CellRow *)(void *)(keep + r * sizeof(CellRow));+ int pr = (r + ro) % CROWS, pc = (c + co) % COLS;+ CellRow *b = (CellRow *)(void *)(cache + pr * sizeof(CellRow));+ if (memcmp(&a->fg[c], &b->fg[pc], sizeof(Cell)) || memcmp(&a->bg[c], &b->bg[pc], sizeof(Cell)) || a->vis[c] != b->vis[pc])+ bad++;+ }+ g_cell_cache = cache, coff = co, roff = ro, cache_c0 = c0, cache_r0 = r0;+ return bad;+}+#endif+void draw_layers_back(int ca, int cb, int ra, int rb) {int offx = g_offx;/* the background's picture: view pixel (x, y) is its ((x + ox) % 320, y + oy), rows outside it the edge's */@@ -269,9 +327,9 @@ void draw_layers_back(int ca, int cb, int ra, int rb) {CellRow *row = cells_row(r);int vy = r * 8 - g_offy, y0 = imax(vy, 0), y1 = imin(vy + 8, VIEW_H);for (int c = ca; c <= cb;) { /* the background, in runs of cells it shows in */- if (!row->vis[c]) { c++; continue; }+ if (!row->vis[ph(c)]) { c++; continue; }int e = c + 1;- while (e <= cb && row->vis[e]) e++;+ while (e <= cb && row->vis[ph(e)]) e++;int xa = imax(c * 8 - offx, 0), xb = imin(e * 8 - offx, VIEW_W);c = e;if (xa >= xb) continue;@@ -307,40 +365,60 @@ void draw_layer_fg(int ca, int cb, int ra, int rb) {}}-/* the rooms' decals: u8 bg count, u8 fg count, then u16 sprite, s16 x, s16 y (centre), u8 flags, u8 frames */-void draw_decals(int fg) {- for (int k = 0; k < 2; k++) {- Room *rm = &g_level.rooms[k];- if (rm->index < 0) continue;- const uint8_t *d = rm->decals;- int n = d[fg], skip = fg ? d[0] : 0;- const uint8_t *p = d + 2 + 8 * skip;- for (int i = 0; i < n; i++, p += 8) {- int id = (int)R16(p), frames = p[7];- if (frames > 1) id += (int)(g_level.frame / 5 % (unsigned)frames); /* 12 a second */- const uint8_t *s = csprite(id);- int x = rm->x + S16(p + 2) - s[0] / 2, y = rm->y + S16(p + 4) - s[1] / 2;- if (x - g_camx >= g_clip_x1 || y - g_camy >= g_clip_y1 || x + s[0] - g_camx <= g_clip_x0 || y + s[1] - g_camy <= g_clip_y0)- continue;- draw_sprite(s, x, y, p[6] & 3);+/* the rooms' decals: u8 bg count, u8 fg count, then u16 sprite, s16 x, s16 y (centre), u8 flags, u8 frames. Those on+ * the view are listed once a frame (decals_prepare: their picture, view place, animated or not), the bg ones first. */+typedef struct { const uint8_t *s; int16_t x, y; uint8_t anim; } VDecal;+#define MAX_VDECALS 192+uint8_t *g_vdecals; /* (MAX_VDECALS of them, in the RAM plat_ram gives) */+static int nvd[2];+void decals_prepare(void) { /* (again only when the camera, the rooms or an animated one's frame changed) */+ static int camx = -99999, camy, tick;+ static uint8_t gen;+ static bool anims;+ int t = (int)(g_level.frame / 5);+ if (g_camx == camx && g_camy == camy && g_cells_gen == gen && (!anims || t == tick)) return;+ camx = g_camx, camy = g_camy, gen = g_cells_gen, tick = t, anims = false;+ VDecal *v = (VDecal *)(void *)g_vdecals;+ int n = 0;+ for (int fg = 0; fg < 2; fg++) {+ for (int k = 0; k < 2; k++) {+ Room *rm = &g_level.rooms[k];+ if (rm->index < 0) continue;+ const uint8_t *d = rm->decals;+ int cnt = d[fg], skip = fg ? d[0] : 0;+ const uint8_t *p = d + 2 + 8 * skip;+ int rx = rm->x - g_camx, ry = rm->y - g_camy;+ for (int i = 0; i < cnt; i++, p += 8) {+ int id = (int)R16(p), frames = p[7];+ if (frames > 1) id += (int)(g_level.frame / 5 % (unsigned)frames); /* 12 a second */+ const uint8_t *sp = csprite(id);+ int x = rx + S16(p + 2) - sp[0] / 2, y = ry + S16(p + 4) - sp[1] / 2;+ if (x >= VIEW_W || y >= VIEW_H || x + sp[0] <= 0 || y + sp[1] <= 0 || n == MAX_VDECALS) continue;+ v[n].s = sp, v[n].x = (int16_t)x, v[n].y = (int16_t)y, v[n].anim = frames > 1;+ anims |= frames > 1;+ n++;+ }}+ nvd[fg] = n;}}-/* the animated decals' boxes, when their frame has changed since the last time this buffer was drawn (5 updates a- * frame of theirs) */-void decals_mark(void (*mark)(int x0, int y0, int x1, int y1)) {- for (int k = 0; k < 2; k++) {- Room *rm = &g_level.rooms[k];- if (rm->index < 0) continue;- const uint8_t *d = rm->decals;- const uint8_t *p = d + 2;- for (int i = d[0] + d[1]; i > 0; i--, p += 8) {- if (p[7] <= 1) continue;- const uint8_t *s = csprite((int)R16(p));- int x = rm->x + S16(p + 2) - s[0] / 2 - g_camx, y = rm->y + S16(p + 4) - s[1] / 2 - g_camy;- mark(x, y, x + s[0], y + s[1]);- }+void draw_decals(int fg) {+ const VDecal *v = (const VDecal *)(const void *)g_vdecals;+ int camx = g_camx, camy = g_camy;+ g_camx = g_camy = 0;+ for (int i = fg ? nvd[0] : 0, e = nvd[fg]; i < e; i++) {+ const uint8_t *sp = v[i].s;+ int x = v[i].x, y = v[i].y;+ if (x >= g_clip_x1 || y >= g_clip_y1 || x + sp[0] <= g_clip_x0 || y + sp[1] <= g_clip_y0) continue;+ draw_sprite(sp, x, y, 0);}+ g_camx = camx, g_camy = camy;+}+/* the animated decals' boxes (their frame changes 12 times a second) */+void decals_mark(void (*mark)(int x0, int y0, int x1, int y1)) {+ const VDecal *v = (const VDecal *)(const void *)g_vdecals;+ for (int i = 0; i < nvd[1]; i++)+ if (v[i].anim) mark(v[i].x, v[i].y, v[i].x + v[i].s[0], v[i].y + v[i].s[1]);}/* ---------------------------------------------------------------- text: a 5 x 7 font for now */@@ -388,6 +466,17 @@ int draw_selftest(void) {blit_sprite(s, 100, 0, DF_FLIPX);for (int y = 0; y < s[1]; y++)if (memcmp(g_fb + (VIEW_Y + y) * 320, g_fb + (VIEW_Y + y) * 320 + 100, s[0])) bad |= 2;+ for (int k = 0; k < 4 && id % 6 == 0; k++) { /* cut by a clip (some of them: it is slow) */+ int cx = (id * 7 + k * 5) % (s[0] + 1), cy = (id * 3 + k * 11) % (s[1] + 1);+ memset(g_fb + VIEW_Y * 320, 0, 320 * s[1]);+ g_clip_x0 = k & 1 ? cx : 0, g_clip_x1 = k & 1 ? VIEW_W : cx, g_clip_y0 = k & 2 ? cy : 0, g_clip_y1 = k & 2 ? VIEW_H : cy;+ draw_sprite(s, 0, 0, 0);+ g_clip_x0 += 100, g_clip_x1 += 100;+ blit_sprite(s, 100, 0, 0);+ g_clip_x0 = 0, g_clip_y0 = 0, g_clip_x1 = VIEW_W, g_clip_y1 = VIEW_H;+ for (int y = 0; y < s[1]; y++)+ if (memcmp(g_fb + (VIEW_Y + y) * 320, g_fb + (VIEW_Y + y) * 320 + 100, s[0])) bad |= 4;+ }}memset(g_fb, 0, 320 * 240);return bad;diff --git a/games/celeste/ce/src/ents.c b/games/celeste/ce/src/ents.cindex 9ff90dec..a84c1056 100644--- a/games/celeste/ce/src/ents.c+++ b/games/celeste/ce/src/ents.c@@ -871,57 +871,71 @@ static void ent_draw(Ent *e) {int g_cull_buf = -1;/* back to front: higher depths first, then the order they came in */static uint8_t order[MAX_ENTS], norder;+/* roughly on the view: what each draws stays within 48 px of its box (Madeline's hair and death go further: she is+ * always in) */+static bool ent_near(const Ent *e) {+ if (e->type == ET_player) return true;+ int l = e->x + e->hx - g_camx, t = e->y + e->hy - g_camy;+ return l < VIEW_W + 48 && t < VIEW_H + 48 && l + e->hw > -48 && t + e->hh > -48;+}void ents_order(void) {norder = 0;for (int i = 0; i < g_nents; i++) {Ent *e = &g_ents[i];- if (e->type == 255 || !(e->flags & EF_VISIBLE)) continue;+ if (e->type == 255 || !(e->flags & EF_VISIBLE) || !ent_near(e)) continue;int k = norder++, d = e->depth;while (k > 0 && g_ents[order[k - 1]].depth < d) order[k] = order[k - 1], k--;order[k] = (uint8_t)i;}}+int g_rec_buf = -1;+static void bb_start(void) { g_bb[0] = g_bb[1] = 0x7FFF, g_bb[2] = g_bb[3] = -0x7FFF, g_bb_sum = 0; }+static void bb_keep(Ent *e, int buf) {+ for (int k = 0; k < 4; k++) e->bb[buf][k] = (int16_t)iclamp(g_bb[k], -0x7FFF, 0x7FFF);+ e->sum[buf] = g_bb_sum;+}void ents_render(int dmax, int dmin) {- int cb = g_cull_buf;+ int cb = g_cull_buf, rb = g_rec_buf;for (int k = 0; k < norder; k++) {Ent *e = &g_ents[order[k]];if (e->depth > dmax || e->depth <= dmin) continue;if (cb >= 0 && (e->bb[cb][0] >= g_clip_x1 || e->bb[cb][2] <= g_clip_x0 || e->bb[cb][1] >= g_clip_y1 ||e->bb[cb][3] <= g_clip_y0))continue;+ if (rb < 0) {+ ent_draw(e);+ continue;+ }+ bb_start(), g_rec = true; /* (drawn and measured) */ent_draw(e);+ g_rec = false, bb_keep(e, rb);}}/* what each one would draw (its box and sum), drawing nothing */-static void measure(Ent *e) {- g_bb[0] = g_bb[1] = 0x7FFF, g_bb[2] = g_bb[3] = -0x7FFF, g_bb_sum = 0;- if (e->flags & EF_VISIBLE) ent_draw(e);- for (int k = 0; k < 4; k++) g_bb[k] = iclamp(g_bb[k], -0x7FFF, 0x7FFF);+static bool ent_static(const Ent *e) { /* what never changes how it looks (until it goes) */+ switch (e->type) {+ case ET_spikesUp: case ET_spikesDown: case ET_spikesLeft: case ET_spikesRight: return !e->ptr;+ case ET_jumpThru: case ET_dashBlock: return true;+ }+ return false;}void ents_measure(int buf, void (*changed)(const int16_t *was, const int *now)) {- g_dry = true;+ g_dry = g_rec = true;for (int i = 0; i < g_nents; i++) {Ent *e = &g_ents[i];- if (e->type == 255) continue;- measure(e);+ if (e->type == 255 || ent_static(e)) continue;+ bb_start();+ if ((e->flags & EF_VISIBLE) && ent_near(e)) ent_draw(e);+ for (int k = 0; k < 4; k++) g_bb[k] = iclamp(g_bb[k], -0x7FFF, 0x7FFF);int16_t *bb = e->bb[buf];if (e->sum[buf] != g_bb_sum || bb[0] != g_bb[0] || bb[1] != g_bb[1] || bb[2] != g_bb[2] || bb[3] != g_bb[3]) {changed(bb, g_bb);- for (int k = 0; k < 4; k++) bb[k] = (int16_t)g_bb[k];- e->sum[buf] = g_bb_sum;+ bb_keep(e, buf);}}- g_dry = false;+ g_dry = g_rec = false;}-void ents_record(int buf) {- g_dry = true;- for (int i = 0; i < g_nents; i++) {- Ent *e = &g_ents[i];- if (e->type == 255) continue;- measure(e);- for (int k = 0; k < 4; k++) e->bb[buf][k] = (int16_t)g_bb[k];- e->sum[buf] = g_bb_sum;- }- g_dry = false;+void ents_forget(int buf) {+ for (int i = 0; i < g_nents; i++) g_ents[i].bb[buf][0] = 1, g_ents[i].bb[buf][2] = 0;}diff --git a/games/celeste/ce/src/level.c b/games/celeste/ce/src/level.cindex 0aa58e5f..e8ba5e2a 100644--- a/games/celeste/ce/src/level.c+++ b/games/celeste/ce/src/level.c@@ -1,6 +1,9 @@/* The level: a chapter's rooms (two at once during a transition), collisions with their tiles, the camera, the* transitions between rooms, deaths and respawns (Celeste's Level and LevelLoader, after src/level.c). */#include "ce.h"+#ifndef __TICE__+#include <stdlib.h>+#endifLevel g_level;#define GRID_BYTES GRID_MAX /* a room's grids in RAM (the biggest of the chapters here) */@@ -12,7 +15,8 @@ uint8_t g_cells_gen;/* the RAM plat_ram gives: the entities, the two rooms' grids and tile tables */static uint8_t *g_dirty; /* the view's cells to draw again: a byte each, a row of COLS at a time */bool level_ram(void) {- unsigned n = sizeof(Ent) * MAX_ENTS + 2 * (GRID_BYTES + 256 * sizeof(TEnt)) + 1024 + CELL_CACHE + CROWS * COLS;+ extern uint8_t *g_vdecals;+ unsigned n = sizeof(Ent) * MAX_ENTS + 2 * (GRID_BYTES + 256 * sizeof(TEnt)) + 1024 + CELL_CACHE + CROWS * COLS + 192 * 8;uint8_t *m = plat_ram(n);if (!m) return false;memset(m, 0, n);@@ -20,6 +24,7 @@ bool level_ram(void) {g_scratch = m, m += 1024;g_cell_cache = m, m += CELL_CACHE;g_dirty = m, m += CROWS * COLS;+ g_vdecals = m, m += 192 * 8;for (int k = 0; k < 2; k++) {g_level.rooms[k].tab = (TEnt *)(void *)m, m += 256 * sizeof(TEnt);grid_mem[k] = m, m += GRID_BYTES;@@ -370,7 +375,7 @@ void level_update(void) {* went, the animated decals), every layer in its order, clipped to them. */static struct { int camx, camy, frame; uint8_t gen, ok; } was[2];static int buf; /* the buffer drawn now */-static bool any_dirty;+static int dirty_r0 = CROWS, dirty_r1 = -1; /* the rows marked */#define MAX_GONE 8static int16_t gone[2][MAX_GONE][4];static uint8_t ngone[2];@@ -387,7 +392,8 @@ static void mark(int x0, int y0, int x1, int y1) { /* view pixels [x0, x1) x [int ca = (x0 + g_offx) >> 3, cb = (x1 - 1 + g_offx) >> 3, ra = (y0 + g_offy) >> 3, rb = (y1 - 1 + g_offy) >> 3;uint8_t *d = g_dirty + ra * COLS + ca;for (int r = ra; r <= rb; r++, d += COLS) memset(d, 1, (size_t)(cb - ca + 1));- any_dirty = true;+ if (ra < dirty_r0) dirty_r0 = ra;+ if (rb > dirty_r1) dirty_r1 = rb;}static void changed(const int16_t *a, const int *b) {mark(a[0], a[1], a[2], a[3]);@@ -423,35 +429,60 @@ void level_render(bool whole) {#endifdraw_cells_update();ents_order();+ decals_prepare();+#ifndef __TICE__+ {+ extern char *getenv(const char *);+ extern int draw_cells_check(void);+ extern int printf(const char *, ...);+ static int on = -1, checked, wrong;+ if (on < 0) on = getenv("CEL_VERIFY") != NULL;+ if (on) {+ int n = draw_cells_check();+ checked++;+ if (n) wrong++;+ if (n && wrong < 5) printf("verify: frame %u: %d cells differ\n", (unsigned)g_level.frame, n);+ if (checked % 100 == 0) printf("verify: %d frames' cells, %d wrong\n", checked, wrong);+ }+ }+#endifBENCH(2);+#ifndef __TICE__+ if (getenv("CEL_WHY")) {+ extern int printf(const char *, ...);+ printf("frame %u: %s%s%s%s%s%s\n", (unsigned)g_level.frame, whole ? "whole " : "", gone_full[buf] ? "gone " : "", !was[buf].ok ? "notok " : "",+ was[buf].camx != g_camx ? "camx " : "", was[buf].camy != g_camy ? "camy " : "", was[buf].gen != g_cells_gen ? "gen" : "");+ }+#endifif (whole || gone_full[buf] || !was[buf].ok || was[buf].camx != g_camx || was[buf].camy != g_camy ||was[buf].gen != g_cells_gen) {+ ents_forget(buf);+ g_rec_buf = buf; /* (what each entity draws, recorded as it is) */render_cells(0, COLS - 1, 0, CROWS - 1);- ents_record(buf);+ g_rec_buf = -1;BENCH(3);} else {- memset(g_dirty, 0, CROWS * COLS);- any_dirty = false;+ dirty_r0 = CROWS, dirty_r1 = -1; /* (g_dirty is all 0 between frames) */ents_measure(buf, changed);for (int i = 0; i < ngone[buf]; i++) mark(gone[buf][i][0], gone[buf][i][1], gone[buf][i][2], gone[buf][i][3]);if ((unsigned)g_level.frame / 5 != (unsigned)was[buf].frame / 5) decals_mark(mark);BENCH(3);g_cull_buf = buf;- if (any_dirty) { /* runs of dirty cells in a row, each grown down over the rows dirty all along it */- uint8_t *row = g_dirty;- for (int r = 0; r < CROWS; r++, row += COLS)+ if (dirty_r1 >= 0) { /* runs of dirty cells in a row, each grown down over the rows dirty all along it */+ uint8_t *row = g_dirty + dirty_r0 * COLS;+ for (int r = dirty_r0; r <= dirty_r1; r++, row += COLS)for (int c = 0; c < COLS;) {- if (!row[c]) { c++; continue; }+ const uint8_t *at = memchr(row + c, 1, (size_t)(COLS - c));+ if (!at) break;+ c = (int)(at - row);int e = c;while (e + 1 < COLS && row[e + 1]) e++;int rb = r;- for (uint8_t *nx = row + COLS; rb + 1 < CROWS; nx += COLS) {- int k = c;- while (k <= e && nx[k]) k++;- if (k <= e) break;+ for (uint8_t *nx = row + COLS; rb < dirty_r1 && !memchr(nx + c, 0, (size_t)(e - c + 1)); nx += COLS) {memset(nx + c, 0, (size_t)(e - c + 1));rb++;}+ memset(row + c, 0, (size_t)(e - c + 1));render_cells(c, e, r, rb);c = e + 1;}diff --git a/games/celeste/ce/src/plat_ce.c b/games/celeste/ce/src/plat_ce.cindex a05c5066..93bccb48 100644--- a/games/celeste/ce/src/plat_ce.c+++ b/games/celeste/ce/src/plat_ce.c@@ -25,6 +25,7 @@ void plat_init(void) {timer_Enable(1, TIMER_CPU, TIMER_NOINT, TIMER_UP);#endifkb_DisableOnLatch();+ kb_SetMode(MODE_3_CONTINUOUS); /* (the keypad scans by itself: reading it is instant) */}/* The RAM the game takes besides its program and static data: an appvar, CELRAM, made last (the save before it, in* RAM and at its full size, is only written in place while playing), so nothing moves it; gone at the end. */@@ -58,6 +59,7 @@ void plat_exit(void) {uint8_t h = ti_Open("CELSAVE", "r"); /* the save goes to the archive (it stays in RAM while playing) */if (h) ti_SetArchiveStatus(true, h), ti_Close(h);gfx_End();+ kb_Reset();kb_ClearOnLatch();}__attribute__((noinline)) void plat_frame(void) { /* (not inlined: the emulator counts frames here) */@@ -68,7 +70,6 @@ void plat_begin_draw(void) { gfx_Wait(); }void plat_palette(const uint16_t *pal, int first, int n) { memcpy(gfx_palette + first, pal, (size_t)n * 2); }unsigned plat_keys(void) {- kb_Scan();unsigned k = 0;uint8_t g1 = kb_Data[1], g2 = kb_Data[2], g6 = kb_Data[6], g7 = kb_Data[7];if (g7 & kb_Left) k |= K_LEFT;