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.

3.7 KB
Raw Blame
#!/usr/bin/env python3
"""Build src/assets_lv.c and src/assets_lv.h: the artwork of the objects of
the later levels (tools/sprites_lv.py). Sprite ids continue after the first
set (src/assets.h), so both sets share one numbering; formats as in
tools/assets.py.

  python3 tools/assets_lv.py [preview.png]
"""
import re
import sys
from pathlib import Path
import numpy as np

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(Path(__file__).resolve().parent))
from assets import Packer, c_ident, rgb565   # noqa: E402
from sprites_lv import lv_sprites            # noqa: E402


def first_names():
    h = (ROOT / 'src/assets.h').read_text()
    return set(re.findall(r'  (SPR_\w+) = \d+,', h))


def preview(sprites, path):
    from PIL import Image, ImageDraw
    cols, cell = 10, 72
    rows = (len(sprites) + cols - 1) // cols
    im = Image.new('RGB', (cols * cell, rows * cell), (150, 30, 40))
    d = ImageDraw.Draw(im)
    for k, (name, art, fmt) in enumerate(sprites):
        img = art.image()
        if fmt == 'A4':
            img[..., :3] = 1.0
        rgba = (np.clip(img, 0, 1) * 255).astype(np.uint8)
        t = Image.fromarray(rgba, 'RGBA')
        s = min(1.0, (cell - 14) / max(t.size))
        if s < 1:
            t = t.resize((max(1, int(t.size[0] * s)), max(1, int(t.size[1] * s))))
        x, y = (k % cols) * cell, (k // cols) * cell
        im.paste(t, (x + (cell - t.size[0]) // 2, y + 12 + (cell - 12 - t.size[1]) // 2), t)
        d.text((x + 1, y), name[:12], fill=(255, 255, 0))
    im.save(path)


def main():
    sprites = lv_sprites()
    if len(sys.argv) > 1:
        preview(sprites, sys.argv[1])
    pk = Packer()
    for name, art, fmt in sprites:
        pk.add(name, art.image(), art.ax, art.ay, fmt)
    pk.layout()
    names = [s[0] for s in pk.sprites]
    assert len(set(names)) == len(names), 'duplicate sprite names'
    clash = set(c_ident(n) for n in names) & first_names()
    assert not clash, 'names used by the first set: %s' % sorted(clash)
    h = ['/* Generated by tools/assets_lv.py. Do not edit. */', '#ifndef NUMDASH_ASSETS_LV_H', '#define NUMDASH_ASSETS_LV_H',
         '#include "assets.h"', 'enum {']
    for i, n in enumerate(names):
        h.append('  %s = SPR_COUNT + %d,' % (c_ident(n), i))
    h.append('  SPR_LV_END = SPR_COUNT + %d' % len(names))
    h.append('};')
    h.append('extern const Sprite sprites_lv[%d];' % len(names))
    h.append('extern const uint8_t sprite_blob_lv[];')
    h.append('extern const uint16_t sprite_pal_rgb_lv[][16];')
    h.append('extern const uint8_t sprite_pal_a_lv[][16];')
    h.append('#endif')
    (ROOT / 'src/assets_lv.h').write_text('\n'.join(h) + '\n')
    c = ['/* Generated by tools/assets_lv.py. Do not edit. */', '#include "assets_lv.h"',
         'const Sprite sprites_lv[%d] = {' % len(names)]
    for (n, w, hh, ax, ay, fmt, pal, off) in pk.sprites:
        c.append('  {%d,%d,%d,%d,%d,%d,%d},' % (w, hh, ax, ay, fmt, pal, off))
    c.append('};')
    pals = pk.palettes or [[(0, 0, 0, 0)] * 16]
    c.append('const uint16_t sprite_pal_rgb_lv[][16] = {')
    for pal in pals:
        c.append('  {' + ','.join('0x%04x' % rgb565(*p[:3]) for p in pal) + '},')
    c.append('};')
    c.append('const uint8_t sprite_pal_a_lv[][16] = {')
    for pal in pals:
        c.append('  {' + ','.join(str(int(round(p[3] * 15))) for p in pal) + '},')
    c.append('};')
    c.append('const uint8_t sprite_blob_lv[] = {')
    blob = bytes(pk.blob)
    for i in range(0, len(blob), 32):
        c.append('  ' + ','.join(str(b) for b in blob[i:i + 32]) + ',')
    c.append('};')
    (ROOT / 'src/assets_lv.c').write_text('\n'.join(c) + '\n')
    print('level sprites: %d, blob: %d bytes, palettes: %d' % (len(names), len(blob), len(pk.palettes)))


if __name__ == '__main__':
    main()