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
#!/usr/bin/env python3
"""Packs Chrome's own dinosaur game pictures into src/sprites.h.
The source is Chromium's sprite sheet for the game at 1x,
components/neterror/resources/images/default_100_percent/offline/100-offline-sprite.png
(https://chromium.googlesource.com/chromium/src, BSD license: see ../LICENSE), at the
positions its offline_sprite_definitions.ts and the game's classes give. Its
pictures have four tones: clear, the ink (#535353), the light grey of the clouds
and the moon (#DADADA) and white; a pixel is 2 bits, rows a whole number of bytes.
The regions are packed together with games/common/np_lz.h's LZ77, which the game
unpacks into RAM when it starts.
Usage: sprites.py 100-offline-sprite.png
"""
import os
import sys
from PIL import Image
HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(HERE, "..", "src", "sprites.h")
# name: x, y, width, height on the sheet (1x)
REGIONS = [
("TREX", 848, 2, 176, 47), # standing frames at 0 (jumping, waiting), 44 (waiting), 88 and 132 (running)
("TREX_CRASH", 1068, 2, 44, 47), # the tRex frame at 220
("TREX_DUCK", 1112, 2, 118, 47), # the ducking frames at 264 and 323, 59 wide
("CACTUS_SMALL", 228, 2, 102, 35), # one, two and three together: 17, 34 and 51 wide
("CACTUS_LARGE", 332, 2, 150, 50), # 25, 50 and 75 wide
("PTERODACTYL", 134, 2, 92, 40), # two frames, 46 wide
("CLOUD", 86, 2, 46, 14),
("HORIZON", 2, 52, 1200, 12), # a flat stretch of ground and a bumpy one, 600 wide
("MOON", 484, 2, 160, 40), # its phases (night_mode.ts: PHASES)
("STAR", 645, 2, 9, 18), # two stars
("TEXT", 655, 2, 191, 24), # the figures and HI (10 x 13 each), then GAME OVER (191 x 11) at y 13
("RESTART", 2, 68, 288, 32), # the restart button's 8 frames, 36 apart
]
def tone(r, g, b, a):
if a < 128:
return 0
v = (r + g + b) / 3
return 1 if v < 150 else 2 if v < 236 else 3
def lz_pack(data):
"""games/common/np_lz.h's np_lz_pack, the same stream (and the same choices)."""
table = [0xFFFF] * 4096
out, i, n = bytearray(), 0, len(data)
flag_at, items = 0, 8
while i < n:
if items == 8:
flag_at = len(out)
out.append(0)
items = 0
length = dist = 0
if i + 3 <= n:
h = ((data[i] << 8) ^ (data[i + 1] << 4) ^ data[i + 2]) & 4095
cand = table[h]
table[h] = i & 0xFFFF
if cand != 0xFFFF and cand < i and i - cand <= 4096:
m = min(n - i, 18)
while length < m and data[cand + length] == data[i + length]:
length += 1
dist = i - cand
if length >= 3:
out += bytes(((dist - 1) & 255, ((dist - 1) >> 8) | ((length - 3) << 4)))
for k in range(1, length):
j = i + k
if j + 3 <= n:
table[((data[j] << 8) ^ (data[j + 1] << 4) ^ data[j + 2]) & 4095] = j & 0xFFFF
i += length
else:
out[flag_at] |= 1 << items
out.append(data[i])
i += 1
items += 1
return bytes(out)
def lz_unpack(data):
out, i = bytearray(), 0
while i < len(data):
flags = data[i]
i += 1
for b in range(8):
if i >= len(data):
break
if flags >> b & 1:
out.append(data[i])
i += 1
else:
dist = (data[i] | (data[i + 1] & 15) << 8) + 1
length = (data[i + 1] >> 4) + 3
i += 2
for _ in range(length):
out.append(out[-dist])
return bytes(out)
def main():
sheet = Image.open(sys.argv[1]).convert("RGBA")
assert sheet.size == (1233, 100), "the 1x sheet, 1233 x 100"
px = sheet.load()
blob, rows = bytearray(), []
for name, x0, y0, w, h in REGIONS:
stride = (w + 3) // 4
rows.append((name, len(blob), w, h, stride))
for y in range(h):
line = bytearray(stride)
for x in range(w):
line[x >> 2] |= tone(*px[x0 + x, y0 + y]) << (2 * (x & 3))
blob += line
packed = lz_pack(bytes(blob))
assert lz_unpack(packed) == bytes(blob)
out = ["/* Generated by tools/sprites.py from Chromium's 100-offline-sprite.png (BSD license, see ../LICENSE). */",
"#ifndef DINO_SPRITES_H", "#define DINO_SPRITES_H", "#include <stdint.h>", "",
"/* a region of the sheet: 2 bits a pixel (0 clear, 1 ink, 2 light grey, 3 white), the lowest first */",
"typedef struct { uint16_t off, w, stride; uint8_t h; } dino_region;",
"enum { " + ", ".join("R_" + r[0] for r in rows) + ", R_COUNT };",
f"#define SHEET_BYTES {len(blob)}",
"static const dino_region REGIONS[R_COUNT] = {",
" " + ", ".join(f"{{{off}, {w}, {stride}, {h}}}" for name, off, w, h, stride in rows) + ",", "};",
f"static const uint8_t SHEET_LZ[{len(packed)}] = {{"]
for i in range(0, len(packed), 24):
out.append(" " + ",".join(str(b) for b in packed[i:i + 24]) + ",")
out += ["};", "#endif", ""]
open(OUT, "w").write("\n".join(out))
print(f"sprites: {len(rows)} regions, {len(blob)} bytes unpacked, {len(packed)} packed")
if __name__ == "__main__":
main()