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
NumDash: faster drawing, same pixels
Mason Chendca569ae3513
diff --git a/games/numdash/src/gfx.c b/games/numdash/src/gfx.cindex 07c390d5..4ce491ef 100644--- a/games/numdash/src/gfx.c+++ b/games/numdash/src/gfx.c@@ -13,7 +13,7 @@ void gfx_clip(int x0, int y0, int x1, int y1) {}void gfx_unclip(void) { clip_x0 = 0; clip_y0 = 0; clip_x1 = GFX_W; clip_y1 = GFX_H; }-static bool clip_rect(int *x, int *y, int *w, int *h) {+static inline bool clip_rect(int *x, int *y, int *w, int *h) {int x0 = *x, y0 = *y, x1 = *x + *w, y1 = *y + *h;int cy0 = clip_y0 > gfx_y0 ? clip_y0 : gfx_y0, cy1 = clip_y1 < gfx_y1 ? clip_y1 : gfx_y1;if (x0 < clip_x0) x0 = clip_x0;@@ -26,6 +26,24 @@ static bool clip_rect(int *x, int *y, int *w, int *h) {}static inline uint16_t *pix(int x, int y) { return gfx_strip + (y - gfx_y0) * GFX_W + x; }static inline unsigned a32_of(unsigned a256) { return a256 >= 256 ? 32 : (a256 + 4) >> 3; }+/* Sets n pixels from p to c, two per store once aligned. */+typedef uint32_t __attribute__((may_alias)) u32_alias;+static inline void fill_span(uint16_t *p, int n, color_t c) {+ if (n <= 0) return;+ if ((uintptr_t)p & 2) { *p++ = c; n--; }+ uint32_t cc = c | (uint32_t)c << 16;+ u32_alias *q = (u32_alias *)p;+ int m = n >> 1;+ for (; m >= 4; m -= 4, q += 4) { q[0] = cc; q[1] = cc; q[2] = cc; q[3] = cc; }+ for (; m > 0; m--) *q++ = cc;+ if (n & 1) *(uint16_t *)q = c;+}+/* n pixels blended normally with c at a32 (0..32), as put() does. */+static inline void mix_span(uint16_t *p, int n, color_t c, unsigned a32) {+ if (!a32) return;+ if (a32 >= 32) { fill_span(p, n, c); return; }+ for (int i = 0; i < n; i++) p[i] = px_mix(p[i], c, a32);+}color_t c_mix(color_t a, color_t b, unsigned t) {if (t >= 256) return b;@@ -61,10 +79,7 @@ color_t c_hsv_lighten(color_t c, int sat_delta, int val_delta) {void gfx_fill(int x, int y, int w, int h, color_t c) {if (!clip_rect(&x, &y, &w, &h)) return;- for (int j = y; j < y + h; j++) {- uint16_t *p = pix(x, j);- for (int i = 0; i < w; i++) p[i] = c;- }+ for (int j = y; j < y + h; j++) fill_span(pix(x, j), w, c);}void gfx_blend(int x, int y, int w, int h, color_t c, unsigned alpha) {unsigned a = a32_of(alpha);@@ -89,8 +104,7 @@ void gfx_vgrad(int x, int y, int w, int h, color_t top, color_t bottom) {if (h0 <= 0 || !clip_rect(&x, &y, &w, &h)) return;for (int j = y; j < y + h; j++) {color_t c = c_mix(top, bottom, h0 > 1 ? (unsigned)((j - y0) * 256 / (h0 - 1)) : 0);- uint16_t *p = pix(x, j);- for (int i = 0; i < w; i++) p[i] = c;+ fill_span(pix(x, j), w, c);}(void)x0; (void)w0;}@@ -108,13 +122,18 @@ void gfx_vgrad_alpha(int x, int y, int w, int h, color_t c, unsigned at, unsignevoid gfx_hgrad_alpha(int x, int y, int w, int h, color_t c, unsigned al, unsigned ar, int mode) {int x0 = x, w0 = w;if (w0 <= 0 || !clip_rect(&x, &y, &w, &h)) return;+ /* the alpha of a column is the same on every row */+ uint8_t acol[GFX_W];+ for (int i = 0; i < w; i++) {+ int k = x + i - x0;+ acol[i] = (uint8_t)a32_of(w0 > 1 ? (al * (unsigned)(w0 - 1 - k) + ar * (unsigned)k) / (unsigned)(w0 - 1) : al);+ }for (int j = y; j < y + h; j++) {uint16_t *p = pix(x, j);- for (int i = 0; i < w; i++) {- int k = x + i - x0;- unsigned a = a32_of(w0 > 1 ? (al * (unsigned)(w0 - 1 - k) + ar * (unsigned)k) / (unsigned)(w0 - 1) : al);- if (!a) continue;- p[i] = mode == BLEND_ADD ? px_add(p[i], c, a) : px_mix(p[i], c, a);+ if (mode == BLEND_ADD) {+ for (int i = 0; i < w; i++) if (acol[i]) p[i] = px_add(p[i], c, acol[i]);+ } else {+ for (int i = 0; i < w; i++) if (acol[i]) p[i] = px_mix(p[i], c, acol[i]);}}}@@ -202,9 +221,11 @@ void gfx_ring(int cx16, int cy16, int r16, int t16, color_t c, unsigned alpha, ivoid gfx_round_rect(int x, int y, int w, int h, int r, color_t c, unsigned alpha) {int x0 = x, y0 = y, w0 = w, h0 = h;if (!clip_rect(&x, &y, &w, &h)) return;+ const unsigned full = a32_of(alpha);for (int j = y; j < y + h; j++) {uint16_t *p = pix(x, j);int ry = j - y0 < r ? r - (j - y0) : (j - y0 >= h0 - r ? (j - y0) - (h0 - r - 1) : 0);+ if (!ry) { mix_span(p, w, c, full); continue; } /* no corner on this row */for (int i = 0; i < w; i++) {int k = x + i - x0;int rx = k < r ? r - k : (k >= w0 - r ? k - (w0 - r - 1) : 0);@@ -237,7 +258,7 @@ void gfx_rectf(float x0, float y0, float x1, float y1, color_t c, unsigned alphaif (ix0 >= x && ix0 < x + w && lcov > 0 && ix0 < (int)ceilf(x0)) put(p + ix0, c, a32_of((unsigned)(lcov * ra)), mode);if (fx1 >= x && fx1 < x + w && rcov > 0 && fx1 >= fx0) put(p + fx1, c, a32_of((unsigned)(rcov * ra)), mode);if (!a) continue;- if (mode == BLEND_NORMAL && a >= 32) for (int i = fx0; i < fx1; i++) p[i] = c;+ if (mode == BLEND_NORMAL && a >= 32) fill_span(p + fx0, fx1 - fx0, c);else if (mode == BLEND_ADD) for (int i = fx0; i < fx1; i++) p[i] = px_add(p[i], c, a);else for (int i = fx0; i < fx1; i++) p[i] = px_mix(p[i], c, a);}@@ -317,18 +338,33 @@ static inline unsigned sample(const Sampler *sm, int u, int v, uint16_t *c) {int gfx_sprite_w(int spr, int xf) { return (xf & 1) ? sprite_def(spr)->h : sprite_def(spr)->w; }int gfx_sprite_h(int spr, int xf) { return (xf & 1) ? sprite_def(spr)->w : sprite_def(spr)->h; }-void gfx_sprite(int spr, int x, int y, int xf, color_t tint, unsigned alpha, int mode) {- if (spr < 0 || !alpha) return;- const Sprite *s = sprite_def(spr);+/* Where gfx_sprite puts a sprite: the anchor's offset in the turned image,+ and the turned image's size. */+static void sprite_place(const Sprite *s, int xf, int *tax, int *tay, int *dw, int *dh) {int w = s->w, h = s->h, rot = xf & XF_ROT;int ax = (xf & XF_FLIPX) ? w - s->ax : s->ax, ay = (xf & XF_FLIPY) ? h - s->ay : s->ay;- int tax, tay, dw = w, dh = h;+ *dw = w;+ *dh = h;switch (rot) {- case 1: tax = h - ay; tay = ax; dw = h; dh = w; break;- case 2: tax = w - ax; tay = h - ay; break;- case 3: tax = ay; tay = w - ax; dw = h; dh = w; break;- default: tax = ax; tay = ay; break;+ case 1: *tax = h - ay; *tay = ax; *dw = h; *dh = w; break;+ case 2: *tax = w - ax; *tay = h - ay; break;+ case 3: *tax = ay; *tay = w - ax; *dw = h; *dh = w; break;+ default: *tax = ax; *tay = ay; break;}+}+void gfx_sprite_rows(int spr, int y, int xf, int *y0, int *y1) {+ int tax, tay, dw, dh;+ sprite_place(sprite_def(spr), xf, &tax, &tay, &dw, &dh);+ *y0 = y - tay;+ *y1 = y - tay + dh;+}++void gfx_sprite(int spr, int x, int y, int xf, color_t tint, unsigned alpha, int mode) {+ if (spr < 0 || !alpha) return;+ const Sprite *s = sprite_def(spr);+ int w = s->w, h = s->h, rot = xf & XF_ROT;+ int tax, tay, dw, dh;+ sprite_place(s, xf, &tax, &tay, &dw, &dh);int dx0 = x - tax, dy0 = y - tay, cx = dx0, cy = dy0, cw = dw, ch = dh;if (!clip_rect(&cx, &cy, &cw, &ch)) return;Sampler sm;@@ -353,12 +389,85 @@ void gfx_sprite(int spr, int x, int y, int xf, color_t tint, unsigned alpha, int}}+/* One row of gfx_sprite_ex: n pixels from p, source position (u, v) in 16.16+ stepped by (sdu, sdv). Inlined for each format and blending mode. */+static inline __attribute__((always_inline)) void ex_span(uint16_t *p, int n, int32_t u, int32_t v, const int32_t sdu, const int32_t sdv,+ const Sampler *sm, const int W, const int H, const unsigned al,+ const int fmt, const int mode) {+ const uint8_t *data = sm->data;+ const int stride = sm->stride;+ for (; n > 0; n--, p++, u += sdu, v += sdv) {+ int32_t uu = u + 65536, vv = v + 65536; /* offset so that -1 < u maps to >= 0 */+ if (uu < 0 || vv < 0) continue;+ int ui = (uu >> 16) - 1, vi = (vv >> 16) - 1;+ unsigned fu = (unsigned)(uu >> 8) & 255, fv = (unsigned)(vv >> 8) & 255;+ unsigned i0, i1, i2, i3; /* texels (u, v), (u+1, v), (u, v+1), (u+1, v+1) */+ if (ui >= 0 && vi >= 0 && ui + 1 < W && vi + 1 < H) {+ if (fmt == 1) {+ const uint8_t *r0 = data + vi * stride + ui, *r1 = r0 + stride;+ i0 = r0[0]; i1 = r0[1]; i2 = r1[0]; i3 = r1[1];+ } else {+ const uint8_t *r0 = data + vi * stride + (ui >> 1), *r1 = r0 + stride;+ if (ui & 1) {+ i0 = r0[0] >> 4; i1 = r0[1] & 15u; i2 = r1[0] >> 4; i3 = r1[1] & 15u;+ } else {+ unsigned b0 = r0[0], b1 = r1[0];+ i0 = b0 & 15; i1 = b0 >> 4; i2 = b1 & 15; i3 = b1 >> 4;+ }+ }+ } else {+ unsigned idx[4];+ for (int k = 0; k < 4; k++) {+ int x = ui + (k & 1), y = vi + (k >> 1);+ if (x < 0 || y < 0 || x >= W || y >= H) { idx[k] = 0; continue; }+ const uint8_t *row = data + y * stride;+ idx[k] = fmt == 1 ? row[x] : (unsigned)((row[x >> 1] >> ((x & 1) * 4)) & 15);+ }+ i0 = idx[0]; i1 = idx[1]; i2 = idx[2]; i3 = idx[3];+ }+ if (i0 == i1 && i0 == i2 && i0 == i3) {+ /* uniform neighbourhood: no filtering needed */+ unsigned a = fmt == 1 ? sm->a32[i0 & 15] : sm->a32[i0];+ if (!a) continue;+ color_t c = fmt == 1 ? sm->col[i0 >> 4] : sm->col[i0];+ put(p, c, (a * al + 128) >> 8, mode);+ continue;+ }+ unsigned w00 = (256 - fu) * (256 - fv) >> 8, w10 = fu * (256 - fv) >> 8, w01 = (256 - fu) * fv >> 8, w11 = fu * fv >> 8;+ if (fmt == 0) {+ /* A4: one colour, only coverage is filtered */+ unsigned ta = sm->a32[i0] * w00 + sm->a32[i1] * w10 + sm->a32[i2] * w01 + sm->a32[i3] * w11;+ if (ta < 128) continue;+ unsigned a32 = (ta * al + 32768) >> 16;+ put(p, sm->col[0], a32 > 32 ? 32 : a32, mode);+ continue;+ }+ const unsigned ix[4] = {i0, i1, i2, i3}, wt[4] = {w00, w10, w01, w11};+ unsigned ta = 0, tr = 0, tg = 0, tb = 0;+ for (int k = 0; k < 4; k++) {+ unsigned a, cc;+ if (fmt == 1) { a = sm->a32[ix[k] & 15]; cc = sm->col[ix[k] >> 4]; } else { a = sm->a32[ix[k]]; cc = sm->col[ix[k]]; }+ a *= wt[k];+ if (!a) continue;+ ta += a;+ tr += ((cc >> 11) & 31) * a;+ tg += ((cc >> 5) & 63) * a;+ tb += (cc & 31) * a;+ }+ if (ta < 128) continue;+ color_t c = (color_t)(((tr / ta) << 11) | ((tg / ta) << 5) | (tb / ta));+ unsigned a32 = (ta * al + 32768) >> 16;+ put(p, c, a32 > 32 ? 32 : a32, mode);+ }+}+/* Rotated / scaled sprite with bilinear filtering. The inverse transform is* stepped in 16.16 fixed point; each row is clipped to the span where the* sample falls inside the texture. */-void gfx_sprite_ex(int spr, int x16, int y16, int ang16, int scale256, int flips, color_t tint, unsigned alpha, int mode) {- if (spr < 0 || !alpha || scale256 <= 0) return;- const Sprite *s = sprite_def(spr);+typedef struct { float sc, cs, sn, cx, cy; int bx, by, bw, bh; } ExGeom;+/* gfx_sprite_ex's transform and the box of the turned sprite (its flips do+ not change the box). */+static void ex_geom(const Sprite *s, int x16, int y16, int ang16, int scale256, ExGeom *g) {float sc = scale256 / 256.f, ang = ang16 / 16.f * 3.14159265f / 180.f;float cs = cosf(ang), sn = sinf(ang), cx = x16 / 16.f, cy = y16 / 16.f;float w = s->w, h = s->h, ax = s->ax, ay = s->ay;@@ -369,7 +478,25 @@ void gfx_sprite_ex(int spr, int x16, int y16, int ang16, int scale256, int flipsfloat rx = px_ * cs - py_ * sn + cx, ry = px_ * sn + py_ * cs + cy;if (rx < mnx) mnx = rx; if (rx > mxx) mxx = rx; if (ry < mny) mny = ry; if (ry > mxy) mxy = ry;}- int bx = (int)floorf(mnx) - 1, by = (int)floorf(mny) - 1, bw = (int)ceilf(mxx) - bx + 2, bh = (int)ceilf(mxy) - by + 2;+ g->sc = sc; g->cs = cs; g->sn = sn; g->cx = cx; g->cy = cy;+ g->bx = (int)floorf(mnx) - 1; g->by = (int)floorf(mny) - 1;+ g->bw = (int)ceilf(mxx) - g->bx + 2; g->bh = (int)ceilf(mxy) - g->by + 2;+}+void gfx_sprite_ex_rows(int spr, int x16, int y16, int ang16, int scale256, int *y0, int *y1) {+ ExGeom g;+ ex_geom(sprite_def(spr), x16, y16, ang16, scale256, &g);+ *y0 = g.by;+ *y1 = g.by + g.bh;+}++void gfx_sprite_ex(int spr, int x16, int y16, int ang16, int scale256, int flips, color_t tint, unsigned alpha, int mode) {+ if (spr < 0 || !alpha || scale256 <= 0) return;+ const Sprite *s = sprite_def(spr);+ ExGeom g;+ ex_geom(s, x16, y16, ang16, scale256, &g);+ const float sc = g.sc, cs = g.cs, sn = g.sn, cx = g.cx, cy = g.cy;+ const float w = s->w, h = s->h, ax = s->ax, ay = s->ay;+ int bx = g.bx, by = g.by, bw = g.bw, bh = g.bh;if (!clip_rect(&bx, &by, &bw, &bh)) return;Sampler sm;sampler_init(&sm, spr, tint, 256);@@ -399,65 +526,17 @@ void gfx_sprite_ex(int spr, int x16, int y16, int ang16, int scale256, int flipsif (i0 >= i1) continue;int32_t u = (int32_t)((uf + i0 * dudx) * 65536), v = (int32_t)((vf + i0 * dvdx) * 65536);uint16_t *p = pix(bx + i0, j);- const uint8_t *data = sm.data;- const int stride = sm.stride, fmt = s->fmt;- for (int i = i0; i < i1; i++, p++, u += sdu, v += sdv) {- int32_t uu = u + 65536, vv = v + 65536; /* offset so that -1 < u maps to >= 0 */- if (uu < 0 || vv < 0) continue;- int ui = (uu >> 16) - 1, vi = (vv >> 16) - 1;- unsigned fu = (unsigned)(uu >> 8) & 255, fv = (unsigned)(vv >> 8) & 255;- unsigned idx[4];- if (ui >= 0 && vi >= 0 && ui + 1 < W && vi + 1 < H) {- if (fmt == 1) {- const uint8_t *r0 = data + vi * stride + ui, *r1 = r0 + stride;- idx[0] = r0[0]; idx[1] = r0[1]; idx[2] = r1[0]; idx[3] = r1[1];- } else {- const uint8_t *r0 = data + vi * stride, *r1 = r0 + stride;- int b0 = ui >> 1, b1 = (ui + 1) >> 1, s0 = (ui & 1) * 4, s1 = ((ui + 1) & 1) * 4;- idx[0] = (r0[b0] >> s0) & 15; idx[1] = (r0[b1] >> s1) & 15;- idx[2] = (r1[b0] >> s0) & 15; idx[3] = (r1[b1] >> s1) & 15;- }- } else {- for (int k = 0; k < 4; k++) {- int x = ui + (k & 1), y = vi + (k >> 1);- if (x < 0 || y < 0 || x >= W || y >= H) { idx[k] = 0; continue; }- const uint8_t *row = data + y * stride;- idx[k] = fmt == 1 ? row[x] : (unsigned)((row[x >> 1] >> ((x & 1) * 4)) & 15);- }- }- if (idx[0] == idx[1] && idx[0] == idx[2] && idx[0] == idx[3]) {- /* uniform neighbourhood: no filtering needed */- unsigned ix = idx[0], a = fmt == 1 ? sm.a32[ix & 15] : sm.a32[ix];- if (!a) continue;- color_t c = fmt == 1 ? sm.col[ix >> 4] : sm.col[ix];- put(p, c, (a * al + 128) >> 8, mode);- continue;- }- unsigned w00 = (256 - fu) * (256 - fv) >> 8, w10 = fu * (256 - fv) >> 8, w01 = (256 - fu) * fv >> 8, w11 = fu * fv >> 8;- unsigned wt[4] = {w00, w10, w01, w11};- unsigned ta = 0, tr = 0, tg = 0, tb = 0;- if (fmt == 0) {- /* A4: one colour, only coverage is filtered */- for (int k = 0; k < 4; k++) ta += sm.a32[idx[k]] * wt[k];- if (ta < 128) continue;- unsigned a32 = (ta * al + 32768) >> 16;- put(p, sm.col[0], a32 > 32 ? 32 : a32, mode);- continue;- }- for (int k = 0; k < 4; k++) {- unsigned ix = idx[k], a, cc;- if (fmt == 1) { a = sm.a32[ix & 15]; cc = sm.col[ix >> 4]; } else { a = sm.a32[ix]; cc = sm.col[ix]; }- a *= wt[k];- if (!a) continue;- ta += a;- tr += ((cc >> 11) & 31) * a;- tg += ((cc >> 5) & 63) * a;- tb += (cc & 31) * a;- }- if (ta < 128) continue;- color_t c = (color_t)(((tr / ta) << 11) | ((tg / ta) << 5) | (tb / ta));- unsigned a32 = (ta * al + 32768) >> 16;- put(p, c, a32 > 32 ? 32 : a32, mode);+ int n = i1 - i0;+ bool add = mode == BLEND_ADD;+ if (s->fmt == 0) {+ if (add) ex_span(p, n, u, v, sdu, sdv, &sm, W, H, al, 0, BLEND_ADD);+ else ex_span(p, n, u, v, sdu, sdv, &sm, W, H, al, 0, BLEND_NORMAL);+ } else if (s->fmt == 1) {+ if (add) ex_span(p, n, u, v, sdu, sdv, &sm, W, H, al, 1, BLEND_ADD);+ else ex_span(p, n, u, v, sdu, sdv, &sm, W, H, al, 1, BLEND_NORMAL);+ } else {+ if (add) ex_span(p, n, u, v, sdu, sdv, &sm, W, H, al, 2, BLEND_ADD);+ else ex_span(p, n, u, v, sdu, sdv, &sm, W, H, al, 2, BLEND_NORMAL);}}}@@ -514,28 +593,37 @@ static void glyph_rows(const Sprite *s, int r, int ew, int eh, int k0, int k1) {h1[i] = m;}}+ /* Row k's own h1 stands in for rows outside the box: it is part of the+ maximum anyway. */for (int k = k0; k < k1; k++) {uint8_t *d = g_dil[k];- for (int i = 0; i < ew; i++) {- uint8_t m = 0;- if (r == 1) {- for (int dy = -1; dy <= 1; dy++) {- int kk = k + dy;- if (kk >= 0 && kk < eh && g_h1[kk][i] > m) m = g_h1[kk][i];- }- } else {- for (int dy = -2; dy <= 2; dy++) {- int kk = k + dy;- if (kk < 0 || kk >= eh) continue;- const uint8_t *h = g_h1[kk];- uint8_t v = h[i];- if (dy > -2 && dy < 2) { /* |dx| <= 2 */- if (i > 0 && h[i - 1] > v) v = h[i - 1];- if (i + 1 < ew && h[i + 1] > v) v = h[i + 1];- }- if (v > m) m = v;- }+ const uint8_t *hm = g_h1[k], *hu = k >= 1 ? g_h1[k - 1] : hm, *hd = k + 1 < eh ? g_h1[k + 1] : hm;+ if (r == 1) {+ for (int i = 0; i < ew; i++) {+ uint8_t m = hm[i];+ if (hu[i] > m) m = hu[i];+ if (hd[i] > m) m = hd[i];+ d[i] = m;}+ continue;+ }+ /* 5x5 without corners: |dy| <= 1 with |dx| <= 2, or |dy| = 2 with |dx| <= 1 */+ const uint8_t *hu2 = k >= 2 ? g_h1[k - 2] : hm, *hd2 = k + 2 < eh ? g_h1[k + 2] : hm;+ uint8_t vb[GB_W + 2], *v = vb + 1; /* h1 over rows k-1..k+1, with zeros around */+ vb[0] = 0;+ for (int i = 0; i < ew; i++) {+ uint8_t m = hm[i];+ if (hu[i] > m) m = hu[i];+ if (hd[i] > m) m = hd[i];+ v[i] = m;+ }+ v[ew] = 0;+ for (int i = 0; i < ew; i++) {+ uint8_t m = v[i];+ if (v[i - 1] > m) m = v[i - 1];+ if (v[i + 1] > m) m = v[i + 1];+ if (hu2[i] > m) m = hu2[i];+ if (hd2[i] > m) m = hd2[i];d[i] = m;}}@@ -555,6 +643,12 @@ static void draw_glyph(const Font *f, int spr, int x, int baseline, color_t top,int k0 = cy - ey0 - sdy, k1 = cy + ch - ey0;glyph_rows(s, r, bw, bh, k0, k1);unsigned al = alpha > 256 ? 256 : alpha;+ /* blend amounts of each coverage level: shadow, and outline or fill */+ uint8_t ash[16], acv[16];+ for (unsigned v = 0; v < 16; v++) {+ ash[v] = (uint8_t)a32_of(v * f->shadow / 15 * al / 15);+ acv[v] = (uint8_t)a32_of(v * al / 15);+ }int cap_top = baseline - f->cap;for (int j = cy; j < cy + ch; j++) {uint16_t *p = pix(cx, j);@@ -563,12 +657,12 @@ static void draw_glyph(const Font *f, int spr, int x, int baseline, color_t top,int lj = j - ey0, sj = lj - sdy;for (int i = 0; i < cw; i++) {int li = cx + i - ex0, si = li - sdx;- unsigned sh = (si >= 0 && sj >= 0 && si < bw && sj < bh) ? g_dil[sj][si] * f->shadow / 15 : 0;+ unsigned sh = (si >= 0 && sj >= 0 && si < bw && sj < bh) ? g_dil[sj][si] : 0;unsigned o = 0, fv = 0;if (li < bw && lj < bh) { o = g_dil[lj][li]; fv = g_cov[lj][li]; }- if (sh && o < 15) put(p + i, 0, a32_of(sh * al / 15), BLEND_NORMAL);- if (o) put(p + i, 0, a32_of(o * al / 15), BLEND_NORMAL);- if (fv) put(p + i, fill, a32_of(fv * al / 15), BLEND_NORMAL);+ if (o < 15) put(p + i, 0, ash[sh], BLEND_NORMAL);+ put(p + i, 0, acv[o], BLEND_NORMAL);+ put(p + i, fill, acv[fv], BLEND_NORMAL);}}}@@ -721,6 +815,17 @@ void gfx_layer_draw(int cx, int cy, int scale256, color_t top, color_t bottom, u/* Anti-aliased convex polygon (vertices in pixels, either winding). */void gfx_poly(const float *xy, int n, color_t c, unsigned alpha, int mode) {if (n < 3 || n > 8 || !alpha) return;+ {+ /* Quick reject: the rows below cover every vertex, so they hold the+ ones drawn (floor(min) - 1 to ceil(max) + 1). */+ float lo = 1e9f, hi = -1e9f;+ for (int k = 0; k < n; k++) {+ if (xy[2 * k + 1] < lo) lo = xy[2 * k + 1];+ if (xy[2 * k + 1] > hi) hi = xy[2 * k + 1];+ }+ int cy0 = clip_y0 > gfx_y0 ? clip_y0 : gfx_y0, cy1 = clip_y1 < gfx_y1 ? clip_y1 : gfx_y1;+ if (hi <= (float)(cy0 - 2) || lo >= (float)(cy1 + 1)) return;+ }float area = 0, nx[8], ny[8], nc[8];for (int k = 0; k < n; k++) {int b = (k + 1) % n;diff --git a/games/numdash/src/gfx.h b/games/numdash/src/gfx.hindex 153189ec..5f446553 100644--- a/games/numdash/src/gfx.h+++ b/games/numdash/src/gfx.h@@ -63,6 +63,10 @@ void gfx_sprite(int spr, int x, int y, int xform, color_t tint, unsigned alpha25* 1/16 px, scale in 1/256. */void gfx_sprite_ex(int spr, int x16, int y16, int angle_deg16, int scale256, int flips, color_t tint, unsigned alpha256, int mode);int gfx_sprite_w(int spr, int xform);+/* Rows [y0, y1) that gfx_sprite and gfx_sprite_ex can draw on (before+ clipping), so callers can skip strips they would not touch. */+void gfx_sprite_rows(int spr, int y, int xform, int *y0, int *y1);+void gfx_sprite_ex_rows(int spr, int x16, int y16, int angle_deg16, int scale256, int *y0, int *y1);/* Sprites of both sets (assets.h, then assets_lv.h). */static inline const Sprite *sprite_def(int spr) { return spr < SPR_COUNT ? &sprites[spr] : &sprites_lv[spr - SPR_COUNT]; }/* Filled rectangle with anti-aliased edges (pixel coordinates). */diff --git a/games/numdash/src/main.c b/games/numdash/src/main.cindex d365200d..5f04a5af 100644--- a/games/numdash/src/main.c+++ b/games/numdash/src/main.c@@ -7,7 +7,8 @@ const char eadk_app_name[] __attribute__((section(".rodata.eadk_app_name"))) = "const uint32_t eadk_api_level __attribute__((section(".rodata.eadk_api_level"))) = 0;#endif-__attribute__((used)) volatile uint32_t perf_frames; /* PERF-TEMP */+/* Frames drawn so far, read by tools/emu.py to measure frame rates. */+__attribute__((used)) volatile uint32_t perf_frames;int main(int argc, char **argv) {(void)argc;@@ -48,7 +49,7 @@ int main(int argc, char **argv) {platform_frame_end();render_ms = (render_ms * 3 + (platform_millis() - t0)) / 4;frames++;- perf_frames++; /* PERF-TEMP */+ perf_frames++;if (now - fps_start >= 1000) {app.fps = frames * 1000 / (now - fps_start);frames = 0;diff --git a/games/numdash/src/scene.c b/games/numdash/src/scene.cindex 7fd89e8c..26cb2197 100644--- a/games/numdash/src/scene.c+++ b/games/numdash/src/scene.c@@ -89,7 +89,11 @@ typedef struct {int16_t angle; /* extra clockwise rotation, 1/16 degree */uint8_t xf;uint8_t ct, detail; /* palette entries: the colour (a program's base colour) and a program's detail colour */- uint8_t alpha, layer, kind;+ uint8_t alpha, kind;+ union {+ uint8_t layer; /* the drawing order, until sorted */+ uint8_t strips; /* then the strips it can touch: first | last << 4 */+ };} Item;/* sprite at a whole pixel; rotated / scaled sprite; four mirrored quartersof a round object; a block drawn from rectangles; the player. K_ADD:@@ -130,8 +134,9 @@ static int fade_at(const Level *L, float x) {static void push(float sx, float sy, int spr, int xf, int ct, int detail, int alpha, int layer, int kind, int scale, int angle16) {if (nitems >= MAX_ITEMS || spr < 0 || sx < -2000 || sx > 2000 || sy < -2000 || sy > 2000) return;- items[nitems++] = (Item){(int16_t)floorf(sx * 4 + .5f), (int16_t)floorf(sy * 4 + .5f), (uint16_t)spr, (uint16_t)scale, (int16_t)angle16,- (uint8_t)xf, (uint8_t)ct, (uint8_t)detail, (uint8_t)(alpha > 255 ? 255 : alpha), (uint8_t)layer, (uint8_t)kind};+ items[nitems++] = (Item){.x = (int16_t)floorf(sx * 4 + .5f), .y = (int16_t)floorf(sy * 4 + .5f), .spr = (uint16_t)spr, .scale = (uint16_t)scale,+ .angle = (int16_t)angle16, .xf = (uint8_t)xf, .ct = (uint8_t)ct, .detail = (uint8_t)detail,+ .alpha = (uint8_t)(alpha > 255 ? 255 : alpha), .kind = (uint8_t)kind, .layer = (uint8_t)layer};}/* ------------------------------------------------------------ palette */@@ -296,6 +301,142 @@ static void part_offset(int xf, float dx, float dy, float *ox, float *oy) {}}+/* ------------------------------------------------------------ programs */++/* Tile programs: rectangles, alpha ramps and convex polygons in half units+ around the item's anchor (2 units with TOP_BIG), y up, turned, flipped and+ scaled like the object. */+typedef struct { float cx, cy, m00, m01, m10, m11; bool aligned; } ProgXf;+typedef struct { int op, n, ct; unsigned a0, a1; float q[16]; } ProgOp;++/* A program is left out of strips beyond 60 units from its anchor. */+static bool prog_culled(const Item *it, int y0, int y1) {+ float cy = it->y * 0.25f, s = PX * 0.5f * it->scale / 256;+ return cy + 60 * s * 2 < y0 || cy - 60 * s * 2 > y1;+}++/* screen = centre + M * local (clockwise turn, y up locally, y down on screen) */+static void prog_xform(const Item *it, ProgXf *m) {+ float s = PX * 0.5f * it->scale / 256;+ m->cx = it->x * 0.25f;+ m->cy = it->y * 0.25f;+ float a = ((it->xf & 3) * 90 + it->angle / 16.f) * (3.14159265f / 180), ca = cosf(a), sa = sinf(a);+ if (fabsf(ca) < 1e-4f) ca = 0;+ if (fabsf(sa) < 1e-4f) sa = 0;+ float fx = (it->xf & XF_FLIPX) ? -s : s, fy = (it->xf & XF_FLIPY) ? -s : s;+ m->m00 = ca * fx;+ m->m01 = sa * fy;+ m->m10 = sa * fx;+ m->m11 = -ca * fy;+ m->aligned = ca == 0 || sa == 0;+}++/* Decodes the op at p (not the end mark), returns the next one. */+static const uint8_t *prog_op(const uint8_t *p, ProgOp *o) {+ o->op = p[0] & 0x7f;+ float k4 = (p[0] & TOP_BIG) ? 4 : 1;+ o->n = 4;+ if (o->op == TOP_POLY) {+ o->n = p[1];+ for (int k = 0; k < o->n * 2; k++) o->q[k] = (int8_t)p[2 + k] * k4;+ p += 2 + o->n * 2;+ o->ct = p[0];+ o->a0 = o->a1 = p[1];+ return p + 2;+ }+ for (int k = 0; k < 4; k++) o->q[k] = (int8_t)p[1 + k] * k4;+ o->ct = p[5];+ o->a0 = p[6];+ o->a1 = o->op == TOP_VGRAD ? p[7] : o->a0;+ return p + (o->op == TOP_VGRAD ? 8 : 7);+}++/* Screen rows around an op's shapes: its corners, with three rows to spare+ (the drawing adds at most one, and the ramp's bands round). */+static void prog_op_rows(const ProgXf *m, const ProgOp *o, float *lo, float *hi) {+ float mn = 1e9f, mx = -1e9f;+ int n = o->op == TOP_POLY ? o->n : 4;+ for (int k = 0; k < n; k++) {+ float lx = o->op == TOP_POLY ? o->q[2 * k] : o->q[(k & 1) ? 2 : 0];+ float ly = o->op == TOP_POLY ? o->q[2 * k + 1] : o->q[(k & 2) ? 3 : 1];+ float y = m->cy + m->m10 * lx + m->m11 * ly;+ if (y < mn) mn = y;+ if (y > mx) mx = y;+ }+ *lo = mn - 3;+ *hi = mx + 3;+}++/* The palette entry of an op's colour; takes a shade slot the first time. */+static int prog_pal(const Item *it, int ct) {+ int e = part_pal(ct, it->ct, it->detail, true);+ if (ct == CT_OBJ || ct == CT_P1ADD || ct == CT_P2ADD || ct == CT_LBG) e = it->ct;+ return e;+}++/* ------------------------------------------------------------ per frame */++/* First | last << 4 for the strips holding rows [r0, r1). */+static uint8_t strip_range(int r0, int r1) {+ if (r0 < 0) r0 = 0;+ if (r1 > GFX_H) r1 = GFX_H;+ if (r0 >= r1) return 0x0f; /* none */+ return (uint8_t)(r0 / STRIP_H | (r1 - 1) / STRIP_H << 4);+}+_Static_assert(STRIPS <= 15, "strip numbers are kept in 4 bits");++/* Once per frame, after sorting: the strips each item can touch (drawing+ clips to the strip anyway, so this only skips calls that would draw+ nothing), and the shades the tile programs take. Drawing takes a shade+ (pal_dyn) the first time a strip reaches its program and the slots can+ run out, so they are taken here in that order: strip by strip, in+ drawing order. */+static void prepare_items(void) {+ uint8_t first[MAX_ITEMS]; /* the first strip that reaches a program */+ uint16_t count[STRIPS + 1] = {0};+ for (int k = 0; k < nitems; k++) {+ Item *it = &items[order[k]];+ int kind = it->kind & 0x7f, r0 = 0, r1 = GFX_H;+ first[k] = STRIPS;+ if (kind == K_PROG) {+ for (int s = 0; s < STRIPS; s++)+ if (!prog_culled(it, s * STRIP_H, s * STRIP_H + STRIP_H)) { first[k] = (uint8_t)s; break; }+ ProgXf m;+ prog_xform(it, &m);+ float lo = 1e9f, hi = -1e9f;+ for (const uint8_t *p = tile_prog_data + tile_prog_off[it->spr]; *p;) {+ ProgOp o;+ float a, b;+ p = prog_op(p, &o);+ prog_op_rows(&m, &o, &a, &b);+ if (a < lo) lo = a;+ if (b > hi) hi = b;+ }+ if (!(lo >= -1000)) lo = -1000; /* (NaN too) */+ if (!(hi <= 1000)) hi = 1000;+ r0 = (int)floorf(lo);+ r1 = (int)ceilf(hi) + 1;+ } else if (kind == K_SPRITE) {+ gfx_sprite_rows(it->spr, (it->y + 2) >> 2, it->xf, &r0, &r1);+ } else if (kind != K_PLAYER) {+ gfx_sprite_ex_rows(it->spr, it->x * 4, it->y * 4, (it->xf & 3) * 90 * 16 + it->angle, it->scale, &r0, &r1);+ }+ count[first[k]]++;+ it->strips = strip_range(r0, r1);+ }+ for (int s = 0; s < STRIPS; s++)+ for (int k = 0, left = count[s]; k < nitems && left; k++) {+ if (first[k] != s) continue;+ left--;+ const Item *it = &items[order[k]];+ for (const uint8_t *p = tile_prog_data + tile_prog_off[it->spr]; *p;) {+ ProgOp o;+ p = prog_op(p, &o);+ prog_pal(it, o.ct);+ }+ }+}+void scene_prepare(const Game *g, const SceneOpts *o) {G = g;O = *o;@@ -457,6 +598,7 @@ void scene_prepare(const Game *g, const SceneOpts *o) {if (frame) { ground_top = GFX_H + 1; draw_gfx_grounds = false; } /* no ground in a turned frame */floor_bottom = (int)floorf(240 - g->ground_gfx * PX + .5f);ceil_bottom = (int)floorf(g->ground_gfx * PX + .5f);+ prepare_items();}/* Colour of a palette entry. */@@ -543,50 +685,31 @@ void scene_draw_player_icon(int mode, float cx, float cy, float rot, float scalegfx_sprite_ex(ball ? SPR_BALL1_P : SPR_CUBE1_P, x16, y16, a16, (int)(256 * scale), 0, p1, alpha, BLEND_NORMAL);}-/* Tile program: rectangles, alpha ramps and convex polygons in half units- around the item's anchor (2 units with TOP_BIG), y up, turned, flipped and- scaled like the object. Colour types BASE and DETAIL are the object's. */+/* Draws the ops of a tile program that reach the current strip. Colour+ types BASE and DETAIL are the object's. */static void draw_prog(const Item *it) {- const uint8_t *p = tile_prog_data + tile_prog_off[it->spr];- float cx = it->x * 0.25f, cy = it->y * 0.25f, s = PX * 0.5f * it->scale / 256;- if (cy + 60 * s * 2 < gfx_y0 || cy - 60 * s * 2 > gfx_y1) return;- float a = ((it->xf & 3) * 90 + it->angle / 16.f) * (3.14159265f / 180), ca = cosf(a), sa = sinf(a);- if (fabsf(ca) < 1e-4f) ca = 0;- if (fabsf(sa) < 1e-4f) sa = 0;- float fx = (it->xf & XF_FLIPX) ? -s : s, fy = (it->xf & XF_FLIPY) ? -s : s;- /* screen = centre + M * local (clockwise turn, y up locally, y down on screen) */- float m00 = ca * fx, m01 = sa * fy, m10 = sa * fx, m11 = -ca * fy;- bool aligned = ca == 0 || sa == 0;+ if (prog_culled(it, gfx_y0, gfx_y1)) return;+ ProgXf m;+ prog_xform(it, &m);+ const float cx = m.cx, cy = m.cy, m00 = m.m00, m01 = m.m01, m10 = m.m10, m11 = m.m11;bool add = (it->kind & K_ADD) != 0;- while (*p) {- int op = p[0] & 0x7f;- float k4 = (p[0] & TOP_BIG) ? 4 : 1;- int ct, n = 4;- unsigned a0, a1;- float q[16];- if (op == TOP_POLY) {- n = p[1];- for (int k = 0; k < n * 2; k++) q[k] = (int8_t)p[2 + k] * k4;- p += 2 + n * 2;- ct = p[0];- a0 = a1 = p[1];- p += 2;- } else {- for (int k = 0; k < 4; k++) q[k] = (int8_t)p[1 + k] * k4;- ct = p[5];- a0 = p[6];- a1 = op == TOP_VGRAD ? p[7] : a0;- p += op == TOP_VGRAD ? 8 : 7;- }- int e = part_pal(ct, it->ct, it->detail, true);- if (ct == CT_OBJ || ct == CT_P1ADD || ct == CT_P2ADD || ct == CT_LBG) e = it->ct;+ for (const uint8_t *p = tile_prog_data + tile_prog_off[it->spr]; *p;) {+ ProgOp o;+ p = prog_op(p, &o);+ float lo, hi;+ prog_op_rows(&m, &o, &lo, &hi);+ if (hi < gfx_y0 || lo > gfx_y1) continue; /* its shade was taken in prepare_items */+ const float *q = o.q;+ int ct = o.ct, n = o.n;+ unsigned a0 = o.a0, a1 = o.a1;+ int e = prog_pal(it, ct);color_t c;int mode;unsigned pa;pal_for(e, add || ct == CT_GLOW, &c, &mode, &pa);unsigned al = (it->alpha + 1) * pa / 255;if (!al) continue;- if (op == TOP_POLY) {+ if (o.op == TOP_POLY) {float xy[16];for (int k = 0; k < n; k++) {xy[2 * k] = cx + m00 * q[2 * k] + m01 * q[2 * k + 1];@@ -605,7 +728,7 @@ static void draw_prog(const Item *it) {xy[2 * k] = cx + m00 * lx[k] + m01 * ly[k];xy[2 * k + 1] = cy + m10 * lx[k] + m11 * ly[k];}- if (aligned) {+ if (m.aligned) {float x0 = fminf(fminf(xy[0], xy[2]), xy[4]), x1 = fmaxf(fmaxf(xy[0], xy[2]), xy[4]);float y0 = fminf(fminf(xy[1], xy[3]), xy[5]), y1 = fmaxf(fmaxf(xy[1], xy[3]), xy[5]);gfx_rectf(x0, y0, x1, y1, c, ab * al >> 8, mode);@@ -708,8 +831,11 @@ void scene_draw(void) {if (!G) return;scene_background(bg_col, G->bg_x, G->cam_y);if (O.editor) draw_grid();+ /* the strip's number, when it is one of the frame's strips */+ int strip = gfx_y1 - gfx_y0 == STRIP_H && gfx_y0 % STRIP_H == 0 ? gfx_y0 / STRIP_H : -1;for (int k = 0; k < nitems; k++) {const Item *it = &items[order[k]];+ if (strip >= 0 && (strip < (it->strips & 15) || strip > it->strips >> 4)) continue;int kind = it->kind & 0x7f;if (kind == K_PLAYER) { draw_player(); continue; }if (kind == K_PROG) { draw_prog(it); continue; }