Files
siegeandClaude Opus 5.5 9c8298745c gfx: AssetSource — override-folder PNGs over the ROM loaders
Loads <dir>/gfx/<name>.png (or <dir>/<name>.png) with tbg_extract's file
names, falling back to the ROM decode per image; works with no ROM (cut-in
timing/origin from manifest.txt). PNG writer moved from tbg_extract into
gfx::save_png. Vendors stb_image v2.30 (public domain/MIT, PNG only).
Verified: AssetSource(no ROM, tbg_extract dir) == ROM loaders for all 696
images. New test target tbg_gfx_tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 16:53:07 -05:00

271 lines
14 KiB
C++

// Dumps everything the game currently reads from the ROM into a folder — the first step toward running
// without the ROM (or replacing assets). The output is ROM-derived: keep it local, never commit it.
//
// usage: tbg_extract ROM OUTDIR [--seconds N]
// OUTDIR/gfx/ PNGs: per stage (0-9) frame, queue box, stage background, cells x4 palettes, block sprites;
// cut-in animation frames, select portraits, effect sheets 00-42, READY/GO letters
// OUTDIR/music/ song_XX.wav (16-bit stereo 32 kHz, up to N s, default 60, cut after 2 s of silence)
// OUTDIR/sfx/ sfx_XX.wav (every id a sound set carries, up to 3 s, from the set that has it)
// OUTDIR/ai_bank81.bin bank $81 (32 KiB) — RomAi reads its tables ($E400-$F1FF) and the shape pointers and
// matrices (81:8218..) from it
// OUTDIR/manifest.txt every file and what it is; anything that failed is listed as SKIPPED
#include <array>
#include <algorithm>
#include <cstdio>
#include <optional>
#include <cstring>
#include <exception>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
#include "audio/sound.hpp"
#include "gfx/asset_source.hpp"
#include "gfx/rom_gfx.hpp"
namespace fs = std::filesystem;
namespace {
std::ofstream g_manifest;
int g_files = 0, g_skipped = 0;
void note(const fs::path& p, const std::string& what) {
g_manifest << p.generic_string() << "\t" << what << "\n";
++g_files;
}
void skipped(const std::string& what, const std::string& why) {
g_manifest << "SKIPPED\t" << what << "\t" << why << "\n";
std::fprintf(stderr, "skipped %s: %s\n", what.c_str(), why.c_str());
++g_skipped;
}
void png(const fs::path& out, const fs::path& rel, const gfx::Image& img, const std::string& what) {
if (gfx::save_png(out / rel, img))
note(rel, what);
else
skipped(rel.generic_string(), "empty image or write error");
}
// --- WAV -------------------------------------------------------------------------------------------------
bool write_wav(const fs::path& p, const std::vector<std::int16_t>& s, int rate) {
std::ofstream f(p, std::ios::binary);
auto u32 = [&](std::uint32_t v) { f.write(reinterpret_cast<const char*>(&v), 4); };
auto u16 = [&](std::uint16_t v) { f.write(reinterpret_cast<const char*>(&v), 2); };
const std::uint32_t bytes = static_cast<std::uint32_t>(s.size() * 2);
f.write("RIFF", 4), u32(36 + bytes), f.write("WAVEfmt ", 8), u32(16), u16(1), u16(2);
u32(static_cast<std::uint32_t>(rate)), u32(static_cast<std::uint32_t>(rate) * 4), u16(4), u16(16);
f.write("data", 4), u32(bytes);
f.write(reinterpret_cast<const char*>(s.data()), static_cast<std::streamsize>(bytes));
return f.good();
}
// Render until max_s seconds, stopping after `quiet_s` seconds of silence once min_s has passed.
std::vector<std::int16_t> render(tbg::audio::Sound& snd, double max_s, double min_s, double quiet_s) {
constexpr int kRate = tbg::audio::Sound::kSampleRate, kChunk = 1600; // 50 ms
std::vector<std::int16_t> out, buf(kChunk * 2);
int quiet = 0;
for (int t = 0; t < static_cast<int>(max_s * kRate); t += kChunk) {
snd.render(buf.data(), kChunk);
out.insert(out.end(), buf.begin(), buf.end());
bool silent = true;
for (std::int16_t v : buf) silent &= v > -64 && v < 64;
quiet = silent ? quiet + kChunk : 0;
if (t > min_s * kRate && quiet >= quiet_s * kRate) {
out.resize(out.size() - static_cast<std::size_t>(quiet) * 2); // trim the trailing silence
break;
}
}
return out;
}
} // namespace
int main(int argc, char** argv) {
if (argc < 3) {
std::fprintf(stderr, "usage: %s ROM OUTDIR [--seconds N]\n", argv[0]);
return 2;
}
double seconds = 60;
for (int a = 3; a + 1 < argc; ++a)
if (std::strcmp(argv[a], "--seconds") == 0) seconds = std::atof(argv[a + 1]);
std::ifstream in(argv[1], std::ios::binary);
const std::vector<std::uint8_t> data{std::istreambuf_iterator<char>(in), {}};
if (!gfx::battle_art_supported(data)) {
std::fprintf(stderr, "%s: not the supported Tetris Battle Gaiden (J) ROM\n", argv[1]);
return 1;
}
const fs::path out = argv[2];
for (const char* d : {"gfx", "music", "sfx"}) fs::create_directories(out / d);
g_manifest.open(out / "manifest.txt");
g_manifest << "# tbg_extract output — ROM-derived, keep local, never commit/distribute\n";
const gfx::Rom rom(data);
// Graphics.
for (int s = 0; s < 10; ++s) {
try {
const gfx::BattleArt a = gfx::load_battle_art(rom, s);
const std::string st = "stage" + std::to_string(s);
png(out, fs::path("gfx") / (st + "_frame.png"), a.frame, "battle: P1 board column frame (field at 24,24)");
png(out, fs::path("gfx") / (st + "_queue_box.png"), a.queue_box, "battle: centre queue box");
png(out, fs::path("gfx") / (st + "_background.png"), a.stage, "battle: stage background 256x224");
for (int p = 0; p < 4; ++p) {
gfx::Image cells(13 * 8, 8);
for (int v = 0; v < 13; ++v)
for (int y = 0; y < 8; ++y)
for (int x = 0; x < 8; ++x) cells.at(v * 8 + x, y) = a.cells[static_cast<std::size_t>(p)][static_cast<std::size_t>(v)].at(x, y);
png(out, fs::path("gfx") / (st + "_cells_p" + std::to_string(p + 1) + ".png"), cells,
"battle: board cell tiles 0-12 (8x8 each, left to right), player " + std::to_string(p + 1) + " palette");
png(out, fs::path("gfx") / (st + "_block_p" + std::to_string(p + 1) + ".png"), a.block[static_cast<std::size_t>(p)],
"battle: falling-piece block sprite, player " + std::to_string(p + 1));
}
} catch (const std::exception& e) {
skipped("stage " + std::to_string(s), e.what());
}
}
for (int c = 0; c < 10; ++c) {
const gfx::CutinAnim anim = gfx::load_cutin(rom, c);
if (anim.frames.empty()) skipped("cut-in " + std::to_string(c), "no frames");
for (std::size_t f = 0; f < anim.frames.size(); ++f)
png(out, fs::path("gfx") / ("cutin_c" + std::to_string(c) + "_f" + std::to_string(f) + ".png"), anim.frames[f],
"power cut-in, character " + std::to_string(c) + ", frame " + std::to_string(f) + " (origin " +
std::to_string(anim.origin_x) + "," + std::to_string(anim.origin_y) + ")");
std::string seq;
for (const auto& [fr, d] : anim.sequence) seq += std::to_string(fr) + "x" + std::to_string(d) + " ";
g_manifest << "#\tcut-in " << c << " sequence (frame x video frames): " << seq << "\n";
}
for (int c = 0; c < 8; ++c)
png(out, fs::path("gfx") / ("portrait_c" + std::to_string(c) + ".png"), gfx::load_select_portrait(rom, c),
"character-select portrait 64x64, character " + std::to_string(c));
for (int p = 0; p < 43; ++p)
if (const gfx::Image fx = gfx::load_effect_sheet(rom, p); fx.w)
png(out, fs::path("gfx") / ("effect_p" + std::to_string(p) + ".png"), fx,
"power effect sprite sheet, power id " + std::to_string(p) + " (8x8 tiles, 16 per row)");
{
const auto letters = gfx::load_ready_go(rom);
static constexpr const char* kNames[] = {"R", "E", "A", "D", "Y", "G", "O"};
for (std::size_t k = 0; k < letters.size(); ++k)
png(out, fs::path("gfx") / (std::string("readygo_") + kNames[k] + ".png"), letters[k], "READY/GO letter 16x16");
}
// AI tables: RomAi reads bank $81 (file offset 0x8000-0xFFFF).
{
const fs::path rel = "ai_bank81.bin";
std::ofstream f(out / rel, std::ios::binary);
f.write(reinterpret_cast<const char*>(data.data() + 0x8000), 0x8000);
note(rel, "bank $81 (32 KiB): RomAi planner tables $E400-$F1FF and shape pointers/matrices at 81:8218");
}
// Music and SFX.
try {
tbg::audio::Sound snd(data);
constexpr int kRate = tbg::audio::Sound::kSampleRate;
// Songs used by the game (src/app/game_sound.cpp, re/notes/sound.md §3): scenes, stage themes and
// their variants, power music.
static constexpr int kSongs[] = {0x24, 0x20, 0x11, 0x17, 0x25, 0x27, 0x23, 0x1A, 0x22, 0x04, // scenes
0x21, 0x02, 0x1C, 0x18, 0x0F, 0x15, 0x00, 0x13, 0x1B, 0x1F, // stages
0x03, 0x10, 0x16, 0x01, 0x14, // variants
0x09, 0x0C, 0x2A, 0x0E, 0x37, 0x0D, 0x2C, 0x39, 0x0B, 0x34, // powers
0x05, 0x36, 0x33, 0x08};
for (int id : kSongs) {
char name[32];
std::snprintf(name, sizeof name, "song_%02X.wav", id);
snd.stop();
render(snd, 0.5, 0, 1); // let the fade-out finish
snd.play_song(id);
const auto pcm = render(snd, seconds, 5, 2);
if (pcm.empty()) {
skipped(name, "silent");
continue;
}
if (write_wav(out / "music" / name, pcm, kRate))
note(fs::path("music") / name, "song id " + std::to_string(id) + " (" +
std::to_string(pcm.size() / 2 / kRate) + " s)");
else
skipped(name, "write error");
}
// SFX: each effect's samples live in the sound sets that list it (sound.md §1, table 0 "B" lists, from
// `re/tools/snd_build_spc.py --list`). For each id a song from such a set is started so the set is
// uploaded — preferably the silent placeholder $2B or a silent power track — then the effect is sent
// (a stop/fade would mute it too). Where only an audible song is available the effect is rendered
// with and without the song playing and the difference kept.
struct SfxSource {
int song;
int set;
bool subtract;
};
auto source_for = [](int id) -> std::optional<SfxSource> {
static constexpr int kBattle[] = {0x00, 0x01, 0x03, 0x04, 0x05, 0x07, 0x08, 0x0A, 0x0E, 0x0F, 0x10, 0x11, 0x12,
0x13, 0x14, 0x15, 0x16, 0x30, 0x31, 0x32, 0x35, 0x38, 0x39};
for (int b : kBattle)
if (b == id) return SfxSource{0x2B, 0x11, false}; // set 11: battle SFX + silent $2B
switch (id) {
case 0x02: case 0x0D: return SfxSource{0x2B, 0x0A, false}; // set 0A
case 0x06: case 0x0C: return SfxSource{0x11, 0x03, true}; // set 03 (character select)
case 0x17: case 0x18: case 0x19: return SfxSource{0x0D, 0x21, false};
case 0x1A: case 0x1B: return SfxSource{0x05, 0x1D, false};
case 0x1C: case 0x1D: case 0x2A: return SfxSource{0x0E, 0x23, false};
case 0x1E: case 0x1F: return SfxSource{0x0B, 0x1A, true};
case 0x20: return SfxSource{0x2D, 0x12, true};
case 0x21: return SfxSource{0x2C, 0x13, true};
case 0x22: return SfxSource{0x31, 0x15, true};
case 0x24: case 0x25: case 0x26: return SfxSource{0x36, 0x24, false};
case 0x27: case 0x28: return SfxSource{0x37, 0x22, false};
case 0x2B: return SfxSource{0x38, 0x1C, true};
case 0x2E: case 0x3B: return SfxSource{0x39, 0x1F, false};
case 0x34: case 0x3A: return SfxSource{0x08, 0x1E, false};
default: return std::nullopt; // no sound set carries it: no sample data exists
}
};
auto fixed = [](tbg::audio::Sound& engine, int frames) {
std::vector<std::int16_t> buf(static_cast<std::size_t>(frames) * 2);
engine.render(buf.data(), frames);
return buf;
};
constexpr int kWarm = kRate / 4, kLen = 3 * kRate;
for (int id = 0; id < 0x40; ++id) {
char name[32];
std::snprintf(name, sizeof name, "sfx_%02X.wav", id);
const auto src = source_for(id);
if (!src) {
skipped(name, "no sound set carries this id (no sample data)");
continue;
}
tbg::audio::Sound with(data);
with.play_song(src->song);
fixed(with, kWarm);
with.sfx(id);
std::vector<std::int16_t> pcm = fixed(with, kLen);
if (src->subtract) {
tbg::audio::Sound base(data);
base.play_song(src->song);
fixed(base, kWarm);
const std::vector<std::int16_t> music = fixed(base, kLen);
for (std::size_t k = 0; k < pcm.size(); ++k)
pcm[k] = static_cast<std::int16_t>(std::clamp(pcm[k] - music[k], -32768, 32767));
}
std::size_t end = pcm.size(); // trim trailing silence
while (end >= 2 && pcm[end - 1] > -64 && pcm[end - 1] < 64 && pcm[end - 2] > -64 && pcm[end - 2] < 64) end -= 2;
pcm.resize(end);
if (pcm.size() < 2 * static_cast<std::size_t>(kRate) / 20) {
skipped(name, "silent (set " + std::to_string(src->set) + ")");
continue;
}
char what[96];
std::snprintf(what, sizeof what, "sound effect id %d (set $%02X, over song $%02X%s)", id, src->set, src->song,
src->subtract ? ", music subtracted" : "");
if (write_wav(out / "sfx" / name, pcm, kRate))
note(fs::path("sfx") / name, what);
else
skipped(name, "write error");
}
} catch (const std::exception& e) {
skipped("music/sfx", e.what());
}
std::printf("wrote %d files to %s (%d skipped; see manifest.txt)\n", g_files, out.c_str(), g_skipped);
return 0;
}