// gfx::AssetSource tests: override folder without a ROM, and (with TBG_ROM=path) ROM decode == PNG round trip // == override result. Exit code = failure count. #include #include #include #include #include #include #include "check.hpp" #include "gfx/asset_source.hpp" #include "gfx/pause_text.hpp" int g_failures = 0; namespace fs = std::filesystem; namespace { bool same(const gfx::Image& a, const gfx::Image& b) { return a.w == b.w && a.h == b.h && a.px == b.px; } gfx::Image pattern(int w, int h, std::uint32_t seed) { gfx::Image img(w, h); for (std::size_t i = 0; i < img.px.size(); ++i) { const std::uint32_t v = static_cast(i) * 2654435761u ^ seed; img.px[i] = (v & 3) == 0 ? 0u : 0xFF000000u | (v & 0xFFFFFFu); } return img; } fs::path temp_dir(const char* name) { const fs::path d = fs::temp_directory_path() / name; fs::remove_all(d); fs::create_directories(d / "gfx"); return d; } void test_overrides_without_rom() { const fs::path d = temp_dir("tbg_gfx_tests_norom"); const gfx::Image bg = pattern(256, 224, 1), cells = pattern(13 * 8, 8, 2), port = pattern(64, 64, 3); CHECK(gfx::save_png(d / "gfx" / "stage3_background.png", bg)); CHECK(gfx::save_png(d / "gfx" / "stage3_cells_p2.png", cells)); CHECK(gfx::save_png(d / "portrait_c5.png", port)); // top level works too CHECK(gfx::save_png(d / "gfx" / "cutin_c1_f0.png", pattern(40, 30, 4))); CHECK(gfx::save_png(d / "gfx" / "cutin_c1_f1.png", pattern(40, 30, 5))); std::ofstream(d / "manifest.txt") << "#\tcut-in 1 sequence (frame x video frames): 0x4 1x6 0x2 \n"; const gfx::AssetSource src({}, d); CHECK(!src.has_rom()); CHECK(src.has_overrides()); const gfx::BattleArt a = src.battle_art(3); CHECK(same(a.stage, bg)); CHECK(same(a.cells[1][4], cells.crop(32, 0, 8, 8))); CHECK(a.cells[0][4].w == 0); // no p1 strip, no ROM CHECK(same(src.portrait(5), port)); CHECK(src.portrait(6).w == 0); const gfx::CutinAnim c = src.cutin(1); CHECK(c.frames.size() == 2); CHECK(c.sequence.size() == 3 && c.sequence[1] == std::make_pair(1, 6)); CHECK(src.cutin(2).frames.empty()); CHECK(!gfx::load_png(d / "missing.png")); fs::remove_all(d); } void test_rom_round_trip(const std::vector& rom) { const fs::path d = temp_dir("tbg_gfx_tests_rom"); const gfx::Rom r(rom); const gfx::BattleArt art = gfx::load_battle_art(r, 2); const gfx::Image fx = gfx::load_effect_sheet(r, 0x29); CHECK(gfx::save_png(d / "gfx" / "stage2_background.png", art.stage)); CHECK(gfx::save_png(d / "gfx" / "effect_p41.png", fx)); const auto png = gfx::load_png(d / "gfx" / "stage2_background.png"); CHECK(png && same(*png, art.stage)); // PNG round trip is lossless for ROM decodes const gfx::AssetSource plain(rom, {}), over(rom, d); CHECK(plain.has_rom()); CHECK(same(plain.battle_art(2).stage, art.stage)); CHECK(same(over.battle_art(2).stage, art.stage)); CHECK(same(over.battle_art(2).frame, art.frame)); // not overridden -> ROM CHECK(same(over.effect_sheet(0x29), fx)); CHECK(same(over.cutin(0).frames.front(), gfx::load_cutin(r, 0).frames.front())); // A replacement really replaces. const gfx::Image fake = pattern(art.frame.w, art.frame.h, 9); CHECK(gfx::save_png(d / "gfx" / "stage2_frame.png", fake)); CHECK(same(over.battle_art(2).frame, fake)); fs::remove_all(d); } void test_win_mark(const std::vector& rom) { // 81:FBF2 (re/notes/anim_battle_extra.md §3): P1's first win starts at (120, 85) and rests at (112, 97); // its second win, 16 px lower. The spin starts at frame 2 for a first win. const gfx::WinMark w = gfx::load_win_mark(gfx::Rom(rom)); CHECK(w.has_flight); int dx = 0, dy = 0; for (std::size_t s = 0; s < 13; ++s) dx += w.path[0][s][0], dy += w.path[0][s][1]; CHECK(dx == -8 && dy == 12); CHECK(dy + w.path[0][13][1] == 28); CHECK(w.spin_start[1] == 2 && w.spin_start[2] == 1); CHECK(w.frames[0][0].w == 16 && w.orb[1].w == 8); CHECK(gfx::WinMark::kTiles[static_cast(w.land[1])] == 0x50); // squash, flat, squash, rest } void test_pause_mood() { CHECK(gfx::pause_mood(0) == 0 && gfx::pause_mood(9) == 0); // 82:AA0F CHECK(gfx::pause_mood(10) == 1 && gfx::pause_mood(17) == 1); CHECK(gfx::pause_mood(18) == 2 && gfx::pause_mood(22) == 2); CHECK(!gfx::PauseTexts({}).ok()); } // Opaque pixels in the box's text line (0-3). int ink(const gfx::Image& box, int line) { int n = 0; for (int y = line * 16; y < line * 16 + 16 && y < box.h; ++y) for (int x = 0; x < box.w; ++x) n += (box.at(x, y) >> 24) != 0; return n; } // The pause screen texts (re/notes/anim_battle_extra.md §4): drawn from the JP strings, or the English patch's // bitmaps (TBG_EN_ROM, optional). void test_pause_texts(const std::vector& rom) { const gfx::PauseTexts jp(rom); CHECK(jp.ok() && !jp.english()); const gfx::Image bomb = jp.power(0x03, 4); // "LEVEL 4 " + 3 lines of description CHECK(bomb.w == gfx::PauseTexts::kBoxW && bomb.h == gfx::PauseTexts::kBoxH); CHECK(ink(bomb, 0) > 100 && ink(bomb, 1) > 100); CHECK(ink(jp.how_to(3), 0) > 50 && ink(jp.how_to(3), 1) > 50); CHECK(ink(jp.mood(6, 0), 0) > 50); CHECK(jp.power(0x2B, 1).w == 0 && jp.mood(10, 0).w == 0 && jp.mood(0, 3).w == 0); if (const char* path = std::getenv("TBG_EN_ROM")) { std::ifstream f(path, std::ios::binary); const std::vector en_rom{std::istreambuf_iterator(f), {}}; const gfx::PauseTexts en(en_rom); CHECK(en.ok() && en.english()); CHECK(ink(en.power(0x21, 4), 0) > 50 && ink(en.mood(0, 2), 0) > 50); } } } // namespace int main() { test_overrides_without_rom(); test_pause_mood(); if (const char* path = std::getenv("TBG_ROM")) { std::ifstream f(path, std::ios::binary); const std::vector rom{std::istreambuf_iterator(f), {}}; if (gfx::battle_art_supported(rom)) test_rom_round_trip(rom), test_win_mark(rom), test_pause_texts(rom); else std::printf("ROM tests skipped (unsupported ROM)\n"), ++g_failures; } else { std::printf("ROM tests skipped (set TBG_ROM)\n"); } std::printf("%s (%d failures)\n", g_failures ? "FAILED" : "ok", g_failures); return g_failures; }