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 CE version redraws what changed

Mason3631a8efd42c1d7
diff --git a/games/celeste/ce/.gitignore b/games/celeste/ce/.gitignore
index 754681b5..f736eb9a 100644
--- a/games/celeste/ce/.gitignore
+++ b/games/celeste/ce/.gitignore
@@ -1,3 +1,5 @@
bin/
build/
obj/
+last.png
+*.sav
diff --git a/games/celeste/ce/data/CELG0.bin b/games/celeste/ce/data/CELG0.bin
index d64d7733..aea22f2f 100644
Binary files a/games/celeste/ce/data/CELG0.bin and b/games/celeste/ce/data/CELG0.bin differ
diff --git a/games/celeste/ce/data/CELG1.bin b/games/celeste/ce/data/CELG1.bin
index ed2f59e9..34df819c 100644
Binary files a/games/celeste/ce/data/CELG1.bin and b/games/celeste/ce/data/CELG1.bin differ
diff --git a/games/celeste/ce/host/blit.c b/games/celeste/ce/host/blit.c
index d62f7772..e26836a2 100644
--- a/games/celeste/ce/host/blit.c
+++ b/games/celeste/ce/host/blit.c
@@ -42,3 +42,24 @@ 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) {
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) {
+ for (; nrows; nrows--, dst += 320) {
+ int runs = *rows++, at = 0;
+ while (runs--) {
+ at += rows[0];
+ for (int i = 0; i < rows[1]; i++) dst[-(at + i)] = rows[2 + i];
+ at += rows[1];
+ rows += 2 + rows[1];
+ }
+ }
+}
+int isqrt24(int v) {
+ unsigned left = 0, root = 0, x = (unsigned)v << 8;
+ for (int i = 0; i < 12; i++) {
+ left = left * 4 + (x >> 30), x <<= 2;
+ unsigned t = root * 4 + 1;
+ root *= 2;
+ if (left >= t) left -= t, root++;
+ }
+ return (int)root;
+}
diff --git a/games/celeste/ce/host/plat_host.c b/games/celeste/ce/host/plat_host.c
index 2c0c8400..4fa2f972 100644
--- a/games/celeste/ce/host/plat_host.c
+++ b/games/celeste/ce/host/plat_host.c
@@ -60,7 +60,7 @@ void plat_init(void) {
}
void plat_exit(void) {}
-static void png(const char *path, const uint8_t *fb) {
+void png(const char *path, const uint8_t *fb) {
static uint8_t raw[240 * (1 + 320 * 3)];
uint8_t *p = raw;
for (int y = 0; y < 240; y++) {
@@ -222,3 +222,7 @@ new_offset:
if (bit()) goto new_offset;
goto literals;
}
+
+#ifdef CEL_BENCH
+unsigned long plat_cycles(void) { return 0; }
+#endif
diff --git a/games/celeste/ce/last.png b/games/celeste/ce/last.png
deleted file mode 100644
index 387a05c1..00000000
Binary files a/games/celeste/ce/last.png and /dev/null differ
diff --git a/games/celeste/ce/makefile b/games/celeste/ce/makefile
index a3f28e0f..2feb178c 100644
--- a/games/celeste/ce/makefile
+++ b/games/celeste/ce/makefile
@@ -10,7 +10,7 @@ DESCRIPTION = "Celeste"
COMPRESSED = YES
COMPRESSED_MODE = zx0
ARCHIVED = YES
-CFLAGS = -Wall -Wextra -Wno-unused-parameter -Oz
+CFLAGS = -Wall -Wextra -Wno-unused-parameter -Oz $(BENCH)
HAS_PRINTF = NO
include $(shell cedev-config --makefile)
diff --git a/games/celeste/ce/src/blit.S b/games/celeste/ce/src/blit.S
index 6034787e..33852ad3 100644
--- a/games/celeste/ce/src/blit.S
+++ b/games/celeste/ce/src/blit.S
@@ -14,11 +14,11 @@ _blit_row:
ld iy, 0
add iy, sp
ld ix, (iy + 6) ; the cells
- ld a, (iy + 9) ; n
ld de, (iy + 12) ; dst
- ld c, (iy + 15) ; rows
exx
- ld d, c ; rows (D')
+ ld a, (iy + 15)
+ ld d, a ; rows (D')
+ ld a, (iy + 9)
ld c, a ; cells left (C')
exx
.Lw_cell:
@@ -314,3 +314,91 @@ _blit_rect:
jr nz, .Lq_row
pop ix
ret
+
+/* void blit_rle_flip(uint8_t *dst, const uint8_t *rows, unsigned nrows): as blit_rle, mirrored: dst is where the top
+ * row's first stored pixel goes (its rightmost), each run going leftwards */
+ .global _blit_rle_flip
+ .type _blit_rle_flip, @function
+_blit_rle_flip:
+ push ix
+ ld iy, 0
+ add iy, sp
+ ld ix, (iy + 6) ; the row's first pixel's place
+ ld hl, (iy + 9) ; the rows
+ ld a, (iy + 12) ; rows left
+.Lf_row:
+ exx
+ ld e, a
+ exx
+ lea de, ix + 0
+ ld a, (hl) ; runs
+ inc hl
+ or a, a
+ jr z, .Lf_next
+.Lf_run:
+ ex af, af'
+ ld bc, 0
+ ld c, (hl) ; skip
+ inc hl
+ ex de, hl
+ or a, a
+ sbc hl, bc
+ ex de, hl
+ ld b, (hl) ; n
+ inc hl
+.Lf_px:
+ ld a, (hl)
+ inc hl
+ ld (de), a
+ dec de
+ djnz .Lf_px
+ ex af, af'
+ dec a
+ jr nz, .Lf_run
+.Lf_next:
+ ld bc, 320
+ add ix, bc
+ exx
+ ld a, e
+ exx
+ dec a
+ jr nz, .Lf_row
+ pop ix
+ ret
+
+/* int isqrt24(int v): the integer square root of 0 <= v < 2^23, two bits at a time from the top (shifts left only) */
+ .global _isqrt24
+ .type _isqrt24, @function
+_isqrt24:
+ ld iy, 0
+ add iy, sp
+ ld iy, (iy + 3) ; v
+ ld hl, 0 ; what is left
+ ld de, 0 ; the root so far
+ ld b, 12
+.Ls_loop:
+ add iy, iy
+ adc hl, hl
+ add iy, iy
+ adc hl, hl ; left = left * 4 + v's next two bits
+ push hl
+ ex de, hl
+ add hl, hl ; root * 2
+ push hl
+ add hl, hl
+ inc hl ; root * 4 + 1
+ ex de, hl
+ or a, a
+ sbc hl, de
+ pop de ; root * 2
+ jr c, .Ls_less
+ inc de ; root * 2 + 1, left - (root * 4 + 1)
+ pop af
+ djnz .Ls_loop
+ ex de, hl
+ ret
+.Ls_less:
+ pop hl
+ djnz .Ls_loop
+ ex de, hl
+ ret
diff --git a/games/celeste/ce/src/ce.h b/games/celeste/ce/src/ce.h
index cbc7a03f..3cddd024 100644
--- a/games/celeste/ce/src/ce.h
+++ b/games/celeste/ce/src/ce.h
@@ -93,6 +93,18 @@ extern const uint8_t *g_remap, *g_remap_red, *g_remap_half; /* sprites drawn t
enum { DF_FLIPX = 1, DF_FLIPY = 2, DF_SOLID = 4 };
enum { TF_OPAQUE = 1 }; /* a tile without see-through pixels */ /* DF_SOLID: every pixel in one colour (draw_color) */
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 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;
+ bb_add(x0, y0, x1, y1, what);
+ return true;
+}
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 */
void draw_sprite_scaled(const uint8_t *spr, int x, int y, int sx, int sy, int ox, int oy, uint8_t flags);
@@ -104,9 +116,12 @@ void draw_pixel(int x, int y, uint8_t c);
void draw_circle_fill(int cx, int cy, int r, uint8_t c);
void draw_text(const char *s, int x, int y, uint8_t c); /* screen coordinates (0..320, 0..240) */
int text_width(const char *s);
-void draw_layers_back(void); /* background, bg tiles */
-void draw_layer_fg(void); /* fg tiles */
+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 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);
/* src/blit.S (host/blit.c) */
typedef struct { uint8_t opaque, drawn; const uint8_t *tile; } Cell; /* a tile to draw: its rows' masks, its data */
@@ -121,6 +136,7 @@ void blit_clip(const Cell *cell, uint8_t *dst, unsigned a, unsigned b, unsigned
void 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 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
@@ -151,7 +167,7 @@ int fdiv(int a, int b); /* floor(a / b), b > 0 */
int round_mu(int c, int dir); /* c / MU rounded as Celeste's floats round it (util.c) */
int bias_add(int b, int amount); /* the bias round_mu needs, kept within 24 bits */
int isqrt(int32_t v);
-int isqrt24(int v); /* v < 2^23 */
+int isqrt24(int v); /* 0 <= v < 2^23 (src/blit.S) */
int sin15(int a); /* a: 1024 a turn, result * 32768 */
static inline int sin256(int a) { return sin15(a) >> 7; }
static inline int cos256(int a) { return sin15(a + 256) >> 7; }
@@ -194,6 +210,8 @@ struct Ent {
int16_t a0, a1, a2, a3;
void *ptr;
f32 ptr2;
+ int16_t bb[2][4]; /* what it drew in each buffer (view pixels; empty if bb[k][0] >= bb[k][2]) */
+ unsigned sum[2];
};
#define MAX_ENTS 96
extern Ent *g_ents; /* (in the RAM plat_ram gives) */
@@ -206,7 +224,11 @@ Ent *ent_new(int type, int x, int y);
void ent_free(Ent *e);
void ents_spawn_room(int slot);
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 */
+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_remove_room(int slot);
void ents_clear(void);
bool rect_hits(int l, int t, int r, int b, const Ent *o);
@@ -253,7 +275,18 @@ extern Level g_level;
bool level_load(int chapter, int room, int intro);
bool level_ram(void); /* the RAM the rooms and entities take; false if there is not enough */
void level_update(void);
-void level_render(void);
+void level_render(bool whole); /* whole: the view all over (else what changed since this buffer was drawn) */
+void level_render_forget(void);
+#ifdef CEL_BENCH /* (tests/bench.sh) the CPU's time in each part of a frame, shown on the screen */
+unsigned long plat_cycles(void);
+extern unsigned long g_bench[16];
+extern unsigned long g_bench_last;
+#define BENCH(k) do { unsigned long t_ = plat_cycles(); g_bench[k] += (t_ - g_bench_last) & 0xFFFFFF; g_bench_last = t_; } while (0)
+#else
+#define BENCH(k) do { } while (0)
+#endif /* both buffers drawn whole next */
+void draw_gone(int x0, int y0, int x1, int y1); /* view pixels drawn over that went: both buffers draw them again */
+void draw_gone_buf(int k, int x0, int y0, int x1, int y1);
bool solid_at(int l, int t, int r, int b, const Ent *self); /* tiles or solid entities over [l, r) x [t, b) */
Ent *solid_first(int l, int t, int r, int b, const Ent *self);
Ent *jumpthru_first_outside(const Ent *e, int x, int y);
diff --git a/games/celeste/ce/src/data.c b/games/celeste/ce/src/data.c
index d75804e9..3272dc6d 100644
--- a/games/celeste/ce/src/data.c
+++ b/games/celeste/ce/src/data.c
@@ -84,15 +84,19 @@ static uint16_t shade(uint16_t c, int fade) {
}
static int cur_fade = 256;
static uint32_t cur_hair = 0xAC3232;
+static bool hair_set;
void palette_apply(int fade) {
uint16_t out[256];
cur_fade = fade;
out[0] = 0;
for (int i = 1; i < 256; i++) out[i] = shade(pal[i], fade);
plat_palette(out, 0, 256);
+ hair_set = false;
palette_hair(cur_hair);
}
void palette_hair(uint32_t rgb) {
+ if (hair_set && rgb == cur_hair) return;
+ hair_set = true;
cur_hair = rgb;
int r = rgb >> 16 & 255, g = rgb >> 8 & 255, b = rgb & 255;
uint16_t h[2];
diff --git a/games/celeste/ce/src/draw.c b/games/celeste/ce/src/draw.c
index 1d56e76a..167e59b3 100644
--- a/games/celeste/ce/src/draw.c
+++ b/games/celeste/ce/src/draw.c
@@ -4,6 +4,17 @@
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;
+int g_bb[4];
+unsigned g_bb_sum;
+void bb_add(int x0, int y0, int x1, int y1, unsigned what) {
+ if (x0 < g_bb[0]) g_bb[0] = x0;
+ if (y0 < g_bb[1]) g_bb[1] = y0;
+ if (x1 > g_bb[2]) g_bb[2] = x1;
+ if (y1 > g_bb[3]) g_bb[3] = y1;
+ g_bb_sum = (g_bb_sum << 2) + what + (unsigned)x0 + ((unsigned)y0 << 6) + ((unsigned)(x1 - x0) << 3); /* (adds only) */
+}
#define FB(x, y) (g_fb + (VIEW_Y + (y)) * 320 + (x))
/* a sprite at view (x, y) (its frame's top left), clipped to the view */
@@ -13,13 +24,13 @@ static void blit_sprite(const uint8_t *s, int x, int y, uint8_t flags) {
bool fx = flags & DF_FLIPX, fy = flags & DF_FLIPY, solid = flags & DF_SOLID;
uint8_t col = g_draw_color;
int left = fx ? x + fw - x0 - w : x + x0; /* the stored part's left on the view */
- if (left >= VIEW_W || left + w <= 0 || !h) return;
+ if (left >= g_clip_x1 || left + w <= g_clip_x0 || !h) return;
int vy = fy ? y + fh - 1 - y0 : y + y0, dy = fy ? -1 : 1;
uint8_t *row = g_fb + (VIEW_Y + vy) * 320;
int step = fy ? -320 : 320;
for (int r = 0; r < h; r++, vy += dy, row += step) {
int n = *p++;
- if (vy < 0 || vy >= VIEW_H) { /* skip the row */
+ if (vy < g_clip_y0 || vy >= g_clip_y1) { /* skip the row */
while (n--) p += 2 + p[1];
continue;
}
@@ -31,8 +42,8 @@ static void blit_sprite(const uint8_t *s, int x, int y, uint8_t flags) {
p += 2 + len;
if (!fx) {
int dx = x + x0 + at, a = 0, b = len;
- if (dx < 0) a = -dx;
- if (dx + b > VIEW_W) b = VIEW_W - dx;
+ if (dx < g_clip_x0) a = g_clip_x0 - dx;
+ if (dx + b > g_clip_x1) b = g_clip_x1 - dx;
if (a < b) {
if (solid) memset(row + dx + a, col, b - a);
else if (g_remap)
@@ -44,7 +55,7 @@ static void blit_sprite(const uint8_t *s, int x, int y, uint8_t flags) {
int base = x + fw - 1 - x0 - at;
uint8_t *d = row + base;
for (int i = 0; i < len; i++, d--)
- if ((unsigned)(base - i) < VIEW_W) *d = solid ? col : g_remap ? g_remap[px[i]] : px[i];
+ if (base - i >= g_clip_x0 && base - i < g_clip_x1) *d = solid ? col : g_remap ? g_remap[px[i]] : px[i];
}
at += len;
}
@@ -53,11 +64,20 @@ 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;
- int l = x + s[2], t = y + s[3];
- if (!flags && !g_remap && l >= 0 && t >= 0 && l + s[4] <= VIEW_W && t + s[5] <= VIEW_H) {
- if (s[5]) blit_rle(g_fb + (VIEW_Y + t) * 320 + l, s + 6, s[5]); /* (all of it on the view) */
+ if (g_dry) {
+ 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 (!(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]);
+ return;
+ }
+ }
blit_sprite(s, x, y, flags);
}
@@ -67,25 +87,11 @@ void draw_sprite_scaled(const uint8_t *s, int x, int y, int sx, int sy, int ox,
draw_sprite(s, x - ((flags & DF_FLIPX) ? s[0] - ox : ox), y - oy, flags);
return;
}
- /* decode into a buffer, then sample it */
- extern uint8_t *g_scratch;
- uint8_t *buf = g_scratch; /* (32 x 32) */
int fw = s[0], fh = s[1], x0 = s[2], y0 = s[3], h = s[5];
if (fw > 32 || fh > 32) {
draw_sprite(s, x - ((flags & DF_FLIPX) ? fw - ox : ox), y - oy, flags);
return;
}
- memset(buf, 0, (size_t)fw * fh);
- const uint8_t *p = s + 6;
- for (int r = 0; r < h; r++) {
- int n = *p++, at = 0;
- while (n--) {
- at += p[0];
- memcpy(buf + (y0 + r) * fw + x0 + at, p + 2, p[1]);
- at += p[1];
- p += 2 + p[1];
- }
- }
bool fx = flags & DF_FLIPX;
/* view pixel vx shows frame column u = ox + (rel * 256 + 128) / sx (rel from the origin, mirrored if flipped): a
* column's u, a row's v, worked out once each */
@@ -96,8 +102,26 @@ void draw_sprite_scaled(const uint8_t *s, int x, int y, int sx, int sy, int ox,
int a = bx - fdiv((fw - ox) * sx + 255, 256) + 1, b = bx - fdiv(-ox * sx, 256) + 1;
dx0 = a, dx1 = b;
}
- dx0 = imax(dx0, 0), dx1 = imin(dx1, VIEW_W);
+ if (dry_box(dx0, dy0, dx1, dy1, (unsigned)(uintptr_t)s + (unsigned)sx * 3 + (unsigned)sy * 5 + flags + (g_remap ? 77u : 0u)))
+ return;
+ dx0 = imax(dx0, g_clip_x0), dx1 = imin(dx1, g_clip_x1);
if (dx1 - dx0 > 64) dx1 = dx0 + 64;
+ int ya = imax(dy0, g_clip_y0), yb = imin(dy1, g_clip_y1);
+ if (dx0 >= dx1 || ya >= yb) return;
+ /* decode into a buffer, then sample it */
+ extern uint8_t *g_scratch;
+ uint8_t *buf = g_scratch; /* (32 x 32) */
+ memset(buf, 0, (size_t)fw * fh);
+ const uint8_t *p = s + 6;
+ for (int r = 0; r < h; r++) {
+ int n = *p++, at = 0;
+ while (n--) {
+ at += p[0];
+ memcpy(buf + (y0 + r) * fw + x0 + at, p + 2, p[1]);
+ at += p[1];
+ p += 2 + p[1];
+ }
+ }
int8_t us[64];
for (int vx = dx0; vx < dx1; vx++) {
int rel = vx - bx;
@@ -105,8 +129,8 @@ void draw_sprite_scaled(const uint8_t *s, int x, int y, int sx, int sy, int ox,
int u = ox + fdiv(rel * 256 + 128, sx);
us[vx - dx0] = (int8_t)((unsigned)u < (unsigned)fw ? u : -1);
}
- uint8_t *row = g_fb + (VIEW_Y + imax(dy0, 0)) * 320;
- for (int vy = imax(dy0, 0); vy < imin(dy1, VIEW_H); vy++, row += 320) {
+ uint8_t *row = g_fb + (VIEW_Y + ya) * 320;
+ for (int vy = ya; vy < yb; vy++, row += 320) {
int v = oy + fdiv((vy - by) * 256 + 128, sy);
if (v < 0 || v >= fh) continue;
const uint8_t *src = buf + v * fw;
@@ -122,10 +146,11 @@ 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 (x < 0) a = -x;
- if (x + 8 > VIEW_W) b = VIEW_W - x;
- if (y < 0) r0 = -y;
- if (y + 8 > VIEW_H) r1 = VIEW_H - y;
+ 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;
+ if (y + 8 > g_clip_y1) r1 = g_clip_y1 - y;
if (a >= b || r0 >= r1) return;
for (int r = r0; r < r1; r++) {
uint8_t *d = FB(x, y + r);
@@ -151,16 +176,18 @@ void draw_rect_screen(int x, int y, int w, int h, uint8_t c) {
}
void draw_rect(int x, int y, int w, int h, uint8_t c) {
x -= g_camx, y -= g_camy;
- if (x < 0) w += x, x = 0;
- if (y < 0) h += y, y = 0;
- if (x + w > VIEW_W) w = VIEW_W - x;
- if (y + h > VIEW_H) h = VIEW_H - y;
+ if (dry_box(x, y, x + w, y + h, c)) return;
+ if (x < g_clip_x0) w -= g_clip_x0 - x, x = g_clip_x0;
+ if (y < g_clip_y0) h -= g_clip_y0 - y, y = g_clip_y0;
+ if (x + w > g_clip_x1) w = g_clip_x1 - x;
+ if (y + h > g_clip_y1) h = g_clip_y1 - y;
if (w <= 0 || h <= 0) return;
for (int r = 0; r < h; r++) memset(FB(x, y + r), c, w);
}
void draw_pixel(int x, int y, uint8_t c) {
x -= g_camx, y -= g_camy;
- if ((unsigned)x < VIEW_W && (unsigned)y < VIEW_H) *FB(x, y) = c;
+ if (dry_box(x, y, x + 1, y + 1, c)) return;
+ if (x >= g_clip_x0 && y >= g_clip_y0 && x < g_clip_x1 && y < g_clip_y1) *FB(x, y) = c;
}
void draw_line(int x0, int y0, int x1, int y1, uint8_t c) {
int dx = iabs(x1 - x0), dy = -iabs(y1 - y0), sx = x0 < x1 ? 1 : -1, sy = y0 < y1 ? 1 : -1, err = dx + dy;
@@ -174,7 +201,7 @@ void draw_line(int x0, int y0, int x1, int y1, uint8_t c) {
}
void draw_circle_fill(int cx, int cy, int r, uint8_t c) {
for (int y = -r; y <= r; y++) {
- int w = isqrt((int32_t)r * r - (int32_t)y * y);
+ int w = isqrt24(r * r - y * y);
draw_rect(cx - w, cy + y, 2 * w + 1, 1, c);
}
}
@@ -207,39 +234,44 @@ static void cells_build(int c0, int r0) {
cache_c0 = c0, cache_r0 = r0, cache_gen = g_cells_gen;
}
-/* a row of cells of a layer: the 39 inside the view at once, the two at its sides clipped */
-static void layer_row(const Cell *cells, uint8_t *line, int offx, unsigned rows) {
- blit_row(cells + 1, COLS - 2, line + 8 - offx, rows);
- blit_clip(cells, line - offx, (unsigned)offx, 8, rows);
- if (offx) blit_clip(cells + COLS - 1, line + 320 - offx, 0, (unsigned)offx, rows);
+/* 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);
}
-static int view_offy;
static unsigned rows_mask(int r, uint8_t **line) { /* row r of cells: its rows in the view, and its top's place */
- int vy = r * 8 - view_offy;
+ int vy = r * 8 - g_offy;
int r0 = vy < 0 ? -vy : 0, r1 = vy + 8 > VIEW_H ? VIEW_H - vy : 8;
*line = g_fb + (VIEW_Y + vy) * 320;
return r0 >= r1 ? 0 : (0xFFu << r0) & (0xFFu >> (8 - r1));
}
-void draw_layers_back(void) {
- int c0 = g_camx >> 3, r0 = g_camy >> 3, offx = g_camx & 7;
- view_offy = g_camy & 7;
+void draw_cells_update(void) {
+ int c0 = g_camx >> 3, r0 = g_camy >> 3;
+ 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);
+}
+
+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 */
int ox = fdiv((g_camx - g_bg_refx) * g_bg_scrollx, 256), oy = fdiv((g_camy - g_bg_refy) * g_bg_scrolly, 256);
ox %= 320;
if (ox < 0) ox += 320;
- for (int r = 0; r < CROWS; r++) {
+ for (int r = ra; r <= rb; r++) {
uint8_t *line;
unsigned rows = rows_mask(r, &line);
if (!rows) continue;
CellRow *row = cells_row(r);
- int vy = r * 8 - view_offy, y0 = imax(vy, 0), y1 = imin(vy + 8, VIEW_H);
- for (int c = 0; c < COLS;) { /* the background, in runs of cells it shows in */
+ 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; }
int e = c + 1;
- while (e < COLS && row->vis[e]) e++;
+ while (e <= cb && row->vis[e]) e++;
int xa = imax(c * 8 - offx, 0), xb = imin(e * 8 - offx, VIEW_W);
c = e;
if (xa >= xb) continue;
@@ -263,17 +295,15 @@ void draw_layers_back(void) {
y += k, d += 320 * k;
}
}
- layer_row(row->bg, line, offx, rows);
+ layer_row(row->bg, line, ca, cb, rows);
}
}
-void draw_layer_fg(void) {
- int offx = g_camx & 7;
- view_offy = g_camy & 7;
- for (int r = 0; r < CROWS; r++) {
+void draw_layer_fg(int ca, int cb, int ra, int rb) {
+ for (int r = ra; r <= rb; r++) {
uint8_t *line;
unsigned rows = rows_mask(r, &line);
- if (rows) layer_row(cells_row(r)->fg, line, offx, rows);
+ if (rows) layer_row(cells_row(r)->fg, line, ca, cb, rows);
}
}
@@ -290,11 +320,28 @@ void draw_decals(int fg) {
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 >= VIEW_W || y - g_camy >= VIEW_H || x + s[0] - g_camx <= 0 || y + s[1] - g_camy <= 0) continue;
+ 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 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]);
+ }
+ }
+}
/* ---------------------------------------------------------------- text: a 5 x 7 font for now */
static const uint8_t FONT[96][5] = {
@@ -323,3 +370,26 @@ void draw_text(const char *s, int x, int y, uint8_t c) {
}
}
}
+
+#ifdef CEL_BENCH
+/* the assembly against the C (tests/bench.sh): 0 when they agree */
+int draw_selftest(void) {
+ int bad = 0;
+ for (int v = 0; v < (1 << 23); v += 99991)
+ if (isqrt24(v) != isqrt(v)) bad |= 1;
+ for (int k = 1; k < 2896; k++)
+ if (isqrt24(k * k) != k || isqrt24(k * k - 1) != k - 1) bad |= 1;
+ g_camx = g_camy = 0;
+ for (int id = 0; id < S_hair_bangs + 12; id++) {
+ const uint8_t *s = sprite(id);
+ if (s[0] > 80 || s[1] > 100 || !s[5]) continue;
+ memset(g_fb + VIEW_Y * 320, 0, 320 * s[1]);
+ draw_sprite(s, 0, 0, DF_FLIPX);
+ 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;
+ }
+ memset(g_fb, 0, 320 * 240);
+ return bad;
+}
+#endif
diff --git a/games/celeste/ce/src/ents.c b/games/celeste/ce/src/ents.c
index 41af0c2f..9ff90dec 100644
--- a/games/celeste/ce/src/ents.c
+++ b/games/celeste/ce/src/ents.c
@@ -35,6 +35,8 @@ Ent *ent_new(int type, int x, int y) {
return e;
}
void ent_free(Ent *e) {
+ for (int k = 0; k < 2; k++) /* what it drew goes */
+ if (e->bb[k][0] < e->bb[k][2]) draw_gone_buf(k, e->bb[k][0], e->bb[k][1], e->bb[k][2], e->bb[k][3]);
e->type = 255;
e->flags = 0;
while (g_nents && g_ents[g_nents - 1].type == 255) g_nents--;
@@ -467,9 +469,12 @@ static void crumble_render(Ent *e) {
if (e->t3 > TIM(.25f)) tiles_render(e, n, n, n, 0, 0);
return;
}
- for (int i = 0; i < n; i++) {
+ for (int i = 0; i < n; i++) { /* shaking: each piece a pixel off at random (from the frame: drawing twice gives the same) */
int sx = 0, sy = 0;
- if (e->t2 > 0) sx = rndn(3) - 1, sy = rndn(3) - 1;
+ if (e->t2 > 0) {
+ unsigned h = ((unsigned)g_level.frame * 37u + (unsigned)i * 11u + (unsigned)e->x) * 0x9E37u;
+ sx = (int)(h >> 9 & 0xFF) % 3 - 1, sy = (int)(h >> 3 & 0xFF) % 3 - 1;
+ }
draw_tile(rec_val(e->rec, i), e->x + i * 8 + sx, e->y + sy);
}
}
@@ -801,8 +806,9 @@ void ents_update(void) {
for (int i = 0; i < g_nents; i++) {
Ent *e = &g_ents[i];
if (e->type == 255 || !(e->flags & EF_ACTIVE)) continue;
+ BENCH(11);
switch (e->type) {
- case ET_player: player_update(); break;
+ case ET_player: player_update(); BENCH(12); break;
case ET_strawberry: case ET_goldenBerry: berry_update(e); break;
case ET_spring: case ET_wallSpringLeft: case ET_wallSpringRight: spring_update(e); break;
case ET_refill: refill_update(e); break;
@@ -812,10 +818,12 @@ void ents_update(void) {
case ET_zipMover: zip_update(e); break;
case 200: points_update(e); break;
}
+ BENCH(13);
}
leader_update();
extern void player_after_update(void);
player_after_update();
+ BENCH(14);
}
/* PlayerCollider and triggers: what Madeline (her hurt box) touches */
void ents_player_touch(Ent *p) {
@@ -840,38 +848,80 @@ void ents_player_touch(Ent *p) {
}
}
}
-void ents_render(int dmax, int dmin) {
- /* back to front: higher depths first, then the order they came in */
- int last = 0x7FFFFF;
- for (;;) {
- int d = -0x7FFFFF;
- for (int i = 0; i < g_nents; i++) {
- Ent *e = &g_ents[i];
- if (e->type != 255 && (e->flags & EF_VISIBLE) && e->depth < last && e->depth > d && e->depth <= dmax && e->depth > dmin)
- d = e->depth;
+static void ent_draw(Ent *e) {
+ switch (e->type) {
+ case ET_player: player_render(); break;
+ case ET_spikesUp: case ET_spikesDown: case ET_spikesLeft: case ET_spikesRight: spikes_render(e); break;
+ case ET_jumpThru: jumpthru_render(e); break;
+ case ET_strawberry: case ET_goldenBerry: berry_render(e); break;
+ case ET_spring: case ET_wallSpringLeft: case ET_wallSpringRight: spring_render(e); break;
+ case ET_refill: refill_render(e); break;
+ case ET_crumbleBlock: crumble_render(e); break;
+ case ET_fallingBlock: case ET_dashBlock: block_render(e); break;
+ case ET_fakeWall: case ET_coverupWall: fakewall_render(e); break;
+ case ET_zipMover: zip_render(e); break;
+ case 200: {
+ const uint8_t *s = sprite(anim_sprite(e));
+ draw_sprite(s, e->x - s[0] / 2, e->y - s[1] / 2, 0);
+ break;
}
- if (d == -0x7FFFFF) return;
- for (int i = 0; i < g_nents; i++) {
- Ent *e = &g_ents[i];
- if (e->type == 255 || !(e->flags & EF_VISIBLE) || e->depth != d) continue;
- switch (e->type) {
- case ET_player: player_render(); break;
- case ET_spikesUp: case ET_spikesDown: case ET_spikesLeft: case ET_spikesRight: spikes_render(e); break;
- case ET_jumpThru: jumpthru_render(e); break;
- case ET_strawberry: case ET_goldenBerry: berry_render(e); break;
- case ET_spring: case ET_wallSpringLeft: case ET_wallSpringRight: spring_render(e); break;
- case ET_refill: refill_render(e); break;
- case ET_crumbleBlock: crumble_render(e); break;
- case ET_fallingBlock: case ET_dashBlock: block_render(e); break;
- case ET_fakeWall: case ET_coverupWall: fakewall_render(e); break;
- case ET_zipMover: zip_render(e); break;
- case 200: {
- const uint8_t *s = sprite(anim_sprite(e));
- draw_sprite(s, e->x - s[0] / 2, e->y - s[1] / 2, 0);
- break;
- }
- }
+ }
+}
+
+int g_cull_buf = -1;
+/* back to front: higher depths first, then the order they came in */
+static uint8_t order[MAX_ENTS], norder;
+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;
+ 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;
+ }
+}
+void ents_render(int dmax, int dmin) {
+ int cb = g_cull_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;
+ ent_draw(e);
+ }
+}
+
+/* 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);
+}
+void ents_measure(int buf, void (*changed)(const int16_t *was, const int *now)) {
+ g_dry = true;
+ for (int i = 0; i < g_nents; i++) {
+ Ent *e = &g_ents[i];
+ if (e->type == 255) continue;
+ measure(e);
+ 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;
}
- last = d;
}
+ g_dry = 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;
}
diff --git a/games/celeste/ce/src/game.c b/games/celeste/ce/src/game.c
index a1b997f3..7b63e385 100644
--- a/games/celeste/ce/src/game.c
+++ b/games/celeste/ce/src/game.c
@@ -10,6 +10,10 @@ enum { SC_TITLE, SC_CHAPTERS, SC_PLAY, SC_PAUSE, SC_COMPLETE };
static int screen = SC_TITLE, sel, chapter = 1;
static int screen_t;
static bool full_redraw = true;
+#ifdef CEL_BENCH
+unsigned long g_bench[16], g_bench_last;
+static int selftest;
+#endif
/* ---------------------------------------------------------------- the save (appvar CELSAVE) */
#define NCHAPTERS 10
@@ -92,6 +96,7 @@ static void hud_draw(void) {
num(buf + 1, (uint32_t)(berries_of(chapter)));
draw_sprite(sprite(S_collectables_strawberry_normal00), g_camx + 2, g_camy + 2, 0);
draw_text(buf, 22, VIEW_Y + 8, WHITE);
+ draw_gone(0, 0, 22 + text_width(buf), 20); /* (drawn over the view: both buffers draw it again) */
}
}
@@ -154,7 +159,7 @@ static void wipe_draw(void) { /* AngledWipe: black bands sliding across, 24 fr
}
void game_draw(void) {
- if (full_redraw) draw_clear_screen(0), full_redraw = false;
+ if (full_redraw) draw_clear_screen(0), full_redraw = false, level_render_forget();
switch (screen) {
case SC_TITLE:
draw_clear_screen(0);
@@ -174,9 +179,36 @@ void game_draw(void) {
}
case SC_PLAY:
case SC_PAUSE:
- level_render();
+ level_render(screen == SC_PAUSE || g_level.wipe || g_level.completed);
hud_draw();
wipe_draw();
+ BENCH(7);
+#ifdef CEL_BENCH
+ { /* thousands of cycles a frame (over the last 32 frames): update, wait, cells, measure, back, entities, fg,
+ * hud, swap, rest */
+ static unsigned frames;
+ static unsigned shown[16];
+ if (++frames % 32 == 0) {
+ for (int k = 0; k < 16; k++) shown[k] = (unsigned)(g_bench[k] / 32000);
+ memset(g_bench, 0, sizeof g_bench);
+ }
+ draw_rect_screen(0, -VIEW_Y, 320, VIEW_Y, 0);
+ char buf[96], *q = buf;
+ for (int k = 0; k < 16; k++) {
+ if (k == 10) *q++ = '/', *q++ = ' ';
+ unsigned v = shown[k];
+ char tmp[8];
+ int n = 0;
+ do tmp[n++] = (char)('0' + v % 10), v /= 10; while (v);
+ while (n) *q++ = tmp[--n];
+ *q++ = ' ';
+ }
+ *q = 0;
+ draw_text(buf, 2, 4, WHITE);
+ if (q - buf > 52) draw_text(buf + 52, 2, 14, WHITE);
+ draw_text(selftest ? "BAD" : "ok", 290, 14, WHITE);
+ }
+#endif
if (screen == SC_PAUSE) {
static const char *items[] = {"Resume", "Retry", "Save & Quit"};
draw_rect_screen(100, 50, 120, 80, 0);
@@ -212,6 +244,13 @@ void game_start_room(int ch, const char *room, int x, int y) {
void game_init(void) {
save_read();
palette_apply(256);
+#ifdef CEL_BENCH /* a test build (tests/bench.sh): straight into that room, the camera going about it */
+#define STR_(x) #x
+#define STR(x) STR_(x)
+ extern int draw_selftest(void);
+ selftest = draw_selftest();
+ game_start_room(1, STR(CEL_BENCH), 0, 0);
+#endif
#ifndef __TICE__
{
extern char *getenv(const char *);
diff --git a/games/celeste/ce/src/gen.h b/games/celeste/ce/src/gen.h
index acc905f8..ead42a86 100644
--- a/games/celeste/ce/src/gen.h
+++ b/games/celeste/ce/src/gen.h
@@ -505,6 +505,8 @@
/* banks' origins: mode (0 none, 1 pixels, 2 justify x256), x, y */
#define BANK_ORIGINS {{1, 16, 32}, {2, 128, 128}, {2, 128, 128}, {2, 128, 128}, {2, 128, 128}}
#define NBANKS 5
+#define S_hair_disc 438
+#define S_hair_bangs 480
#define COMMON 96
#define HAIR 1
#define HAIR_DARK 2
diff --git a/games/celeste/ce/src/level.c b/games/celeste/ce/src/level.c
index ca060ca9..0aa58e5f 100644
--- a/games/celeste/ce/src/level.c
+++ b/games/celeste/ce/src/level.c
@@ -10,14 +10,16 @@ uint8_t *g_cell_cache; /* draw.c's */
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;
+ unsigned n = sizeof(Ent) * MAX_ENTS + 2 * (GRID_BYTES + 256 * sizeof(TEnt)) + 1024 + CELL_CACHE + CROWS * COLS;
uint8_t *m = plat_ram(n);
if (!m) return false;
memset(m, 0, n);
g_ents = (Ent *)(void *)m, m += sizeof(Ent) * MAX_ENTS;
g_scratch = m, m += 1024;
g_cell_cache = m, m += CELL_CACHE;
+ g_dirty = m, m += CROWS * COLS;
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;
@@ -362,13 +364,150 @@ void level_update(void) {
}
}
-void level_render(void) {
- g_camx = (g_level.camx >> 8) + g_level.shakex;
- g_camy = (g_level.camy >> 8) + g_level.shakey;
- draw_layers_back();
+/* ---------------------------------------------------------------- drawing what changed */
+/* Each of the two buffers still has what was drawn in it two frames ago. While the camera and the rooms' tiles are
+ * where they were then, only the cells where something changed are drawn again (an entity's box then and now, what
+ * 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;
+#define MAX_GONE 8
+static int16_t gone[2][MAX_GONE][4];
+static uint8_t ngone[2];
+static bool gone_full[2];
+void draw_gone_buf(int k, int x0, int y0, int x1, int y1) {
+ if (ngone[k] == MAX_GONE) { gone_full[k] = true; return; }
+ int16_t *g = gone[k][ngone[k]++];
+ g[0] = (int16_t)x0, g[1] = (int16_t)y0, g[2] = (int16_t)x1, g[3] = (int16_t)y1;
+}
+void draw_gone(int x0, int y0, int x1, int y1) { draw_gone_buf(0, x0, y0, x1, y1), draw_gone_buf(1, x0, y0, x1, y1); }
+static void mark(int x0, int y0, int x1, int y1) { /* view pixels [x0, x1) x [y0, y1) */
+ x0 = imax(x0, 0), y0 = imax(y0, 0), x1 = imin(x1, VIEW_W), y1 = imin(y1, VIEW_H);
+ if (x0 >= x1 || y0 >= y1) return;
+ 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;
+}
+static void changed(const int16_t *a, const int *b) {
+ mark(a[0], a[1], a[2], a[3]);
+ mark(b[0], b[1], b[2], b[3]);
+}
+static void render_cells(int ca, int cb, int ra, int rb) {
+ g_clip_x0 = imax(ca * 8 - g_offx, 0), g_clip_x1 = imin(cb * 8 + 8 - g_offx, VIEW_W);
+ g_clip_y0 = imax(ra * 8 - g_offy, 0), g_clip_y1 = imin(rb * 8 + 8 - g_offy, VIEW_H);
+ draw_layers_back(ca, cb, ra, rb);
+ BENCH(4);
draw_decals(0);
+ BENCH(10);
ents_render(32767, -10000);
- draw_layer_fg();
+ BENCH(5);
+ draw_layer_fg(ca, cb, ra, rb);
+ BENCH(6);
draw_decals(1);
+ BENCH(10);
ents_render(-10000, -32768);
+ BENCH(5);
+}
+void level_render(bool whole) {
+ g_camx = (g_level.camx >> 8) + g_level.shakex;
+ g_camy = (g_level.camy >> 8) + g_level.shakey;
+#ifdef CEL_BENCH /* back and forth across the room at 90 px/s (Madeline's run), and up and down at half that */
+ {
+ static int t;
+ int sx = g_level.room->w - VIEW_W, sy = g_level.room->h - VIEW_H;
+ t += 3;
+ int a = t / 2 % (2 * sx + 1), b = t / 4 % (2 * sy + 1);
+ g_camx = g_level.room->x + (a > sx ? 2 * sx - a : a), g_camy = g_level.room->y + (b > sy ? 2 * sy - b : b);
+ }
+#endif
+ draw_cells_update();
+ ents_order();
+ BENCH(2);
+ if (whole || gone_full[buf] || !was[buf].ok || was[buf].camx != g_camx || was[buf].camy != g_camy ||
+ was[buf].gen != g_cells_gen) {
+ render_cells(0, COLS - 1, 0, CROWS - 1);
+ ents_record(buf);
+ BENCH(3);
+ } else {
+ memset(g_dirty, 0, CROWS * COLS);
+ any_dirty = false;
+ 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)
+ for (int c = 0; c < COLS;) {
+ if (!row[c]) { c++; continue; }
+ 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;
+ memset(nx + c, 0, (size_t)(e - c + 1));
+ rb++;
+ }
+ render_cells(c, e, r, rb);
+ c = e + 1;
+ }
+ }
+ g_cull_buf = -1;
+#ifndef __TICE__
+ { /* CEL_VERIFY: each such frame against the whole view drawn again */
+ extern char *getenv(const char *);
+ static int verify = -1, bad;
+ if (verify < 0) verify = getenv("CEL_VERIFY") != NULL;
+ if (verify) {
+ static uint8_t keep[320 * VIEW_H];
+ uint8_t *v = g_fb + VIEW_Y * 320;
+ memcpy(keep, v, sizeof keep);
+ render_cells(0, COLS - 1, 0, CROWS - 1);
+ int n = 0, fx = -1, fy = -1;
+ for (int i = 0; i < 320 * VIEW_H; i++)
+ if (keep[i] != v[i]) { if (!n) fx = i % 320, fy = i / 320; n++; }
+ if (n && !bad && getenv("CEL_OUT")) { /* the first: both pictures */
+ extern void png(const char *, const uint8_t *);
+ extern int sprintf(char *, const char *, ...);
+ static uint8_t fb2[320 * 240];
+ char path[512];
+ memcpy(fb2, g_fb, sizeof fb2);
+ sprintf(path, "%s/verify_full.png", getenv("CEL_OUT"));
+ png(path, fb2);
+ memcpy(fb2 + VIEW_Y * 320, keep, sizeof keep);
+ sprintf(path, "%s/verify_part.png", getenv("CEL_OUT"));
+ png(path, fb2);
+ extern int printf(const char *, ...);
+ printf("ents:");
+ for (int i = 0; i < g_nents; i++)
+ if (g_ents[i].type != 255)
+ printf(" %d(%d,%d d%d bb %d %d %d %d / %d %d %d %d)", g_ents[i].type, g_ents[i].x - g_camx, g_ents[i].y - g_camy,
+ g_ents[i].depth, g_ents[i].bb[0][0], g_ents[i].bb[0][1], g_ents[i].bb[0][2], g_ents[i].bb[0][3],
+ g_ents[i].bb[1][0], g_ents[i].bb[1][1], g_ents[i].bb[1][2], g_ents[i].bb[1][3]);
+ printf("\n");
+ }
+ if (n && bad++ < 20) {
+ extern int printf(const char *, ...);
+ printf("verify: frame %u: %d pixels differ, first at (%d, %d)\n", (unsigned)g_level.frame, n, fx, fy);
+ }
+ memcpy(v, keep, sizeof keep);
+ static int parts;
+ if (++parts % 100 == 0) {
+ extern int printf(const char *, ...);
+ printf("verify: %d frames drawn in part, %d wrong\n", parts, bad);
+ }
+ }
+ }
+#endif
+ }
+ g_clip_x0 = 0, g_clip_y0 = 0, g_clip_x1 = VIEW_W, g_clip_y1 = VIEW_H;
+ ngone[buf] = 0, gone_full[buf] = false;
+ was[buf].camx = g_camx, was[buf].camy = g_camy, was[buf].gen = g_cells_gen, was[buf].frame = (int)g_level.frame;
+ was[buf].ok = !whole;
+ buf ^= 1;
}
+void level_render_forget(void) { was[0].ok = was[1].ok = 0; }
diff --git a/games/celeste/ce/src/main.c b/games/celeste/ce/src/main.c
index 06b4333e..d8f91998 100644
--- a/games/celeste/ce/src/main.c
+++ b/games/celeste/ce/src/main.c
@@ -37,17 +37,21 @@ int main(void) {
last = now;
if (acc > 32768 * 6) acc = 32768 * 6; /* far behind: no rush */
int n = 0;
+ BENCH(9);
while (acc >= 32768 && n < 3 && g_running) {
game_frame();
g_perf_updates++;
acc -= 32768;
n++;
}
+ BENCH(0);
if (!g_running) break;
if (n) {
plat_begin_draw();
+ BENCH(1);
game_draw();
plat_frame();
+ BENCH(8);
g_perf_frames++;
} else
plat_wait((unsigned)(32768 - acc) / 60);
diff --git a/games/celeste/ce/src/plat_ce.c b/games/celeste/ce/src/plat_ce.c
index e31d92b0..a05c5066 100644
--- a/games/celeste/ce/src/plat_ce.c
+++ b/games/celeste/ce/src/plat_ce.c
@@ -19,6 +19,11 @@ void plat_init(void) {
timer_Disable(2);
timer_Set(2, 0);
timer_Enable(2, TIMER_32K, TIMER_NOINT, TIMER_UP);
+#ifdef CEL_BENCH
+ timer_Disable(1);
+ timer_Set(1, 0);
+ timer_Enable(1, TIMER_CPU, TIMER_NOINT, TIMER_UP);
+#endif
kb_DisableOnLatch();
}
/* The RAM the game takes besides its program and static data: an appvar, CELRAM, made last (the save before it, in
@@ -108,3 +113,13 @@ bool plat_load(const char *name, void *data, unsigned n) {
return ok;
}
void plat_unpack(void *dst, const void *src) { zx0_Decompress(dst, src); }
+
+#ifdef CEL_BENCH
+unsigned long plat_cycles(void) { /* its low 24 bits, read twice until the reads agree (a byte may carry mid-read) */
+ volatile unsigned *t = (volatile unsigned *)TIMER_COUNT_ADDR(1);
+ for (;;) {
+ unsigned a = *t, b = *t;
+ if (((b - a) & 0xFFFFFF) < 200) return b;
+ }
+}
+#endif
diff --git a/games/celeste/ce/src/player.c b/games/celeste/ce/src/player.c
index 2ae9988d..46dec3d4 100644
--- a/games/celeste/ce/src/player.c
+++ b/games/celeste/ce/src/player.c
@@ -956,15 +956,17 @@ static void hair_update(void) { /* PlayerHair.AfterUpdate (1/256 px): the node
P->hairy[0] = (E->y - 9 * P->scale_y / 1024 + (int8_t)m[1]) * 256;
int tx = P->hairx[0] - fac * 128, ty = P->hairy[0] + 512;
for (int i = 1; i < P->hair_count; i++) {
+ /* (lengths from 1/16 px: their squares stay in 24 bits) */
int dx = tx - P->hairx[i], dy = ty - P->hairy[i];
if (iabs(dx) > 32 * 256 || iabs(dy) > 32 * 256) P->hairx[i] = tx, P->hairy[i] = ty, dx = dy = 0;
- int l = isqrt((int32_t)dx * dx + (int32_t)dy * dy);
+ int l = isqrt24((dx >> 4) * (dx >> 4) + (dy >> 4) * (dy >> 4)) << 4;
int mv = (256 - i * 128 / P->hair_count) * 64 / 60;
if (l <= mv) P->hairx[i] = tx, P->hairy[i] = ty;
- else P->hairx[i] += (int)((int32_t)dx * mv / l), P->hairy[i] += (int)((int32_t)dy * mv / l);
+ else P->hairx[i] += dx * mv / l, P->hairy[i] += dy * mv / l;
dx = P->hairx[i] - P->hairx[i - 1], dy = P->hairy[i] - P->hairy[i - 1];
- l = isqrt((int32_t)dx * dx + (int32_t)dy * dy);
- if (l > 768) P->hairx[i] = P->hairx[i - 1] + (int)((int32_t)dx * 768 / l), P->hairy[i] = P->hairy[i - 1] + (int)((int32_t)dy * 768 / l);
+ if (iabs(dx) > 32 * 256 || iabs(dy) > 32 * 256) P->hairx[i] = P->hairx[i - 1], P->hairy[i] = P->hairy[i - 1], dx = dy = 0;
+ l = isqrt24((dx >> 4) * (dx >> 4) + (dy >> 4) * (dy >> 4)) << 4;
+ if (l > 768) P->hairx[i] = P->hairx[i - 1] + dx * 768 / l, P->hairy[i] = P->hairy[i - 1] + dy * 768 / l;
tx = P->hairx[i] - fac * 128, ty = P->hairy[i] + 512;
}
}
@@ -1223,58 +1225,20 @@ void player_before_down_transition(void) {
/* PlayerHair.Render: the bangs, then circles shrinking down the hair (hair00, scaled 0.25 + (1 - i / n) * 0.75), each
* outlined in black by drawing it a pixel off in four directions first. A circle's rows come from its mask, a row at a
* time (the scale's samples: no division per pixel). */
-static void hline(int y, int x0, int x1, uint8_t c) { /* view pixels [x0, x1] of row y */
- if ((unsigned)y >= VIEW_H) return;
- if (x0 < 0) x0 = 0;
- if (x1 >= VIEW_W) x1 = VIEW_W - 1;
- if (x0 <= x1) memset(g_fb + (VIEW_Y + y) * 320 + x0, c, (unsigned)(x1 - x0 + 1));
-}
-static void hair_draw(bool outline) {
+/* PlayerHair.Render: the bangs, then circles shrinking down the hair (hair00, scaled 0.25 + (1 - i / n) * 0.75), each
+ * outlined in black by drawing it a pixel off in four directions first: all outlines, then the hair from its end.
+ * Each piece is a picture of its own (tools/pack_ce.py: S_hair_disc, S_hair_bangs; its place at (8, 8)). */
+static void hair_draw(void) {
const uint8_t *m = player_meta(cur_sprite());
if (!(m[2] & 0x80)) return;
int n = P->hair_count, bangs = m[2] & 0x7F;
- for (int i = outline ? 0 : n - 1; outline ? i < n : i >= 0; i += outline ? 1 : -1) {
- int cx = (i == 0 ? P->hairx[0] >> 8 : (P->hairx[i] + 128) >> 8) - g_camx;
- int cy = (i == 0 ? P->hairy[0] >> 8 : (P->hairy[i] + 128) >> 8) - g_camy;
- if (i == 0) { /* the bangs: their own picture, both shades, unscaled */
- const uint16_t *mask = hair_mask(1 + (bangs < 3 ? bangs : 0));
- bool flip = P->facing < 0;
- for (int v = 0; v < 10; v++) {
- unsigned bits = mask[v], dark = mask[10 + v];
- if (!bits) continue;
- int y = cy - 5 + v;
- for (int u = 0; u < 10; u++) {
- if (!(bits >> u & 1)) continue;
- int x = cx + (flip ? 4 - u : u - 5);
- if (outline) {
- hline(y, x - 1, x + 1, BLACK);
- hline(y - 1, x, x, BLACK), hline(y + 1, x, x, BLACK);
- } else
- hline(y, x, x, dark >> u & 1 ? HAIR_DARK : HAIR);
- }
- }
- continue;
- }
- /* a circle: hair00's rows (an 8 x 8 disc in a 10 x 10 frame), scaled by k (1/256) around the frame's middle */
- const uint16_t *mask = hair_mask(0);
- int k = 64 + (256 - i * 256 / n) * 3 / 4;
- int half = (5 * k + 128) >> 8;
- for (int dy = -half; dy < half; dy++) {
- int v = 5 + fdiv(dy * 256 + 128, k);
- if ((unsigned)v >= 10) continue;
- unsigned bits = mask[v];
- if (!bits) continue;
- int u0 = 0, u1 = 9;
- while (!(bits >> u0 & 1)) u0++;
- while (!(bits >> u1 & 1)) u1--;
- int x0 = cx + fdiv((u0 - 5) * k, 256), x1 = cx + fdiv((u1 - 4) * k - 1, 256);
- if (outline) {
- hline(cy + dy, x0 - 1, x1 + 1, BLACK);
- hline(cy + dy - 1, x0, x1, BLACK), hline(cy + dy + 1, x0, x1, BLACK);
- } else
- hline(cy + dy, x0, x1, HAIR);
+ int disc = S_hair_disc + ((n - 1) * (n - 2) >> 1) * 2 - 2; /* (+ 2 * i) */
+ int b = S_hair_bangs + (2 * (bangs < 3 ? bangs : 0) + (P->facing < 0)) * 2;
+ for (int pass = 1; pass >= 0; pass--) /* (the outlines, then the hair) */
+ for (int i = pass ? 0 : n - 1; pass ? i < n : i >= 0; i += pass ? 1 : -1) {
+ int x = i == 0 ? P->hairx[0] >> 8 : (P->hairx[i] + 128) >> 8, y = i == 0 ? P->hairy[0] >> 8 : (P->hairy[i] + 128) >> 8;
+ draw_sprite(sprite((i == 0 ? b : disc + 2 * i) + pass), x - 8, y - 8, 0);
}
- }
}
static void death_effect(int x, int y, int t) { /* DeathEffect: 8 hair circles flying out, t 0..256 */
@@ -1297,14 +1261,13 @@ void player_render(void) {
else if (P->death_t >= 0 && P->death_t <= TIM(.834f)) death_effect(E->x, E->y - 6, P->death_t * 256 / TIM(.834f));
return;
}
- palette_hair(P->hair_rgb);
+ palette_hair(P->hair_rgb); /* (measured or drawn: once a frame at least) */
if (P->state == ST_INTRORESPAWN) {
int t = imin(P->intro_t * 256 / TIM(.6f), 256);
death_effect(E->x + P->respawn_fromx * (256 - t) / 256, E->y - 6 + P->respawn_fromy * (256 - t) / 256, 256 - t);
return;
}
- hair_draw(true);
- hair_draw(false);
+ hair_draw();
extern const uint8_t *g_remap;
extern const uint8_t *g_remap_red;
if (is_tired() && P->flash) g_remap = g_remap_red; /* (Color.Red: tired) */
diff --git a/games/celeste/ce/src/util.c b/games/celeste/ce/src/util.c
index 5e53e4c4..12bb0e22 100644
--- a/games/celeste/ce/src/util.c
+++ b/games/celeste/ce/src/util.c
@@ -24,18 +24,6 @@ int bias_add(int b, int amount) {
return b > 4000000 ? 4000000 : b < -4000000 ? -4000000 : b;
}
-int isqrt24(int v) { /* v < 2^23 */
- if (v <= 0) return 0;
- unsigned r = 0, b = 1u << 22, x = (unsigned)v;
- while (b > x) b >>= 2;
- while (b) {
- if (x >= r + b) x -= r + b, r = (r >> 1) + b;
- else r >>= 1;
- b >>= 2;
- }
- return (int)r;
-}
-
int isqrt(int32_t v) {
if (v <= 0) return 0;
uint32_t r = 0, b = 1ul << 30, x = (uint32_t)v;
diff --git a/games/celeste/ce/tests/bench.sh b/games/celeste/ce/tests/bench.sh
new file mode 100755
index 00000000..a4551bf3
--- /dev/null
+++ b/games/celeste/ce/tests/bench.sh
@@ -0,0 +1,18 @@
+#!/bin/bash
+# Drawing speed in the emulator (tools/ce_emu.py): a build that goes straight into a room and moves the camera about it
+# (every frame drawn whole), and the game's own build standing still in the first room (what changed drawn only).
+# tests/bench.sh [ROOM] [extra ce_emu options]
+set -e
+HERE="$(cd "$(dirname "$0")/.." && pwd)"
+ROOM="${1:-11}"
+shift || true
+cd "$HERE"
+export PATH="$HOME/Documents/Assets/toolchains/CEdev/bin:$PATH"
+make -s OBJDIR=build/bench/obj BINDIR=build/bench/bin BENCH=-DCEL_BENCH=$ROOM 2>&1 | grep -E "error|success" || true
+make -s 2>&1 | grep -E "error|success" || true
+echo "camera moving (room $ROOM)"
+python3 ../../../tools/ce_emu.py build/bench/bin/CELESTE.8xp bin/CEL*.8xv --frame _plat_frame --ms 12000 \
+ --frames-from 2000 --shots 4000 --out build/bench "$@" | tail -n +3
+echo "standing still (room 1)"
+python3 ../../../tools/ce_emu.py bin/CELESTE.8xp bin/CEL*.8xv --frame _plat_frame --keys "1000-1100:2nd,2000-2100:2nd" \
+ --ms 16000 --frames-from 6000 --shots 8000 --out build/bench/still "$@" | tail -n +3
diff --git a/games/celeste/ce/tools/pack_ce.py b/games/celeste/ce/tools/pack_ce.py
index 372ff613..700ca262 100644
--- a/games/celeste/ce/tools/pack_ce.py
+++ b/games/celeste/ce/tools/pack_ce.py
@@ -444,6 +444,56 @@ class Common:
for f in family("collectables/strawberry/wings"):
self.image(f.path)
+ def hair_sprites(self):
+ """Madeline's hair as src/player.c draws it: circles down it (hair00 scaled 0.25 + (1 - i / n) * 0.75, for 2-7
+ pieces) and the bangs (bangs00-02, both ways), each also outlined (itself a pixel off each way, black). 16 x 16,
+ the piece's place at (8, 8): S_hair_disc + 2 * ((n - 1) * (n - 2) / 2 + i - 1) (+1 outlined), S_hair_bangs +
+ 2 * (2 * bangs + flipped) (+1 outlined)."""
+ def masks(p):
+ img = self.images[p]
+ on = [[img[y, x, 3] >= 128 for x in range(10)] for y in range(10)]
+ dark = [[on[y][x] and img[y, x, 0] < 200 for x in range(10)] for y in range(10)]
+ return on, dark
+
+ def outlined(f):
+ o = np.zeros_like(f)
+ m = f != 0
+ for dx, dy in ((0, 0), (1, 0), (-1, 0), (0, 1), (0, -1)):
+ o[np.roll(np.roll(m, dy, 0), dx, 1)] = BLACK
+ return o
+
+ disc, _ = masks("characters/player/hair00")
+ first = None
+ for n in range(2, 8):
+ for i in range(1, n):
+ k = 64 + (256 - i * 256 // n) * 3 // 4
+ half = (5 * k + 128) >> 8
+ f = np.zeros((16, 16), np.uint8)
+ for dy in range(-half, half):
+ v = 5 + (dy * 256 + 128) // k
+ if not 0 <= v < 10 or not any(disc[v]):
+ continue
+ us = [u for u in range(10) if disc[v][u]]
+ x0, x1 = (us[0] - 5) * k // 256, ((us[-1] - 4) * k - 1) // 256
+ f[dy + 8, x0 + 8:x1 + 9] = HAIR
+ a = self.sprites.add(("hair disc", n, i), f)
+ assert self.sprites.add(("hair disc outlined", n, i), outlined(f)) == a + 1
+ first = a if first is None else first
+ H.append(f"#define S_hair_disc {first}")
+ first = None
+ for b in range(3):
+ on, dark = masks(f"characters/player/bangs0{b}")
+ for flip in (0, 1):
+ f = np.zeros((16, 16), np.uint8)
+ for v in range(10):
+ for u in range(10):
+ if on[v][u]:
+ f[v - 5 + 8, (4 - u if flip else u - 5) + 8] = HAIR_DARK if dark[v][u] else HAIR
+ a = self.sprites.add(("hair bangs", b, flip), f)
+ assert self.sprites.add(("hair bangs outlined", b, flip), outlined(f)) == a + 1
+ first = a if first is None else first
+ H.append(f"#define S_hair_bangs {first}")
+
def palette(self):
counts = collections.Counter()
for k, img in self.images.items():
@@ -465,6 +515,7 @@ class Common:
for k in self.images:
H.append(f"#define S_{cname(k)} {self.sprites.ids[k]}")
anims = self.anims.write(self.blob, self.sprites)
+ self.hair_sprites()
# Madeline's frames: hair x, y (signed), bangs frame | has hair << 7, one each sprite id (0 for the others)
meta = self.blob.item("player meta")
n = len(self.sprites.list)