Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

optimise meow

Lily Pitioteca7ade22eee
diff --git a/games/hillclimb/src/data.bin b/games/hillclimb/src/data.bin
index 264cc751..fe840198 100644
Binary files a/games/hillclimb/src/data.bin and b/games/hillclimb/src/data.bin differ
diff --git a/games/hillclimb/src/gfx.c b/games/hillclimb/src/gfx.c
index 247586dd..79cd9242 100644
--- a/games/hillclimb/src/gfx.c
+++ b/games/hillclimb/src/gfx.c
@@ -316,6 +316,78 @@ __attribute__((noinline)) static void paint_image(const Cmd *k, int y0, int y1)
}
}
+/* HC_LOSSLESS_CROP_V1: separate pixel path for cropped images. */
+__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;
+ int32_t umax = (w - 1) << 16, vmax = (h - 1) << 16;
+ uint32_t 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);
+ 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);
+ }
+ }
+}
+
+__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);
+ 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];
+ if (y1 > k->blit.y + crop[1] + crop[3]) y1 = k->blit.y + crop[1] + crop[3];
+ if (x0 >= x1 || y0 >= y1) return;
+ bool solid = im->flags & IMG_OPAQUE && ga == 255;
+ const uint16_t *pal565 = solid ? img_pal565(im) : NULL;
+ for (int y = y0; y < y1; y++) {
+ const uint8_t *src = px + (y - k->blit.y - crop[1]) * crop[2];
+ uint16_t *p = band + (y - by0) * SCREEN_W;
+ if (solid) {
+ for (int x = x0; x < x1; x++) p[x] = pal565[src[x - k->blit.x - crop[0]]];
+ continue;
+ }
+ for (int x = x0; x < x1; x++) {
+ uint32_t c = pal[src[x - k->blit.x - crop[0]]], a = c >> 24;
+ if (!a) continue;
+ if (ga < 255) {
+ uint32_t rb = (c & 0xFF00FF) * ga >> 8, g = ((c >> 8) & 0xFF00FF) * ga >> 8;
+ c = (rb & 0xFF00FF) | (g & 0xFF00FF) << 8, a = c >> 24;
+ }
+ over(p + x, c & 255, (c >> 8) & 255, (c >> 16) & 255, a);
+ }
+ }
+}
+
__attribute__((noinline)) static void paint_blit(const Cmd *k, int y0, int y1) {
const Img *im = k->blit.im;
const uint32_t *pal = img_pal(im);
@@ -493,8 +565,10 @@ 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: paint_image(k, y0, y1); break;
- case C_BLIT: paint_blit(k, y0, y1); break;
+ case C_IMAGE: 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_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.h
index a2cd91f4..a8706448 100644
--- a/games/hillclimb/src/res.h
+++ b/games/hillclimb/src/res.h
@@ -8,9 +8,9 @@ typedef struct {
uint16_t w, h, ncolors, flags; /* flags 1: opaque */
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;
+ uint16_t pad; /* if cropped, original clear palette index */
} Img;
-enum { IMG_OPAQUE = 1 };
+enum { IMG_OPAQUE = 1, IMG_CROPPED = 2 };
typedef struct {
uint8_t w, h;
@@ -26,9 +26,21 @@ 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); }
-static inline const uint8_t *img_px(const Img *i) {
+/* 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. */
+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];
+}
const Font *font(int id);
static inline const Glyph *font_glyphs(const Font *f) { return (const Glyph *)(const void *)(f + 1); }
static inline const uint8_t *font_bits(const Font *f) { return (const uint8_t *)(font_glyphs(f) + f->count); }
diff --git a/games/hillclimb/tools/pack.py b/games/hillclimb/tools/pack.py
index 9978faee..c9486631 100644
--- a/games/hillclimb/tools/pack.py
+++ b/games/hillclimb/tools/pack.py
@@ -245,6 +245,45 @@ 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')):
+ 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:
+ 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):
+ 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
+ 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)
+ return out if len(out) < len(blob) else blob
+
+
def sized(im, how, crop):
if crop == "trim":
im = im.crop(im.getbbox())
@@ -442,7 +481,8 @@ def main():
colors[name] = more[0]
vp, art_lines = vehicle_pictures(ref)
pics += vp
- blobs = [picture(im, ppm, pv, opaque, colors.get(name, 256)) for name, im, ppm, pv, opaque in pics]
+ blobs = [_hc_crop_picture_blob(picture(im, ppm, pv, opaque, colors.get(name, 256)), 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]
head = 16 + 4 * (len(blobs) + len(fonts))