Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
FNAF CE: the office drawn only where it changes, faster cameras and jumpscares
Mason363f842930d2431
diff --git a/games/fnaf/ce/engine/ce.h b/games/fnaf/ce/engine/ce.hindex e71ec75d..28a1ff56 100644--- a/games/fnaf/ce/engine/ce.h+++ b/games/fnaf/ce/engine/ce.h@@ -20,6 +20,10 @@ bool ce_init(void); /* false: the data is missing (ce_missing namesvoid ce_quit(void);/* shows the frame drawn; keep: the next frame starts as a copy of it */void ce_show(bool keep);+/* scr ready to be drawn into (after ce_show the LCD may not have taken the frame yet): before drawing */+void ce_wait(void);+/* frames shown: a buffer holds what was drawn into it 2 frames ago (a frame kept counts as 3) */+extern uint32_t ce_frames;/* the keys held: the CE's (CK_*) */enum {diff --git a/games/fnaf/ce/engine/ce_calc.c b/games/fnaf/ce/engine/ce_calc.cindex edfa5917..a758d207 100644--- a/games/fnaf/ce/engine/ce_calc.c+++ b/games/fnaf/ce/engine/ce_calc.c@@ -19,6 +19,7 @@ static uint8_t save_handle;bool ce_init(void) {kb_DisableOnLatch();+ kb_SetMode(MODE_3_CONTINUOUS); /* (the keys always there: no waiting for a scan) */gfx_Begin();gfx_SetDrawBuffer();memset(gfx_palette, 0, 512);@@ -34,22 +35,34 @@ void ce_quit(void) {save_handle = 0;}gfx_End();+ kb_Reset();/* a key still down would act on the home screen: wait until it is let go (2 seconds at most) */for (clock_t t = clock(); ce_keys() && clock() - t < 2 * CLOCKS_PER_SEC;) {}os_ClrHomeFull();}+uint32_t ce_frames;+static bool waiting, keeping; /* shown, scr not ready yet (ce_wait); to be the shown frame's copy */+void ce_show(bool keep) {+ ce_frames += keep ? 3 : 1;gfx_SwapDraw();+ waiting = true, keeping = keep;if (ce_pal_dirty) { /* the palette as the new frame starts: once the LCD has taken its address (gfx_SwapDraw* cleared the flag) */while (!(lcd_IntStatus & LCD_INT_LNBU)) {}memcpy(gfx_palette, ce_pal, sizeof ce_pal);ce_pal_dirty = false;+ ce_wait();}+}++void ce_wait(void) {+ if (!waiting) return;gfx_Wait();+ waiting = false;scr = (uint8_t *)gfx_vbuffer;- if (keep) memcpy(scr, scr == gfx_vram ? gfx_vram + SCR_W * SCR_H : gfx_vram, SCR_W * SCR_H);+ if (keeping) memcpy(scr, scr == gfx_vram ? gfx_vram + SCR_W * SCR_H : gfx_vram, SCR_W * SCR_H);}uint32_t ce_keys(void) {@@ -64,7 +77,6 @@ uint32_t ce_keys(void) {{3, kb_4, CK_DIGIT(4)}, {4, kb_5, CK_DIGIT(5)}, {5, kb_6, CK_DIGIT(6)}, {3, kb_7, CK_DIGIT(7)},{4, kb_8, CK_DIGIT(8)}, {5, kb_9, CK_DIGIT(9)},};- kb_Scan();uint32_t k = 0;for (unsigned i = 0; i < sizeof map / sizeof map[0]; i++)if (kb_Data[map[i].group] & map[i].bit) k |= map[i].key;diff --git a/games/fnaf/ce/engine/ce_host.c b/games/fnaf/ce/engine/ce_host.cindex 8e55daf5..1a43265b 100644--- a/games/fnaf/ce/engine/ce_host.c+++ b/games/fnaf/ce/engine/ce_host.c@@ -29,7 +29,10 @@ bool ce_init(void) {void ce_quit(void) { ce_save_done(); }+uint32_t ce_frames;+void ce_show(bool keep) {+ ce_frames += keep ? 3 : 1;if (ce_pal_dirty) memcpy(shown_pal, ce_pal, sizeof ce_pal), ce_pal_dirty = false;shown ^= 1;scr = buffers[shown ^ 1];@@ -37,6 +40,7 @@ void ce_show(bool keep) {host_frames++;}+void ce_wait(void) {}uint32_t ce_keys(void) { return host_keys; }uint32_t ce_ticks(void) { return host_ticks; }diff --git a/games/fnaf/ce/engine/draw_ez80.s b/games/fnaf/ce/engine/draw_ez80.sindex ec4a21e6..9c8c1dd0 100644--- a/games/fnaf/ce/engine/draw_ez80.s+++ b/games/fnaf/ce/engine/draw_ez80.s@@ -45,7 +45,8 @@ _ce_double:djnz 1bret-; void ce_remap(uint8_t *p, int n, const uint8_t *lut): each of n bytes through the table (256-aligned)+; void ce_remap(uint8_t *p, int n, const uint8_t *lut): each of n bytes (under 1024) through the table+; (256-aligned), 4 at a time.section .text._ce_remap.global _ce_remap.type _ce_remap, @function@@ -55,23 +56,51 @@ _ce_remap:ld hl, (iy + 3)ld bc, (iy + 6)ld de, (iy + 9)- ld a, b- or a, c- ret z+ ld a, c+ and a, 3+ jr z, 2f+ push bc+ ld b, a1:ld e, (hl)ld a, (de)ld (hl), ainc hl- dec bc- ld a, b- or a, c- jr nz, 1b+ djnz 1b+ pop bc+2:+ srl b+ rr c+ srl b+ rr c+ ld a, c+ or a, a+ ret z+ ld b, a+3:+ ld e, (hl)+ ld a, (de)+ ld (hl), a+ inc hl+ ld e, (hl)+ ld a, (de)+ ld (hl), a+ inc hl+ ld e, (hl)+ ld a, (de)+ ld (hl), a+ inc hl+ ld e, (hl)+ ld a, (de)+ ld (hl), a+ inc hl+ djnz 3bret; void ce_alpha(const uint8_t *rows, int stride, int h, uint8_t *dst, const uint8_t *ink): h rows of 2-bit alpha; (stride bytes each, 4 pixels a byte, the first in the top bits) onto the screen (320 a row) from dst, alpha 1 to 3-; as ink[1] to ink[3], 0 left as it is+; as ink[1] to ink[3], 0 left as it is. IX the alpha, DE the screen, HL the inks (L the alpha), B the bytes left+; in the row, C the byte's pixels to come.section .text._ce_alpha.global _ce_alpha.type _ce_alpha, @function@@ -81,36 +110,71 @@ _ce_alpha:add iy, spld a, (iy + 9)or a, a- jr z, al_done+ jp z, al_doneld (al_stride), ald a, (iy + 12)or a, a- jr z, al_done+ jp z, al_doneld (al_h), ald ix, (iy + 18)+ ld hl, al_inkld a, (ix + 1)- ld (al_ink1), a+ ld (al_ink + 1), ald a, (ix + 2)- ld (al_ink2), a+ ld (al_ink + 2), ald a, (ix + 3)- ld (al_ink3), a- ld hl, (iy + 6)+ ld (al_ink + 3), a+ ld ix, (iy + 6)ld de, (iy + 15)al_row:push deld a, (al_stride)ld b, aal_byte:- ld a, (hl)- inc hl+ ld a, (ix + 0)+ inc ixor a, ajr z, al_skip4+ rlca+ rlcald c, a- call al_px- call al_px- call al_px- call al_px- djnz al_byte+ and a, 3+ jr z, 1f+ ld l, a+ ld a, (hl)+ ld (de), a+1: inc de+ ld a, c+ rlca+ rlca+ ld c, a+ and a, 3+ jr z, 2f+ ld l, a+ ld a, (hl)+ ld (de), a+2: inc de+ ld a, c+ rlca+ rlca+ ld c, a+ and a, 3+ jr z, 3f+ ld l, a+ ld a, (hl)+ ld (de), a+3: inc de+ ld a, c+ rlca+ rlca+ and a, 3+ jr z, 4f+ ld l, a+ ld a, (hl)+ ld (de), a+4: inc de+ dec b+ jp nz, al_bytejr al_nextal_skip4:inc de@@ -120,11 +184,10 @@ al_skip4:djnz al_byteal_next:pop de- push hl- ld hl, 320- add hl, deex de, hl- pop hl+ ld bc, 320+ add hl, bc+ ex de, hlld a, (al_h)dec ald (al_h), a@@ -132,28 +195,77 @@ al_next:al_done:pop ixret-; one pixel: C's top two bits (C shifted on), at DE (DE on)-al_px:- sla c- jr c, 2f- sla c- jr nc, 1f- ld a, (al_ink1)- ld (de), a-1: inc de- ret-2: sla c- ld a, (al_ink3)- jr c, 3f- ld a, (al_ink2)-3: ld (de), a- inc de- ret.section .bss._ce_alpha- .local al_stride, al_h, al_ink1, al_ink2, al_ink3+ .local al_stride, al_hal_stride: .ds 1al_h: .ds 1-al_ink1: .ds 1-al_ink2: .ds 1-al_ink3: .ds 1+ .section .bss._ce_alpha_ink+ .balign 256+ .local al_ink+al_ink: .ds 4++; void ce_runs(uint8_t *at, const uint8_t *runs, int n, uint8_t c): n runs of c (y, x, length: bytes; y never+; going back, lengths 1 or more) from at, 320 a row+ .section .text._ce_runs+ .global _ce_runs+ .type _ce_runs, @function+_ce_runs:+ push ix+ ld iy, 0+ add iy, sp+ ld de, (iy + 6)+ ld ix, (iy + 9)+ ld hl, (iy + 12)+ ld (ru_n), hl+ ld a, (iy + 15)+ ld (ru_c), a+ xor a, a+ ld (ru_y), a+ru_next:+ ld hl, (ru_n)+ ld bc, 0+ or a, a+ sbc hl, bc+ jr z, ru_done+ dec hl+ ld (ru_n), hl+ ld a, (ix + 0)+ ld hl, ru_y+ru_row:+ cp a, (hl)+ jr z, ru_at+ inc (hl)+ ex de, hl+ ld bc, 320+ add hl, bc+ ex de, hl+ jr ru_row+ru_at:+ ld hl, 0+ ld l, (ix + 1)+ add hl, de+ ld a, (ru_c)+ ld (hl), a+ ld bc, 0+ ld c, (ix + 2)+ dec c+ jr z, ru_one+ push de+ push hl+ pop de+ inc de+ ldir+ pop de+ru_one:+ lea ix, ix + 3+ jr ru_next+ru_done:+ pop ix+ ret++ .section .bss._ce_runs+ .local ru_n, ru_c, ru_y+ru_n: .ds 3+ru_c: .ds 1+ru_y: .ds 1diff --git a/games/fnaf/ce/engine/gfx.c b/games/fnaf/ce/engine/gfx.cindex 3caf2ce9..73551809 100644--- a/games/fnaf/ce/engine/gfx.c+++ b/games/fnaf/ce/engine/gfx.c@@ -22,6 +22,9 @@ void ce_alpha(const uint8_t *rows, int stride, int h, uint8_t *dst, const uint8_if (b >> 6) dst[4 * n + k] = ink[b >> 6];}}+void ce_runs(uint8_t *at, const uint8_t *runs, int n, uint8_t c) {+ for (; n--; runs += 3) memset(at + runs[0] * SCR_W + runs[1], c, runs[2]);+}#endifstatic Assets A;@@ -73,6 +76,13 @@ void pic_into(int pic, uint8_t *dst, int stride) {pic_decode(p, dst, stride, maps[slot_of(A.pic_group[pic])], in_place ? NULL : temp);}+void pic_into_wide(int pic, uint8_t *dst, int stride) {+ const uint8_t *p = asset(A.pic_first + pic);+ bool in_place = !pic_keeps(p) && stride == 2 * pic_width(p);+ if (!in_place) gfx_temp_uses++;+ pic_decode_wide(p, dst, stride, maps[slot_of(A.pic_group[pic])], in_place ? NULL : temp);+}+void pic_full(int pic, uint8_t *dst, int stride) {int base = pic_base(asset(A.pic_first + pic));if (base >= 0) pic_full(base, dst, stride);@@ -80,18 +90,50 @@ void pic_full(int pic, uint8_t *dst, int stride) {}/* ------------------------------------------------------------------ drawing */+Box d_touch;+static Box cl = {0, 0, SCR_W, SCR_H};++void d_clip(Box b) {+ cl.x0 = b.x0 > 0 ? b.x0 : 0, cl.y0 = b.y0 > 0 ? b.y0 : 0;+ cl.x1 = b.x1 < SCR_W ? b.x1 : SCR_W, cl.y1 = b.y1 < SCR_H ? b.y1 : SCR_H;+}++bool d_meets(Box b) { return b.x0 < cl.x1 && b.x1 > cl.x0 && b.y0 < cl.y1 && b.y1 > cl.y0; }++/* (x0, y0) to (x1, y1) on the screen: in d_touch, then clipped (false: nothing left) */+static bool box_clip(int *x0, int *y0, int *x1, int *y1) {+ if (*x0 < 0) *x0 = 0;+ if (*y0 < 0) *y0 = 0;+ if (*x1 > SCR_W) *x1 = SCR_W;+ if (*y1 > SCR_H) *y1 = SCR_H;+ if (*x0 >= *x1 || *y0 >= *y1) return false;+ if (*x0 < d_touch.x0) d_touch.x0 = *x0;+ if (*y0 < d_touch.y0) d_touch.y0 = *y0;+ if (*x1 > d_touch.x1) d_touch.x1 = *x1;+ if (*y1 > d_touch.y1) d_touch.y1 = *y1;+ if (*x0 < cl.x0) *x0 = cl.x0;+ if (*y0 < cl.y0) *y0 = cl.y0;+ if (*x1 > cl.x1) *x1 = cl.x1;+ if (*y1 > cl.y1) *y1 = cl.y1;+ return *x0 < *x1 && *y0 < *y1;+}+static bool clip(int *x, int *y, int *w, int *h, int *sx, int *sy) {- if (*x < 0) *sx -= *x, *w += *x, *x = 0;- if (*y < 0) *sy -= *y, *h += *y, *y = 0;- if (*x + *w > SCR_W) *w = SCR_W - *x;- if (*y + *h > SCR_H) *h = SCR_H - *y;- return *w > 0 && *h > 0;+ int x0 = *x, y0 = *y, x1 = x0 + *w, y1 = y0 + *h;+ if (!box_clip(&x0, &y0, &x1, &y1)) return false;+ *sx += x0 - *x, *sy += y0 - *y;+ *x = x0, *y = y0, *w = x1 - x0, *h = y1 - y0;+ return true;}void d_fill(int x, int y, int w, int h, uint8_t c) {int sx = 0, sy = 0;if (!clip(&x, &y, &w, &h, &sx, &sy)) return;- for (uint8_t *p = scr + y * SCR_W + x; h--; p += SCR_W) memset(p, c, (size_t)w);+ uint8_t *p = scr + y * SCR_W + x;+ if (w == 1)+ for (; h--; p += SCR_W) *p = c;+ else+ for (; h--; p += SCR_W) memset(p, c, (size_t)w);}void d_frame(int x, int y, int w, int h, uint8_t c) {@@ -104,7 +146,7 @@ void d_frame(int x, int y, int w, int h, uint8_t c) {void d_remap(int x, int y, int w, int h, const uint8_t *lut) {int sx = 0, sy = 0;if (!clip(&x, &y, &w, &h, &sx, &sy)) return;- for (uint8_t *row = scr + y * SCR_W + x; h--; row += SCR_W) ce_remap(row, w, lut);+ for (uint8_t *row = scr + y * SCR_W + x; h--; row += SCR_W) ce_remap(row, w, lut); /* (w at most 320) */}void d_blit(const uint8_t *src, int stride, int sx, int sy, int w, int h, int x, int y, bool dbl) {@@ -114,17 +156,15 @@ void d_blit(const uint8_t *src, int stride, int sx, int sy, int w, int h, int x,for (uint8_t *p = scr + y * SCR_W + x; h--; p += SCR_W, s += stride) memcpy(p, s, (size_t)w);return;}- /* rows doubled: source row r at screen rows y + 2r and y + 2r + 1 */- if (x < 0) sx -= x, w += x, x = 0;- if (x + w > SCR_W) w = SCR_W - x;- if (w <= 0) return;- for (int r = 0; r < h; r++) {- int yy = y + 2 * r;- if (yy + 1 < 0 || yy >= SCR_H) continue;- const uint8_t *s = src + (sy + r) * stride + sx;- if (yy >= 0) memcpy(scr + yy * SCR_W + x, s, (size_t)w);- if (yy + 1 < SCR_H) memcpy(scr + (yy + 1) * SCR_W + x, s, (size_t)w);- }+ /* rows doubled: source row r at screen rows y + 2r and y + 2r + 1 (sy2: the first row on the screen, in+ * halves) */+ int h2 = 2 * h, sy2 = 2 * sy;+ if (!clip(&x, &y, &w, &h2, &sx, &sy2)) return;+ uint8_t *p = scr + y * SCR_W + x;+ const uint8_t *s = src + (sy2 >> 1) * stride + sx;+ if (sy2 & 1) memcpy(p, s, (size_t)w), p += SCR_W, s += stride, h2--;+ for (; h2 >= 2; h2 -= 2, p += 2 * SCR_W, s += stride) memcpy(p, s, (size_t)w), memcpy(p + SCR_W, s, (size_t)w);+ if (h2) memcpy(p, s, (size_t)w);}void d_blit_key(const uint8_t *src, int stride, int w, int h, int x, int y, uint8_t key) {@@ -138,6 +178,7 @@ void d_blit_key(const uint8_t *src, int stride, int w, int h, int x, int y, uintvoid d_blit2(const uint8_t *src, int stride, int dx, uint8_t fill) {static uint8_t row[SCR_W];+ d_touch = (Box){0, 0, SCR_W, SCR_H}; /* (the clip not looked at: only drawn whole) */for (int r = 0; r < SCR_H / 2; r++) {uint8_t *d = scr + 2 * r * SCR_W;const uint8_t *s = src + r * stride;@@ -175,9 +216,16 @@ void d_snow(int n, uint32_t seed, const uint8_t *greys) {uint32_t r = 0x6F4A7C15u;for (int i = 0; i < 1024; i++) r ^= r << 13, r ^= r >> 17, r ^= r << 5, at[i] = (unsigned)(r % (SCR_W * SCR_H));}- unsigned k = (unsigned)seed & 1023, step = ((unsigned)(seed >> 10) & 510) | 1;+ d_touch = (Box){0, 0, SCR_W, SCR_H}; /* (as d_blit2) */+ /* (from a random place in at, a random odd step at a time) */+ const unsigned *p = at + ((unsigned)seed & 1023), *end = at + 1024;+ unsigned step = ((unsigned)(seed >> 10) & 510) | 1;uint8_t c = (uint8_t)(seed >> 20);- while (n--) scr[at[k]] = greys[c++ & 7], k = (k + step) & 1023;+ while (n--) {+ scr[*p] = greys[c++ & 7];+ p += step;+ if (p >= end) p -= 1024;+ }}/* ------------------------------------------------------------------ text */@@ -186,18 +234,18 @@ int d_line_h(int font) { return font_at(font)[0]; }/* 2-bit alpha rows (4 pixels a byte, the first in the top bits) at (x, y) in an ink; clipped */static void alpha(const uint8_t *rows, int w, int h, int x, int y, Ink ink) {- int stride = (w + 3) >> 2;- if (x >= 0 && y >= 0 && x + 4 * stride <= SCR_W && y + h <= SCR_H) { /* (all of it on the screen: quick) */+ int stride = (w + 3) >> 2, x0 = x, y0 = y, x1 = x + w, y1 = y + h;+ if (!box_clip(&x0, &y0, &x1, &y1)) return;+ if (x0 == x && y0 == y && x1 == x + w && y1 == y + h && x + 4 * stride <= SCR_W) { /* (all of it: quick) */ce_alpha(rows, stride, h, scr + y * SCR_W + x, ink);return;}- for (int r = 0; r < h; r++, rows += stride) {- int yy = y + r;- if (yy < 0 || yy >= SCR_H) continue;- uint8_t *d = scr + yy * SCR_W;- for (int c = 0; c < w; c++) {- int a = rows[c >> 2] >> (6 - 2 * (c & 3)) & 3, xx = x + c;- if (a && xx >= 0 && xx < SCR_W) d[xx] = ink[a];+ for (int r = y0 - y; r < y1 - y; r++) { /* (the row's last pixels, past w, are 0) */+ const uint8_t *row = rows + r * stride;+ uint8_t *d = scr + (y + r) * SCR_W;+ for (int c = x0 - x; c < x1 - x; c++) {+ int a = row[c >> 2] >> (6 - 2 * (c & 3)) & 3;+ if (a) d[x + c] = ink[a];}}}@@ -206,17 +254,47 @@ static void glyph(const uint8_t *f, const uint8_t *gl, int x, int y, Ink ink) {if (gl[1]) alpha(f + (gl[6] | gl[7] << 8), gl[1], gl[2], x + (int8_t)gl[3], y + (int8_t)gl[4], ink);}+/* how far a font's glyphs reach around the pen: left of it, above, right of the advance, below */+#define FONTS_KNOWN 8+static struct {+ bool known;+ int8_t x0, y0, x1, y1;+} reach[FONTS_KNOWN];+int d_text_n(int font, int x, int y, const char *s, int n, Ink ink) {const uint8_t *f = font_at(font);- int x0 = x;+ int w = d_text_wn(font, s, n);+ if (font < FONTS_KNOWN && !reach[font].known) {+ int x0 = 0, y0 = 127, x1 = 0, y1 = -128;+ for (int c = 0; c < 95; c++) {+ const uint8_t *gl = f + 2 + 8 * c;+ if (!gl[1]) continue;+ int gx = (int8_t)gl[3], gy = (int8_t)gl[4];+ x0 = gx < x0 ? gx : x0, y0 = gy < y0 ? gy : y0;+ x1 = gx + gl[1] - gl[0] > x1 ? gx + gl[1] - gl[0] : x1, y1 = gy + gl[2] > y1 ? gy + gl[2] : y1;+ }+ reach[font].x0 = (int8_t)x0, reach[font].y0 = (int8_t)(y0 < y1 ? y0 : 0);+ reach[font].x1 = (int8_t)x1, reach[font].y1 = (int8_t)(y0 < y1 ? y1 : 0);+ reach[font].known = true;+ }+ bool whole = false;+ if (font < FONTS_KNOWN) { /* (the whole string in the clip: its glyphs drawn as they are) */+ int x0 = x + reach[font].x0, y0 = y + reach[font].y0, x1 = x + w + reach[font].x1, y1 = y + reach[font].y1;+ int c0 = x0, r0 = y0, c1 = x1, r1 = y1;+ if (!box_clip(&c0, &r0, &c1, &r1)) return w;+ whole = c0 == x0 && r0 == y0 && c1 == x1 && r1 == y1;+ }for (; *s && n-- > 0; s++) {int ch = (uint8_t)*s;if (ch < 32 || ch > 126) ch = '?';const uint8_t *gl = f + 2 + 8 * (ch - 32);- glyph(f, gl, x, y, ink);+ if (!whole) glyph(f, gl, x, y, ink);+ else if (gl[1])+ ce_alpha(f + (gl[6] | gl[7] << 8), (gl[1] + 3) >> 2, gl[2], scr + (y + (int8_t)gl[4]) * SCR_W + x + (int8_t)gl[3],+ ink);x += gl[0];}- return x - x0;+ return w;}int d_text(int font, int x, int y, const char *s, Ink ink) { return d_text_n(font, x, y, s, 1 << 20, ink); }diff --git a/games/fnaf/ce/engine/gfx.h b/games/fnaf/ce/engine/gfx.hindex 2c1c271d..1091d282 100644--- a/games/fnaf/ce/engine/gfx.h+++ b/games/fnaf/ce/engine/gfx.h@@ -32,11 +32,23 @@ const uint8_t *map_remap(int group, int which); /* 0: each colour darker, 1: lint pic_group_of(int pic);void pic_into(int pic, uint8_t *dst, int stride);void pic_full(int pic, uint8_t *dst, int stride);+void pic_into_wide(int pic, uint8_t *dst, int stride); /* (pic_into, each pixel twice across) */int pic_w(int pic);int pic_h(int pic);-/* drawing on scr, clipped to the screen */+/* drawing on scr, clipped to the screen and to d_clip's box (the whole screen to start with); d_touch: the box of+ * the screen that the drawing since it was emptied (BOX_NONE) would have covered, clipped or not */+typedef struct {+ int x0, y0, x1, y1;+} Box;+#define BOX_NONE ((Box){SCR_W, SCR_H, 0, 0})+#define BOX_ALL ((Box){0, 0, SCR_W, SCR_H})+extern Box d_touch;+void d_clip(Box b);+bool d_meets(Box b); /* the clip meets b */void d_fill(int x, int y, int w, int h, uint8_t c);+/* n runs of c (y, x, length: bytes, y never going back) from (x, y), all on the screen and in the clip */+void ce_runs(uint8_t *at, const uint8_t *runs, int n, uint8_t c);void d_frame(int x, int y, int w, int h, uint8_t c);void d_remap(int x, int y, int w, int h, const uint8_t *lut);/* (sx, sy, w, h) of an image (stride bytes a row) at (x, y): rows doubled (dbl: the image's rows are half) */diff --git a/games/fnaf/ce/engine/pic.c b/games/fnaf/ce/engine/pic.cindex 76c83874..8a444d76 100644--- a/games/fnaf/ce/engine/pic.c+++ b/games/fnaf/ce/engine/pic.c@@ -17,6 +17,13 @@ void pic_decode(const uint8_t *pic, uint8_t *dst, int stride, const uint8_t *mappic_expand(grid, dst, stride, bw, bh, map);}+void pic_decode_wide(const uint8_t *pic, uint8_t *dst, int stride, const uint8_t *map, uint8_t *temp) {+ int bw = bw_of(pic), bh = pic[3];+ uint8_t *grid = temp ? temp : dst + 2 * bh * stride - bw * bh; /* (in place: as pic_decode, read before written) */+ zx0_Decompress(grid, pic + (pic[0] & 1 ? 6 : 4));+ pic_expand_wide(grid, dst, stride, bw, bh, map);+}+#ifndef CE_ASMvoid pic_expand(const uint8_t *grid, uint8_t *dst, int stride, int bw, int bh, const uint8_t *map) {for (int by = 0; by < bh; by++) {@@ -28,4 +35,16 @@ void pic_expand(const uint8_t *grid, uint8_t *dst, int stride, int bw, int bh, c}}}++void pic_expand_wide(const uint8_t *grid, uint8_t *dst, int stride, int bw, int bh, const uint8_t *map) {+ for (int by = 0; by < bh; by++) {+ uint8_t *r0 = dst + 2 * by * stride, *r1 = r0 + stride;+ for (int bx = 0; bx < bw; bx++, r0 += 4, r1 += 4) {+ uint8_t k = *grid++;+ if (k == 255) continue;+ r0[0] = r0[1] = map[k], r0[2] = r0[3] = map[256 + k];+ r1[0] = r1[1] = map[512 + k], r1[2] = r1[3] = map[768 + k];+ }+ }+}#endifdiff --git a/games/fnaf/ce/engine/pic.h b/games/fnaf/ce/engine/pic.hindex 63753cd4..81c04caf 100644--- a/games/fnaf/ce/engine/pic.h+++ b/games/fnaf/ce/engine/pic.h@@ -21,4 +21,7 @@ bool pic_keeps(const uint8_t *pic); /* some blocks keep what dst holds */void pic_decode(const uint8_t *pic, uint8_t *dst, int stride, const uint8_t *map, uint8_t *temp);/* (the grid's blocks into pixels: pic_ez80.s on the calculator) */void pic_expand(const uint8_t *grid, uint8_t *dst, int stride, int bw, int bh, const uint8_t *map);+/* the same, each pixel twice across (dst twice as wide: bw at most 255) */+void pic_decode_wide(const uint8_t *pic, uint8_t *dst, int stride, const uint8_t *map, uint8_t *temp);+void pic_expand_wide(const uint8_t *grid, uint8_t *dst, int stride, int bw, int bh, const uint8_t *map);#endifdiff --git a/games/fnaf/ce/engine/pic_ez80.s b/games/fnaf/ce/engine/pic_ez80.sindex 3cda30f4..d74501ad 100644--- a/games/fnaf/ce/engine/pic_ez80.s+++ b/games/fnaf/ce/engine/pic_ez80.s@@ -1,6 +1,7 @@; The grid's blocks into pixels on the calculator: pic.c's pic_expand in eZ80 assembly (pic.h).;; void pic_expand(const uint8_t *grid, uint8_t *dst, int stride, int bw, int bh, const uint8_t *map)+; void pic_expand_wide(the same): each pixel twice across (bw at most 255);; Each byte of the grid a block of 2 x 2: its codeword's four bytes from the map's four pages (DE: D the page, E the; codeword), into the block's two rows (IY, IX); 255 leaves the block as it is.@@ -86,3 +87,80 @@ ex_keep:ex_done:pop ixret++ .section .text._pic_expand_wide+ .global _pic_expand_wide+ .type _pic_expand_wide, @function+_pic_expand_wide:+ push ix+ ld iy, 0+ add iy, sp+ ld a, (iy + 15) ; bw+ or a, a+ jp z, ew_done+ ld (ex_bw), a+ ld a, (iy + 18) ; bh+ or a, a+ jp z, ew_done+ ld (ex_bh), a+ ld hl, (iy + 9) ; dst: the first row of blocks+ ld (ex_row), hl+ ld bc, (iy + 12) ; stride+ ld de, (iy + 21) ; the map: its first page+ ld hl, (iy + 6) ; the grid+ push hl+ ld hl, (iy + 12)+ add hl, hl+ ld (ex_stride2), hl+ pop hl+ew_rows:+ ld iy, (ex_row)+ lea ix, iy + 0+ add ix, bc ; (the second row: a stride further)+ push bc+ ld a, (ex_bw)+ ld b, a+ew_block:+ ld a, (hl)+ inc hl+ cp a, 255+ jr z, ew_keep+ ld e, a+ ld a, (de)+ ld (iy + 0), a+ ld (iy + 1), a+ inc d+ ld a, (de)+ ld (iy + 2), a+ ld (iy + 3), a+ inc d+ ld a, (de)+ ld (ix + 0), a+ ld (ix + 1), a+ inc d+ ld a, (de)+ ld (ix + 2), a+ ld (ix + 3), a+ dec d+ dec d+ dec d+ew_keep:+ lea iy, iy + 4+ lea ix, ix + 4+ djnz ew_block+ pop bc+ push hl+ ld hl, (ex_row)+ push de+ ld de, (ex_stride2)+ add hl, de+ pop de+ ld (ex_row), hl+ pop hl+ ld a, (ex_bh)+ dec a+ ld (ex_bh), a+ jr nz, ew_rows+ew_done:+ pop ix+ retdiff --git a/games/fnaf/ce/host.mk b/games/fnaf/ce/host.mkindex f2ffc2fd..84fb7303 100644--- a/games/fnaf/ce/host.mk+++ b/games/fnaf/ce/host.mk@@ -7,7 +7,7 @@ FLAGS = -std=gnu11 -O1 -g -Wall -Wextra -Wno-unused-parameter -Wno-missing-fieldSRC = src/main.c src/scenes.c src/view.c src/save.c src/rules.c engine/pic.c engine/gfx.c engine/ce_host.cHEADERS = $(wildcard src/*.h engine/*.h ../src/*.h) ../src/game.c ../src/night.c ../src/calls.c-.PHONY: all test clean+.PHONY: all test test-ce cleanall: build/play build/picsbuild/play: $(SRC) tests/play.c $(HEADERS)@@ -22,5 +22,9 @@ build/pics: engine/pic.c engine/gfx.c engine/ce_host.c tests/pics.c $(HEADERS)test: build/play build/picspython3 tests/check.py build/play build/pics+# (with the calculator's decoder in tools/ce_emu too: the CE toolchain on PATH)+test-ce: build/play build/pics+ python3 tests/check.py build/play build/pics --ce+clean:$(Q)rm -rf builddiff --git a/games/fnaf/ce/src/fnaf.h b/games/fnaf/ce/src/fnaf.hindex 47b6011e..a3f4769d 100644--- a/games/fnaf/ce/src/fnaf.h+++ b/games/fnaf/ce/src/fnaf.h@@ -14,7 +14,8 @@#define RAM_NEEDED (SCENE_W * SCENE_H + 160 * 120) /* scene, then extra (160 x 120) */extern uint8_t *scene, *extra;extern int scene_pic; /* the picture scene holds, -1 none */-void scene_want(int pic); /* scene holds pic: its bases (or the frames before it) decoded first as needed */+void scene_want(int pic); /* scene holds pic: its bases (or the frames before it) decoded first as needed; a+ * picture half the screen's width (the jumpscares) twice across, 320 a row */extern int extra_pic; /* what extra holds (a picture: the monitor's frame, a twitch...; or EXTRA_*) */#define EXTRA_FACES (-2) /* the custom night's four faces */void extra_want(int pic, int stride);@@ -35,6 +36,9 @@ Ink ink_fade(Ink ink, int a);void view_paused(void);void static_over(int a, int group); /* the static over the screen, of a strength (of 32) */void view_reset(void); /* a new night: the office's pictures again */+#ifdef HOST+extern int view_partial, view_wrong; /* (tests) office frames drawn only where they changed; not as drawn whole */+#endifint office_pan(void);/* save.c */void save_open(void);diff --git a/games/fnaf/ce/src/main.c b/games/fnaf/ce/src/main.cindex 8d6dd8d8..2a6efb50 100644--- a/games/fnaf/ce/src/main.c+++ b/games/fnaf/ce/src/main.c@@ -43,7 +43,10 @@ void scene_want(int pic) {int from = n - 1;if (chain[from] == scene_pic) from--;scene_pic = -1;- for (int k = from; k >= 0; k--) pic_into(chain[k], scene, SCENE_W);+ bool wide = pic_w(pic) == SCR_W / 2;+ for (int k = from; k >= 0; k--)+ if (wide) pic_into_wide(chain[k], scene, SCR_W);+ else pic_into(chain[k], scene, SCENE_W);scene_pic = pic;}@@ -127,6 +130,7 @@ int main(void) {if (acc > 32768 * 8) acc = 0; /* far behind: no rush */if (!g.running) break;if (steps) {+ ce_wait();game_draw();ce_show(keep_frame);keep_frame = false;diff --git a/games/fnaf/ce/src/view.c b/games/fnaf/ce/src/view.cindex 80694573..33809dfc 100644--- a/games/fnaf/ce/src/view.c+++ b/games/fnaf/ce/src/view.c@@ -63,6 +63,118 @@ static void prepare(void) {f.seed = rnd();}+/* ------------------------------------------------------------------ drawing only what changed+ * The office mostly stands still. A frame of it is measured first: drawn with nothing drawn, sig_add taking what+ * decides the office, part and part_end around each part that changes on its own (the fan, the clock, a+ * caption...): what it shows, and where it goes (d_touch; a part shown before is known). When the buffer drawn+ * into holds the same office from 2 frames ago, only the parts that changed since are drawn again: the frame each+ * time, clipped to them, with the parts that it doesn't meet left out. */+#define PARTS 16+#define BASE 16+typedef struct {+ Box at;+ const void *what; /* what a part shows: its text, a tag... (and how, where: always at the same place) */+ uint32_t how;+} Part;+typedef struct {+ bool ok;+ uint32_t frame;+ int n, nb;+ Part p[PARTS];+ int base[BASE]; /* what decides the office */+} Drawn;+static Drawn now, last, drawn[2]; /* this frame's; the last one measured (its parts' places); each buffer's */+static uint8_t *drawn_scr; /* (the first buffer: drawn[0]) */+static bool measuring, partly; /* measuring; drawing again where it changed (part_at: the part reached) */+static int part_at;+static const char tags[4]; /* (what the parts with no text of their own show) */+#ifdef HOST+int view_partial, view_wrong;+#endif++static void sig_add(int v) {+ if (measuring && now.nb < BASE) now.base[now.nb] = v;+ if (measuring) now.nb++;+}++/* a part that changes on its own: true when it's to be drawn (measured, part_end after it; or met by the clip) */+static bool part(const void *what, uint32_t how) {+ if (measuring) {+ int k = now.n++;+ if (k >= PARTS) return true;+ if (k < last.n && last.p[k].what == what && last.p[k].how == how) { /* (known: drawn alike before) */+ now.p[k] = last.p[k];+ return false;+ }+ now.p[k] = (Part){BOX_NONE, what, how};+ d_touch = BOX_NONE;+ return true;+ }+ if (!partly) return true;+ int k = part_at++;+ return k >= PARTS || d_meets(now.p[k].at);+}+static void part_end(void) {+ if (measuring && now.n <= PARTS) now.p[now.n - 1].at = d_touch;+}++static bool has(const Drawn *d, const Part *q) {+ for (int i = 0; i < d->n; i++) {+ const Part *o = &d->p[i];+ if (o->what == q->what && o->how == q->how && o->at.x0 == q->at.x0 && o->at.y0 == q->at.y0 &&+ o->at.x1 == q->at.x1 && o->at.y1 == q->at.y1)+ return true;+ }+ return false;+}++static int add_box(Box *list, int n, Box b) {+ if (b.x0 >= b.x1 || b.y0 >= b.y1) return n;+ for (int i = 0; i < n; i++)+ if (list[i].x0 <= b.x0 && list[i].y0 <= b.y0 && list[i].x1 >= b.x1 && list[i].y1 >= b.y1) return n;+ list[n] = b;+ return n + 1;+}++/* draws a frame (draw) only where it changed since the buffer's last one */+static void draw_changes(void (*draw)(void)) {+ now.n = now.nb = 0;+ measuring = true;+ d_clip((Box){0, 0, 0, 0});+ draw();+ measuring = false;+ d_clip(BOX_ALL);+ if (!drawn_scr) drawn_scr = scr;+ Drawn *d = &drawn[scr != drawn_scr], *o = &drawn[scr == drawn_scr];+ bool fits = now.n <= PARTS && now.nb <= BASE;+ bool same = fits && d->ok && o->ok && d->frame + 2 == ce_frames && o->frame + 1 == ce_frames &&+ d->nb == now.nb && !memcmp(d->base, now.base, sizeof now.base[0] * (size_t)now.nb);+ if (!same) {+ draw();+ } else {+ Box dirty[2 * PARTS];+ int nd = 0;+ for (int i = 0; i < now.n; i++)+ if (!has(d, &now.p[i])) nd = add_box(dirty, nd, now.p[i].at);+ for (int i = 0; i < d->n; i++)+ if (!has(&now, &d->p[i])) nd = add_box(dirty, nd, d->p[i].at);+ partly = true;+ for (int i = 0; i < nd; i++) part_at = 0, d_clip(dirty[i]), draw();+ partly = false;+ d_clip(BOX_ALL);+#ifdef HOST+ static uint8_t as_drawn[SCR_W * SCR_H];+ memcpy(as_drawn, scr, sizeof as_drawn);+ draw();+ view_partial++;+ if (memcmp(as_drawn, scr, sizeof as_drawn)) view_wrong++;+#endif+ }+ now.ok = fits, now.frame = ce_frames;+ *d = now;+ if (fits) last = now;+}+static const uint8_t GREYS[8] = {UI_BLACK, UI_WHITE1, UI_GREY1, UI_SPARE, UI_DIM, UI_GREY2, UI_EDGE, UI_BOX};static const uint8_t DARK_GREYS[8] = {UI_BLACK, UI_BLACK, UI_BOX, UI_BLACK, UI_EDGE, UI_BLACK, UI_BOX, UI_GREY1};@@ -91,9 +203,12 @@ static void tiny_text(int x, int y, const char *s, uint8_t c) {for (; *s; s++, x += 4) {int k = *s >= '1' && *s <= '7' ? *s - '1' : *s - 'A' + 7;uint16_t b = tiny[k];- for (int r = 0; r < 5; r++)- for (int col = 0; col < 3; col++)- if (b >> (14 - r * 3 - col) & 1) scr[(y + r) * SCR_W + x + col] = c;+ uint8_t *d = scr + y * SCR_W + x;+ for (int r = 0; r < 5; r++, d += SCR_W, b = (uint16_t)(b << 3)) {+ if (b & 0x4000) d[0] = c;+ if (b & 0x2000) d[1] = c;+ if (b & 0x1000) d[2] = c;+ }}}@@ -126,6 +241,7 @@ static void text_shadow(int font, int x, int y, const char *s, int n, Ink ink) {/* ------------------------------------------------------------------ around the view */static void clock_and_night(void) {+ if (!part(tags, (uint32_t)(N.hour << 4 | N.night))) return;char h[3] = {'1', '2', 0}; /* 12 AM, then 1 to 5 */if (N.hour) h[0] = (char)('0' + N.hour), h[1] = 0;int x = 310 - d_text_w(FONT_BIG, " AM") - d_text_w(FONT_CLOCK, h);@@ -134,11 +250,13 @@ static void clock_and_night(void) {int nx = 310 - d_text_w(FONT_HUD, "Night ") - d_text_w(FONT_PIXEL, "0");nx += d_text(FONT_HUD, nx, 31, "Night ", INK_WHITE);small_digits(FONT_PIXEL, nx, 30, N.night, INK_WHITE);+ part_end();}static void power_hud(void) {- int x = 8 + d_text(FONT_HUD, 8, 206, "Power left: ", INK_WHITE);int p = N.power / 10;+ if (!part(tags + 1, (uint32_t)(p << 3 | N.usage))) return;+ int x = 8 + d_text(FONT_HUD, 8, 206, "Power left: ", INK_WHITE);small_digits(FONT_PIXEL, x, 205, p, INK_WHITE);d_text(FONT_HUD, x + d_text_w(FONT_PIXEL, p >= 10 ? "00" : "0") + 1, 206, "%", INK_WHITE);x = 8 + d_text(FONT_HUD, 8, 222, "Usage: ", INK_WHITE);@@ -148,18 +266,20 @@ static void power_hud(void) {d_fill(x + 9 * i, 222, 7, 12, bars[i]);d_fill(x + 9 * i + 1, 223, 5, 2, lit[i]);}+ part_end();}static void call_hud(int grp) {if (N.night > 5 || N.call_muted || N.t >= call_end(N.night)) return;- if (N.t > 3) {+ if (N.t > 3 && part(tags + 2, 0)) {d_remap(6, 6, 74, 15, map_remap(grp, 0));d_frame(6, 6, 74, 15, UI_SILVER);d_text(FONT_HUD, 12, 8, "MUTE CALL", INK_WHITE);d_text(FONT_TEXT, 84, 8, "0", INK_DIM);+ part_end();}const char *s = call_line(N.night, N.t);- if (!s) return;+ if (!s || !part(s, 0)) return;const char *line;int len, n = 0;while (n < 3 && wrap(FONT_TEXT, s, 300, n, &line, &len)) n++;@@ -170,6 +290,7 @@ static void call_hud(int grp) {int w = d_text_wn(FONT_TEXT, line, len);text_shadow(FONT_TEXT, 160 - w / 2, y + 14 * k, line, len, INK_CALL);}+ part_end();}void view_captions(void) {@@ -177,21 +298,26 @@ void view_captions(void) {for (int i = 3; i >= 0; i--) {if (!g.cap[i] || g.cap_t[i] <= 0) continue;int a = g.cap_t[i] > 0.5f ? 32 : (int)(g.cap_t[i] * 64);- d_text(FONT_TEXT, 9, y + 1, g.cap[i], INK_BLACK);- d_text(FONT_TEXT, 8, y, g.cap[i], ink_fade(INK_CAPTION, a));+ if (part(g.cap[i], (uint32_t)(a << 8 | y))) {+ d_text(FONT_TEXT, 9, y + 1, g.cap[i], INK_BLACK);+ d_text(FONT_TEXT, 8, y, g.cap[i], ink_fade(INK_CAPTION, a));+ part_end();+ }y -= 13;}}/* the bar at the bottom to bring the monitor up or down */static void flip_bar(int grp) {+ if (!part(tags + 3, 0)) return;d_remap(118, 229, 84, 9, map_remap(grp, 1));d_frame(118, 229, 84, 9, UI_SPARE);for (int k = 0; k < 2; k++)for (int i = 0; i < 4; i++) {- scr[(235 - i) * SCR_W + 152 + 9 * k + i] = UI_WHITE;- scr[(235 - i) * SCR_W + 152 + 9 * k + 7 - i] = UI_WHITE;+ d_fill(152 + 9 * k + i, 235 - i, 1, 1, UI_WHITE);+ d_fill(152 + 9 * k + 7 - i, 235 - i, 1, 1, UI_WHITE);}+ part_end();}/* ------------------------------------------------------------------ the office */@@ -199,8 +325,13 @@ int office_pan(void) { return (int)N.pan; }static int office(void) {int px = (int)N.pan;+ sig_add(px);+ sig_add(N.dark);if (N.dark) {- if (N.dark == 3 && (N.dark_t > 0.35f || (int)(N.dark_t * 30) & 1)) {+ bool black = N.dark == 3 && (N.dark_t > 0.35f || (int)(N.dark_t * 30) & 1);+ sig_add(black);+ sig_add(N.dark == 2 && f.freddy_face);+ if (black) {d_fill(0, 0, SCR_W, SCR_H, UI_BLACK);return pic_group_of(PIC_DARK);}@@ -213,27 +344,36 @@ static int office(void) {return pic_group_of(PIC_DARK);}office_pictures();+ bool lit_l = N.light == 1 + LEFT && !f.light_off, lit_r = N.light == 1 + RIGHT && !f.light_off;+ sig_add(lit_l ? 1 + (N.pos[BONNIE] == AT_DOOR) : 0);+ sig_add(lit_r ? 1 + (N.pos[CHICA] == AT_DOOR) : 0);scene_want(PIC_OFFICE);d_blit(scene, SCENE_W, px, 0, SCR_W, SCENE_H, 0, 0, true);- if (N.light == 1 + LEFT && !f.light_off) {+ if (lit_l) {want_patch(N.pos[BONNIE] == AT_DOOR ? PIC_LIGHT_L_WHO : PIC_LIGHT_L, LIGHT_L_W);d_blit(patch, LIGHT_L_W, 0, 0, LIGHT_L_W, LIGHT_L_H, LIGHT_L_X - px, 2 * LIGHT_L_Y, true);}- if (N.light == 1 + RIGHT && !f.light_off) {+ if (lit_r) {want_patch(N.pos[CHICA] == AT_DOOR ? PIC_LIGHT_R_WHO : PIC_LIGHT_R, LIGHT_R_W);d_blit(patch, LIGHT_R_W, 0, 0, LIGHT_R_W, LIGHT_R_H, LIGHT_R_X - px, 2 * LIGHT_R_Y, true);}- d_blit(fans[f.fan], 48, 0, 0, 48, 24, FAN_X - px, FAN_Y, true);+ if (part(fans, (uint32_t)(px << 2 | f.fan))) {+ d_blit(fans[f.fan], 48, 0, 0, 48, 24, FAN_X - px, FAN_Y, true);+ part_end();+ }/* the buttons (all off: as the office has them) */int bl = N.door[LEFT] | (N.light == 1 + LEFT) << 1, br = N.door[RIGHT] | (N.light == 1 + RIGHT) << 1;+ sig_add(bl << 2 | br);if (bl) d_blit(panel_l[bl - 1], 32, 0, 0, 26, 40, PANEL_L_X - px, PANEL_Y, true);if (br) d_blit(panel_r[br - 1], 48, 0, 0, 48, 40, PANEL_R_X - px, PANEL_Y, true);for (int s = 0; s < 2; s++) {int vis = (int)(N.door_anim[s] * 120 + 0.5f);+ sig_add(vis);if (vis > 0)d_blit(s ? door_r : door_l, s ? DOOR_RW : DOOR_LW, 0, 120 - vis, s ? DOOR_R_W : DOOR_L_W, vis,(s ? DOOR_R_X : DOOR_L_X) - px, 0, true);}+ sig_add(N.gold == 3);if (N.gold == 3) {extra_want(PIC_OFFICE_GOLD, GOLD_W);d_blit(extra, GOLD_W, 0, 0, GOLD_W, GOLD_H / 2, GOLD_X - px, GOLD_Y, true);@@ -268,18 +408,29 @@ static int cam_picture(void) {return -1;}+/* a button of the map: 17 x 11, its edge and its inside */+static void map_button(uint8_t *p, uint8_t fill) {+ memset(p, UI_MAP_LINE, 17);+ for (int r = 1; r < 10; r++) {+ p += SCR_W;+ p[0] = p[16] = UI_MAP_LINE;+ memset(p + 1, fill, 15);+ }+ memset(p + SCR_W, UI_MAP_LINE, 17);+}+static void camera_map(void) {const uint8_t *m = asset(ASSET_CAMMAP);int size = m[0], runs = m[1] | m[2] << 8;int mx = SCR_W - size - 4, my = SCR_H - size - 12;const uint8_t *r = m + 3;- for (int i = 0; i < runs; i++, r += 3) memset(scr + (my + r[0]) * SCR_W + mx + r[1], UI_WHITE, r[2]);+ ce_runs(scr + my * SCR_W + mx, r, runs, UI_WHITE); /* (the cameras are only drawn whole: not clipped) */+ r += 3 * runs;bool blink = !((int)(g.t * 3) & 1);for (int k = 0; k < CAM_COUNT; k++) {int cx = mx + r[2 * k], cy = my + r[2 * k + 1];bool sel = k == N.cam;- d_fill(cx - 8, cy - 5, 17, 11, sel && blink ? UI_MAP_ON : UI_MAP_OFF);- d_frame(cx - 8, cy - 5, 17, 11, UI_MAP_LINE);+ map_button(scr + (cy - 5) * SCR_W + cx - 8, sel && blink ? UI_MAP_ON : UI_MAP_OFF);tiny_text(cx - (cam_labels[k][1] ? 3 : 1), cy - 2, cam_labels[k], UI_WHITE);}d_text(FONT_HUD, mx - 2, my - 18, cam_names[N.cam], INK_WHITE);@@ -334,6 +485,14 @@ static int hallucination(int grp) {}/* the night: paused, the picture only, darker */+static void night_draw(bool paused);+static void office_frame(void) { night_draw(false); }++static void night_frame(bool paused) {+ if (!paused && N.flip <= 0 && !f.halluc) draw_changes(office_frame);+ else night_draw(paused);+}+static void night_draw(bool paused) {bool cams = N.flip >= 1 && !N.dark;int grp;@@ -364,10 +523,10 @@ static void night_draw(bool paused) {void view_night(void) {prepare();- night_draw(false);+ night_frame(false);}-void view_paused(void) { night_draw(true); }+void view_paused(void) { night_frame(true); }/* ------------------------------------------------------------------ jumpscares */void view_scare(void) {@@ -379,7 +538,9 @@ void view_scare(void) {int w = g.scare, k = (int)(g.scare_t * fps[w]);k = loops[w] ? k % count[w] : k < count[w] ? k : count[w] - 1;int dx = g.scare_t > 0.1f ? f.shake : 0;- scene_want(first[w] + k);- d_blit2(scene, SCENE_W, dx, UI_BLACK);+ scene_want(first[w] + k); /* (twice across: its rows doubled here) */+ d_blit(scene, SCR_W, 0, 0, SCR_W, SCR_H / 2, dx, 0, true);+ if (dx > 0) d_fill(0, 0, dx, SCR_H, UI_BLACK);+ if (dx < 0) d_fill(SCR_W + dx, 0, -dx, SCR_H, UI_BLACK);pal_set(pic_group_of(first[w]), 32, 0);}diff --git a/games/fnaf/ce/tests/ce_pics/src/main.c b/games/fnaf/ce/tests/ce_pics/src/main.cindex 6b76f1dd..d7ada75a 100644--- a/games/fnaf/ce/tests/ce_pics/src/main.c+++ b/games/fnaf/ce/tests/ce_pics/src/main.c@@ -1,6 +1,7 @@/* The calculator's decoder (engine/pic_ez80.s) checked: every picture decoded with its bases, a sum of its bytes- * (two 16-bit sums, as tests/pics.c --sums makes them on a computer) into the appvar FNAFSUM. tests/check.py runs- * it in tools/ce_emu and compares; it also times the decoding (the cycles in FNAFSUM's last 4 bytes...). */+ * (two 16-bit sums, as tests/pics.c --sums makes them on a computer) into the appvar FNAFSUM, then the time it took+ * (32768ths of a second) and how many pixels decoded twice across (pic_expand_wide) differ from the plain decoding.+ * tests/check.py --ce runs it in tools/ce_emu and compares. */#include <fileioc.h>#include <string.h>#include <time.h>@@ -9,6 +10,12 @@static const uint8_t *vars[DATA_VARS];static uint8_t sums[PIC_COUNT * 4 + 8];+static void wide_full(int p, uint8_t *dst, int stride) { /* (its bases first, as pic_full) */+ int base = pic_base(asset(PIC_FIRST + p));+ if (base >= 0) wide_full(base, dst, stride);+ pic_into_wide(p, dst, stride);+}+int main(void) {if (!ce_data(DATA_VAR, DATA_VARS, vars)) return 1;static const Assets a = {vars, asset_at, pic_group, PIC_FIRST, MAP_FIRST, FONT_FIRST};@@ -28,6 +35,19 @@ int main(void) {sums[4 * p + 2] = (uint8_t)s2, sums[4 * p + 3] = (uint8_t)(s2 >> 8);}uint32_t t = (uint32_t)(clock() - t0);+ /* the pictures half the screen's width (decoded twice across in the game): each pixel as the plain decoding's */+ uint32_t wrong = 0;+ for (int p = 0; p < PIC_COUNT; p++) {+ int w = pic_w(p), h = pic_h(p);+ if (w != SCR_W / 2) continue;+ uint8_t *wide = buf + w * h;+ pic_full(p, buf, w);+ memset(wide, 0, (size_t)(2 * w * h));+ wide_full(p, wide, 2 * w);+ for (int i = 0; i < w * h; i++)+ if (wide[2 * i] != buf[i] || wide[2 * i + 1] != buf[i]) wrong++;+ }+ memcpy(sums + PIC_COUNT * 4 + 4, &wrong, 4);#ifdef DUMP /* (one picture's bytes, to look at: FNAFDMP) */pic_full(DUMP, buf, pic_w(DUMP));uint8_t d = ti_Open("FNAFDMP", "w");diff --git a/games/fnaf/ce/tests/check.py b/games/fnaf/ce/tests/check.pyindex ee1c6e4a..6ffe4407 100644--- a/games/fnaf/ce/tests/check.py+++ b/games/fnaf/ce/tests/check.py@@ -1,13 +1,15 @@#!/usr/bin/env python3"""Checks of the CE version of Five Nights at Freddy's on a computer (make -f host.mk test): every picture decodedas tools/pack.py made it, the menus, whole nights played by tests/play.c's own player, each animatronic's attack,-the power going out, Golden Freddy, the custom night, pausing, Home and the save, and random keys.+the power going out, Golden Freddy, the custom night, pausing, Home and the save, and random keys. With --ce, the+calculator's own decoder too (tests/ce_pics in tools/ce_emu: the CE toolchain's cedev-config on PATH).- python3 tests/check.py build/play build/pics [--quick]"""+ python3 tests/check.py build/play build/pics [--quick] [--ce]"""import osimport randomimport reimport shutil+import structimport subprocessimport sysimport tempfile@@ -15,6 +17,8 @@ import tempfilePLAY = sys.argv[1] if len(sys.argv) > 1 else "build/play"PICTURES = sys.argv[2] if len(sys.argv) > 2 else "build/pics"QUICK = "--quick" in sys.argv+CE = "--ce" in sys.argv+HERE = os.path.dirname(os.path.abspath(__file__))ENV = dict(os.environ, ASAN_OPTIONS="detect_leaks=0", UBSAN_OPTIONS="halt_on_error=1:print_stacktrace=1")failures = []SC_TITLE, SC_NIGHT, SC_SCARE, SC_SIX, SC_PAPER, SC_OVER, SC_CUSTOM = 0, 4, 5, 6, 7, 8, 9@@ -47,6 +51,9 @@ def run(args, frames, saves=None, what=""):s[k] = [int(x) for x in v.split(",")] if "," in v else float(v) if "." in v else int(v)events = [dict(zip(e.split()[1::2], e.split()[2::2]), kind=e.split()[0])for e in re.findall(r"EVENT (.*)", p.stdout)]+ if s.get("wrong"):+ failures.append(f"{what or args}: {s['wrong']} of {s['partial']} office frames drawn where they changed "+ "aren't as drawn whole")return s, events@@ -64,6 +71,36 @@ def save_file(saves):p = subprocess.run([PICTURES], capture_output=True, text=True, env=ENV, timeout=600)check(p.returncode == 0 and "runtime error" not in p.stderr, f"pictures: {p.stdout[-300:]} {p.stderr[-1500:]}")+# the calculator's decoder (eZ80): every picture's sums as the computer's, and the jumpscares decoded twice across+# as the plain decoding+if CE:+ game = os.path.dirname(HERE)+ root = os.path.abspath(os.path.join(game, "..", "..", ".."))+ for d in (game, os.path.join(HERE, "ce_pics")):+ p = subprocess.run(["make", "-C", d], capture_output=True, text=True)+ check(p.returncode == 0, f"the CE build in {d}: {p.stdout[-500:]} {p.stderr[-1500:]}")+ out = tempfile.mkdtemp(prefix="fnafce_pics_")+ appvars = sorted(f for f in os.listdir(os.path.join(game, "bin")) if re.match(r"FNAFD\d+\.8xv$", f))+ p = subprocess.run([sys.executable, os.path.join(root, "tools", "ce_emu.py"),+ os.path.join(HERE, "ce_pics", "bin", "PICTEST.8xp")] ++ [os.path.join(game, "bin", f) for f in appvars] ++ ["--ms", "120000", "--out", out, "--vars-out", out], capture_output=True, text=True)+ sums = os.path.join(out, "host.sums")+ subprocess.run([PICTURES, "--sums", sums], capture_output=True, env=ENV)+ var = os.path.join(out, "FNAFSUM.8xv")+ if not os.path.exists(var) or not os.path.exists(sums):+ failures.append(f"the CE decoder: no sums {p.stdout[-500:]}")+ else:+ v = open(var, "rb").read()+ data = v[55 + 17 + 2:-2] # (an .8xv: its header, the variable's entry, the data's size)+ host = open(sums, "rb").read()+ n = len(host) // 4+ bad = [i for i in range(n) if data[4 * i:4 * i + 4] != host[4 * i:4 * i + 4]]+ wide = struct.unpack("<I", data[4 * n + 4:4 * n + 8])[0]+ check(not bad, f"the CE decoder: pictures {bad[:20]} aren't as on the computer")+ check(wide == 0, f"the CE decoder: {wide} pixels decoded twice across aren't as the plain decoding")+ shutil.rmtree(out, ignore_errors=True)+# the title, New Game, the newspaper (skipped), how to play, the first night's card, then the officesaves = tempfile.mkdtemp(prefix="fnafce_check_")s, _ = run(["--keys", "60:ok,200:ok,700:ok"], 1400, saves, "a new game")diff --git a/games/fnaf/ce/tests/play.c b/games/fnaf/ce/tests/play.cindex 907cd875..011a45c1 100644--- a/games/fnaf/ce/tests/play.c+++ b/games/fnaf/ce/tests/play.c@@ -171,9 +171,9 @@ int main(int argc, char **argv) {}ce_quit();printf("END scene %d night %d hour %d power %d t %.1f frames %d running %d paused %d reached %d stars %d "- "pos %d,%d,%d foxy %d gold %d dark %d muted %d ai %d,%d,%d,%d doors %d,%d cam %d keys_seen %d pounds %d\n",+ "pos %d,%d,%d foxy %d gold %d dark %d muted %d ai %d,%d,%d,%d doors %d,%d cam %d keys_seen %d pounds %d partial %d wrong %d\n",g.scene, g.n.night, g.n.hour, g.n.power, g.n.t, f, g.running, g.paused, g.reached, g.stars, g.n.pos[0],g.n.pos[1], g.n.pos[2], g.n.foxy, g.n.gold, g.n.dark, g.n.call_muted, g.n.ai[0], g.n.ai[1], g.n.ai[2],- g.n.ai[3], g.n.door[0], g.n.door[1], g.n.cam, g.keys_seen, g.n.foxy_pounds);+ g.n.ai[3], g.n.door[0], g.n.door[1], g.n.cam, g.keys_seen, g.n.foxy_pounds, view_partial, view_wrong);return 0;}