fix(dungeon): align object drawing and tests

This commit is contained in:
scawful
2025-12-22 14:55:59 -05:00
parent 26ce12cd6f
commit d5e06e943f
18 changed files with 256 additions and 189 deletions

View File

@@ -3,9 +3,11 @@
#include <chrono>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "rom/rom.h"
#include "test/test_utils.h"
#include "zelda3/dungeon/dungeon_editor_system.h"
#include "zelda3/dungeon/dungeon_object_editor.h"
#include "zelda3/dungeon/room.h"
@@ -21,12 +23,22 @@ class DungeonEditorSystemIntegrationTest : public ::testing::Test {
GTEST_SKIP() << "Dungeon editor tests require ROM file (unavailable on Linux CI)";
#endif
// Use the real ROM from build directory
rom_path_ = "build/bin/zelda3.sfc";
yaze::test::TestRomManager::SkipIfRomMissing(
yaze::test::RomRole::kVanilla,
"DungeonEditorSystemIntegrationTest");
rom_path_ =
yaze::test::TestRomManager::GetRomPath(yaze::test::RomRole::kVanilla);
ASSERT_FALSE(rom_path_.empty())
<< "ROM path not set for vanilla role. "
<< yaze::test::TestRomManager::GetRomRoleHint(
yaze::test::RomRole::kVanilla);
// Load ROM
rom_ = std::make_unique<Rom>();
ASSERT_TRUE(rom_->LoadFromFile(rom_path_).ok());
auto load_status = rom_->LoadFromFile(rom_path_);
ASSERT_TRUE(load_status.ok())
<< "Failed to load ROM from " << rom_path_ << ": "
<< load_status.message();
// Initialize dungeon editor system
dungeon_editor_system_ = std::make_unique<DungeonEditorSystem>(rom_.get());
@@ -99,8 +111,8 @@ TEST_F(DungeonEditorSystemIntegrationTest, ObjectEditorIntegration) {
ASSERT_TRUE(dungeon_editor_system_->SetCurrentRoom(0x0000).ok());
// Test object insertion
ASSERT_TRUE(object_editor->InsertObject(5, 5, 0x10, 0x12, 0).ok());
ASSERT_TRUE(object_editor->InsertObject(10, 10, 0x20, 0x22, 1).ok());
ASSERT_TRUE(object_editor->InsertObject(5, 5, 0x10, 0x0F, 0).ok());
ASSERT_TRUE(object_editor->InsertObject(10, 10, 0x20, 0x0F, 1).ok());
// Verify objects were added
EXPECT_EQ(object_editor->GetObjectCount(), 2);
@@ -125,8 +137,8 @@ TEST_F(DungeonEditorSystemIntegrationTest, UndoRedoFunctionality) {
ASSERT_NE(object_editor, nullptr);
// Add some objects
ASSERT_TRUE(object_editor->InsertObject(5, 5, 0x10, 0x12, 0).ok());
ASSERT_TRUE(object_editor->InsertObject(10, 10, 0x20, 0x22, 1).ok());
ASSERT_TRUE(object_editor->InsertObject(5, 5, 0x10, 0x0F, 0).ok());
ASSERT_TRUE(object_editor->InsertObject(10, 10, 0x20, 0x0F, 1).ok());
// Verify objects were added
EXPECT_EQ(object_editor->GetObjectCount(), 2);
@@ -158,8 +170,8 @@ TEST_F(DungeonEditorSystemIntegrationTest, SaveLoadFunctionality) {
auto object_editor = dungeon_editor_system_->GetObjectEditor();
ASSERT_NE(object_editor, nullptr);
ASSERT_TRUE(object_editor->InsertObject(5, 5, 0x10, 0x12, 0).ok());
ASSERT_TRUE(object_editor->InsertObject(10, 10, 0x20, 0x22, 1).ok());
ASSERT_TRUE(object_editor->InsertObject(5, 5, 0x10, 0x0F, 0).ok());
ASSERT_TRUE(object_editor->InsertObject(10, 10, 0x20, 0x0F, 1).ok());
// Save room
ASSERT_TRUE(dungeon_editor_system_->SaveRoom(0x0000).ok());

View File

@@ -5,8 +5,10 @@
#include <gtest/gtest.h>
#include <cstdio>
#include <string>
#include "rom/rom.h"
#include "test/test_utils.h"
#include "zelda3/dungeon/object_drawer.h"
#include "zelda3/dungeon/room.h"
#include "zelda3/game_data.h"
@@ -20,11 +22,11 @@ class DungeonGraphicsTransparencyTest : public ::testing::Test {
void SetUp() override {
rom_ = std::make_unique<Rom>();
const char* rom_path = std::getenv("YAZE_TEST_ROM_PATH");
if (!rom_path) {
rom_path = "zelda3.sfc";
}
yaze::test::TestRomManager::SkipIfRomMissing(
yaze::test::RomRole::kVanilla,
"DungeonGraphicsTransparencyTest");
const std::string rom_path =
yaze::test::TestRomManager::GetRomPath(yaze::test::RomRole::kVanilla);
auto status = rom_->LoadFromFile(rom_path);
if (!status.ok()) {
GTEST_SKIP() << "ROM file not available: " << status.message();
@@ -78,7 +80,8 @@ TEST_F(DungeonGraphicsTransparencyTest, GraphicsBufferHasTransparentPixels) {
// Test 2: Verify room graphics buffer after CopyRoomGraphicsToBuffer
TEST_F(DungeonGraphicsTransparencyTest, RoomGraphicsBufferHasTransparentPixels) {
// Create room 0 (Ganon's room - known to have walls)
Room room(0x00, rom_.get());
Room room = LoadRoomHeaderFromRom(rom_.get(), 0x00);
room.SetGameData(&game_data_);
room.LoadRoomGraphics(0xFF);
room.CopyRoomGraphicsToBuffer();
@@ -86,15 +89,22 @@ TEST_F(DungeonGraphicsTransparencyTest, RoomGraphicsBufferHasTransparentPixels)
const auto& gfx16 = room.get_gfx_buffer();
ASSERT_GT(gfx16.size(), 0);
// Count zeros in the room's graphics buffer
// Count zeros in the room's graphics buffer (background blocks only)
constexpr int kBlockSize = 4096;
constexpr int kBgBlocks = 8;
int zero_count = 0;
for (size_t i = 0; i < gfx16.size(); i++) {
if (gfx16[i] == 0) zero_count++;
int total_pixels = 0;
for (int block = 0; block < kBgBlocks; block++) {
int base = block * kBlockSize;
for (int i = 0; i < kBlockSize; i++) {
if (gfx16[base + i] == 0) zero_count++;
total_pixels++;
}
}
float zero_percent = 100.0f * zero_count / gfx16.size();
printf("[RoomGraphics] Room 0: Zeros: %d / %zu (%.1f%%)\n", zero_count,
gfx16.size(), zero_percent);
float zero_percent = 100.0f * zero_count / total_pixels;
printf("[RoomGraphics] Room 0: Zeros: %d / %d (%.1f%%)\n", zero_count,
total_pixels, zero_percent);
// Log first 64 bytes (one tile's worth) to see actual values
printf("[RoomGraphics] First 64 bytes:\n");
@@ -107,39 +117,44 @@ TEST_F(DungeonGraphicsTransparencyTest, RoomGraphicsBufferHasTransparentPixels)
}
// Print value distribution
int value_counts[8] = {0};
int value_counts[16] = {0};
int other_count = 0;
for (size_t i = 0; i < gfx16.size(); i++) {
if (gfx16[i] < 8) {
value_counts[gfx16[i]]++;
} else {
other_count++;
for (int block = 0; block < kBgBlocks; block++) {
int base = block * kBlockSize;
for (int i = 0; i < kBlockSize; i++) {
uint8_t value = gfx16[base + i];
if (value < 16) {
value_counts[value]++;
} else {
other_count++;
}
}
}
printf("[RoomGraphics] Value distribution:\n");
for (int v = 0; v < 8; v++) {
for (int v = 0; v < 16; v++) {
printf(" Value %d: %d (%.1f%%)\n", v, value_counts[v],
100.0f * value_counts[v] / gfx16.size());
100.0f * value_counts[v] / total_pixels);
}
if (other_count > 0) {
printf(" Values >7: %d (%.1f%%) - UNEXPECTED for 3BPP!\n", other_count,
100.0f * other_count / gfx16.size());
printf(" Values >15: %d (%.1f%%) - UNEXPECTED for 4BPP!\n", other_count,
100.0f * other_count / total_pixels);
}
EXPECT_GT(zero_percent, 5.0f)
<< "Room graphics buffer should have transparent pixels. "
<< "Got " << zero_percent << "%. Check CopyRoomGraphicsToBuffer().";
// All values should be 0-7 for 3BPP graphics
// Background blocks should not exceed 4BPP (0-15) values.
EXPECT_EQ(other_count, 0)
<< "Found " << other_count << " pixels with values > 7. "
<< "3BPP graphics should only have values 0-7.";
<< "Found " << other_count << " pixels with values > 15 in BG blocks. "
<< "BG graphics should only have values 0-15.";
}
// Test 3: Verify specific tile has expected mix of transparent/opaque
TEST_F(DungeonGraphicsTransparencyTest, SpecificTileTransparency) {
Room room(0x00, rom_.get());
Room room = LoadRoomHeaderFromRom(rom_.get(), 0x00);
room.SetGameData(&game_data_);
room.LoadRoomGraphics(0xFF);
room.CopyRoomGraphicsToBuffer();

View File

@@ -202,8 +202,8 @@ TEST_F(DungeonObjectRomValidationTest, DrawRoutineMapping_AllType1ObjectsHaveRou
int routine = drawer.GetDrawRoutineId(id);
EXPECT_GE(routine, 0)
<< "Object 0x" << std::hex << id << " should have a valid draw routine";
EXPECT_LT(routine, 40)
<< "Object 0x" << std::hex << id << " routine ID should be < 40";
EXPECT_LT(routine, drawer.GetDrawRoutineCount())
<< "Object 0x" << std::hex << id << " routine ID should be < registry size";
}
}

View File

@@ -1,10 +1,12 @@
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include "app/gfx/core/bitmap.h"
#include "app/gfx/types/snes_tile.h"
#include "zelda3/dungeon/object_drawer.h"
#include "zelda3/game_data.h"
#include "rom/rom.h"
#include "test/test_utils.h"
namespace yaze {
namespace zelda3 {
@@ -44,10 +46,10 @@ TEST_F(DungeonPaletteTest, PaletteOffsetIsCorrectFor8BPP) {
// Row 0, Col 1: Index 2
tiledata[1] = 2;
// Create TileInfo with palette index 1
// Create TileInfo with palette index 2 (first dungeon bank)
gfx::TileInfo tile_info;
tile_info.id_ = 0;
tile_info.palette_ = 1; // Palette 1
tile_info.palette_ = 2; // Palette 2
tile_info.horizontal_mirror_ = false;
tile_info.vertical_mirror_ = false;
tile_info.over_ = false;
@@ -56,33 +58,30 @@ TEST_F(DungeonPaletteTest, PaletteOffsetIsCorrectFor8BPP) {
drawer_->DrawTileToBitmap(bitmap, tile_info, 0, 0, tiledata.data());
// Check pixels
// Dungeon tiles use 15-color sub-palettes (not 8 like overworld).
// Formula: final_color = (pixel - 1) + (palette * 15)
// For palette 1, offset is 15.
// Pixel at (0,0) was 1. Result should be (1-1) + 15 = 15.
// Pixel at (1,0) was 2. Result should be (2-1) + 15 = 16.
// Dungeon tiles use 16-color CGRAM banks with index 0 as transparent.
// Formula: final_color = pixel + ((palette - 2) * 16) for palette 2-7.
// Palette 2 maps to the first dungeon bank (offset 0).
// Pixel at (0,0) was 1. Result should be 1.
// Pixel at (1,0) was 2. Result should be 2.
const auto& data = bitmap.vector();
// Bitmap data is row-major.
// (0,0) is index 0.
EXPECT_EQ(data[0], 15); // (1-1) + 15 = 15
EXPECT_EQ(data[1], 16); // (2-1) + 15 = 16
EXPECT_EQ(data[0], 1);
EXPECT_EQ(data[1], 2);
// Test with palette 0
tile_info.palette_ = 0;
// Test with palette 3 (offset 16)
tile_info.palette_ = 3;
drawer_->DrawTileToBitmap(bitmap, tile_info, 0, 0, tiledata.data());
// Offset 0 * 15 = 0.
// Pixel 1 -> (1-1) + 0 = 0
// Pixel 2 -> (2-1) + 0 = 1
EXPECT_EQ(data[0], 0);
EXPECT_EQ(data[1], 1);
EXPECT_EQ(data[0], 17); // 1 + 16
EXPECT_EQ(data[1], 18); // 2 + 16
// Test with palette 7 (wraps to palette 1 due to 6 sub-palette limit)
// Test with palette 7 (last dungeon bank)
tile_info.palette_ = 7;
drawer_->DrawTileToBitmap(bitmap, tile_info, 0, 0, tiledata.data());
// Palette 7 wraps to 7 % 6 = 1, offset 1 * 15 = 15.
EXPECT_EQ(data[0], 15); // (1-1) + 15 = 15
EXPECT_EQ(data[1], 16); // (2-1) + 15 = 16
// Palette 7 maps to bank 5 (offset 80).
EXPECT_EQ(data[0], 81); // 1 + 80
EXPECT_EQ(data[1], 82); // 2 + 80
}
TEST_F(DungeonPaletteTest, PaletteOffsetWorksWithConvertedData) {
@@ -100,7 +99,7 @@ TEST_F(DungeonPaletteTest, PaletteOffsetWorksWithConvertedData) {
gfx::TileInfo tile_info;
tile_info.id_ = 0;
tile_info.palette_ = 2; // Palette 2 → offset 30 (2 * 15)
tile_info.palette_ = 4; // Palette 4 → offset 32 (2 * 16)
tile_info.horizontal_mirror_ = false;
tile_info.vertical_mirror_ = false;
tile_info.over_ = false;
@@ -108,17 +107,21 @@ TEST_F(DungeonPaletteTest, PaletteOffsetWorksWithConvertedData) {
drawer_->DrawTileToBitmap(bitmap, tile_info, 0, 0, tiledata.data());
const auto& data = bitmap.vector();
// Dungeon tiles use 15-color sub-palettes.
// Formula: final_color = (pixel - 1) + (palette * 15)
// Pixel 3: (3-1) + 30 = 32
// Pixel 5: (5-1) + 30 = 34
EXPECT_EQ(data[0], 32);
EXPECT_EQ(data[1], 34);
// Dungeon tiles use 16-color CGRAM banks.
// Formula: final_color = pixel + ((palette - 2) * 16)
// Pixel 3: 3 + 32 = 35
// Pixel 5: 5 + 32 = 37
EXPECT_EQ(data[0], 35);
EXPECT_EQ(data[1], 37);
}
TEST_F(DungeonPaletteTest, InspectActualPaletteColors) {
// Load actual ROM file
auto load_result = rom_->LoadFromFile("zelda3.sfc");
yaze::test::TestRomManager::SkipIfRomMissing(
yaze::test::RomRole::kVanilla, "DungeonPaletteTest.InspectActualPaletteColors");
const std::string rom_path =
yaze::test::TestRomManager::GetRomPath(yaze::test::RomRole::kVanilla);
auto load_result = rom_->LoadFromFile(rom_path);
if (!load_result.ok()) {
GTEST_SKIP() << "ROM file not found, skipping";
}

View File

@@ -1,7 +1,10 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include "rom/rom.h"
#include "test/test_utils.h"
#include "zelda3/dungeon/room.h"
namespace yaze {
@@ -14,8 +17,10 @@ class DungeonRoomTest : public ::testing::Test {
#if defined(__linux__)
GTEST_SKIP() << "Dungeon room tests require ROM file (unavailable on Linux CI)";
#else
if (!rom_.LoadFromFile("./zelda3.sfc").ok()) {
GTEST_SKIP() << "Failed to load test ROM (zelda3.sfc)";
TestRomManager::SkipIfRomMissing(RomRole::kVanilla, "DungeonRoomTest");
const std::string rom_path = TestRomManager::GetRomPath(RomRole::kVanilla);
if (!rom_.LoadFromFile(rom_path).ok()) {
GTEST_SKIP() << "Failed to load test ROM (" << rom_path << ")";
}
#endif
}

View File

@@ -4,7 +4,10 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include "rom/rom.h"
#include "test/test_utils.h"
#include "zelda3/dungeon/room.h"
#include "zelda3/dungeon/room_object.h"
@@ -24,12 +27,10 @@ class RoomIntegrationTest : public ::testing::Test {
// Load the ROM file
rom_ = std::make_unique<Rom>();
// Check if ROM file exists
const char* rom_path = std::getenv("YAZE_TEST_ROM_PATH");
if (!rom_path) {
rom_path = "zelda3.sfc";
}
yaze::test::TestRomManager::SkipIfRomMissing(
yaze::test::RomRole::kVanilla, "RoomIntegrationTest");
const std::string rom_path =
yaze::test::TestRomManager::GetRomPath(yaze::test::RomRole::kVanilla);
auto status = rom_->LoadFromFile(rom_path);
if (!status.ok()) {
GTEST_SKIP() << "ROM file not available: " << status.message();
@@ -59,6 +60,7 @@ class RoomIntegrationTest : public ::testing::Test {
TEST_F(RoomIntegrationTest, BasicLoadSaveRoundTrip) {
// Load room 0 (Hyrule Castle Entrance)
Room room1(0x00, rom_.get());
room1.LoadObjects();
// Get original object count
size_t original_count = room1.GetTileObjects().size();
@@ -73,6 +75,7 @@ TEST_F(RoomIntegrationTest, BasicLoadSaveRoundTrip) {
// Load the room again
Room room2(0x00, rom_.get());
room2.LoadObjects();
// Verify object count matches
EXPECT_EQ(room2.GetTileObjects().size(), original_count);
@@ -108,6 +111,7 @@ TEST_F(RoomIntegrationTest, MultiRoomLoadSaveRoundTrip) {
// Load room
Room room1(room_id, rom_.get());
room1.LoadObjects();
auto original_objects = room1.GetTileObjects();
if (original_objects.empty()) {
@@ -120,6 +124,7 @@ TEST_F(RoomIntegrationTest, MultiRoomLoadSaveRoundTrip) {
// Reload and verify
Room room2(room_id, rom_.get());
room2.LoadObjects();
auto reloaded_objects = room2.GetTileObjects();
EXPECT_EQ(reloaded_objects.size(), original_objects.size());
@@ -146,6 +151,7 @@ TEST_F(RoomIntegrationTest, MultiRoomLoadSaveRoundTrip) {
TEST_F(RoomIntegrationTest, LayerPreservation) {
// Load a room known to have multiple layers
Room room(0x01, rom_.get());
room.LoadObjects();
auto objects = room.GetTileObjects();
ASSERT_GT(objects.size(), 0);
@@ -170,6 +176,7 @@ TEST_F(RoomIntegrationTest, LayerPreservation) {
ASSERT_TRUE(room.SaveObjects().ok());
Room room2(0x01, rom_.get());
room2.LoadObjects();
auto reloaded = room2.GetTileObjects();
// Verify layer counts match
@@ -199,6 +206,7 @@ TEST_F(RoomIntegrationTest, LayerPreservation) {
TEST_F(RoomIntegrationTest, ObjectTypeDistribution) {
Room room(0x00, rom_.get());
room.LoadObjects();
auto objects = room.GetTileObjects();
ASSERT_GT(objects.size(), 0);
@@ -222,6 +230,7 @@ TEST_F(RoomIntegrationTest, ObjectTypeDistribution) {
ASSERT_TRUE(room.SaveObjects().ok());
Room room2(0x00, rom_.get());
room2.LoadObjects();
auto reloaded = room2.GetTileObjects();
// Verify type distribution matches
@@ -250,6 +259,7 @@ TEST_F(RoomIntegrationTest, BinaryDataExactMatch) {
// when no modifications are made
Room room(0x02, rom_.get());
room.LoadObjects();
// Get the ROM location where objects are stored
auto rom_data = rom_->vector();
@@ -309,6 +319,7 @@ TEST_F(RoomIntegrationTest, BinaryDataExactMatch) {
TEST_F(RoomIntegrationTest, KnownRoomData) {
// Room 0x00 (Hyrule Castle Entrance) - verify known objects exist
Room room(0x00, rom_.get());
room.LoadObjects();
auto objects = room.GetTileObjects();
ASSERT_GT(objects.size(), 0) << "Room 0x00 should have objects";