#!/usr/bin/env python3 """Rebuild a battle frame from Mesen dumps (VRAM/CGRAM/OAM of re/lua/gfx_dma.lua) with the mode-1 layout documented in re/notes/graphics.md, and compare it to the Mesen screenshot of the same frame. usage: gfx_render.py DUMP_PREFIX OUT.png e.g. re/runs/gfx/dma1/f04200""" import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from gfx_snes import Layer, diff_stats, read_png_rgb, render_mode1, write_png # noqa: E402 # Battle screen (PPU state at the battle frame, re/notes/graphics.md): BG1 chr word $2000 map $7800, # BG2 chr $3000 map $7C00, BG3 chr $7000 map $6800 (off on main); OBJ base 0, 2nd table +$1000 words. BATTLE_LAYERS = {1: Layer(0x4000, 0xF000, 4), 2: Layer(0x6000, 0xF800, 4), 3: Layer(0xE000, 0xD000, 2, 6, 1020)} BATTLE_MAIN = 0x13 # BG1 | BG2 | OBJ if __name__ == '__main__': pre, outp = sys.argv[1], sys.argv[2] vram = open(pre + '.vram', 'rb').read() cgram = open(pre + '.cgram', 'rb').read() oam = open(pre + '.oam', 'rb').read() sizes = ((8, 8), (32, 32)) if os.path.exists(pre + '.ppu.txt'): # OBSEL size mode (battle 1 = 8/32, cut-in 0 = 8/16) st = dict(line.strip().split('=', 1) for line in open(pre + '.ppu.txt') if '=' in line) mode = int(st.get('ppu.oamMode', 1)) table = [((8, 8), (16, 16)), ((8, 8), (32, 32)), ((8, 8), (64, 64)), ((16, 16), (32, 32)), ((16, 16), (64, 64)), ((32, 32), (64, 64))] sizes = table[mode] if mode < len(table) else sizes img = render_mode1(vram, cgram, oam, BATTLE_LAYERS, BATTLE_MAIN, obj_sizes=sizes) write_png(outp, 256, 224, img) if os.path.exists(pre + '.png'): w, h, ref = read_png_rgb(pre + '.png') if (w, h) == (256, 224): print(f'{outp}: {100 * diff_stats(img, ref):.2f}% pixels differ from {pre}.png') else: print(f'screenshot is {w}x{h}, not compared')