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 draws in assembly

Mason363bbcf1ea56234
diff --git a/games/celeste/ce/data/CEL10.bin b/games/celeste/ce/data/CEL10.bin
index 12c5a7fc..08314948 100644
Binary files a/games/celeste/ce/data/CEL10.bin and b/games/celeste/ce/data/CEL10.bin differ
diff --git a/games/celeste/ce/data/CEL11.bin b/games/celeste/ce/data/CEL11.bin
index 359325a3..a1acbdff 100644
Binary files a/games/celeste/ce/data/CEL11.bin and b/games/celeste/ce/data/CEL11.bin differ
diff --git a/games/celeste/ce/data/CEL12.bin b/games/celeste/ce/data/CEL12.bin
index a3bed0f1..3328b9f4 100644
Binary files a/games/celeste/ce/data/CEL12.bin and b/games/celeste/ce/data/CEL12.bin differ
diff --git a/games/celeste/ce/data/CEL13.bin b/games/celeste/ce/data/CEL13.bin
index 58d21b5a..992fae63 100644
Binary files a/games/celeste/ce/data/CEL13.bin and b/games/celeste/ce/data/CEL13.bin differ
diff --git a/games/celeste/ce/data/CEL14.bin b/games/celeste/ce/data/CEL14.bin
index 7ef4fa81..e9be5df1 100644
Binary files a/games/celeste/ce/data/CEL14.bin and b/games/celeste/ce/data/CEL14.bin differ
diff --git a/games/celeste/ce/host/blit.c b/games/celeste/ce/host/blit.c
new file mode 100644
index 00000000..d62f7772
--- /dev/null
+++ b/games/celeste/ce/host/blit.c
@@ -0,0 +1,44 @@
+/* src/blit.S's loops in C, for the computer build. */
+#include "../src/ce.h"
+
+static void tile_rows(const Cell *c, uint8_t *d, unsigned a, unsigned b, unsigned rows) {
+ const uint8_t *s = c->tile;
+ 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];
+ }
+}
+void blit_row(const Cell *cells, unsigned n, uint8_t *dst, unsigned rows) {
+ for (unsigned i = 0; i < n; i++, dst += 8)
+ if (cells[i].drawn & rows) tile_rows(&cells[i], dst, 0, 8, rows);
+}
+void blit_clip(const Cell *cell, uint8_t *dst, unsigned a, unsigned b, unsigned rows) {
+ if (cell->drawn & rows) tile_rows(cell, dst, a, b, rows);
+}
+void blit_copy(uint8_t *dst, const uint8_t *src, unsigned n) { memcpy(dst, src, n); }
+void blit_rle(uint8_t *dst, const uint8_t *rows, unsigned nrows) {
+ for (unsigned r = 0; r < nrows; r++, dst += 320) {
+ uint8_t *d = dst;
+ int n = *rows++;
+ while (n--) {
+ d += rows[0];
+ memcpy(d, rows + 2, rows[1]);
+ d += rows[1];
+ rows += 2 + rows[1];
+ }
+ }
+}
+void build_cells(const uint8_t *fg, const uint8_t *bg, const TEnt *table, Cell *ofg, Cell *obg, uint8_t *vis,
+ unsigned n) {
+ for (unsigned i = 0; i < n; i++) {
+ ofg[i] = table[fg[i]].c, obg[i] = table[bg[i]].c;
+ if (ofg[i].opaque == 0xFF) obg[i].opaque = obg[i].drawn = 0, vis[i] = 0;
+ else vis[i] = obg[i].opaque != 0xFF;
+ }
+}
+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);
+}
diff --git a/games/celeste/ce/host/plat_host.c b/games/celeste/ce/host/plat_host.c
index 87b54026..2c0c8400 100644
--- a/games/celeste/ce/host/plat_host.c
+++ b/games/celeste/ce/host/plat_host.c
@@ -116,6 +116,7 @@ void plat_frame(void) {
}
}
void plat_begin_draw(void) {}
+uint8_t *plat_ram(unsigned n) { return calloc(1, n); }
void plat_palette(const uint16_t *p, int first, int n) { memcpy(pal + first, p, (size_t)n * 2); }
unsigned plat_keys(void) {
@@ -124,7 +125,7 @@ unsigned plat_keys(void) {
Room *rm = g_level.room;
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 * 90; i++) nt += g_bg4[i] != 0;
+ for (int i = 0; i < 320 * 180; i++) nt += g_bg8[i] != 0;
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/last.png b/games/celeste/ce/last.png
new file mode 100644
index 00000000..387a05c1
Binary files /dev/null and b/games/celeste/ce/last.png differ
diff --git a/games/celeste/ce/makefile b/games/celeste/ce/makefile
index ce7e8d64..a3f28e0f 100644
--- a/games/celeste/ce/makefile
+++ b/games/celeste/ce/makefile
@@ -21,7 +21,7 @@ bin/%.8xv: data/%.bin tools/appvar.py
@mkdir -p bin
python3 tools/appvar.py bin $<
-HOST_SRC = $(filter-out src/plat_ce.c,$(wildcard src/*.c)) host/plat_host.c
+HOST_SRC = $(filter-out src/plat_ce.c,$(wildcard src/*.c)) host/plat_host.c host/blit.c
HOST_FLAGS = -std=gnu11 -O1 -g -Wall -Wextra -Wno-unused-parameter -Isrc
host: build/host/celeste
build/host/celeste: $(HOST_SRC) $(wildcard src/*.h)
diff --git a/games/celeste/ce/src/blit.S b/games/celeste/ce/src/blit.S
new file mode 100644
index 00000000..6034787e
--- /dev/null
+++ b/games/celeste/ce/src/blit.S
@@ -0,0 +1,316 @@
+/* The CE's drawing loops, in eZ80 assembly (src/draw.c calls them; host/blit.c is the same in C for the computer).
+ * Arguments are on the stack (3 bytes each, after the return address); ix is kept, the rest may change. The
+ * alternate registers are used (no interrupts run while the game does). */
+ .assume adl=1
+ .section .text
+
+/* void blit_row(const Cell *cells, unsigned n, uint8_t *dst, unsigned rows): n tiles side by side from dst (8 pixels
+ * apart, 320 a row). A cell is { u8 opaque rows, u8 drawn rows, u24 tile }: a row (bit 0 the top one) also in `rows`
+ * is copied whole if opaque, else pixel by pixel (0 is see-through) if drawn, else left. 0 < n */
+ .global _blit_row
+ .type _blit_row, @function
+_blit_row:
+ push ix
+ 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 c, a ; cells left (C')
+ exx
+.Lw_cell:
+ exx
+ ld a, (ix + 1)
+ and a, d
+ jr z, .Lw_empty
+ ld b, a ; drawn rows (B')
+ ld a, (ix + 0)
+ and a, d
+ ld e, a ; opaque rows (E')
+ ld h, 8 ; rows left (H')
+ exx
+ push de ; (the cell's dst)
+ ld hl, (ix + 2)
+.Lw_row:
+ exx
+ srl e
+ jr nc, .Lw_nf
+ srl b
+ exx
+ ld bc, 8
+ ldir
+ ld bc, 312
+.Lw_adv:
+ ex de, hl
+ add hl, bc
+ ex de, hl
+ exx
+ dec h
+ exx
+ jr nz, .Lw_row
+ pop de
+.Lw_next:
+ ld hl, 8
+ add hl, de
+ ex de, hl
+ lea ix, ix + 5
+ exx
+ dec c
+ exx
+ jr nz, .Lw_cell
+ pop ix
+ ret
+.Lw_empty:
+ exx
+ jr .Lw_next
+.Lw_nf:
+ srl b
+ exx
+ jr nc, .Lw_skip
+ ld b, 8
+.Lw_px:
+ ld a, (hl)
+ inc hl
+ or a, a
+ jr z, .Lw_clear
+ ld (de), a
+.Lw_clear:
+ inc de
+ djnz .Lw_px
+ ld bc, 312
+ jr .Lw_adv
+.Lw_skip:
+ ld bc, 8
+ add hl, bc
+ ld bc, 320
+ jr .Lw_adv
+
+/* 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 */
+ .global _blit_clip
+ .type _blit_clip, @function
+_blit_clip:
+ push ix
+ ld ix, 0
+ add ix, sp
+ ld iy, (ix + 6) ; the cell
+ ld a, (iy + 1)
+ and a, (ix + 18)
+ jr z, .Lc_done
+ exx
+ ld b, a ; drawn rows
+ ld a, (iy + 0)
+ and a, (ix + 18)
+ ld e, a ; opaque rows
+ ld h, 8
+ exx
+ ld hl, (iy + 2)
+ ld de, (ix + 12) ; a
+ add hl, de
+ ld iy, (ix + 9) ; dst
+ add iy, de
+ ld a, (ix + 15)
+ sub a, e ; b - a
+ ld (ix + 15), a ; (the count)
+.Lc_row:
+ lea de, iy + 0
+ push hl
+ exx
+ srl e
+ jr nc, .Lc_nf
+ srl b
+ exx
+ ld bc, 0
+ ld c, (ix + 15)
+ ldir
+ jr .Lc_adv
+.Lc_nf:
+ srl b
+ exx
+ jr nc, .Lc_adv
+ ld b, (ix + 15)
+.Lc_px:
+ ld a, (hl)
+ inc hl
+ or a, a
+ jr z, .Lc_clear
+ ld (de), a
+.Lc_clear:
+ inc de
+ djnz .Lc_px
+.Lc_adv:
+ pop hl
+ ld bc, 8
+ add hl, bc
+ ld bc, 320
+ add iy, bc
+ exx
+ dec h
+ exx
+ jr nz, .Lc_row
+.Lc_done:
+ pop ix
+ ret
+
+/* void blit_copy(uint8_t *dst, const uint8_t *src, unsigned n): n > 0 */
+ .global _blit_copy
+ .type _blit_copy, @function
+_blit_copy:
+ pop iy ; the return address
+ pop de
+ pop hl
+ pop bc
+ push bc
+ push hl
+ push de
+ ldir
+ jp (iy)
+
+/* void blit_rle(uint8_t *dst, const uint8_t *rows, unsigned nrows): a sprite's stored rows (src/draw.c) at dst, 320
+ * a row, neither clipped nor flipped: each row u8 runs, then (u8 skip, u8 n, n pixels) each; 0 < nrows < 256 */
+ .global _blit_rle
+ .type _blit_rle, @function
+_blit_rle:
+ push ix
+ ld iy, 0
+ add iy, sp
+ ld ix, (iy + 6) ; the row's start
+ ld hl, (iy + 9) ; the rows
+ ld a, (iy + 12) ; rows left
+.Lr_row:
+ exx
+ ld e, a
+ exx
+ lea de, ix + 0
+ ld a, (hl) ; runs
+ inc hl
+ or a, a
+ jr z, .Lr_next
+ ld bc, 0
+.Lr_run:
+ ld c, (hl) ; skip
+ inc hl
+ ex de, hl
+ add hl, bc
+ ex de, hl
+ ld c, (hl) ; n
+ inc hl
+ ldir
+ dec a
+ jr nz, .Lr_run
+.Lr_next:
+ ld bc, 320
+ add ix, bc
+ exx
+ ld a, e
+ exx
+ dec a
+ jr nz, .Lr_row
+ pop ix
+ ret
+
+/* void build_cells(const uint8_t *fg, const uint8_t *bg, const uint8_t *table, Cell *ofg, Cell *obg, uint8_t *vis,
+ * unsigned n): n cells of a row of the grids (bytes: the room's own tile numbers) as Cells, from the room's table of
+ * them (8 bytes each: the Cell, then 3 unused); vis[i]: 1 where neither tile is opaque (the background shows) */
+ .global _build_cells
+ .type _build_cells, @function
+_build_cells:
+ push ix
+ ld ix, 0
+ add ix, sp
+ ld iy, (ix + 21) ; vis
+.Lb_cell:
+ ld hl, (ix + 6)
+ ld a, (hl)
+ inc hl
+ ld (ix + 6), hl
+ or a, a
+ sbc hl, hl
+ ld l, a
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld de, (ix + 12)
+ add hl, de
+ ld a, (hl)
+ ld (iy + 0), a ; (the fg's opaque rows, for now)
+ ld de, (ix + 15)
+ ld bc, 5
+ ldir
+ ld (ix + 15), de
+ ld hl, (ix + 9)
+ ld a, (hl)
+ inc hl
+ ld (ix + 9), hl
+ or a, a
+ sbc hl, hl
+ ld l, a
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld de, (ix + 12)
+ add hl, de
+ ld a, (hl) ; the bg's opaque rows
+ ld de, (ix + 18)
+ push de
+ ld bc, 5
+ ldir
+ ld (ix + 18), de
+ pop de ; (its cell)
+ ld c, a
+ ld a, (iy + 0)
+ inc a ; 0 if the fg's opaque: the bg's hidden, not drawn
+ jr nz, .Lb_fgnot
+ ld (de), a
+ inc de
+ ld (de), a
+ jr .Lb_hidden
+.Lb_fgnot:
+ ld a, c
+ inc a ; 0 if the bg's opaque
+ jr z, .Lb_hidden
+ ld a, 1
+.Lb_hidden:
+ ld (iy + 0), a
+ inc iy
+ ld hl, (ix + 24)
+ dec hl
+ ld (ix + 24), hl
+ add hl, de
+ or a, a
+ sbc hl, de
+ jr nz, .Lb_cell
+ pop ix
+ ret
+
+/* void blit_rect(uint8_t *dst, const uint8_t *src, unsigned w, unsigned h, unsigned src_step): h rows of w bytes,
+ * dst 320 a row, src src_step a row; 0 < w, 0 < h < 256 */
+ .global _blit_rect
+ .type _blit_rect, @function
+_blit_rect:
+ push ix
+ ld ix, 0
+ add ix, sp
+ ld de, (ix + 6)
+ ld hl, (ix + 9)
+ ld a, (ix + 15)
+.Lq_row:
+ push de
+ push hl
+ ld bc, (ix + 12)
+ ldir
+ pop hl
+ ld bc, (ix + 18)
+ add hl, bc
+ pop de
+ ex de, hl
+ ld bc, 320
+ add hl, bc
+ ex de, hl
+ dec a
+ jr nz, .Lq_row
+ pop ix
+ ret
diff --git a/games/celeste/ce/src/ce.h b/games/celeste/ce/src/ce.h
index 98e7ce80..cbc7a03f 100644
--- a/games/celeste/ce/src/ce.h
+++ b/games/celeste/ce/src/ce.h
@@ -42,6 +42,7 @@ unsigned plat_keys(void);
unsigned plat_ticks(void); /* a 32768 Hz clock (wraps at 16 bits) */
void plat_wait(unsigned ticks);
const uint8_t *plat_appvar(const char *name); /* an appvar's data in place, NULL if missing */
+uint8_t *plat_ram(unsigned n); /* n bytes of the calculator's free RAM for the game (NULL: not that much) */
bool plat_save(const char *name, const void *data, unsigned n);
bool plat_load(const char *name, void *data, unsigned n);
void plat_unpack(void *dst, const void *src); /* ZX0 */
@@ -82,13 +83,15 @@ const uint16_t *hair_mask(int which); /* 0 hair, 1-3 bangs: 10 rows (mask
extern bool g_has_bg; /* the chapter has backgrounds */
extern int g_bg_scrollx, g_bg_scrolly; /* how they move with the camera (1/256) */
extern uint8_t g_bg_map[16]; /* their 16 colours */
-extern uint8_t g_bg4[320 * 180 / 2]; /* the room's: 4 bits a pixel, the left one low */
+extern uint8_t g_bg8[320 * 180]; /* the room's picture of them */
extern int g_bg_refx, g_bg_refy; /* the camera it was made for */
+extern uint8_t g_cells_gen; /* changes when the rooms' tiles do (draw.c keeps the view's cells) */
extern int g_chapter_berries; /* the chapter's strawberries */
extern const uint8_t *g_remap, *g_remap_red, *g_remap_half; /* sprites drawn through g_remap if set */
/* ---------------------------------------------------------------- drawing (draw.c) */
-enum { DF_FLIPX = 1, DF_FLIPY = 2, DF_SOLID = 4 }; /* DF_SOLID: every pixel in one colour (draw_color) */
+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 */
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 */
@@ -105,6 +108,25 @@ void draw_layers_back(void); /* background, bg tiles */
void draw_layer_fg(void); /* fg tiles */
void draw_decals(int fg);
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 */
+typedef struct {
+ Cell c;
+#ifdef __TICE__
+ uint8_t pad[3]; /* (8 bytes: build_cells finds them by shifting) */
+#endif
+} TEnt;
+void blit_row(const Cell *cells, unsigned n, uint8_t *dst, unsigned rows);
+void blit_clip(const Cell *cell, uint8_t *dst, unsigned a, unsigned b, unsigned rows);
+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 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
+#define CROWS 24
+typedef struct { Cell fg[COLS], bg[COLS]; uint8_t vis[COLS]; } CellRow;
+#define CELL_CACHE (CROWS * sizeof(CellRow))
/* ---------------------------------------------------------------- floats (sf.c): IEEE single precision in whole numbers */
typedef uint32_t f32;
@@ -129,6 +151,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 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; }
@@ -173,7 +196,7 @@ struct Ent {
f32 ptr2;
};
#define MAX_ENTS 96
-extern Ent g_ents[MAX_ENTS];
+extern Ent *g_ents; /* (in the RAM plat_ram gives) */
extern int g_nents;
static inline int e_left(const Ent *e) { return e->x + e->hx; }
static inline int e_top(const Ent *e) { return e->y + e->hy; }
@@ -197,7 +220,7 @@ typedef struct {
int gx, gy, tw, th; /* its grid: the top left cell's world pixel, cells (with MARGIN) */
uint8_t *grid; /* fg[tw * th], bg[tw * th], solid bits (rb a row) */
int rb;
- const uint8_t *tiles[256];
+ TEnt *tab; /* its tiles, by its own numbers (256) */
const uint8_t *decals, *ents, *trigs, *bg;
int bgx, bgy;
uint8_t ntiles;
@@ -228,6 +251,7 @@ typedef struct {
} Level;
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);
bool solid_at(int l, int t, int r, int b, const Ent *self); /* tiles or solid entities over [l, r) x [t, b) */
diff --git a/games/celeste/ce/src/draw.c b/games/celeste/ce/src/draw.c
index 830e1651..1d56e76a 100644
--- a/games/celeste/ce/src/draw.c
+++ b/games/celeste/ce/src/draw.c
@@ -13,15 +13,16 @@ 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) return;
- for (int r = 0; r < h; r++) {
- int vy = fy ? y + fh - 1 - (y0 + r) : y + y0 + r;
+ if (left >= VIEW_W || left + w <= 0 || !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 */
while (n--) p += 2 + p[1];
continue;
}
- uint8_t *row = FB(0, vy);
int at = 0;
while (n--) {
at += p[0];
@@ -41,17 +42,24 @@ static void blit_sprite(const uint8_t *s, int x, int y, uint8_t flags) {
} else {
/* stored pixel x0 + at + i goes to x + fw - 1 - (x0 + at + i) */
int base = x + fw - 1 - x0 - at;
- for (int i = 0; i < len; i++) {
- int dx = base - i;
- if ((unsigned)dx < VIEW_W) row[dx] = solid ? col : g_remap ? g_remap[px[i]] : px[i];
- }
+ 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];
}
at += len;
}
}
}
-void draw_sprite(const uint8_t *spr, int x, int y, uint8_t flags) { blit_sprite(spr, x - g_camx, y - g_camy, 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) */
+ return;
+ }
+ blit_sprite(s, x, y, flags);
+}
/* scaled (sx, sy: 1/256, nearest), its origin (ox, oy, frame pixels) at world (x, y) */
void draw_sprite_scaled(const uint8_t *s, int x, int y, int sx, int sy, int ox, int oy, uint8_t flags) {
@@ -60,9 +68,13 @@ void draw_sprite_scaled(const uint8_t *s, int x, int y, int sx, int sy, int ox,
return;
}
/* decode into a buffer, then sample it */
- static uint8_t buf[64 * 64];
+ 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 > 64 || fh > 64) return;
+ 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++) {
@@ -75,23 +87,33 @@ void draw_sprite_scaled(const uint8_t *s, int x, int y, int sx, int sy, int ox,
}
}
bool fx = flags & DF_FLIPX;
- /* the destination box: frame pixel u lands at x + (u - ox) * sx / 256 */
- int dx0 = x - g_camx + fdiv(-ox * sx, 256), dx1 = x - g_camx + fdiv((fw - ox) * sx + 255, 256);
- int dy0 = y - g_camy + fdiv(-oy * sy, 256), dy1 = y - g_camy + fdiv((fh - oy) * sy + 255, 256);
+ /* 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 */
+ int bx = x - g_camx, by = y - g_camy;
+ int dx0 = bx + fdiv(-ox * sx, 256), dx1 = bx + fdiv((fw - ox) * sx + 255, 256);
+ int dy0 = by + fdiv(-oy * sy, 256), dy1 = by + fdiv((fh - oy) * sy + 255, 256);
if (fx) {
- int a = x - g_camx - fdiv((fw - ox) * sx + 255, 256) + 1, b = x - g_camx - fdiv(-ox * sx, 256) + 1;
+ int a = bx - fdiv((fw - ox) * sx + 255, 256) + 1, b = bx - fdiv(-ox * sx, 256) + 1;
dx0 = a, dx1 = b;
}
- for (int vy = imax(dy0, 0); vy < imin(dy1, VIEW_H); vy++) {
- int v = oy + fdiv((vy - (y - g_camy)) * 256 + 128, sy);
+ dx0 = imax(dx0, 0), dx1 = imin(dx1, VIEW_W);
+ if (dx1 - dx0 > 64) dx1 = dx0 + 64;
+ int8_t us[64];
+ for (int vx = dx0; vx < dx1; vx++) {
+ int rel = vx - bx;
+ if (fx) rel = -rel;
+ 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) {
+ int v = oy + fdiv((vy - by) * 256 + 128, sy);
if (v < 0 || v >= fh) continue;
- uint8_t *row = FB(0, vy);
- for (int vx = imax(dx0, 0); vx < imin(dx1, VIEW_W); vx++) {
- int rel = vx - (x - g_camx);
- if (fx) rel = -rel;
- int u = ox + fdiv(rel * 256 + 128, sx);
- if (u < 0 || u >= fw) continue;
- uint8_t c = buf[v * fw + u];
+ const uint8_t *src = buf + v * fw;
+ for (int vx = dx0; vx < dx1; vx++) {
+ int u = us[vx - dx0];
+ if (u < 0) continue;
+ uint8_t c = src[u];
if (c) row[vx] = (flags & DF_SOLID) ? g_draw_color : g_remap ? g_remap[c] : c;
}
}
@@ -116,7 +138,7 @@ 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[tile] & 1);
+ if (tile) blit_tile(tile_data(tile), x - g_camx, y - g_camy, g_tile_flags[2 * tile] == 0xFF);
}
void draw_rect_screen(int x, int y, int w, int h, uint8_t c) {
@@ -159,92 +181,99 @@ void draw_circle_fill(int cx, int cy, int r, uint8_t c) {
void draw_clear_screen(uint8_t c) { memset(g_fb, c, 320 * 240); }
/* ---------------------------------------------------------------- the tile layers */
-/* the tiles at world cell (cx, cy): the current room's grid first, then the other's */
-static void cell(int cx, int cy, const uint8_t **fg, const uint8_t **bg) {
- *fg = *bg = NULL;
- for (int k = 0; k < 2; k++) {
- Room *rm = &g_level.rooms[k ? g_level.slot ^ 1 : g_level.slot];
- if (rm->index < 0) continue;
- int x = cx - (rm->gx >> 3), y = cy - (rm->gy >> 3);
- if ((unsigned)x >= (unsigned)rm->tw || (unsigned)y >= (unsigned)rm->th) continue;
- const uint8_t *g = rm->grid + y * rm->tw + x;
- int f = g[0], b = g[rm->tw * rm->th];
- *fg = f ? rm->tiles[f] : NULL;
- *bg = b ? rm->tiles[b] : NULL;
- return;
+/* 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) */
+extern uint8_t *g_cell_cache;
+static int cache_c0 = 1 << 20, cache_r0;
+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);
+ 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;
+ if ((unsigned)ry >= (unsigned)rm->th) continue;
+ int a = imax(c0, gx), b = imin(c0 + COLS, 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));
+ }
}
+ cache_c0 = c0, cache_r0 = r0, cache_gen = g_cells_gen;
}
-/* is a tile (its data) without see-through pixels: the tile's flags, from its pointer */
-static bool opaque_tile(const uint8_t *t) {
- for (int i = 0; i < 64; i++)
- if (!t[i]) return false;
- return true;
+/* 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);
}
-#define COLS 42
-static const uint8_t *row_fg[COLS], *row_bg[COLS];
-static uint8_t row_kind[COLS]; /* 0: an opaque fg tile, 1: an opaque bg tile, 2: the background shows */
-
-/* the room's backgrounds: the view's pixel (x, y) is the picture's ((x + ox) % 320, y + oy), the offset moving with the
- * camera by the chapter's scroll factor (rows above or below it: its first or last) */
-static void background_rows(int vy0, int vy1, int vx0, int vx1) {
- int ox = fdiv((g_camx - g_bg_refx) * g_bg_scrollx, 256), oy = fdiv((g_camy - g_bg_refy) * g_bg_scrolly, 256);
- ox = ((ox % 320) + 320) % 320;
- for (int y = vy0; y < vy1; y++) {
- const uint8_t *src = g_bg4 + iclamp(y + oy, 0, VIEW_H - 1) * 160;
- uint8_t *d = FB(0, y);
- int sx = (vx0 + ox) % 320;
- for (int x = vx0; x < vx1; x++) {
- uint8_t b = src[sx >> 1];
- d[x] = g_bg_map[(sx & 1) ? b >> 4 : b & 15];
- if (++sx == 320) sx = 0;
- }
- }
+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 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, offy = g_camy & 7;
- for (int r = 0; r <= VIEW_H / 8 + 1; r++) {
- int vy = r * 8 - offy;
- if (vy >= VIEW_H) break;
- for (int c = 0; c < COLS; c++) {
- const uint8_t *f, *b;
- cell(c0 + c, r0 + r, &f, &b);
- row_fg[c] = f, row_bg[c] = b;
- row_kind[c] = f && opaque_tile(f) ? 0 : b && opaque_tile(b) ? 1 : 2;
- }
- int y0 = imax(vy, 0), y1 = imin(vy + 8, VIEW_H);
- /* the background behind cells that show it, in runs */
- for (int c = 0; c < COLS;) {
- if (row_kind[c] != 2) { c++; continue; }
- int e = c;
- while (e < COLS && row_kind[e] == 2) e++;
- int x0 = imax(c * 8 - offx, 0), x1 = imin(e * 8 - offx, VIEW_W);
- if (x0 < x1) {
- if (g_has_bg) background_rows(y0, y1, x0, x1);
- else
- for (int y = y0; y < y1; y++) memset(FB(x0, y), 0, x1 - x0);
- }
+ int c0 = g_camx >> 3, r0 = g_camy >> 3, offx = g_camx & 7;
+ view_offy = g_camy & 7;
+ if (c0 != cache_c0 || r0 != cache_r0 || g_cells_gen != cache_gen) cells_build(c0, r0);
+ /* 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++) {
+ 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 */
+ if (!row->vis[c]) { c++; continue; }
+ int e = c + 1;
+ while (e < COLS && row->vis[e]) e++;
+ int xa = imax(c * 8 - offx, 0), xb = imin(e * 8 - offx, VIEW_W);
c = e;
+ if (xa >= xb) continue;
+ unsigned n = (unsigned)(xb - xa), sx = (unsigned)(xa + ox);
+ if (sx >= 320) sx -= 320;
+ unsigned first = n < 320 - sx ? n : 320 - sx;
+ uint8_t *d = g_fb + (VIEW_Y + y0) * 320 + xa;
+ if (!g_has_bg) {
+ for (int y = y0; y < y1; y++, d += 320) memset(d, BLACK, n);
+ continue;
+ }
+ /* its rows: those above the picture repeat its first, those below its last */
+ for (int y = y0; y < y1;) {
+ int sy = y + oy, k, step = 320;
+ if (sy < 0) k = imin(y1 - y, -sy), sy = 0, step = 0;
+ else if (sy >= VIEW_H) k = y1 - y, sy = VIEW_H - 1, step = 0;
+ else k = imin(y1 - y, VIEW_H - sy);
+ const uint8_t *src = g_bg8 + sy * 320;
+ blit_rect(d, src + sx, first, (unsigned)k, (unsigned)step);
+ if (first < n) blit_rect(d + first, src, n - first, (unsigned)k, (unsigned)step);
+ y += k, d += 320 * k;
+ }
}
- for (int c = 0; c < COLS; c++) {
- if (row_kind[c] == 0 || !row_bg[c]) continue;
- blit_tile(row_bg[c], c * 8 - offx, vy, row_kind[c] == 1);
- }
+ layer_row(row->bg, line, offx, rows);
}
}
void draw_layer_fg(void) {
- int c0 = g_camx >> 3, r0 = g_camy >> 3, offx = g_camx & 7, offy = g_camy & 7;
- for (int r = 0; r <= VIEW_H / 8 + 1; r++) {
- int vy = r * 8 - offy;
- if (vy >= VIEW_H) break;
- for (int c = 0; c < COLS; c++) {
- const uint8_t *f, *b;
- cell(c0 + c, r0 + r, &f, &b);
- if (f) blit_tile(f, c * 8 - offx, vy, opaque_tile(f));
- }
+ int offx = g_camx & 7;
+ view_offy = g_camy & 7;
+ for (int r = 0; r < CROWS; r++) {
+ uint8_t *line;
+ unsigned rows = rows_mask(r, &line);
+ if (rows) layer_row(cells_row(r)->fg, line, offx, rows);
}
}
diff --git a/games/celeste/ce/src/ents.c b/games/celeste/ce/src/ents.c
index e21cdf09..41af0c2f 100644
--- a/games/celeste/ce/src/ents.c
+++ b/games/celeste/ce/src/ents.c
@@ -3,7 +3,7 @@
#include "ce.h"
#include "fconst.h"
-Ent g_ents[MAX_ENTS];
+Ent *g_ents;
int g_nents;
static int trig_ox, trig_oy;
diff --git a/games/celeste/ce/src/gen.h b/games/celeste/ce/src/gen.h
index 64608c28..acc905f8 100644
--- a/games/celeste/ce/src/gen.h
+++ b/games/celeste/ce/src/gen.h
@@ -547,5 +547,6 @@
#define TT_goldenBerryCollectTrigger 4
#define EV_end_city 0
#define FORMAT 1
+#define GRID_MAX 6603 /* the biggest room's grids */
#define MARGIN 4
#endif
diff --git a/games/celeste/ce/src/level.c b/games/celeste/ce/src/level.c
index fff0e13e..ca060ca9 100644
--- a/games/celeste/ce/src/level.c
+++ b/games/celeste/ce/src/level.c
@@ -3,8 +3,27 @@
#include "ce.h"
Level g_level;
-#define GRID_BYTES 9000 /* a room's grids in RAM (the biggest of the chapters here, with a margin) */
-static uint8_t grid_mem[2][GRID_BYTES];
+#define GRID_BYTES GRID_MAX /* a room's grids in RAM (the biggest of the chapters here) */
+static uint8_t *grid_mem[2];
+uint8_t *g_scratch; /* 1 KB for passing things (a scaled sprite's pixels) */
+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 */
+bool level_ram(void) {
+ unsigned n = sizeof(Ent) * MAX_ENTS + 2 * (GRID_BYTES + 256 * sizeof(TEnt)) + 1024 + CELL_CACHE;
+ 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;
+ 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;
+ }
+ return true;
+}
static int pending_room = -1, pending_intro;
/* a room record (tools/pack_ce.py Chapter.room) */
@@ -40,12 +59,18 @@ const char *level_room_name(void) {
return buf;
}
-uint8_t g_bg4[320 * 180 / 2];
+uint8_t g_bg8[320 * 180];
int g_bg_refx, g_bg_refy;
static const uint8_t *bg_loaded;
-static void background_load(const Room *rm) { /* the room's picture of the backgrounds, into RAM */
+static void background_load(const Room *rm) { /* the room's picture of the backgrounds: 4 bits a pixel, to 8 */
if (!rm->bg || rm->bg == bg_loaded) return;
- plat_unpack(g_bg4, rm->bg);
+ uint8_t *src = g_bg8 + 320 * 180 / 2, *d = g_bg8;
+ plat_unpack(src, rm->bg);
+ for (unsigned i = 0; i < 320 * 180 / 2; i++) { /* (in place: what is written never passes what is read) */
+ uint8_t b = *src++;
+ *d++ = g_bg_map[b & 15];
+ *d++ = g_bg_map[b >> 4];
+ }
bg_loaded = rm->bg;
g_bg_refx = rm->bgx, g_bg_refy = rm->bgy;
}
@@ -63,7 +88,8 @@ static bool room_load(int slot, int index) {
rm->ntiles = p[0];
for (int i = 0; i <= rm->ntiles; i++) {
int t = (int)R16(p + 1 + 2 * i);
- rm->tiles[i] = t ? tile_data(t) : NULL;
+ rm->tab[i].c.tile = t ? tile_data(t) : NULL;
+ rm->tab[i].c.opaque = g_tile_flags[2 * t], rm->tab[i].c.drawn = g_tile_flags[2 * t + 1];
}
p += 1 + 2 * (rm->ntiles + 1);
int raw = (int)R16(p);
@@ -76,6 +102,7 @@ static bool room_load(int slot, int index) {
rm->bgx = S24(p + 14), rm->bgy = S24(p + 17);
rm->bg = R24(p + 20) == 0xFFFFFF ? NULL : HPTR(p + 20);
rm->index = index;
+ g_cells_gen++;
return true;
}
@@ -152,6 +179,7 @@ static int spawn_near(int x, int y) { /* the room's spawn nearest (x, y), as a
static void load_into_current(int index, int intro) {
ents_clear();
g_level.rooms[0].index = g_level.rooms[1].index = -1;
+ g_cells_gen++;
g_level.slot = 0;
g_level.room = &g_level.rooms[0];
room_load(0, index);
@@ -167,7 +195,12 @@ static void load_into_current(int index, int intro) {
}
bool level_load(int chapter, int room, int intro) {
- memset(&g_level, 0, sizeof g_level);
+ { /* (the RAM's pointers stay) */
+ Room keep[2];
+ memcpy(keep, g_level.rooms, sizeof keep);
+ memset(&g_level, 0, sizeof g_level);
+ memcpy(g_level.rooms, keep, sizeof keep);
+ }
bg_loaded = NULL;
g_level.chapter = chapter;
g_level.rooms[0].index = g_level.rooms[1].index = -1;
@@ -230,6 +263,7 @@ static void transition_update(void) {
int old = g_level.slot ^ 1;
ents_remove_room(old);
g_level.rooms[old].index = -1;
+ g_cells_gen++;
spawn_near(g_player.e->x, g_player.e->y);
player_on_transition();
g_level.transitioning = false;
diff --git a/games/celeste/ce/src/main.c b/games/celeste/ce/src/main.c
index c74e5542..06b4333e 100644
--- a/games/celeste/ce/src/main.c
+++ b/games/celeste/ce/src/main.c
@@ -9,10 +9,19 @@ uint32_t g_perf_updates, g_perf_frames; /* (read by the tests and the emulator
int main(void) {
plat_init();
- if (!data_open()) {
+ bool ram = level_ram(), data = data_open();
+ ram = ram && data;
+ if (!ram) {
+ static const uint16_t white[2] = {0, 0x7FFF};
+ plat_palette(white, 0, 2);
draw_clear_screen(0);
- draw_text("Celeste needs its appvars:", 20, 100, 255);
- draw_text("send CELG*.8xv and CEL*.8xv too.", 20, 112, 255);
+ if (!data) {
+ draw_text("Celeste needs its appvars:", 20, 100, 1);
+ draw_text("send CELG*.8xv and CEL*.8xv too.", 20, 112, 1);
+ } else {
+ draw_text("Celeste needs 30 KB more", 20, 100, 1);
+ draw_text("free RAM.", 20, 112, 1);
+ }
plat_frame();
while (!(plat_keys() & (K_QUIT | K_BACK | K_OK))) plat_wait(300);
plat_exit();
diff --git a/games/celeste/ce/src/plat_ce.c b/games/celeste/ce/src/plat_ce.c
index 1adb66a9..e31d92b0 100644
--- a/games/celeste/ce/src/plat_ce.c
+++ b/games/celeste/ce/src/plat_ce.c
@@ -21,13 +21,41 @@ void plat_init(void) {
timer_Enable(2, TIMER_32K, TIMER_NOINT, TIMER_UP);
kb_DisableOnLatch();
}
+/* 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. */
+#define SAVE_SIZE 512
+uint8_t *plat_ram(unsigned n) {
+ uint8_t h = ti_Open("CELSAVE", "r");
+ if (h) {
+ if (ti_IsArchived(h)) ti_SetArchiveStatus(false, h);
+ ti_Close(h);
+ }
+ h = ti_Open("CELSAVE", "r+");
+ if (!h) h = ti_Open("CELSAVE", "w");
+ if (!h) return NULL;
+ if (ti_GetSize(h) < SAVE_SIZE) ti_Resize(SAVE_SIZE, h);
+ ti_Close(h);
+ ti_Delete("CELRAM");
+ h = ti_Open("CELRAM", "w");
+ if (!h) return NULL;
+ if (ti_Resize(n, h) != (int)n) {
+ ti_Close(h);
+ ti_Delete("CELRAM");
+ return NULL;
+ }
+ uint8_t *p = ti_GetDataPtr(h);
+ ti_Close(h);
+ return p;
+}
+
void plat_exit(void) {
- uint8_t h = ti_Open("CELSAVE", "r+"); /* the save goes to the archive (it stays in RAM while playing) */
+ ti_Delete("CELRAM");
+ 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_ClearOnLatch();
}
-void plat_frame(void) {
+__attribute__((noinline)) void plat_frame(void) { /* (not inlined: the emulator counts frames here) */
gfx_SwapDraw();
g_fb = (uint8_t *)gfx_vbuffer;
}
@@ -66,7 +94,7 @@ const uint8_t *plat_appvar(const char *name) {
return p;
}
bool plat_save(const char *name, const void *data, unsigned n) {
- uint8_t h = ti_Open(name, "w");
+ uint8_t h = ti_Open(name, "r+"); /* (in place: made at its size by plat_ram) */
if (!h) return false;
bool ok = ti_Write(data, n, 1, h) == 1;
ti_Close(h);
diff --git a/games/celeste/ce/src/player.c b/games/celeste/ce/src/player.c
index 0a1a96ef..2ae9988d 100644
--- a/games/celeste/ce/src/player.c
+++ b/games/celeste/ce/src/player.c
@@ -1220,34 +1220,60 @@ void player_before_down_transition(void) {
}
/* ---------------------------------------------------------------- drawing */
+/* 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) {
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) {
- const uint16_t *mask = hair_mask(i == 0 ? 1 + (bangs < 3 ? bangs : 0) : 0);
- int scale = 64 + (256 - i * 256 / n) * 3 / 4; /* 0.25 + (1 - i / n) * 0.75 */
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;
- bool flip = i == 0 && P->facing < 0;
- int sx = (i == 0 ? 256 : scale) * P->scale_x / 1024, sy = i == 0 ? 256 : scale;
- if (sx < 16) sx = 16;
- int x0 = cx - (5 * sx + 128) / 256, x1 = cx + (5 * sx + 128) / 256, y0 = cy - (5 * sy + 128) / 256, y1 = cy + (5 * sy + 128) / 256;
- for (int y = y0; y < y1; y++)
- for (int x = x0; x < x1; x++) {
- int u = 5 + fdiv((x - cx) * 256 + 128, sx), v = 5 + fdiv((y - cy) * 256 + 128, sy);
- if ((unsigned)u >= 10 || (unsigned)v >= 10) continue;
- if (flip) u = 9 - u;
- if (!(mask[v] >> u & 1)) continue;
- if (outline) {
- static const int8_t O[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
- for (int k = 0; k < 4; k++) {
- int px = x + O[k][0], py = y + O[k][1];
- if ((unsigned)px < VIEW_W && (unsigned)py < VIEW_H) g_fb[(VIEW_Y + py) * 320 + px] = BLACK;
- }
- } else if ((unsigned)x < VIEW_W && (unsigned)y < VIEW_H)
- g_fb[(VIEW_Y + y) * 320 + x] = mask[10 + v] >> u & 1 ? HAIR_DARK : HAIR;
+ 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);
+ }
}
}
diff --git a/games/celeste/ce/src/util.c b/games/celeste/ce/src/util.c
index 12bb0e22..5e53e4c4 100644
--- a/games/celeste/ce/src/util.c
+++ b/games/celeste/ce/src/util.c
@@ -24,6 +24,18 @@ 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/emu.sh b/games/celeste/ce/tests/emu.sh
new file mode 100755
index 00000000..435a3f2d
--- /dev/null
+++ b/games/celeste/ce/tests/emu.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+# The CE build in the emulator (tools/ce_emu.py): Forsaken City from the title, running right and jumping.
+# tests/emu.sh OUT_DIR [extra ce_emu options]
+set -e
+HERE="$(cd "$(dirname "$0")/.." && pwd)"
+OUT="${1:-$HERE/build/emu}"
+shift || true
+cd "$HERE"
+PATH="$HOME/Documents/Assets/toolchains/CEdev/bin:$PATH" make -s 2>&1 | grep -E "error|success" || true
+python3 ../../../platform/ti84ce/check.py .
+python3 ../../../tools/ce_emu.py bin/CELESTE.8xp bin/CEL*.8xv --frame _plat_frame \
+ --keys "1000-1100:2nd,2000-2100:2nd,5000-7000:right,6000-6300:2nd,7500-8000:left,8200-8400:clear" \
+ --shots 1500,2500,5500,6500,7800,9000 --ms 10000 --frames-from 4500 --out "$OUT" "$@"
diff --git a/games/celeste/ce/tools/pack_ce.py b/games/celeste/ce/tools/pack_ce.py
index 54da0e24..372ff613 100644
--- a/games/celeste/ce/tools/pack_ce.py
+++ b/games/celeste/ce/tools/pack_ce.py
@@ -767,13 +767,6 @@ class Chapter:
self.sprites = SpriteSet(self.blob, "chapter")
# decals' sprites: each family's frames in a row (an animated decal plays them)
self.decal_ids = {}
- for p in decals:
- fam = decal_family(p)
- first = None
- for s in fam:
- i = self.sprites.add(s.path, pal.map(s.image()))
- first = i if first is None else first
- self.decal_ids[p] = (first, len(fam))
for p in ent_paths:
self.sprites.add(p, pal.map(self.image(p)))
# the backgrounds' 16 colours (chapter palette indices), then each room's picture: 4 bits a pixel (the left
@@ -801,9 +794,12 @@ class Chapter:
for t in self.tiles.list[i:i + 1000]:
it.raw(t)
chunks.append(it)
+ # each tile's rows: those without see-through pixels, those with something (bit 0: the top row)
flags = self.blob.item("tile flags")
- for i in range(len(self.tiles.list)):
- flags.u8(1 if self.tiles.opaque(i) else 0)
+ for t in self.tiles.list:
+ full = sum(1 << r for r in range(8) if 0 not in t[r * 8:r * 8 + 8])
+ some = sum(1 << r for r in range(8) if any(t[r * 8:r * 8 + 8]))
+ flags.u8(full).u8(some)
table = self.sprites.table()
self.blob.add(table)
h = self.head
@@ -888,6 +884,7 @@ class Chapter:
bits[x >> 3] |= 1 << (x & 7)
raw += bits
packed = zx0(bytes(raw))
+ GRID_MAX[0] = max(GRID_MAX[0], len(raw))
it.u8(len(ids) - 1)
for v in ids:
it.u16(v)
@@ -931,9 +928,26 @@ class Chapter:
cur.chunks.append(it)
return table
+ def decal_sprite(self, p, fl):
+ """A decal's frames as sprites, turned as it is placed (fl: 1 flipped across, 2 upside down): the CE draws
+ them as they are stored, fast. (first sprite, frames)"""
+ if (p, fl) not in self.decal_ids:
+ first = None
+ fam = decal_family(p)
+ for s in fam:
+ img = s.image()
+ if fl & 1:
+ img = img[:, ::-1]
+ if fl & 2:
+ img = img[::-1]
+ i = self.sprites.add((s.path, fl), self.pal.map(np.ascontiguousarray(img)))
+ first = i if first is None else first
+ self.decal_ids[(p, fl)] = (first, len(fam))
+ return self.decal_ids[(p, fl)]
+
def decals(self, r):
"""u8 bg count, u8 fg count, then each: u16 sprite, s16 x, s16 y (its centre, room coordinates),
- u8 flags (1 flip x, 2 flip y), u8 frames (an animated one plays them, 12 a second)."""
+ u8 flags (0: the sprites are turned already), u8 frames (an animated one plays them, 12 a second)."""
it = self.blob.item("decals " + r.name)
lists = []
for layer in ("bgdecals", "fgdecals"):
@@ -941,11 +955,11 @@ class Chapter:
lst = []
for d in (el.children if el else []):
p = decal_base(d["texture"])
- sid, n = self.decal_ids[p]
+ fl = (1 if d.get("scaleX", 1) < 0 else 0) | (2 if d.get("scaleY", 1) < 0 else 0)
+ sid, n = self.decal_sprite(p, fl)
if sid is None:
continue
- fl = (1 if d.get("scaleX", 1) < 0 else 0) | (2 if d.get("scaleY", 1) < 0 else 0)
- lst.append((sid, int(round(d["x"])), int(round(d["y"])), fl, n))
+ lst.append((sid, int(round(d["x"])), int(round(d["y"])), 0, n))
lists.append(lst)
it.u8(len(lists[0])).u8(len(lists[1]))
for lst in lists:
@@ -1091,6 +1105,7 @@ class Chapter:
EVENTS = {}
+GRID_MAX = [0]
MISSING = collections.Counter()
@@ -1148,6 +1163,7 @@ def main():
for n, i in EVENTS.items():
H.append(f"#define EV_{cname(n)} {i}")
H.append(f"#define FORMAT {FORMAT}")
+ H.append(f"#define GRID_MAX {GRID_MAX[0]} /* the biggest room's grids */")
H.append(f"#define MARGIN {MARGIN}")
H.append("#endif")
open(GEN_H, "w").write("\n".join(H) + "\n")