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
Hill Climb Racing: Lily's smaller pictures and fonts, at full speed
Mason3639c82e9f0fe8e
diff --git a/games/hillclimb/src/data.bin b/games/hillclimb/src/data.binindex 7ad6d10d..78f640ad 100644Binary files a/games/hillclimb/src/data.bin and b/games/hillclimb/src/data.bin differdiff --git a/games/hillclimb/src/gfx.c b/games/hillclimb/src/gfx.cindex b5953424..f5d68716 100644--- a/games/hillclimb/src/gfx.c+++ b/games/hillclimb/src/gfx.c@@ -316,52 +316,69 @@ __attribute__((noinline)) static void paint_image(const Cmd *k, int y0, int y1)}}-/* HC_LOSSLESS_CROP_V1: separate pixel path for cropped images. */+/* four texels (premultiplied) mixed for the fractions of (u, v) and faded by ga, over the pixel (as paint_image) */+static inline void mix4(uint16_t *p, uint32_t c00, uint32_t c01, uint32_t c10, uint32_t c11, int32_t u, int32_t v,+ uint32_t ga) {+ if (!((c00 | c01 | c10 | c11) >> 24)) return; /* (all four see-through) */+ uint32_t fu = (u >> 12) & 15, fv = (v >> 12) & 15;+ uint32_t w11 = fu * fv, w01 = fu * (16 - fv), w10 = (16 - fu) * fv, w00 = 256 - w11 - w01 - w10;+ uint32_t rb = ((c00 & 0xFF00FF) * w00 + (c01 & 0xFF00FF) * w01 + (c10 & 0xFF00FF) * w10 + (c11 & 0xFF00FF) * w11) >> 8;+ uint32_t ag = (((c00 >> 8) & 0xFF00FF) * w00 + ((c01 >> 8) & 0xFF00FF) * w01 + ((c10 >> 8) & 0xFF00FF) * w10 ++ ((c11 >> 8) & 0xFF00FF) * w11) >> 8;+ if (ga < 255) rb = (rb & 0xFF00FF) * ga >> 8, ag = (ag & 0xFF00FF) * ga >> 8;+ uint32_t a = (ag >> 16) & 255;+ if (a >= 3) over(p, rb & 255, ag & 255, (rb >> 16) & 255, a);+}++/* a cropped picture's texel (u, v) from its crop's top-left: see-through outside it */+static inline uint32_t crop_texel(const uint32_t *pal, const uint8_t *px, int cw, int ch, int u, int v, uint32_t clear) {+ return (unsigned)u < (unsigned)cw && (unsigned)v < (unsigned)ch ? pal[px[v * cw + u]] : clear;+}++/* paint_image for a cropped picture: the texels around its crop are see-through, so only the pixels whose filter+ * reaches the crop are painted (with a texel to spare), and inside it the texels are read straight */__attribute__((noinline)) static void paint_image_cropped(const Cmd *k, int y0, int y1) {const Img *im = k->image.im;const uint32_t *pal = img_pal(im);- const uint8_t *px = img_px(im);- int w = im->w, h = im->h;+ const uint8_t *px = img_px(im), *crop = img_crop(im);+ int w = im->w, h = im->h, cx = crop[0], cy = crop[1], cw = crop[2], ch = crop[3];int32_t umax = (w - 1) << 16, vmax = (h - 1) << 16;- uint32_t ga = k->alpha;+ int32_t ulo = cx > 2 ? (cx - 2) << 16 : 0, uhi = cx + cw + 1 < w - 1 ? (cx + cw + 1) << 16 : umax;+ int32_t vlo = cy > 2 ? (cy - 2) << 16 : 0, vhi = cy + ch + 1 < h - 1 ? (cy + ch + 1) << 16 : vmax;+ uint32_t clear = pal[im->clear], ga = k->alpha;for (int y = y0; y < y1; y++) {int32_t ur = k->image.u0 + k->image.ub * y, vr = k->image.v0 + k->image.vb * y;int x0 = k->x0, x1 = k->x1;- span(ur, k->image.ua, 0, umax, &x0, &x1);- span(vr, k->image.va, 0, vmax, &x0, &x1);+ span(ur, k->image.ua, ulo, uhi, &x0, &x1);+ span(vr, k->image.va, vlo, vhi, &x0, &x1);if (x0 >= x1) continue;int32_t u = ur + k->image.ua * x0, v = vr + k->image.va * x0;uint16_t *p = band + (y - by0) * SCREEN_W + x0;for (int x = x0; x < x1; x++, p++, u += k->image.ua, v += k->image.va) {int iu = u >> 16, iv = v >> 16;if ((unsigned)iu > (unsigned)(w - 1) || (unsigned)iv > (unsigned)(h - 1)) continue; /* (rounding at the ends) */- int iu1 = iu < w - 1 ? iu + 1 : iu, iv1 = iv < h - 1 ? w : 0;- uint32_t c00 = pal[img_crop_index(im, px, iu, iv)];- uint32_t c01 = pal[img_crop_index(im, px, iu1, iv)];- uint32_t c10 = pal[img_crop_index(im, px, iu, iv + (iv1 ? 1 : 0))];- uint32_t c11 = pal[img_crop_index(im, px, iu1, iv + (iv1 ? 1 : 0))];- if (!((c00 | c01 | c10 | c11) >> 24)) continue; /* (all four see-through) */- uint32_t fu = (u >> 12) & 15, fv = (v >> 12) & 15;- uint32_t w11 = fu * fv, w01 = fu * (16 - fv), w10 = (16 - fu) * fv, w00 = 256 - w11 - w01 - w10;- uint32_t rb = ((c00 & 0xFF00FF) * w00 + (c01 & 0xFF00FF) * w01 + (c10 & 0xFF00FF) * w10 + (c11 & 0xFF00FF) * w11) >> 8;- uint32_t ag = (((c00 >> 8) & 0xFF00FF) * w00 + ((c01 >> 8) & 0xFF00FF) * w01 + ((c10 >> 8) & 0xFF00FF) * w10 +- ((c11 >> 8) & 0xFF00FF) * w11) >> 8;- if (ga < 255) rb = (rb & 0xFF00FF) * ga >> 8, ag = (ag & 0xFF00FF) * ga >> 8;- uint32_t a = (ag >> 16) & 255;- if (a < 3) continue;- over(p, rb & 255, ag & 255, (rb >> 16) & 255, a);+ int cu = iu - cx, cv = iv - cy;+ uint32_t c00, c01, c10, c11;+ if ((unsigned)cu < (unsigned)(cw - 1) && (unsigned)cv < (unsigned)(ch - 1)) {+ const uint8_t *r0 = px + cv * cw + cu;+ c00 = pal[r0[0]], c01 = pal[r0[1]], c10 = pal[r0[cw]], c11 = pal[r0[cw + 1]];+ } else { /* on the crop's edge */+ int cu1 = iu < w - 1 ? cu + 1 : cu, cv1 = iv < h - 1 ? cv + 1 : cv;+ c00 = crop_texel(pal, px, cw, ch, cu, cv, clear), c01 = crop_texel(pal, px, cw, ch, cu1, cv, clear);+ c10 = crop_texel(pal, px, cw, ch, cu, cv1, clear), c11 = crop_texel(pal, px, cw, ch, cu1, cv1, clear);+ }+ mix4(p, c00, c01, c10, c11, u, v, ga);}}}+/* paint_blit for a cropped picture: only its crop (around it, it is see-through) */__attribute__((noinline)) static void paint_blit_cropped(const Cmd *k, int y0, int y1) {const Img *im = k->blit.im;const uint32_t *pal = img_pal(im);- const uint8_t *px = img_px(im);+ const uint8_t *px = img_px(im), *crop = img_crop(im);uint32_t ga = k->alpha;int x0 = k->x0, x1 = k->x1;- /* HC_LOSSLESS_CROP_V1: transparent margins need no blit work. */- const uint8_t *crop = img_crop(im);if (x0 < k->blit.x + crop[0]) x0 = k->blit.x + crop[0];if (x1 > k->blit.x + crop[0] + crop[2]) x1 = k->blit.x + crop[0] + crop[2];if (y0 < k->blit.y + crop[1]) y0 = k->blit.y + crop[1];@@ -415,48 +432,75 @@ __attribute__((noinline)) static void paint_blit(const Cmd *k, int y0, int y1) {}}-/* HC_FONT_ROW_RLE_V1: decode only a requested glyph row; no frame cache or heap. */-__attribute__((noinline)) static void paint_font_rle_row(const Glyph *g, const uint8_t *bitmap,- unsigned ry, int gx, uint16_t *p,- uint16_t oc, uint16_t color) {- const uint8_t *table = bitmap + g->off;- unsigned start = (unsigned)table[2 * ry] | (unsigned)table[2 * ry + 1] << 8;- unsigned stop = (unsigned)table[2 * ry + 2] | (unsigned)table[2 * ry + 3] << 8;- const uint8_t *q = table + 2 * ((unsigned)g->h + 1) + start;- const uint8_t *end = table + 2 * ((unsigned)g->h + 1) + stop;- unsigned x = 0;- while (q < end && x < g->w) {- unsigned cmd = *q++;- unsigned n = (cmd & 127u) + 1u;- if (x + n > g->w) return; /* unexpected/corrupt glyph */- if (cmd & 128u) {- if (q >= end) return;+/* row ry of a packed glyph (Glyph.packed), its left at gx, onto the band's row p: the row's ops, n + 1 bytes as they+ * are (an op n under 128) or the next byte n - 127 times (from 128 up); where the letter covers a pixel fully, the+ * pixel is its colour whatever the outline */+static inline void paint_glyph_row(const Glyph *g, const uint8_t *bits, int ry, int gx, uint16_t *p, uint16_t c,+ uint16_t oc) {+ const uint8_t *rows = bits + g->off, *ops = rows + 2 * (g->h + 1);+ const uint8_t *q = ops + (rows[2 * ry] | rows[2 * ry + 1] << 8), *end = ops + (rows[2 * ry + 2] | rows[2 * ry + 3] << 8);+ for (int x = gx; q < end;) {+ int op = *q++, n = (op & 127) + 1;+ if (op & 128) {uint8_t b = *q++;- if (b) {- for (unsigned j = 0; j < n; j++) {- int sx = gx + (int)x + (int)j;- if ((unsigned)sx >= SCREEN_W) continue;- if (b & 15) blend(p + sx, oc, (b & 15) * 17u);- if (b >> 4) blend(p + sx, color, (b >> 4) * 17u);+ int a = x > 0 ? x : 0, z = x + n < SCREEN_W ? x + n : SCREEN_W;+ x += n;+ if (b >> 4 == 15)+ for (; a < z; a++) p[a] = c;+ else if (b)+ for (; a < z; a++) {+ if (b & 15) blend(p + a, oc, (b & 15) * 17u);+ if (b >> 4) blend(p + a, c, (b >> 4) * 17u);}+ continue;+ }+ for (; n; n--, x++) {+ uint8_t b = *q++;+ if (!b || (unsigned)x >= SCREEN_W) continue;+ if (b >> 4 == 15) p[x] = c;+ else {+ if (b & 15) blend(p + x, oc, (b & 15) * 17u);+ if (b >> 4) blend(p + x, c, (b >> 4) * 17u);}- } else {- if ((unsigned)(end - q) < n) return;- for (unsigned j = 0; j < n; j++) {- uint8_t b = q[j];- if (!b) continue;- int sx = gx + (int)x + (int)j;- if ((unsigned)sx >= SCREEN_W) continue;- if (b & 15) blend(p + sx, oc, (b & 15) * 17u);- if (b >> 4) blend(p + sx, color, (b >> 4) * 17u);- }- q += n;}- x += n;}}-/* HC_MENU_TILES_EXPERIMENT_V1: bilinear sampling uses the same math and palettes. */+/* row v of a tiled picture (the menus'), the whole of it (see-through around a crop), as colour indices */+static void tiled_row(const Img *im, int v, uint8_t *out) {+ const uint8_t *px = img_px(im);+ int w = im->w, h = im->h;+ if (im->flags & IMG_CROPPED) {+ const uint8_t *c = img_crop(im);+ memset(out, im->clear, w);+ if (v < c[1] || v >= c[1] + c[3]) return;+ out += c[0], v -= c[1], w = c[2], h = c[3];+ }+ int cols = (w + 7) >> 3, ry = v & 7;+ const uint8_t *at = px + 2 * (v >> 3) * cols, *tiles = px + 2 * cols * ((h + 7) >> 3);+ for (int tx = 0; tx < cols; tx++, out += 8) {+ const uint8_t *t = tiles + (at[2 * tx] | at[2 * tx + 1] << 8);+ int n = *t++, tw = w - 8 * tx < 8 ? w - 8 * tx : 8;+ int bits = n <= 1 ? 0 : n == 2 ? 1 : n <= 4 ? 2 : 4;+ const uint8_t *in = t + n + ry * bits; /* (a whole tile's row: from a whole byte) */+ if (!n) memcpy(out, t + ry * tw, tw);+ else if (n == 1) memset(out, t[0], tw);+ else if (tw < 8)+ for (int i = 0, bit = ry * tw * bits; i < tw; i++, bit += bits)+ out[i] = t[(t[n + (bit >> 3)] >> (8 - bits - (bit & 7))) & ((1 << bits) - 1)];+ else if (bits == 4)+ for (int i = 0; i < 8; i += 2) out[i] = t[in[i >> 1] >> 4], out[i + 1] = t[in[i >> 1] & 15];+ else if (bits == 2)+ for (int i = 0; i < 8; i += 4) {+ uint8_t b = in[i >> 2];+ out[i] = t[b >> 6], out[i + 1] = t[b >> 4 & 3], out[i + 2] = t[b >> 2 & 3], out[i + 3] = t[b & 3];+ }+ else+ for (int i = 0; i < 8; i++) out[i] = t[in[0] >> (7 - i) & 1];+ }+}++/* paint_image for a tiled picture: level, its rows unpacked (the last two kept); turned, each texel looked up */__attribute__((noinline)) static void paint_image_tiled(const Cmd *k, int y0, int y1) {const Img *im = k->image.im;const uint32_t *pal = img_pal(im);@@ -464,6 +508,9 @@ __attribute__((noinline)) static void paint_image_tiled(const Cmd *k, int y0, inint w = im->w, h = im->h;int32_t umax = (w - 1) << 16, vmax = (h - 1) << 16;uint32_t ga = k->alpha;+ bool level = !k->image.ub && !k->image.va;+ uint8_t rows[2][SCREEN_W];+ int have[2] = {-1, -1};for (int y = y0; y < y1; y++) {int32_t ur = k->image.u0 + k->image.ub * y, vr = k->image.v0 + k->image.vb * y;int x0 = k->x0, x1 = k->x1;@@ -472,51 +519,68 @@ __attribute__((noinline)) static void paint_image_tiled(const Cmd *k, int y0, inif (x0 >= x1) continue;int32_t u = ur + k->image.ua * x0, v = vr + k->image.va * x0;uint16_t *p = band + (y - by0) * SCREEN_W + x0;- for (int x = x0; x < x1; x++, p++, u += k->image.ua, v += k->image.va) {- int iu = u >> 16, iv = v >> 16;- if ((unsigned)iu > (unsigned)(w - 1) || (unsigned)iv > (unsigned)(h - 1)) continue; /* (rounding at the ends) */- int iu1 = iu < w - 1 ? iu + 1 : iu, iv1 = iv < h - 1 ? w : 0;- uint32_t c00 = pal[img_tile_index(im, px, iu, iv)];- uint32_t c01 = pal[img_tile_index(im, px, iu1, iv)];- uint32_t c10 = pal[img_tile_index(im, px, iu, iv + (iv1 ? 1 : 0))];- uint32_t c11 = pal[img_tile_index(im, px, iu1, iv + (iv1 ? 1 : 0))];- if (!((c00 | c01 | c10 | c11) >> 24)) continue; /* (all four see-through) */- uint32_t fu = (u >> 12) & 15, fv = (v >> 12) & 15;- uint32_t w11 = fu * fv, w01 = fu * (16 - fv), w10 = (16 - fu) * fv, w00 = 256 - w11 - w01 - w10;- uint32_t rb = ((c00 & 0xFF00FF) * w00 + (c01 & 0xFF00FF) * w01 + (c10 & 0xFF00FF) * w10 + (c11 & 0xFF00FF) * w11) >> 8;- uint32_t ag = (((c00 >> 8) & 0xFF00FF) * w00 + ((c01 >> 8) & 0xFF00FF) * w01 + ((c10 >> 8) & 0xFF00FF) * w10 +- ((c11 >> 8) & 0xFF00FF) * w11) >> 8;- if (ga < 255) rb = (rb & 0xFF00FF) * ga >> 8, ag = (ag & 0xFF00FF) * ga >> 8;- uint32_t a = (ag >> 16) & 255;- if (a < 3) continue;- over(p, rb & 255, ag & 255, (rb >> 16) & 255, a);+ if (!level) {+ for (int x = x0; x < x1; x++, p++, u += k->image.ua, v += k->image.va) {+ int iu = u >> 16, iv = v >> 16;+ if ((unsigned)iu > (unsigned)(w - 1) || (unsigned)iv > (unsigned)(h - 1)) continue; /* (rounding at the ends) */+ int iu1 = iu < w - 1 ? iu + 1 : iu, iv1 = iv < h - 1 ? iv + 1 : iv;+ mix4(p, pal[img_tile_index(im, px, iu, iv)], pal[img_tile_index(im, px, iu1, iv)],+ pal[img_tile_index(im, px, iu, iv1)], pal[img_tile_index(im, px, iu1, iv1)], u, v, ga);+ }+ continue;+ }+ int iv = v >> 16, iv1 = iv < h - 1 ? iv + 1 : iv; /* (v the same along the row) */+ if ((unsigned)iv > (unsigned)(h - 1)) continue;+ const uint8_t *r0 = NULL, *r1 = NULL;+ for (int s = 0; s < 2; s++) {+ if (have[s] == iv) r0 = rows[s];+ if (have[s] == iv1) r1 = rows[s];+ }+ if (!r0) {+ int s = r1 == rows[0];+ tiled_row(im, have[s] = iv, rows[s]);+ r0 = rows[s];+ }+ if (!r1) {+ int s = r0 == rows[0];+ tiled_row(im, have[s] = iv1, rows[s]);+ r1 = rows[s];+ }+ for (int x = x0; x < x1; x++, p++, u += k->image.ua) {+ int iu = u >> 16;+ if ((unsigned)iu > (unsigned)(w - 1)) continue; /* (rounding at the ends) */+ int iu1 = iu < w - 1 ? iu + 1 : iu;+ mix4(p, pal[r0[iu]], pal[r0[iu1]], pal[r1[iu]], pal[r1[iu1]], u, v, ga);}}}-/* HC_MENU_TILES_EXPERIMENT_V1: menu-only tile blitting; original hot blit unchanged. */+/* paint_blit for a tiled picture: each row unpacked, then as paint_blit */__attribute__((noinline)) static void paint_blit_tiled(const Cmd *k, int y0, int y1) {const Img *im = k->blit.im;const uint32_t *pal = img_pal(im);- const uint8_t *px = img_px(im);- int x0 = k->x0, x1 = k->x1;uint32_t ga = k->alpha;- if (im->flags & IMG_CROPPED) {+ int x0 = k->x0, x1 = k->x1;+ if (im->flags & IMG_CROPPED) { /* (only its crop) */const uint8_t *c = img_crop(im);if (x0 < k->blit.x + c[0]) x0 = k->blit.x + c[0];if (x1 > k->blit.x + c[0] + c[2]) x1 = k->blit.x + c[0] + c[2];if (y0 < k->blit.y + c[1]) y0 = k->blit.y + c[1];if (y1 > k->blit.y + c[1] + c[3]) y1 = k->blit.y + c[1] + c[3];}- if (x0 >= x1 || y0 >= y1) return;- bool solid = (im->flags & IMG_OPAQUE) && ga == 255;+ bool solid = im->flags & IMG_OPAQUE && ga == 255;const uint16_t *pal565 = solid ? img_pal565(im) : NULL;- for (int y = y0; y < y1; y++) {+ uint8_t row[SCREEN_W];+ for (int y = y0; y < y1 && x0 < x1; y++) {+ tiled_row(im, y - k->blit.y, row);+ const uint8_t *src = row + x0 - k->blit.x;uint16_t *p = band + (y - by0) * SCREEN_W;+ if (solid) {+ for (int x = x0; x < x1; x++) p[x] = pal565[*src++];+ continue;+ }for (int x = x0; x < x1; x++) {- uint8_t index = img_tile_index(im, px, x - k->blit.x, y - k->blit.y);- if (solid) { p[x] = pal565[index]; continue; }- uint32_t c = pal[index], a = c >> 24;+ uint32_t c = pal[*src++], a = c >> 24;if (!a) continue;if (ga < 255) {uint32_t rb = (c & 0xFF00FF) * ga >> 8, g = ((c >> 8) & 0xFF00FF) * ga >> 8;@@ -540,10 +604,8 @@ __attribute__((noinline)) static void paint_text(const Cmd *k, int y0, int y1) {pen += g->adv;int ya = gy > y0 ? gy : y0, yb = gy + g->h < y1 ? gy + g->h : y1;for (int y = ya; y < yb; y++) {- /* HC_FONT_ROW_RLE_V1: preserve the original hot path for raw glyphs. */- if (g->pad[0] == 1) {- paint_font_rle_row(g, bits, (unsigned)(y - gy), gx,- band + (y - by0) * SCREEN_W, k->text.oc, k->text.c);+ if (g->packed) {+ paint_glyph_row(g, bits, y - gy, gx, band + (y - by0) * SCREEN_W, k->text.c, k->text.oc);continue;}const uint8_t *row = bits + g->off + (y - gy) * g->w;@@ -683,12 +745,16 @@ void gfx_end(void) {case C_RRECT: paint_rrect(k, y0, y1); break;case C_VGRAD: paint_vgrad(k, y0, y1); break;case C_ROWS: paint_rows(k, y0, y1); break;- case C_IMAGE: if (k->image.im->flags & IMG_TILED) paint_image_tiled(k, y0, y1);- else if (k->image.im->flags & IMG_CROPPED) paint_image_cropped(k, y0, y1);- else paint_image(k, y0, y1); break;- case C_BLIT: if (k->blit.im->flags & IMG_TILED) paint_blit_tiled(k, y0, y1);- else if (k->blit.im->flags & IMG_CROPPED) paint_blit_cropped(k, y0, y1);- else paint_blit(k, y0, y1); break;+ case C_IMAGE:+ if (k->image.im->flags & IMG_TILED) paint_image_tiled(k, y0, y1);+ else if (k->image.im->flags & IMG_CROPPED) paint_image_cropped(k, y0, y1);+ else paint_image(k, y0, y1);+ break;+ case C_BLIT:+ if (k->blit.im->flags & IMG_TILED) paint_blit_tiled(k, y0, y1);+ else if (k->blit.im->flags & IMG_CROPPED) paint_blit_cropped(k, y0, y1);+ else paint_blit(k, y0, y1);+ break;case C_TEXT: paint_text(k, y0, y1); break;case C_CIRCLE: paint_circle(k, y0, y1, false); break;case C_RING: paint_circle(k, y0, y1, true); break;diff --git a/games/hillclimb/src/res.h b/games/hillclimb/src/res.hindex f86b8358..ecfc20c6 100644--- a/games/hillclimb/src/res.h+++ b/games/hillclimb/src/res.h@@ -5,17 +5,19 @@#include "data.h"typedef struct {- uint16_t w, h, ncolors, flags; /* flags 1: opaque */+ uint16_t w, h, ncolors, flags; /* flags IMG_* */uint16_t ppm4; /* its pixels a meter x 4 (0: a picture of the menus) */int16_t px4, py4; /* its pivot x 4 */- uint16_t pad; /* if cropped, original clear palette index */+ uint16_t clear; /* a cropped one's see-through colour, all around its crop */} Img;+/* opaque: its colours also as RGB565; cropped: only the box around its visible pixels kept (its x, y, w, h, a byte+ * each, before the pixels); tiled (pictures of the menus): its pixels in 8 x 8 tiles, each with its own few colours */enum { IMG_OPAQUE = 1, IMG_CROPPED = 2, IMG_TILED = 4 };typedef struct {uint8_t w, h;int8_t x, y; /* the bitmap's top-left from the pen, y from the baseline */- uint8_t adv, pad[3];+ uint8_t adv, packed, pad[2]; /* packed: its rows in runs (tools/pack.py pack_glyphs) */uint32_t off;} Glyph;typedef struct {@@ -26,47 +28,29 @@ extern const uint8_t hc_data[];const Img *img(int id);static inline const uint32_t *img_pal(const Img *i) { return (const uint32_t *)(const void *)(i + 1); }static inline const uint16_t *img_pal565(const Img *i) { return (const uint16_t *)(const void *)(img_pal(i) + i->ncolors); }-/* HC_LOSSLESS_CROP_V1: flags 2 marks an optional 4-byte x/y/w/h crop rectangle.- * The original dimensions and pivot stay unchanged; pad stores a clear palette index. */+/* a cropped picture's box (x, y, w, h) */static inline const uint8_t *img_crop(const Img *i) {return (const uint8_t *)(img_pal(i) + i->ncolors) + (i->flags & IMG_OPAQUE ? 2 * i->ncolors : 0);}-static inline const uint8_t *img_px(const Img *i) {- return img_crop(i) + ((i->flags & IMG_CROPPED) ? 4 : 0);-}-/* Called only for cropped images. Uncropped sprites/terrain keep direct byte reads. */-static inline uint8_t img_crop_index(const Img *i, const uint8_t *px, int x, int y) {- const uint8_t *c = img_crop(i);- unsigned xx = (unsigned)(x - c[0]), yy = (unsigned)(y - c[1]);- if (xx >= c[2] || yy >= c[3]) return (uint8_t)i->pad;- return px[yy * c[2] + xx];-}-/* HC_MENU_TILES_EXPERIMENT_V1: direct-access 8x8 local palette image indices.- * No allocation. Only used for menu-only IMG_TILED records. */+static inline const uint8_t *img_px(const Img *i) { return img_crop(i) + (i->flags & IMG_CROPPED ? 4 : 0); }+/* a tiled picture's pixel (x, y): its tiles' offsets (u16, from after them), then each tile: its colours' count n and+ * the colours, then its pixels as indices of them, 1, 2 or 4 bits each (high bits first); n 0: its pixels as they are */static inline uint8_t img_tile_index(const Img *i, const uint8_t *px, int x, int y) {unsigned w = i->w, h = i->h;if (i->flags & IMG_CROPPED) {const uint8_t *c = img_crop(i);- unsigned xx = (unsigned)(x - c[0]), yy = (unsigned)(y - c[1]);- if (xx >= c[2] || yy >= c[3]) return (uint8_t)i->pad;- x = (int)xx; y = (int)yy; w = c[2]; h = c[3];+ unsigned cx = (unsigned)(x - c[0]), cy = (unsigned)(y - c[1]);+ if (cx >= c[2] || cy >= c[3]) return (uint8_t)i->clear;+ x = (int)cx, y = (int)cy, w = c[2], h = c[3];}- unsigned cols = (w + 7) >> 3, rows = (h + 7) >> 3;- unsigned ti = ((unsigned)y >> 3) * cols + ((unsigned)x >> 3);- const uint8_t *payload = px + 2 * cols * rows;- unsigned off = px[2 * ti] | ((unsigned)px[2 * ti + 1] << 8);- const uint8_t *t = payload + off;- unsigned n = *t++;- unsigned tw = w - ((unsigned)x & ~7u);- if (tw > 8) tw = 8;- unsigned pos = ((unsigned)y & 7u) * tw + ((unsigned)x & 7u);+ unsigned cols = (w + 7) >> 3, rows = (h + 7) >> 3, at = ((unsigned)y >> 3) * cols + ((unsigned)x >> 3);+ const uint8_t *t = px + 2 * cols * rows + (px[2 * at] | px[2 * at + 1] << 8);+ unsigned n = *t++, tw = w - ((unsigned)x & ~7u) < 8 ? w - ((unsigned)x & ~7u) : 8;+ unsigned pos = ((unsigned)y & 7) * tw + ((unsigned)x & 7);if (!n) return t[pos];if (n == 1) return t[0];- unsigned bits = n == 2 ? 1 : n <= 4 ? 2 : 4;- const uint8_t *indices = t + n;- unsigned bit = pos * bits;- unsigned v = (indices[bit >> 3] >> (8 - bits - (bit & 7u))) & ((1u << bits) - 1u);- return t[v];+ unsigned bits = n == 2 ? 1 : n <= 4 ? 2 : 4, bit = pos * bits;+ return t[(t[n + (bit >> 3)] >> (8 - bits - (bit & 7))) & ((1u << bits) - 1)];}const Font *font(int id);diff --git a/games/hillclimb/tools/pack.py b/games/hillclimb/tools/pack.pyindex 2ea460bd..9036bfff 100644--- a/games/hillclimb/tools/pack.py+++ b/games/hillclimb/tools/pack.py@@ -4,12 +4,14 @@python3 -I tools/pack.py REF (REF: the folder tools/fetch.py filled)data.bin: "HCRD", the number of pictures and fonts, then a table of their offsets (u32), then each one:-- a picture: w, h, colours, flags (u16 each; flags 1: opaque), its pixels a meter x 4 (0: a picture of the menus),- its pivot x 4 (s16, s16), 0; its colours as premultiplied R, G, B, A bytes; for an opaque one also as RGB565;- then w x h colour indices, row by row+- a picture: w, h, colours, flags (u16 each; flags 1: opaque, 2: cropped, 4: tiled), its pixels a meter x 4 (0: a+ picture of the menus), its pivot x 4 (s16, s16), its clear colour (u16: a cropped one's, else 0); its colours as+ premultiplied R, G, B, A bytes; for an opaque one also as RGB565; for a cropped one the box around its visible+ pixels (x, y, w, h: u8 each), the rest of it being the clear colour (cropped); then the colour indices of its pixels+ (of the box's), row by row, or for a tiled one (tiled) in 8 x 8 tiles- a font: first character, count, line height, ascent (u8 each); then a glyph each (w, h, x and y of its top-left- from the pen, advance, 3 bytes of 0, offset of its bitmap: u8 u8 s8 s8 u8, u32) and the bitmaps, a byte a pixel: the- letter's coverage in the high 4 bits, its dark outline's in the low 4+ from the pen, advance, packed, 2 bytes of 0, offset of its bitmap: u8 u8 s8 s8 u8 u8, u32) and the bitmaps, a byte+ a pixel: the letter's coverage in the high 4 bits, its dark outline's in the low 4; packed ones in runs (pack_glyphs)data.h names them (IMG_*, FONT_*); art.h is tools/art.py's vehicles in meters, for src/run.c.icon(REF) drew src/icon.png once (it is kept as it is now, with NumPlay's corners: tools/round_icons.py)."""@@ -245,135 +247,98 @@ def picture(im, ppm, pivot, opaque=False, colors=256):return out-# HC_LOSSLESS_CROP_V1: compact only transparent margins; terrain stays direct.-def _hc_crop_picture_blob(blob, name):- if name.startswith(('GROUND_', 'SURF_', 'PIT_', 'SKY', 'DIRT',- 'SNOW', 'GRASS', 'SLIME', 'FIRE')):+def cropped(blob, name):+ """The picture (not the ground's, sky's or bits') kept as the box around its visible pixels when that is+ smaller, its first clear colour (the colours with alpha 0 are all 0, premultiplied) all around it."""+ if name.startswith(("GROUND_", "SURF_", "PIT_", "SKY", "DIRT", "SNOW", "GRASS", "SLIME", "FIRE")):return blob- w, h, n, flags, ppm4, px4, py4, pad = struct.unpack_from('<HHHHHhhH', blob)- if flags or pad or not w or not h or n > 256:+ w, h, n, flags = struct.unpack_from("<HHHH", blob)+ if flags:return blob- pal = blob[16:16 + 4*n]- clear = [i for i in range(n) if pal[i*4 + 3] == 0]- if not clear or any(pal[i*4:i*4+3] != b'\0\0\0' for i in clear):+ alpha = np.frombuffer(blob, np.uint8, 4 * n, 16)[3::4]+ clear = np.nonzero(alpha == 0)[0]+ px = np.frombuffer(blob, np.uint8, w * h, 16 + 4 * n).reshape(h, w)+ ys, xs = np.nonzero(alpha[px])+ if not len(clear) or not len(ys):return blob- visible = {i for i in range(n) if pal[i*4+3] != 0}- px = blob[16+4*n:16+4*n+w*h]- if len(px) != w*h:- raise ValueError('Hill Climb image pixel data truncated')- x0, y0, x1, y1 = w, h, -1, -1- for y in range(h):- row = px[y*w:(y+1)*w]- for x, idx in enumerate(row):- if idx in visible:- x0 = min(x0, x); y0 = min(y0, y)- x1 = max(x1, x); y1 = max(y1, y)- if x1 < x0:- return blob- cw, ch = x1-x0+1, y1-y0+1+ x0, y0, cw, ch = xs.min(), ys.min(), xs.max() + 1 - xs.min(), ys.max() + 1 - ys.min()if max(x0, y0, cw, ch) > 255:return blob- hdr = bytearray(blob[:16])- struct.pack_into('<H', hdr, 6, flags | 2)- struct.pack_into('<H', hdr, 14, clear[0])- out = hdr + pal + bytes((x0, y0, cw, ch))- for y in range(y0, y0+ch):- out += px[y*w+x0:y*w+x0+cw]- while len(out) % 4:- out.append(0)+ out = bytearray(blob[:16 + 4 * n])+ struct.pack_into("<H", out, 6, flags | 2)+ struct.pack_into("<H", out, 14, clear[0])+ out += bytes((x0, y0, cw, ch)) + px[y0:y0 + ch, x0:x0 + cw].tobytes()+ out += bytes(-len(out) % 4)return out if len(out) < len(blob) else blob-# HC_PALETTE_COMPACT_V1: preserve exact premultiplied RGBA/RGB565.-def _hc_compact_palette_blob(blob):- """Drop unused/duplicate colour entries in one already-cropped HCRD record.-- Works on both ordinary and HC_LOSSLESS_CROP_V1 records; the latter's- outside-transparent index is remapped too. Never alters logical pixels.- """- w, h, n, flags, ppm4, px4, py4, pad = struct.unpack_from('<HHHHHhhH', blob)- assert flags & ~3 == 0 and 1 <= n <= 256- rgba = [blob[16 + 4*i:20 + 4*i] for i in range(n)]- pal_end = 16 + 4*n- rgb565 = [blob[pal_end + 2*i:pal_end + 2*i + 2] for i in range(n)] if flags & 1 else None- at = pal_end + (2*n if flags & 1 else 0)- if flags & 2:- x0, y0, cw, ch = blob[at:at + 4]+def compacted(blob):+ """The picture without the colours its pixels don't use, and each colour once."""+ blob = bytes(blob)+ w, h, n, flags = struct.unpack_from("<HHHH", blob)+ clear = struct.unpack_from("<H", blob, 14)[0]+ at = 16 + 4 * n + (2 * n if flags & 1 else 0)+ rgba = [blob[16 + 4 * i:20 + 4 * i] for i in range(n)]+ c565 = [blob[16 + 4 * n + 2 * i:18 + 4 * n + 2 * i] if flags & 1 else b"" for i in range(n)]+ box = blob[at:at + 4] if flags & 2 else b""+ if box:at += 4- else:- cw, ch = w, h- px = blob[at:at + cw*ch]- keep = set(px)- if flags & 2:- keep.add(pad) # original transparent colour outside crop bounds- unique, remap, colors = {}, {}, []- for old in sorted(keep):- key = rgba[old] + (rgb565[old] if rgb565 is not None else b'')- if key not in unique:- unique[key] = len(colors)- colors.append(old)- remap[old] = unique[key]- hdr = bytearray(blob[:16])- struct.pack_into('<H', hdr, 4, len(colors))- if flags & 2:- struct.pack_into('<H', hdr, 14, remap[pad])- out = hdr + b''.join(rgba[i] for i in colors)- if rgb565 is not None:- out += b''.join(rgb565[i] for i in colors)+ w, h = box[2], box[3]+ px = blob[at:at + w * h]+ first, remap, keep = {}, {}, []+ for i in sorted(set(px) | ({clear} if flags & 2 else set())):+ key = rgba[i] + c565[i]+ if key not in first:+ first[key] = len(keep)+ keep.append(i)+ remap[i] = first[key]+ out = bytearray(blob[:16])+ struct.pack_into("<H", out, 4, len(keep))if flags & 2:- out += bytes((x0, y0, cw, ch))- out += bytes(remap[i] for i in px)- out += bytes((-len(out)) % 4)- return bytes(out) if len(out) < len(blob) else bytes(blob)--# HC_MENU_TILES_EXPERIMENT_V1: lossless local-palette 8x8 menu images (16-bit tile offsets).-def _hc_menu_tile_blob(blob, name):- if not (name in {'MENU_BG', 'LOGO', 'LOCK', 'START'} or- name.startswith(('STAGE_', 'ICON_', 'UP_', 'THUMB_V', 'THUMB_S'))):+ struct.pack_into("<H", out, 14, remap[clear])+ out += b"".join(rgba[i] for i in keep) + b"".join(c565[i] for i in keep) + box + bytes(remap[i] for i in px)+ out += bytes(-len(out) % 4)+ return bytes(out) if len(out) < len(blob) else blob+++def tiled(blob, name):+ """A picture of the menus (not drawn in a run; not the menus' backdrop either, the whole screen every frame) in+ 8 x 8 tiles, when that saves enough: their offsets (u16, from after them), then each tile: its number of colours n+ and the colours (indices of the picture's), then its pixels as indices of those, 1, 2 or 4 bits each, high bits+ first (n 1: none; n 0: the picture's indices, as they are, when more than 16 or no smaller)."""+ if name not in ("LOGO", "LOCK", "START") and not name.startswith(("STAGE_", "ICON_", "UP_", "THUMB_")):return blob- w, h, n, flags, _, _, _, _ = struct.unpack_from('<HHHHHhhH', blob)- if flags & 4:- raise ValueError('tile encoding applied twice')- p = 16 + n*4 + (2*n if flags&1 else 0)- if flags&2:- cx, cy, cw, ch=blob[p:p+4]- p+=4- else:- cw, ch=w,h- pixels=blob[p:p+cw*ch]- if len(pixels)!=cw*ch:- raise ValueError('truncated input for menu tiling')- cols=(cw+7)//8;rows=(ch+7)//8- offsets=[];tiles=[];length=0- for ty in range(rows):- for tx in range(cols):- tw=min(8,cw-8*tx);th=min(8,ch-8*ty)- raw=b''.join(pixels[(8*ty+dy)*cw+8*tx:(8*ty+dy)*cw+8*tx+tw] for dy in range(th))- cmap={}; pal=[]; codes=[]- for val in raw:- if val not in cmap:- cmap[val]=len(pal);pal.append(val)- codes.append(cmap[val])- count=len(pal)- if count<=16:- bits=0 if count==1 else 1 if count==2 else 2 if count<=4 else 4- t=bytearray((count,))+bytearray(pal)+ w, h, n, flags = struct.unpack_from("<HHHH", blob)+ assert w <= 320, name # (gfx.c unpacks a row at a time, the screen's width at most)+ at = 16 + 4 * n + (2 * n if flags & 1 else 0)+ if flags & 2:+ w, h = blob[at + 2], blob[at + 3]+ at += 4+ px = np.frombuffer(blob, np.uint8, w * h, at).reshape(h, w)+ offs, tiles = [], bytearray()+ for ty in range(0, h, 8):+ for tx in range(0, w, 8):+ raw = px[ty:ty + 8, tx:tx + 8].tobytes()+ cols = list(dict.fromkeys(raw))+ t = b"\0" + raw+ if len(cols) <= 16:+ bits = 0 if len(cols) == 1 else 1 if len(cols) == 2 else 2 if len(cols) <= 4 else 4+ packed = bytearray((len(cols),)) + bytes(cols)if bits:- for k in range(0,len(codes),8//bits):- v=0- for j,c in enumerate(codes[k:k+8//bits]):- v|=c<<(8-bits*(j+1))- t.append(v)- if len(t)>=1+len(raw):t=b'\0'+raw- else:- t=b'\0'+raw- offsets.append(length);tiles.append(bytes(t));length+=len(t)- if length>=65536:return blob- data=b''.join(struct.pack('<H',o) for o in offsets)+b''.join(tiles)- out=bytearray(blob[:p])+data- struct.pack_into('<H',out,6,flags|4)- while len(out)%4:out.append(0)- return bytes(out) if len(blob)-len(out)>=96 and len(out)*100<=len(blob)*96 else blob+ k = 8 // bits+ for i in range(0, len(raw), k):+ packed.append(sum(cols.index(c) << (8 - bits * (j + 1)) for j, c in enumerate(raw[i:i + k])))+ if len(packed) < len(t):+ t = packed+ offs.append(len(tiles))+ tiles += t+ if len(tiles) >= 65536:+ return blob+ out = bytearray(blob[:at]) + b"".join(struct.pack("<H", o) for o in offs) + tiles+ struct.pack_into("<H", out, 6, flags | 4)+ out += bytes(-len(out) % 4)+ return bytes(out) if len(blob) - len(out) >= 96 and len(out) * 100 <= len(blob) * 96 else blob+def sized(im, how, crop):if crop == "trim":@@ -529,64 +494,56 @@ def font_data(ttf, cap, weight, outline):out += datawhile len(out) % 4:out.append(0)- return _hc_pack_font_blob(out)+ return pack_glyphs(out)-# HC_FONT_ROW_RLE_V1: lossless per-glyph row PackBits, direct row offsets.-def _hc_rle_font_row(raw):- out = bytearray()- i = 0- while i < len(raw):+def pack_row(row):+ """A row of a glyph in runs: a byte n under 128 then n + 1 bytes as they are, or 128 + n then a byte that comes n + 1+ times (3 times or more)."""+ def same(i): # how many times row[i] comes from i on (128 at most)j = i + 1- while j < len(raw) and raw[j] == raw[i] and j - i < 128:+ while j < len(row) and row[j] == row[i] and j - i < 128:j += 1- if j - i >= 3:- out += bytes((0x80 | (j-i-1), raw[i]))- i = j+ return j - i+ out, i = bytearray(), 0+ while i < len(row):+ n = same(i)+ if n >= 3:+ out += bytes((128 + n - 1, row[i]))+ i += ncontinuestart = i- i += j - i- while i < len(raw) and i - start < 128:- j = i + 1- while j < len(raw) and raw[j] == raw[i] and j - i < 128:- j += 1- if j - i >= 3:+ i += n+ while i < len(row) and i - start < 128:+ n = same(i)+ if n >= 3:break- i += min(j-i, 128-(i-start))- out.append(i-start-1)- out += raw[start:i]+ i += min(n, 128 - (i - start))+ out.append(i - start - 1)+ out += row[start:i]return bytes(out)-def _hc_pack_font_blob(blob):- """Existing font/glyph structs, except pad[0]=1 marks compressed glyphs.-- Each compressed glyph payload starts with h+1 little-endian uint16 row- offsets, followed by the PackBits streams; other glyphs stay direct.- """- first, count, height, ascent = struct.unpack_from('<BBBB', blob)- base = 4 + 12*count- out = bytearray(blob[:base])- for j in range(count):- p = 4 + 12*j- w, h = struct.unpack_from('<BB', blob, p)- oldoff = struct.unpack_from('<I', blob, p+8)[0]- raw = bytes(blob[base+oldoff:base+oldoff+w*h])- assert len(raw) == w*h and not any(blob[p+5:p+8])- struct.pack_into('<I', out, p+8, len(out)-base)- if w and h:- rows = [_hc_rle_font_row(raw[y*w:(y+1)*w]) for y in range(h)]- starts = [0]- for row in rows:- starts.append(starts[-1] + len(row))- if starts[-1] <= 65535:- candidate = struct.pack('<'+'H'*(h+1), *starts) + b''.join(rows)- if len(candidate) < len(raw):- out[p+5] = 1- out += candidate- continue- out += raw- out += bytes((-len(out)) % 4)+def pack_glyphs(font):+ """The font with its glyphs packed where that is smaller (Glyph.packed): where each row starts (u16, h + 1 of+ them, from after them), then the rows (pack_row)."""+ count = font[1]+ base = 4 + 12 * count+ out = bytearray(font[:base])+ for g in range(4, base, 12):+ w, h = font[g], font[g + 1]+ at = base + struct.unpack_from("<I", font, g + 8)[0]+ raw = bytes(font[at:at + w * h])+ struct.pack_into("<I", out, g + 8, len(out) - base)+ rows = [pack_row(raw[y * w:(y + 1) * w]) for y in range(h)]+ starts = [sum(len(r) for r in rows[:y]) for y in range(h + 1)]+ packed = struct.pack(f"<{h + 1}H", *starts) + b"".join(rows) if starts[-1] < 65536 else raw+ if w and h and len(packed) < len(raw):+ out[g + 5] = 1+ out += packed+ else:+ out += raw+ out += bytes(-len(out) % 4)return out@@ -630,7 +587,7 @@ def main():colors[name] = more[0]vp, art_lines = vehicle_pictures(ref)pics += vp- blobs = [_hc_menu_tile_blob(_hc_compact_palette_blob(_hc_crop_picture_blob(picture(im, ppm, pv, opaque, colors.get(name, 256)), name)), name)+ blobs = [tiled(compacted(cropped(picture(im, ppm, pv, opaque, colors.get(name, 256)), name)), name)for name, im, ppm, pv, opaque in pics]ttf = os.path.join(ref, "fonts", "Teko.ttf")fonts = [font_data(ttf, cap, wt, ol) for _, cap, wt, ol in FONTS]