refactor: Remove manual object renderer and dungeon renderer from the editor

- Deleted the ManualObjectRenderer and DungeonRenderer classes to streamline the codebase and reduce redundancy.
- Updated DungeonCanvasViewer and DungeonEditorV2 to remove references to the deleted renderers, ensuring proper functionality without them.
- Enhanced the rendering logic in DungeonCanvasViewer to directly handle object rendering and background layers, improving performance and maintainability.
- Added debug logging to track rendering processes and ensure proper graphics loading.
This commit is contained in:
scawful
2025-10-09 15:47:15 -04:00
parent 44800ceccc
commit 9d0b6737cd
21 changed files with 1789 additions and 1077 deletions

View File

@@ -71,23 +71,46 @@ void DungeonCanvasViewer::DrawDungeonCanvas(int room_id) {
ImGui::BeginGroup();
if (rooms_) {
gui::InputHexByte("Layout", &(*rooms_)[room_id].layout);
auto& room = (*rooms_)[room_id];
// Store previous values to detect changes
static int prev_blockset = -1;
static int prev_palette = -1;
static int prev_layout = -1;
static int prev_spriteset = -1;
gui::InputHexByte("Layout", &room.layout);
ImGui::SameLine();
gui::InputHexByte("Blockset", &(*rooms_)[room_id].blockset);
gui::InputHexByte("Gfx", &room.blockset);
ImGui::SameLine();
gui::InputHexByte("Spriteset", &(*rooms_)[room_id].spriteset);
gui::InputHexByte("Spriteset", &room.spriteset);
ImGui::SameLine();
gui::InputHexByte("Palette", &(*rooms_)[room_id].palette);
gui::InputHexByte("Palette", &room.palette);
gui::InputHexByte("Floor1", &(*rooms_)[room_id].floor1);
gui::InputHexByte("Floor1", &room.floor1);
ImGui::SameLine();
gui::InputHexByte("Floor2", &(*rooms_)[room_id].floor2);
gui::InputHexByte("Floor2", &room.floor2);
ImGui::SameLine();
gui::InputHexWord("Message ID", &(*rooms_)[room_id].message_id_);
ImGui::SameLine();
if (Button("Load Room Graphics")) {
(void)LoadAndRenderRoomGraphics(room_id);
gui::InputHexWord("Message ID", &room.message_id_);
// Check if critical properties changed and trigger reload
if (prev_blockset != room.blockset || prev_palette != room.palette ||
prev_layout != room.layout || prev_spriteset != room.spriteset) {
// Only reload if ROM is properly loaded
if (room.rom() && room.rom()->is_loaded()) {
// Force reload of room graphics
room.LoadRoomGraphics(room.blockset);
room.RenderRoomGraphics();
// Update background layers
UpdateRoomBackgroundLayers(room_id);
}
prev_blockset = room.blockset;
prev_palette = room.palette;
prev_layout = room.layout;
prev_spriteset = room.spriteset;
}
}
@@ -106,9 +129,14 @@ void DungeonCanvasViewer::DrawDungeonCanvas(int room_id) {
auto& bg1_bitmap = room.bg1_buffer().bitmap();
bool needs_render = !bg1_bitmap.is_active() || bg1_bitmap.width() == 0;
// Render immediately if needed
if (needs_render) {
// Render immediately if needed (but only once per room change)
static int last_rendered_room = -1;
static bool has_rendered = false;
if (needs_render && (last_rendered_room != room_id || !has_rendered)) {
printf("[DungeonCanvasViewer] Loading and rendering graphics for room %d\n", room_id);
(void)LoadAndRenderRoomGraphics(room_id);
last_rendered_room = room_id;
has_rendered = true;
}
// Load room objects if not already loaded
@@ -120,24 +148,6 @@ void DungeonCanvasViewer::DrawDungeonCanvas(int room_id) {
// This already includes objects drawn by ObjectDrawer in Room::RenderObjectsToBackground()
RenderRoomBackgroundLayers(room_id);
// TEMPORARY: Render all objects as primitives until proper rendering is fixed
// This ensures we can see objects while debugging the texture pipeline
if (current_palette_id_ < current_palette_group_.size()) {
auto room_palette = current_palette_group_[current_palette_id_];
// Render regular objects as colored rectangles (FALLBACK)
for (const auto& object : room.GetTileObjects()) {
RenderObjectInCanvas(object, room_palette);
}
// Render special objects with primitive shapes (overlays)
RenderStairObjects(room, room_palette);
RenderChests(room);
RenderDoorObjects(room);
RenderWallObjects(room);
RenderPotObjects(room);
}
// Render sprites as simple 16x16 squares with labels
// (Sprites are not part of the background buffers)
RenderSprites(room);
@@ -175,107 +185,6 @@ void DungeonCanvasViewer::DrawDungeonCanvas(int room_id) {
}
}
void DungeonCanvasViewer::RenderObjectInCanvas(const zelda3::RoomObject &object,
const gfx::SnesPalette &palette) {
// Validate ROM is loaded
if (!rom_ || !rom_->is_loaded()) {
return;
}
// Convert room coordinates to canvas coordinates
auto [canvas_x, canvas_y] = RoomToCanvasCoordinates(object.x_, object.y_);
// Check if object is within canvas bounds
if (!IsWithinCanvasBounds(canvas_x, canvas_y, 32)) {
return; // Skip objects outside visible area
}
// Calculate palette hash for caching
uint64_t palette_hash = 0;
for (size_t i = 0; i < palette.size() && i < 16; ++i) {
palette_hash ^= std::hash<uint16_t>{}(palette[i].snes()) + 0x9e3779b9 +
(palette_hash << 6) + (palette_hash >> 2);
}
// Check cache first
for (auto& cached : object_render_cache_) {
if (cached.object_id == object.id_ && cached.object_x == object.x_ &&
cached.object_y == object.y_ && cached.object_size == object.size_ &&
cached.palette_hash == palette_hash && cached.is_valid) {
canvas_.DrawBitmap(cached.rendered_bitmap, canvas_x, canvas_y, 1.0f, 255);
return;
}
}
// Create a mutable copy of the object to ensure tiles are loaded
auto mutable_object = object;
mutable_object.set_rom(rom_);
mutable_object.EnsureTilesLoaded();
// Try to render the object with proper graphics
auto render_result = object_renderer_.RenderObject(mutable_object, palette);
if (render_result.ok()) {
auto object_bitmap = std::move(render_result.value());
// Ensure the bitmap is valid and has content
if (object_bitmap.width() > 0 && object_bitmap.height() > 0) {
object_bitmap.SetPalette(palette);
// Add to cache
ObjectRenderCache cache_entry;
cache_entry.object_id = object.id_;
cache_entry.object_x = object.x_;
cache_entry.object_y = object.y_;
cache_entry.object_size = object.size_;
cache_entry.palette_hash = palette_hash;
cache_entry.rendered_bitmap = std::move(object_bitmap); // Move bitmap into cache
cache_entry.is_valid = true;
if (object_render_cache_.size() >= 200) { // Limit cache size
object_render_cache_.erase(object_render_cache_.begin());
}
object_render_cache_.push_back(std::move(cache_entry));
// Get pointer to cached bitmap and queue texture creation
gfx::Bitmap* cached_bitmap = &object_render_cache_.back().rendered_bitmap;
gfx::Arena::Get().QueueTextureCommand(
gfx::Arena::TextureCommandType::CREATE, cached_bitmap);
canvas_.DrawBitmap(*cached_bitmap, canvas_x, canvas_y, 1.0f, 255);
return;
}
}
// Fallback: Draw object as colored rectangle with ID if rendering fails
ImVec4 object_color;
// Color-code objects based on layer
switch (object.layer_) {
case zelda3::RoomObject::LayerType::BG1:
object_color = ImVec4(0.8f, 0.4f, 0.4f, 0.8f); // Red-ish for BG1
break;
case zelda3::RoomObject::LayerType::BG2:
object_color = ImVec4(0.4f, 0.8f, 0.4f, 0.8f); // Green-ish for BG2
break;
case zelda3::RoomObject::LayerType::BG3:
object_color = ImVec4(0.4f, 0.4f, 0.8f, 0.8f); // Blue-ish for BG3
break;
default:
object_color = ImVec4(0.6f, 0.6f, 0.6f, 0.8f); // Gray for unknown
break;
}
// Calculate object size (8x8 is base, size affects width/height)
int object_width = 8 + (object.size_ & 0x0F) * 8;
int object_height = 8 + ((object.size_ >> 4) & 0x0F) * 8;
canvas_.DrawRect(canvas_x, canvas_y, object_width, object_height, object_color);
canvas_.DrawRect(canvas_x, canvas_y, object_width, object_height,
ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); // Black border
// Draw object ID
std::string object_text = absl::StrFormat("0x%X", object.id_);
canvas_.DrawText(object_text, canvas_x + object_width + 2, canvas_y);
}
void DungeonCanvasViewer::DisplayObjectInfo(const zelda3::RoomObject &object,
int canvas_x, int canvas_y) {
@@ -287,37 +196,6 @@ void DungeonCanvasViewer::DisplayObjectInfo(const zelda3::RoomObject &object,
canvas_.DrawText(info_text, canvas_x, canvas_y - 12);
}
void DungeonCanvasViewer::RenderStairObjects(const zelda3::Room& room,
const gfx::SnesPalette& palette) {
// Render stair objects with special highlighting to show they enable layer transitions
// Stair object IDs from room.h: {0x139, 0x138, 0x13B, 0x12E, 0x12D}
constexpr uint16_t stair_ids[] = {0x139, 0x138, 0x13B, 0x12E, 0x12D};
for (const auto& object : room.GetTileObjects()) {
bool is_stair = false;
for (uint16_t stair_id : stair_ids) {
if (object.id_ == stair_id) {
is_stair = true;
break;
}
}
if (is_stair) {
auto [canvas_x, canvas_y] = RoomToCanvasCoordinates(object.x_, object.y_);
if (IsWithinCanvasBounds(canvas_x, canvas_y, 32)) {
// Draw stair object with special highlighting
canvas_.DrawRect(canvas_x - 2, canvas_y - 2, 20, 20,
ImVec4(1.0f, 1.0f, 0.0f, 0.8f)); // Yellow highlight
// Draw text label
std::string stair_text = absl::StrFormat("STAIR\n0x%X", object.id_);
canvas_.DrawText(stair_text, canvas_x + 22, canvas_y);
}
}
}
}
void DungeonCanvasViewer::RenderSprites(const zelda3::Room& room) {
// Render sprites as simple 8x8 squares with sprite name/ID
for (const auto& sprite : room.GetSprites()) {
@@ -364,179 +242,6 @@ void DungeonCanvasViewer::RenderSprites(const zelda3::Room& room) {
}
}
void DungeonCanvasViewer::RenderChests(const zelda3::Room& room) {
// Render chest objects from tile objects - chests are objects with IDs 0xF9, 0xFA
for (const auto& object : room.GetTileObjects()) {
if (object.id_ == 0xF9 || object.id_ == 0xFA) { // Chest object IDs
auto [canvas_x, canvas_y] = RoomToCanvasCoordinates(object.x_, object.y_);
if (IsWithinCanvasBounds(canvas_x, canvas_y, 16)) {
// Determine if it's a big chest based on object ID
bool is_big_chest = (object.id_ == 0xFA);
// Draw chest base
ImVec4 chest_color = is_big_chest ?
ImVec4(0.8f, 0.6f, 0.2f, 0.9f) : // Gold for big chest
ImVec4(0.6f, 0.4f, 0.2f, 0.9f); // Brown for small chest
int chest_size = is_big_chest ? 16 : 8; // Big chests are larger
canvas_.DrawRect(canvas_x, canvas_y + 4, chest_size, 4, chest_color);
// Draw chest lid (slightly lighter)
ImVec4 lid_color = is_big_chest ?
ImVec4(0.9f, 0.7f, 0.3f, 0.9f) :
ImVec4(0.7f, 0.5f, 0.3f, 0.9f);
canvas_.DrawRect(canvas_x, canvas_y + 2, chest_size, 3, lid_color);
// Draw chest borders
canvas_.DrawRect(canvas_x, canvas_y + 2, chest_size, 6, ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
// Draw text label
std::string chest_text = is_big_chest ? "BIG\nCHEST" : "CHEST";
canvas_.DrawText(chest_text, canvas_x + chest_size + 2, canvas_y + 3);
}
}
}
}
void DungeonCanvasViewer::RenderDoorObjects(const zelda3::Room& room) {
// Render door objects from tile objects based on IDs from assembly constants
constexpr uint16_t door_ids[] = {0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E};
for (const auto& object : room.GetTileObjects()) {
bool is_door = false;
for (uint16_t door_id : door_ids) {
if (object.id_ == door_id) {
is_door = true;
break;
}
}
if (is_door) {
auto [canvas_x, canvas_y] = RoomToCanvasCoordinates(object.x_, object.y_);
if (IsWithinCanvasBounds(canvas_x, canvas_y, 16)) {
// Draw door frame
canvas_.DrawRect(canvas_x, canvas_y, 16, 16, ImVec4(0.5f, 0.3f, 0.2f, 0.8f)); // Brown frame
// Draw door opening (darker)
canvas_.DrawRect(canvas_x + 2, canvas_y + 2, 12, 12, ImVec4(0.1f, 0.1f, 0.1f, 0.9f));
// Draw door border
canvas_.DrawRect(canvas_x, canvas_y, 16, 16, ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
// Draw text label
std::string door_text = absl::StrFormat("DOOR\n0x%X", object.id_);
canvas_.DrawText(door_text, canvas_x + 18, canvas_y + 4);
}
}
}
}
void DungeonCanvasViewer::RenderWallObjects(const zelda3::Room& room) {
// Render wall objects with proper dimensions based on properties
for (const auto& object : room.GetTileObjects()) {
if (object.id_ >= 0x10 && object.id_ <= 0x1F) { // Wall objects range
auto [canvas_x, canvas_y] = RoomToCanvasCoordinates(object.x_, object.y_);
if (IsWithinCanvasBounds(canvas_x, canvas_y, 32)) {
// Different wall types based on ID
ImVec4 wall_color;
std::string wall_type;
switch (object.id_) {
case 0x10: // Basic wall
wall_color = ImVec4(0.6f, 0.6f, 0.6f, 0.8f);
wall_type = "WALL";
break;
case 0x11: // Corner wall
wall_color = ImVec4(0.7f, 0.7f, 0.6f, 0.8f);
wall_type = "CORNER";
break;
case 0x12: // Decorative wall
wall_color = ImVec4(0.8f, 0.7f, 0.6f, 0.8f);
wall_type = "DEC_WALL";
break;
default:
wall_color = ImVec4(0.5f, 0.5f, 0.5f, 0.8f);
wall_type = "WALL";
break;
}
// Calculate wall size with proper length handling
int wall_width, wall_height;
// For walls, use the size field to determine length and orientation
if (object.id_ >= 0x10 && object.id_ <= 0x1F) {
uint8_t size_x = object.size_ & 0x0F;
uint8_t size_y = (object.size_ >> 4) & 0x0F;
if (size_x > size_y) {
// Horizontal wall
wall_width = 8 + size_x * 8;
wall_height = 8;
} else if (size_y > size_x) {
// Vertical wall
wall_width = 8;
wall_height = 8 + size_y * 8;
} else {
// Square wall or corner
wall_width = 8 + size_x * 4;
wall_height = 8 + size_y * 4;
}
} else {
// For other objects, use standard size calculation
wall_width = 8 + (object.size_ & 0x0F) * 4;
wall_height = 8 + ((object.size_ >> 4) & 0x0F) * 4;
}
wall_width = std::min(wall_width, 256);
wall_height = std::min(wall_height, 256);
canvas_.DrawRect(canvas_x, canvas_y, wall_width, wall_height, wall_color);
canvas_.DrawRect(canvas_x, canvas_y, wall_width, wall_height, ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
// Add stone block pattern
for (int i = 0; i < wall_width; i += 8) {
for (int j = 0; j < wall_height; j += 8) {
canvas_.DrawRect(canvas_x + i, canvas_y + j, 6, 6,
ImVec4(wall_color.x * 0.9f, wall_color.y * 0.9f, wall_color.z * 0.9f, wall_color.w));
}
}
// Draw text label
std::string wall_text = absl::StrFormat("%s\n0x%X\n%dx%d", wall_type.c_str(), object.id_, wall_width/16, wall_height/16);
canvas_.DrawText(wall_text, canvas_x + wall_width + 2, canvas_y + 4);
}
}
}
}
void DungeonCanvasViewer::RenderPotObjects(const zelda3::Room& room) {
// Render pot objects based on assembly constants - Object_Pot is 0x2F
for (const auto& object : room.GetTileObjects()) {
if (object.id_ == 0x2F || object.id_ == 0x2B) { // Pot objects from assembly
auto [canvas_x, canvas_y] = RoomToCanvasCoordinates(object.x_, object.y_);
if (IsWithinCanvasBounds(canvas_x, canvas_y, 8)) {
// Draw pot base (wider at bottom)
canvas_.DrawRect(canvas_x + 1, canvas_y + 5, 6, 3, ImVec4(0.7f, 0.5f, 0.3f, 0.8f)); // Brown base
// Draw pot middle
canvas_.DrawRect(canvas_x + 2, canvas_y + 3, 4, 3, ImVec4(0.8f, 0.6f, 0.4f, 0.8f)); // Lighter middle
// Draw pot rim
canvas_.DrawRect(canvas_x + 2, canvas_y + 2, 4, 2, ImVec4(0.9f, 0.7f, 0.5f, 0.8f)); // Lightest top
// Draw pot outline
canvas_.DrawRect(canvas_x + 1, canvas_y + 2, 6, 6, ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
// Draw text label
std::string pot_text = absl::StrFormat("POT\n0x%X", object.id_);
canvas_.DrawText(pot_text, canvas_x + 9, canvas_y + 3);
}
}
}
}
// Coordinate conversion helper functions
std::pair<int, int> DungeonCanvasViewer::RoomToCanvasCoordinates(int room_x,
int room_y) const {
@@ -744,9 +449,15 @@ void DungeonCanvasViewer::RenderRoomBackgroundLayers(int room_id) {
// Queue texture creation for background layer 1 via Arena's deferred system
gfx::Arena::Get().QueueTextureCommand(
gfx::Arena::TextureCommandType::CREATE, &bg1_bitmap);
// CRITICAL FIX: Process texture queue immediately to ensure texture is created before drawing
gfx::Arena::Get().ProcessTextureQueue(nullptr);
}
canvas_.DrawBitmap(bg1_bitmap, 0, 0, 1.0f, 255);
// Only draw if texture was successfully created
if (bg1_bitmap.texture()) {
canvas_.DrawBitmap(bg1_bitmap, 0, 0, 1.0f, 255);
}
}
if (bg2_bitmap.is_active() && bg2_bitmap.width() > 0 && bg2_bitmap.height() > 0) {
@@ -754,8 +465,25 @@ void DungeonCanvasViewer::RenderRoomBackgroundLayers(int room_id) {
// Queue texture creation for background layer 2 via Arena's deferred system
gfx::Arena::Get().QueueTextureCommand(
gfx::Arena::TextureCommandType::CREATE, &bg2_bitmap);
// CRITICAL FIX: Process texture queue immediately to ensure texture is created before drawing
gfx::Arena::Get().ProcessTextureQueue(nullptr);
}
canvas_.DrawBitmap(bg2_bitmap, 0, 0, 1.0f, 200);
// Only draw if texture was successfully created
if (bg2_bitmap.texture()) {
canvas_.DrawBitmap(bg2_bitmap, 0, 0, 1.0f, 200);
}
}
// DEBUG: Check if background buffers have content
if (bg1_bitmap.is_active() && bg1_bitmap.width() > 0) {
printf("[RenderRoomBackgroundLayers] BG1 bitmap: %dx%d, active=%d\n",
bg1_bitmap.width(), bg1_bitmap.height(), bg1_bitmap.is_active());
}
if (bg2_bitmap.is_active() && bg2_bitmap.width() > 0) {
printf("[RenderRoomBackgroundLayers] BG2 bitmap: %dx%d, active=%d\n",
bg2_bitmap.width(), bg2_bitmap.height(), bg2_bitmap.is_active());
}
// TEST: Draw a bright red rectangle to verify canvas drawing works

View File

@@ -65,17 +65,9 @@ class DungeonCanvasViewer {
}
private:
void RenderObjectInCanvas(const zelda3::RoomObject &object,
const gfx::SnesPalette &palette);
void DisplayObjectInfo(const zelda3::RoomObject &object, int canvas_x,
int canvas_y);
void RenderStairObjects(const zelda3::Room& room,
const gfx::SnesPalette& palette);
void RenderSprites(const zelda3::Room& room);
void RenderChests(const zelda3::Room& room);
void RenderDoorObjects(const zelda3::Room& room);
void RenderWallObjects(const zelda3::Room& room);
void RenderPotObjects(const zelda3::Room& room);
// Coordinate conversion helpers
std::pair<int, int> RoomToCanvasCoordinates(int room_x, int room_y) const;

View File

@@ -151,12 +151,6 @@ absl::Status DungeonEditor::Load() {
// Set up component callbacks
object_interaction_.SetCurrentRoom(&rooms_, current_room_id_);
object_interaction_.SetObjectPlacedCallback([this](const zelda3::RoomObject& object) {
renderer_.ClearObjectCache();
});
object_interaction_.SetCacheInvalidationCallback([this]() {
renderer_.ClearObjectCache();
});
// Set up toolset callbacks
toolset_.SetUndoCallback([this]() { PRINT_IF_ERROR(Undo()); });
@@ -492,10 +486,6 @@ void DungeonEditor::DrawRoomPropertiesDebugPopup() {
if (ImGui::Button("Reload Objects")) {
room.LoadObjects();
}
ImGui::SameLine();
if (ImGui::Button("Clear Cache")) {
renderer_.ClearObjectCache();
}
ImGui::SameLine();
if (ImGui::Button("Close")) {
ImGui::CloseCurrentPopup();
@@ -609,14 +599,6 @@ void DungeonEditor::DrawDungeonCanvas(int room_id) {
static bool show_layout_objects = false;
ImGui::Checkbox("Show Layout Objects", &show_layout_objects);
if (ImGui::Button("Clear Object Cache")) {
renderer_.ClearObjectCache();
}
ImGui::SameLine();
ImGui::Text("Cache: %zu objects", renderer_.GetCacheSize());
// Object statistics and metadata
ImGui::Separator();
ImGui::Text("Room Statistics:");
@@ -630,7 +612,6 @@ void DungeonEditor::DrawDungeonCanvas(int room_id) {
// Palette information
ImGui::Text("Current Palette Group: %llu",
static_cast<unsigned long long>(current_palette_group_id_));
ImGui::Text("Cache Size: %zu objects", renderer_.GetCacheSize());
// Object type breakdown
ImGui::Separator();
@@ -755,9 +736,6 @@ void DungeonEditor::DrawDungeonCanvas(int room_id) {
canvas_.DrawBitmap(bg2_bitmap, 0, 0, 1.0f, 200);
}
// Render sprites as simple 16x16 squares with labels
// (Sprites are not part of the background buffers)
renderer_.RenderSprites(rooms_[room_id]);
}
// Phase 5: Render with integrated object editor

View File

@@ -32,7 +32,6 @@
#include "dungeon_object_selector.h"
#include "dungeon_toolset.h"
#include "dungeon_object_interaction.h"
#include "dungeon_renderer.h"
#include "dungeon_room_loader.h"
#include "dungeon_usage_tracker.h"
@@ -65,7 +64,7 @@ class DungeonEditor : public Editor {
explicit DungeonEditor(Rom* rom = nullptr)
: rom_(rom), object_renderer_(rom), preview_object_(0, 0, 0, 0, 0),
room_selector_(rom), canvas_viewer_(rom), object_selector_(rom),
object_interaction_(&canvas_), renderer_(&canvas_, rom), room_loader_(rom) {
object_interaction_(&canvas_), room_loader_(rom) {
type_ = EditorType::kDungeon;
// Initialize the new dungeon editor system
if (rom) {
@@ -197,7 +196,6 @@ class DungeonEditor : public Editor {
// Refactored components
DungeonToolset toolset_;
DungeonObjectInteraction object_interaction_;
DungeonRenderer renderer_;
DungeonRoomLoader room_loader_;
DungeonUsageTracker usage_tracker_;

View File

@@ -142,11 +142,6 @@ absl::Status DungeonEditorV2::Load() {
// Initialize unified object editor card
object_editor_card_ = std::make_unique<ObjectEditorCard>(renderer_, rom_, &canvas_viewer_);
// Initialize manual renderer for debugging (uses canvas from canvas_viewer_)
manual_renderer_ = std::make_unique<ManualObjectRenderer>(
&canvas_viewer_.canvas(), rom_);
printf("[DungeonEditorV2] Manual renderer initialized for debugging\n");
// Wire palette changes to trigger room re-renders
palette_editor_.SetOnPaletteChanged([this](int /*palette_id*/) {
// Re-render all active rooms when palette changes
@@ -422,9 +417,11 @@ void DungeonEditorV2::DrawRoomTab(int room_id) {
return;
}
auto& room = rooms_[room_id];
// Lazy load room data
if (!rooms_[room_id].IsLoaded()) {
auto status = room_loader_.LoadRoom(room_id, rooms_[room_id]);
if (!room.IsLoaded()) {
auto status = room_loader_.LoadRoom(room_id, room);
if (!status.ok()) {
ImGui::TextColored(ImVec4(1, 0, 0, 1), "Failed to load room: %s",
status.message().data());
@@ -432,10 +429,31 @@ void DungeonEditorV2::DrawRoomTab(int room_id) {
}
}
// Initialize room graphics and objects if not already done
// This ensures objects are drawn to background buffers before canvas displays them
if (room.IsLoaded()) {
// Load room graphics (populates blocks, gfx sheets)
if (room.blocks().empty()) {
room.RenderRoomGraphics();
}
// Load room objects (populates tile_objects_)
if (room.GetTileObjects().empty()) {
room.LoadObjects();
}
// Render objects to background buffers (CRITICAL: this must happen before canvas drawing)
// This uses ObjectDrawer to draw all objects into bg1_buffer_ and bg2_buffer_
auto& bg1_bitmap = room.bg1_buffer().bitmap();
if (!bg1_bitmap.is_active() || bg1_bitmap.width() == 0) {
room.RenderObjectsToBackground();
}
}
// Room info header
ImGui::Text("Room %03X", room_id);
ImGui::SameLine();
if (rooms_[room_id].IsLoaded()) {
if (room.IsLoaded()) {
ImGui::TextColored(ImVec4(0.4f, 0.8f, 0.4f, 1.0f), ICON_MD_CHECK " Loaded");
} else {
ImGui::TextColored(ImVec4(0.8f, 0.4f, 0.4f, 1.0f), ICON_MD_PENDING " Not Loaded");
@@ -840,6 +858,11 @@ void DungeonEditorV2::DrawRoomGraphicsCard() {
blocks = room.blocks();
}
// Only render room graphics if ROM is properly loaded
if (room.rom() && room.rom()->is_loaded()) {
room.RenderRoomGraphics();
}
int current_block = 0;
constexpr int max_blocks_per_row = 2;
constexpr int block_width = 128;
@@ -852,6 +875,13 @@ void DungeonEditorV2::DrawRoomGraphicsCard() {
if (block < static_cast<int>(gfx::Arena::Get().gfx_sheets().size())) {
auto& gfx_sheet = gfx::Arena::Get().gfx_sheets()[block];
// Create texture if it doesn't exist
if (!gfx_sheet.texture() && gfx_sheet.is_active() && gfx_sheet.width() > 0) {
gfx::Arena::Get().QueueTextureCommand(
gfx::Arena::TextureCommandType::CREATE, &gfx_sheet);
gfx::Arena::Get().ProcessTextureQueue(nullptr);
}
// Calculate grid position
int row = current_block / max_blocks_per_row;
int col = current_block % max_blocks_per_row;
@@ -865,6 +895,16 @@ void DungeonEditorV2::DrawRoomGraphicsCard() {
(ImTextureID)(intptr_t)gfx_sheet.texture(),
ImVec2(x, y),
ImVec2(x + block_width, y + block_height));
} else {
// Draw placeholder for missing graphics
room_gfx_canvas.draw_list()->AddRectFilled(
ImVec2(x, y),
ImVec2(x + block_width, y + block_height),
IM_COL32(64, 64, 64, 255));
room_gfx_canvas.draw_list()->AddText(
ImVec2(x + 10, y + 10),
IM_COL32(255, 255, 255, 255),
"No Graphics");
}
}
current_block++;

View File

@@ -14,7 +14,6 @@
#include "dungeon_object_selector.h"
#include "dungeon_room_loader.h"
#include "object_editor_card.h"
#include "manual_object_renderer.h"
#include "app/gui/editor_card_manager.h"
#include "app/zelda3/dungeon/room.h"
#include "app/zelda3/dungeon/room_entrance.h"
@@ -146,7 +145,6 @@ class DungeonEditorV2 : public Editor {
gui::DungeonObjectEmulatorPreview object_emulator_preview_;
gui::PaletteEditorWidget palette_editor_;
std::unique_ptr<ObjectEditorCard> object_editor_card_; // Unified object editor
std::unique_ptr<ManualObjectRenderer> manual_renderer_; // Debugging renderer
bool is_loaded_ = false;

View File

@@ -1,208 +0,0 @@
#include "dungeon_renderer.h"
#include "absl/strings/str_format.h"
#include "app/gfx/arena.h"
namespace yaze::editor {
void DungeonRenderer::RenderObjectInCanvas(const zelda3::RoomObject& object,
const gfx::SnesPalette& palette) {
// Validate ROM is loaded
if (!rom_ || !rom_->is_loaded()) {
return;
}
// Convert room coordinates to canvas coordinates
auto [canvas_x, canvas_y] = RoomToCanvasCoordinates(object.x_, object.y_);
// Check if object is within canvas bounds
if (!IsWithinCanvasBounds(canvas_x, canvas_y, 32)) {
return; // Skip objects outside visible area
}
// Calculate palette hash for caching
uint64_t palette_hash = 0;
for (size_t i = 0; i < palette.size() && i < 16; ++i) {
palette_hash ^= std::hash<uint16_t>{}(palette[i].snes()) + 0x9e3779b9 +
(palette_hash << 6) + (palette_hash >> 2);
}
// Check cache first
for (auto& cached : object_render_cache_) {
if (cached.object_id == object.id_ && cached.object_x == object.x_ &&
cached.object_y == object.y_ && cached.object_size == object.size_ &&
cached.palette_hash == palette_hash && cached.is_valid) {
canvas_->DrawBitmap(cached.rendered_bitmap, canvas_x, canvas_y, 1.0f, 255);
return;
}
}
// Create a mutable copy of the object to ensure tiles are loaded
auto mutable_object = object;
mutable_object.set_rom(rom_);
mutable_object.EnsureTilesLoaded();
// Try to render the object with proper graphics
auto render_result = object_renderer_.RenderObject(mutable_object, palette);
if (render_result.ok()) {
auto object_bitmap = std::move(render_result.value());
// Ensure the bitmap is valid and has meaningful content
if (object_bitmap.width() > 0 && object_bitmap.height() > 0 &&
object_bitmap.data() != nullptr) {
object_bitmap.SetPalette(palette);
// Queue texture creation for the object bitmap via Arena's deferred system
gfx::Arena::Get().QueueTextureCommand(
gfx::Arena::TextureCommandType::CREATE, &object_bitmap);
canvas_->DrawBitmap(object_bitmap, canvas_x, canvas_y, 1.0f, 255);
// Cache the successfully rendered bitmap
ObjectRenderCache cache_entry;
cache_entry.object_id = object.id_;
cache_entry.object_x = object.x_;
cache_entry.object_y = object.y_;
cache_entry.object_size = object.size_;
cache_entry.palette_hash = palette_hash;
cache_entry.rendered_bitmap = object_bitmap;
cache_entry.is_valid = true;
// Add to cache (limit cache size)
if (object_render_cache_.size() >= 100) {
object_render_cache_.erase(object_render_cache_.begin());
}
object_render_cache_.push_back(std::move(cache_entry));
return;
}
}
// Fallback: Draw object as colored rectangle with ID if rendering fails
ImVec4 object_color;
// Color-code objects based on layer for better identification
switch (object.layer_) {
case zelda3::RoomObject::LayerType::BG1:
object_color = ImVec4(0.8f, 0.4f, 0.4f, 0.8f); // Red-ish for BG1
break;
case zelda3::RoomObject::LayerType::BG2:
object_color = ImVec4(0.4f, 0.8f, 0.4f, 0.8f); // Green-ish for BG2
break;
case zelda3::RoomObject::LayerType::BG3:
object_color = ImVec4(0.4f, 0.4f, 0.8f, 0.8f); // Blue-ish for BG3
break;
default:
object_color = ImVec4(0.6f, 0.6f, 0.6f, 0.8f); // Gray for unknown
break;
}
// Calculate object size (16x16 is base, size affects width/height)
int object_width = 16 + (object.size_ & 0x0F) * 8;
int object_height = 16 + ((object.size_ >> 4) & 0x0F) * 8;
canvas_->DrawRect(canvas_x, canvas_y, object_width, object_height, object_color);
canvas_->DrawRect(canvas_x, canvas_y, object_width, object_height,
ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); // Black border
}
void DungeonRenderer::DisplayObjectInfo(const zelda3::RoomObject& object,
int canvas_x, int canvas_y) {
std::string info_text = absl::StrFormat("ID:%d X:%d Y:%d S:%d", object.id_,
object.x_, object.y_, object.size_);
canvas_->DrawText(info_text, canvas_x, canvas_y - 12);
}
void DungeonRenderer::RenderSprites(const zelda3::Room& room) {
// Render sprites as simple 16x16 squares with sprite name/ID
for (const auto& sprite : room.GetSprites()) {
auto [canvas_x, canvas_y] = RoomToCanvasCoordinates(sprite.x(), sprite.y());
if (IsWithinCanvasBounds(canvas_x, canvas_y, 16)) {
// Draw 16x16 square for sprite
ImVec4 sprite_color;
// Color-code sprites based on layer for identification
if (sprite.layer() == 0) {
sprite_color = ImVec4(0.2f, 0.8f, 0.2f, 0.8f); // Green for layer 0
} else {
sprite_color = ImVec4(0.2f, 0.2f, 0.8f, 0.8f); // Blue for layer 1
}
canvas_->DrawRect(canvas_x, canvas_y, 16, 16, sprite_color);
canvas_->DrawRect(canvas_x, canvas_y, 16, 16, ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); // Border
}
}
}
void DungeonRenderer::RenderRoomBackgroundLayers(int room_id) {
// Get canvas dimensions to limit rendering
int canvas_width = canvas_->width();
int canvas_height = canvas_->height();
// Validate canvas dimensions
if (canvas_width <= 0 || canvas_height <= 0) {
return;
}
// BG1 (background layer 1) - main room graphics
auto& bg1_bitmap = gfx::Arena::Get().bg1().bitmap();
if (bg1_bitmap.is_active() && bg1_bitmap.width() > 0 &&
bg1_bitmap.height() > 0) {
float scale_x = static_cast<float>(canvas_width) / bg1_bitmap.width();
float scale_y = static_cast<float>(canvas_height) / bg1_bitmap.height();
float scale = std::min(scale_x, scale_y);
int scaled_width = static_cast<int>(bg1_bitmap.width() * scale);
int scaled_height = static_cast<int>(bg1_bitmap.height() * scale);
int offset_x = (canvas_width - scaled_width) / 2;
int offset_y = (canvas_height - scaled_height) / 2;
canvas_->DrawBitmap(bg1_bitmap, offset_x, offset_y, scale, 255);
}
// BG2 (background layer 2) - sprite graphics (overlay)
auto& bg2_bitmap = gfx::Arena::Get().bg2().bitmap();
if (bg2_bitmap.is_active() && bg2_bitmap.width() > 0 &&
bg2_bitmap.height() > 0) {
float scale_x = static_cast<float>(canvas_width) / bg2_bitmap.width();
float scale_y = static_cast<float>(canvas_height) / bg2_bitmap.height();
float scale = std::min(scale_x, scale_y);
int scaled_width = static_cast<int>(bg2_bitmap.width() * scale);
int scaled_height = static_cast<int>(bg2_bitmap.height() * scale);
int offset_x = (canvas_width - scaled_width) / 2;
int offset_y = (canvas_height - scaled_height) / 2;
canvas_->DrawBitmap(bg2_bitmap, offset_x, offset_y, scale, 200);
}
}
absl::Status DungeonRenderer::RefreshGraphics(int room_id, uint64_t palette_id,
const gfx::PaletteGroup& palette_group) {
if (!rom_ || !rom_->is_loaded()) {
return absl::FailedPreconditionError("ROM not loaded");
}
// This would need access to room data - will be called from main editor
return absl::OkStatus();
}
std::pair<int, int> DungeonRenderer::RoomToCanvasCoordinates(int room_x, int room_y) const {
// Dungeon tiles are 8x8 pixels, convert room coordinates (tiles) to pixels
return {room_x * 8, room_y * 8};
}
std::pair<int, int> DungeonRenderer::CanvasToRoomCoordinates(int canvas_x, int canvas_y) const {
// Convert canvas pixels back to room coordinates (tiles)
return {canvas_x / 8, canvas_y / 8};
}
bool DungeonRenderer::IsWithinCanvasBounds(int canvas_x, int canvas_y, int margin) const {
auto canvas_size = canvas_->canvas_size();
auto global_scale = canvas_->global_scale();
int scaled_width = static_cast<int>(canvas_size.x * global_scale);
int scaled_height = static_cast<int>(canvas_size.y * global_scale);
return (canvas_x >= -margin && canvas_y >= -margin &&
canvas_x <= scaled_width + margin &&
canvas_y <= scaled_height + margin);
}
} // namespace yaze::editor

View File

@@ -1,75 +0,0 @@
#ifndef YAZE_APP_EDITOR_DUNGEON_DUNGEON_RENDERER_H
#define YAZE_APP_EDITOR_DUNGEON_DUNGEON_RENDERER_H
#include <vector>
#include "absl/status/status.h"
#include "app/gfx/snes_palette.h"
#include "app/gui/canvas.h"
#include "app/rom.h"
#include "app/zelda3/dungeon/object_renderer.h"
#include "app/zelda3/dungeon/room.h"
#include "app/zelda3/dungeon/room_layout.h"
#include "app/zelda3/dungeon/room_object.h"
namespace yaze {
namespace editor {
/**
* @brief Handles rendering of dungeon objects, layouts, and backgrounds
*
* This component manages all rendering operations for the dungeon editor,
* including object caching, background layers, and layout visualization.
*/
class DungeonRenderer {
public:
explicit DungeonRenderer(gui::Canvas* canvas, Rom* rom)
: canvas_(canvas), rom_(rom), object_renderer_(rom) {}
// Object rendering
void RenderObjectInCanvas(const zelda3::RoomObject& object,
const gfx::SnesPalette& palette);
void DisplayObjectInfo(const zelda3::RoomObject& object, int canvas_x, int canvas_y);
void RenderSprites(const zelda3::Room& room);
// Background rendering
void RenderRoomBackgroundLayers(int room_id);
absl::Status RefreshGraphics(int room_id, uint64_t palette_id,
const gfx::PaletteGroup& palette_group);
// Graphics management
absl::Status LoadAndRenderRoomGraphics(int room_id,
std::array<zelda3::Room, 0x128>& rooms);
absl::Status ReloadAllRoomGraphics(std::array<zelda3::Room, 0x128>& rooms);
// Cache management
void ClearObjectCache() { object_render_cache_.clear(); }
size_t GetCacheSize() const { return object_render_cache_.size(); }
// Coordinate conversion helpers
std::pair<int, int> RoomToCanvasCoordinates(int room_x, int room_y) const;
std::pair<int, int> CanvasToRoomCoordinates(int canvas_x, int canvas_y) const;
bool IsWithinCanvasBounds(int canvas_x, int canvas_y, int margin = 32) const;
private:
gui::Canvas* canvas_;
Rom* rom_;
zelda3::ObjectRenderer object_renderer_;
// Object rendering cache
struct ObjectRenderCache {
int object_id;
int object_x, object_y, object_size;
uint64_t palette_hash;
gfx::Bitmap rendered_bitmap;
bool is_valid;
};
std::vector<ObjectRenderCache> object_render_cache_;
uint64_t last_palette_hash_ = 0;
};
} // namespace editor
} // namespace yaze
#endif // YAZE_APP_EDITOR_DUNGEON_DUNGEON_RENDERER_H

View File

@@ -1,170 +0,0 @@
#include "manual_object_renderer.h"
#include <cstdio>
#include <cstring>
#include "app/gfx/arena.h"
#include "app/gfx/bitmap.h"
namespace yaze {
namespace editor {
ManualObjectRenderer::ManualObjectRenderer(gui::Canvas* canvas, Rom* rom)
: canvas_(canvas), rom_(rom) {}
absl::Status ManualObjectRenderer::RenderSimpleBlock(uint16_t object_id, int x, int y,
const gfx::SnesPalette& palette) {
if (!canvas_ || !rom_) {
return absl::InvalidArgumentError("Canvas or ROM not initialized");
}
printf("[ManualRenderer] Rendering object 0x%04X at (%d, %d)\n", object_id, x, y);
// Create a simple 16x16 tile manually
auto tile_bitmap = CreateSimpleTile(object_id, palette);
if (!tile_bitmap) {
return absl::InternalError("Failed to create simple tile");
}
// Draw directly to canvas
canvas_->DrawBitmap(*tile_bitmap, x, y, 1.0f, 255);
return absl::OkStatus();
}
void ManualObjectRenderer::RenderTestPattern(int x, int y, int width, int height,
uint8_t color_index) {
if (!canvas_) return;
printf("[ManualRenderer] Drawing test pattern: %dx%d at (%d,%d) color=%d\n",
width, height, x, y, color_index);
// Create a simple colored rectangle using ImGui
ImVec4 color = ImVec4(
(color_index & 0x01) ? 1.0f : 0.0f, // Red bit
(color_index & 0x02) ? 1.0f : 0.0f, // Green bit
(color_index & 0x04) ? 1.0f : 0.0f, // Blue bit
1.0f // Alpha
);
// Draw using ImGui primitives for testing
ImDrawList* draw_list = ImGui::GetWindowDrawList();
if (draw_list) {
ImVec2 p1 = ImVec2(x, y);
ImVec2 p2 = ImVec2(x + width, y + height);
draw_list->AddRectFilled(p1, p2, ImGui::ColorConvertFloat4ToU32(color));
}
}
void ManualObjectRenderer::DebugGraphicsSheet(int sheet_index) {
if (!rom_ || sheet_index < 0 || sheet_index >= 223) {
printf("[ManualRenderer] Invalid sheet index: %d\n", sheet_index);
return;
}
auto& arena = gfx::Arena::Get();
const auto& sheet = arena.gfx_sheet(sheet_index);
printf("[ManualRenderer] Graphics Sheet %d Debug Info:\n", sheet_index);
printf(" - Is Active: %s\n", sheet.is_active() ? "YES" : "NO");
printf(" - Width: %d\n", sheet.width());
printf(" - Height: %d\n", sheet.height());
printf(" - Has Surface: %s\n", sheet.surface() ? "YES" : "NO");
printf(" - Has Texture: %s\n", sheet.texture() ? "YES" : "NO");
if (sheet.is_active() && sheet.width() > 0 && sheet.height() > 0) {
printf(" - Format: %s\n", sheet.surface() && sheet.surface()->format ?
SDL_GetPixelFormatName(sheet.surface()->format->format) : "Unknown");
}
}
void ManualObjectRenderer::TestPaletteRendering(int x, int y) {
printf("[ManualRenderer] Testing palette rendering at (%d, %d)\n", x, y);
// Draw test squares with different color indices
for (int i = 0; i < 8; i++) {
int test_x = x + (i * 20);
int test_y = y;
RenderTestPattern(test_x, test_y, 16, 16, i);
}
}
std::unique_ptr<gfx::Bitmap> ManualObjectRenderer::CreateSimpleTile(uint16_t tile_id,
const gfx::SnesPalette& palette) {
// Fill with a simple pattern based on tile_id
uint8_t base_color = tile_id & 0x07; // Use lower 3 bits for color
// Create tile data manually
auto tile_data = CreateSolidTile(base_color);
if (tile_data.empty()) {
printf("[ManualRenderer] Failed to create tile data\n");
return nullptr;
}
// Create a 16x16 bitmap with the tile data
auto bitmap = std::make_unique<gfx::Bitmap>();
bitmap->Create(16, 16, 8, tile_data); // 16x16 pixels, 8-bit depth
if (!bitmap->is_active()) {
printf("[ManualRenderer] Failed to create bitmap\n");
return nullptr;
}
// Apply palette
bitmap->SetPalette(palette);
printf("[ManualRenderer] Created simple tile: ID=0x%04X, color=%d\n", tile_id, base_color);
return bitmap;
}
std::vector<uint8_t> ManualObjectRenderer::CreateSolidTile(uint8_t color_index) {
std::vector<uint8_t> tile_data(16 * 16, color_index);
return tile_data;
}
std::vector<uint8_t> ManualObjectRenderer::CreatePatternTile(uint8_t pattern_type,
uint8_t color_index) {
std::vector<uint8_t> tile_data(16 * 16);
switch (pattern_type) {
case 0: // Solid
std::fill(tile_data.begin(), tile_data.end(), color_index);
break;
case 1: // Checkerboard
for (int y = 0; y < 16; y++) {
for (int x = 0; x < 16; x++) {
tile_data[y * 16 + x] = ((x + y) % 2) ? color_index : 0;
}
}
break;
case 2: // Horizontal stripes
for (int y = 0; y < 16; y++) {
uint8_t color = (y % 4 < 2) ? color_index : 0;
for (int x = 0; x < 16; x++) {
tile_data[y * 16 + x] = color;
}
}
break;
case 3: // Vertical stripes
for (int x = 0; x < 16; x++) {
uint8_t color = (x % 4 < 2) ? color_index : 0;
for (int y = 0; y < 16; y++) {
tile_data[y * 16 + x] = color;
}
}
break;
default:
std::fill(tile_data.begin(), tile_data.end(), color_index);
break;
}
return tile_data;
}
} // namespace editor
} // namespace yaze

View File

@@ -1,90 +0,0 @@
#ifndef YAZE_APP_EDITOR_DUNGEON_MANUAL_OBJECT_RENDERER_H
#define YAZE_APP_EDITOR_DUNGEON_MANUAL_OBJECT_RENDERER_H
#include <vector>
#include <memory>
#include "app/gfx/snes_palette.h"
#include "app/gui/canvas.h"
#include "app/rom.h"
#include "app/zelda3/dungeon/room_object.h"
namespace yaze {
namespace editor {
/**
* @brief Manual object renderer for debugging and testing basic object rendering
*
* This class provides simple, manual rendering of basic dungeon objects
* to help debug the graphics pipeline and understand object data structures.
*
* Features:
* - Manual tile creation for simple objects
* - Direct graphics sheet access
* - Palette debugging and testing
* - Simple pattern rendering (solid blocks, lines, etc.)
*/
class ManualObjectRenderer {
public:
explicit ManualObjectRenderer(gui::Canvas* canvas, Rom* rom);
/**
* @brief Render a simple solid block object manually
* @param object_id Object ID to render
* @param x X position in pixels
* @param y Y position in pixels
* @param palette Current palette to use
* @return Status of the rendering operation
*/
absl::Status RenderSimpleBlock(uint16_t object_id, int x, int y,
const gfx::SnesPalette& palette);
/**
* @brief Render a test pattern to verify graphics pipeline
* @param x X position in pixels
* @param y Y position in pixels
* @param width Width in pixels
* @param height Height in pixels
* @param color_index Color index to use
*/
void RenderTestPattern(int x, int y, int width, int height, uint8_t color_index);
/**
* @brief Debug graphics sheet loading and display info
* @param sheet_index Graphics sheet index to examine
*/
void DebugGraphicsSheet(int sheet_index);
/**
* @brief Test palette rendering with different colors
* @param x X position in pixels
* @param y Y position in pixels
*/
void TestPaletteRendering(int x, int y);
/**
* @brief Create a simple 16x16 tile manually
* @param tile_id Tile ID to create
* @param palette Palette to apply
* @return Created bitmap
*/
std::unique_ptr<gfx::Bitmap> CreateSimpleTile(uint16_t tile_id,
const gfx::SnesPalette& palette);
private:
gui::Canvas* canvas_;
Rom* rom_;
// Simple tile creation helpers
std::vector<uint8_t> CreateSolidTile(uint8_t color_index);
std::vector<uint8_t> CreatePatternTile(uint8_t pattern_type, uint8_t color_index);
// Graphics debugging
void LogGraphicsInfo(int sheet_index);
void LogPaletteInfo(const gfx::SnesPalette& palette);
};
} // namespace editor
} // namespace yaze
#endif // YAZE_APP_EDITOR_DUNGEON_MANUAL_OBJECT_RENDERER_H

View File

@@ -14,7 +14,6 @@ set(
app/editor/dungeon/dungeon_object_selector.cc
app/editor/dungeon/dungeon_toolset.cc
app/editor/dungeon/dungeon_object_interaction.cc
app/editor/dungeon/dungeon_renderer.cc
app/editor/dungeon/dungeon_room_loader.cc
app/editor/dungeon/dungeon_usage_tracker.cc
app/editor/dungeon/object_editor_card.cc
@@ -43,7 +42,6 @@ set(
app/editor/system/popup_manager.cc
app/editor/system/proposal_drawer.cc
app/editor/agent/agent_chat_history_codec.cc
app/editor/dungeon/manual_object_renderer.cc
)
if(YAZE_WITH_GRPC)

View File

@@ -8,63 +8,56 @@
namespace yaze {
namespace zelda3 {
ObjectDrawer::ObjectDrawer(Rom* rom) : rom_(rom) {}
ObjectDrawer::ObjectDrawer(Rom* rom) : rom_(rom) {
InitializeDrawRoutines();
}
absl::Status ObjectDrawer::DrawObject(const RoomObject& object,
gfx::BackgroundBuffer& bg1,
gfx::BackgroundBuffer& bg2) {
gfx::BackgroundBuffer& bg2,
const gfx::PaletteGroup& palette_group) {
if (!rom_ || !rom_->is_loaded()) {
return absl::FailedPreconditionError("ROM not loaded");
}
if (!routines_initialized_) {
return absl::FailedPreconditionError("Draw routines not initialized");
}
// Ensure object has tiles loaded
auto mutable_obj = const_cast<RoomObject&>(object);
printf("[DrawObject] Setting ROM for object ID=0x%02X\n", object.id_);
mutable_obj.set_rom(rom_);
printf("[DrawObject] Calling EnsureTilesLoaded for object ID=0x%02X\n", object.id_);
mutable_obj.EnsureTilesLoaded();
// Get tiles - silently skip objects that can't load tiles
if (object.tiles().empty()) {
// Many objects may not have tiles loaded yet - this is normal
// Just skip them rather than failing the whole draw operation
return absl::OkStatus();
}
const auto& tile = object.tiles()[0]; // Base tile for object
// Check if tiles were actually loaded on the mutable object
printf("[DrawObject] After EnsureTilesLoaded: mutable object has %zu tiles\n",
mutable_obj.tiles().size());
// Select buffer based on layer
auto& target_bg = (object.layer_ == RoomObject::LayerType::BG2) ? bg2 : bg1;
// Dispatch to pattern-specific drawing based on object ID
// This is reverse-engineered from the game's drawing routines
// Skip objects that don't have tiles loaded - check mutable object
if (mutable_obj.tiles().empty()) {
printf("[DrawObject] Object ID=0x%02X has no tiles loaded, skipping\n", object.id_);
return absl::OkStatus();
}
printf("[DrawObject] Object ID=0x%02X has %zu tiles, proceeding with drawing\n",
object.id_, mutable_obj.tiles().size());
// Look up draw routine for this object
int routine_id = GetDrawRoutineId(object.id_);
if (object.id_ == 0x34) {
// Object 0x34: 1x1 solid block (simplest)
Draw1x1Solid(object, target_bg, tile);
}
else if (object.id_ >= 0x00 && object.id_ <= 0x08) {
// Objects 0x00-0x08: Rightward 2x2 patterns
DrawRightwards2x2(object, target_bg, tile);
}
else if (object.id_ >= 0x60 && object.id_ <= 0x68) {
// Objects 0x60-0x68: Downward 2x2 patterns
DrawDownwards2x2(object, target_bg, tile);
}
else if (object.id_ >= 0x09 && object.id_ <= 0x14) {
// Objects 0x09-0x14: Diagonal acute patterns
DrawDiagonalAcute(object, target_bg, tile);
}
else if (object.id_ >= 0x15 && object.id_ <= 0x20) {
// Objects 0x15-0x20: Diagonal grave patterns
DrawDiagonalGrave(object, target_bg, tile);
}
else if (object.id_ == 0x33 || (object.id_ >= 0x70 && object.id_ <= 0x71)) {
// 4x4 block objects
Draw4x4Block(object, target_bg, tile);
}
else {
// Default: Draw as simple 1x1 at position
Draw1x1Solid(object, target_bg, tile);
if (routine_id < 0 || routine_id >= static_cast<int>(draw_routines_.size())) {
// Fallback to simple 1x1 drawing
WriteTile16(target_bg, object.x_, object.y_, mutable_obj.tiles()[0]);
return absl::OkStatus();
}
// Execute the appropriate draw routine
draw_routines_[routine_id](this, object, target_bg, mutable_obj.tiles());
return absl::OkStatus();
}
@@ -72,13 +65,16 @@ absl::Status ObjectDrawer::DrawObject(const RoomObject& object,
absl::Status ObjectDrawer::DrawObjectList(
const std::vector<RoomObject>& objects,
gfx::BackgroundBuffer& bg1,
gfx::BackgroundBuffer& bg2) {
gfx::BackgroundBuffer& bg2,
const gfx::PaletteGroup& palette_group) {
printf("[DrawObjectList] Drawing %zu objects\n", objects.size());
int drawn_count = 0;
int skipped_count = 0;
for (const auto& object : objects) {
auto status = DrawObject(object, bg1, bg2);
auto status = DrawObject(object, bg1, bg2, palette_group);
if (status.ok()) {
drawn_count++;
} else {
@@ -90,7 +86,8 @@ absl::Status ObjectDrawer::DrawObjectList(
}
}
if (drawn_count > 0 || skipped_count > 0) {
// Only log if there are failures
if (skipped_count > 0) {
printf("[ObjectDrawer] Drew %d objects, skipped %d\n", drawn_count, skipped_count);
}
@@ -98,85 +95,474 @@ absl::Status ObjectDrawer::DrawObjectList(
}
// ============================================================================
// Pattern Drawing Implementations
// Draw Routine Registry Initialization
// ============================================================================
void ObjectDrawer::Draw1x1Solid(const RoomObject& obj,
gfx::BackgroundBuffer& bg,
const gfx::Tile16& tile) {
// Simple 1x1 tile placement
WriteTile16(bg, obj.x_, obj.y_, tile);
void ObjectDrawer::InitializeDrawRoutines() {
// Initialize draw routine registry based on ZScream's subtype1_routines table
// This maps object IDs to draw routine indices (0-24)
// Routine 0: RoomDraw_Rightwards2x2_1to15or32
for (int id = 0x00; id <= 0x00; id++) {
object_to_routine_map_[id] = 0;
}
// Routine 1: RoomDraw_Rightwards2x4_1to15or26
for (int id = 0x01; id <= 0x02; id++) {
object_to_routine_map_[id] = 1;
}
// Routine 2: RoomDraw_Rightwards2x4spaced4_1to16
for (int id = 0x03; id <= 0x04; id++) {
object_to_routine_map_[id] = 2;
}
// Routine 3: RoomDraw_Rightwards2x4spaced4_1to16_BothBG
for (int id = 0x05; id <= 0x06; id++) {
object_to_routine_map_[id] = 3;
}
// Routine 4: RoomDraw_Rightwards2x2_1to16
for (int id = 0x07; id <= 0x08; id++) {
object_to_routine_map_[id] = 4;
}
// Routine 5: RoomDraw_DiagonalAcute_1to16
for (int id = 0x09; id <= 0x09; id++) {
object_to_routine_map_[id] = 5;
}
// Routine 6: RoomDraw_DiagonalGrave_1to16
for (int id = 0x0A; id <= 0x0B; id++) {
object_to_routine_map_[id] = 6;
}
// Continue mapping more object IDs...
// (This is a simplified version - the full table has 248 entries)
// Initialize draw routine function array
draw_routines_.clear();
draw_routines_.reserve(25);
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwards2x2_1to15or32(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwards2x4_1to15or26(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwards2x4spaced4_1to16(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwards2x4spaced4_1to16_BothBG(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwards2x2_1to16(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawDiagonalAcute_1to16(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawDiagonalGrave_1to16(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawDiagonalAcute_1to16_BothBG(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawDiagonalGrave_1to16_BothBG(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwards1x2_1to16_plus2(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwardsHasEdge1x1_1to16_plus3(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwardsHasEdge1x1_1to16_plus2(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwardsTopCorners1x2_1to16_plus13(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwardsBottomCorners1x2_1to16_plus13(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->CustomDraw(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwards4x4_1to16(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwards1x1Solid_1to16_plus3(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawDoorSwitcherer(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwardsDecor4x4spaced2_1to16(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwardsStatue2x3spaced2_1to16(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwardsPillar2x4spaced4_1to16(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwardsDecor4x3spaced4_1to16(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwardsDoubled2x2spaced2_1to16(obj, bg, tiles);
});
draw_routines_.push_back([](ObjectDrawer* self, const RoomObject& obj, gfx::BackgroundBuffer& bg, const std::vector<gfx::Tile16>& tiles) {
self->DrawRightwardsDecor2x2spaced12_1to16(obj, bg, tiles);
});
routines_initialized_ = true;
}
void ObjectDrawer::DrawRightwards2x2(const RoomObject& obj,
gfx::BackgroundBuffer& bg,
const gfx::Tile16& tile) {
// Pattern: Draws 2x2 tiles rightward
// Size byte determines how many times to repeat
int repeat_count = (obj.size_ & 0x0F) + 1; // Low nibble = width
int ObjectDrawer::GetDrawRoutineId(int16_t object_id) const {
auto it = object_to_routine_map_.find(object_id);
if (it != object_to_routine_map_.end()) {
return it->second;
}
for (int i = 0; i < repeat_count; i++) {
// Each iteration draws a 2x2 tile16
int tile_x = obj.x_ + (i * 2); // Each tile16 is 2x2 8x8 tiles
int tile_y = obj.y_;
WriteTile16(bg, tile_x, tile_y, tile);
// Default to simple 1x1 solid for unmapped objects
return -1;
}
// ============================================================================
// Draw Routine Implementations (Based on ZScream patterns)
// ============================================================================
void ObjectDrawer::DrawRightwards2x2_1to15or32(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Draws 2x2 tiles rightward (object 0x00)
// Size byte determines how many times to repeat (1-15 or 32)
int size = obj.size_;
if (size == 0) size = 32; // Special case for object 0x00
for (int s = 0; s < size; s++) {
if (tiles.size() >= 4) {
WriteTile16(bg, obj.x_ + (s * 2), obj.y_, tiles[0]); // Top-left
WriteTile16(bg, obj.x_ + (s * 2) + 1, obj.y_, tiles[1]); // Top-right
WriteTile16(bg, obj.x_ + (s * 2), obj.y_ + 1, tiles[2]); // Bottom-left
WriteTile16(bg, obj.x_ + (s * 2) + 1, obj.y_ + 1, tiles[3]); // Bottom-right
}
}
}
void ObjectDrawer::DrawDownwards2x2(const RoomObject& obj,
gfx::BackgroundBuffer& bg,
const gfx::Tile16& tile) {
// Pattern: Draws 2x2 tiles downward
// Size byte determines how many times to repeat
int repeat_count = ((obj.size_ >> 4) & 0x0F) + 1; // High nibble = height
void ObjectDrawer::DrawRightwards2x4_1to15or26(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Draws 2x4 tiles rightward (objects 0x01-0x02)
int size = obj.size_;
if (size == 0) size = 26; // Special case
for (int i = 0; i < repeat_count; i++) {
int tile_x = obj.x_;
int tile_y = obj.y_ + (i * 2);
WriteTile16(bg, tile_x, tile_y, tile);
for (int s = 0; s < size; s++) {
if (tiles.size() >= 8) {
// Draw 2x4 pattern
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 2; x++) {
int tile_index = y * 2 + x;
WriteTile16(bg, obj.x_ + (s * 2) + x, obj.y_ + y, tiles[tile_index]);
}
}
}
}
}
void ObjectDrawer::DrawDiagonalAcute(const RoomObject& obj,
gfx::BackgroundBuffer& bg,
const gfx::Tile16& tile) {
// Pattern: Diagonal line going down-right (/)
int length = (obj.size_ & 0x0F) + 1;
void ObjectDrawer::DrawRightwards2x4spaced4_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Draws 2x4 tiles rightward with spacing (objects 0x03-0x04)
int size = obj.size_ & 0x0F;
for (int i = 0; i < length; i++) {
int tile_x = obj.x_ + i;
int tile_y = obj.y_ + i;
WriteTile16(bg, tile_x, tile_y, tile);
for (int s = 0; s < size; s++) {
if (tiles.size() >= 8) {
// Draw 2x4 pattern with spacing
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 2; x++) {
int tile_index = y * 2 + x;
WriteTile16(bg, obj.x_ + (s * 6) + x, obj.y_ + y, tiles[tile_index]);
}
}
}
}
}
void ObjectDrawer::DrawDiagonalGrave(const RoomObject& obj,
gfx::BackgroundBuffer& bg,
const gfx::Tile16& tile) {
// Pattern: Diagonal line going down-left (\)
int length = (obj.size_ & 0x0F) + 1;
void ObjectDrawer::DrawRightwards2x4spaced4_1to16_BothBG(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Same as above but draws to both BG1 and BG2 (objects 0x05-0x06)
DrawRightwards2x4spaced4_1to16(obj, bg, tiles);
// Note: BothBG would require access to both buffers - simplified for now
}
void ObjectDrawer::DrawRightwards2x2_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Draws 2x2 tiles rightward (objects 0x07-0x08)
int size = obj.size_ & 0x0F;
for (int i = 0; i < length; i++) {
int tile_x = obj.x_ - i;
int tile_y = obj.y_ + i;
WriteTile16(bg, tile_x, tile_y, tile);
for (int s = 0; s < size; s++) {
if (tiles.size() >= 4) {
WriteTile16(bg, obj.x_ + (s * 2), obj.y_, tiles[0]);
WriteTile16(bg, obj.x_ + (s * 2) + 1, obj.y_, tiles[1]);
WriteTile16(bg, obj.x_ + (s * 2), obj.y_ + 1, tiles[2]);
WriteTile16(bg, obj.x_ + (s * 2) + 1, obj.y_ + 1, tiles[3]);
}
}
}
void ObjectDrawer::Draw4x4Block(const RoomObject& obj,
gfx::BackgroundBuffer& bg,
const gfx::Tile16& tile) {
// Pattern: 4x4 tile16 block (8x8 8x8 tiles total)
for (int yy = 0; yy < 4; yy++) {
for (int xx = 0; xx < 4; xx++) {
int tile_x = obj.x_ + (xx * 2);
int tile_y = obj.y_ + (yy * 2);
WriteTile16(bg, tile_x, tile_y, tile);
void ObjectDrawer::DrawDiagonalAcute_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Diagonal line going down-right (/) (object 0x09)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size + 6; s++) {
if (tiles.size() >= 5) {
for (int i = 0; i < 5; i++) {
WriteTile16(bg, obj.x_ + s, obj.y_ + (i - s), tiles[i]);
}
}
}
}
void ObjectDrawer::DrawDiagonalGrave_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Diagonal line going down-left (\) (objects 0x0A-0x0B)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size + 6; s++) {
if (tiles.size() >= 5) {
for (int i = 0; i < 5; i++) {
WriteTile16(bg, obj.x_ + s, obj.y_ + (i + s), tiles[i]);
}
}
}
}
void ObjectDrawer::DrawDiagonalAcute_1to16_BothBG(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Diagonal acute for both BG layers (objects 0x15-0x1F)
DrawDiagonalAcute_1to16(obj, bg, tiles);
}
void ObjectDrawer::DrawDiagonalGrave_1to16_BothBG(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Diagonal grave for both BG layers (objects 0x16-0x20)
DrawDiagonalGrave_1to16(obj, bg, tiles);
}
void ObjectDrawer::DrawRightwards1x2_1to16_plus2(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: 1x2 tiles rightward with +2 offset (object 0x21)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 2) {
WriteTile16(bg, obj.x_ + s + 2, obj.y_, tiles[0]);
WriteTile16(bg, obj.x_ + s + 2, obj.y_ + 1, tiles[1]);
}
}
}
void ObjectDrawer::DrawRightwardsHasEdge1x1_1to16_plus3(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: 1x1 tiles with edge detection +3 offset (object 0x22)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 1) {
WriteTile16(bg, obj.x_ + s + 3, obj.y_, tiles[0]);
}
}
}
void ObjectDrawer::DrawRightwardsHasEdge1x1_1to16_plus2(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: 1x1 tiles with edge detection +2 offset (objects 0x23-0x2E)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 1) {
WriteTile16(bg, obj.x_ + s + 2, obj.y_, tiles[0]);
}
}
}
void ObjectDrawer::DrawRightwardsTopCorners1x2_1to16_plus13(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Top corner 1x2 tiles with +13 offset (object 0x2F)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 2) {
WriteTile16(bg, obj.x_ + s + 13, obj.y_, tiles[0]);
WriteTile16(bg, obj.x_ + s + 13, obj.y_ + 1, tiles[1]);
}
}
}
void ObjectDrawer::DrawRightwardsBottomCorners1x2_1to16_plus13(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Bottom corner 1x2 tiles with +13 offset (object 0x30)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 2) {
WriteTile16(bg, obj.x_ + s + 13, obj.y_ + 1, tiles[0]);
WriteTile16(bg, obj.x_ + s + 13, obj.y_ + 2, tiles[1]);
}
}
}
void ObjectDrawer::CustomDraw(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Custom draw routine (objects 0x31-0x32)
// For now, fall back to simple 1x1
if (tiles.size() >= 1) {
WriteTile16(bg, obj.x_, obj.y_, tiles[0]);
}
}
void ObjectDrawer::DrawRightwards4x4_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: 4x4 block rightward (object 0x33)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 16) {
// Draw 4x4 block
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
int tile_index = y * 4 + x;
WriteTile16(bg, obj.x_ + (s * 4) + x, obj.y_ + y, tiles[tile_index]);
}
}
}
}
}
void ObjectDrawer::DrawRightwards1x1Solid_1to16_plus3(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: 1x1 solid tiles +3 offset (object 0x34)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 1) {
WriteTile16(bg, obj.x_ + s + 3, obj.y_, tiles[0]);
}
}
}
void ObjectDrawer::DrawDoorSwitcherer(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Door switcher (object 0x35)
// Special door logic - simplified for now
if (tiles.size() >= 1) {
WriteTile16(bg, obj.x_, obj.y_, tiles[0]);
}
}
void ObjectDrawer::DrawRightwardsDecor4x4spaced2_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: 4x4 decoration with spacing (objects 0x36-0x37)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 16) {
// Draw 4x4 block with spacing
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
int tile_index = y * 4 + x;
WriteTile16(bg, obj.x_ + (s * 6) + x, obj.y_ + y, tiles[tile_index]);
}
}
}
}
}
void ObjectDrawer::DrawRightwardsStatue2x3spaced2_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: 2x3 statue with spacing (object 0x38)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 6) {
// Draw 2x3 statue
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 2; x++) {
int tile_index = y * 2 + x;
WriteTile16(bg, obj.x_ + (s * 4) + x, obj.y_ + y, tiles[tile_index]);
}
}
}
}
}
void ObjectDrawer::DrawRightwardsPillar2x4spaced4_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: 2x4 pillar with spacing (objects 0x39, 0x3D)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 8) {
// Draw 2x4 pillar
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 2; x++) {
int tile_index = y * 2 + x;
WriteTile16(bg, obj.x_ + (s * 6) + x, obj.y_ + y, tiles[tile_index]);
}
}
}
}
}
void ObjectDrawer::DrawRightwardsDecor4x3spaced4_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: 4x3 decoration with spacing (objects 0x3A-0x3B)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 12) {
// Draw 4x3 decoration
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 4; x++) {
int tile_index = y * 4 + x;
WriteTile16(bg, obj.x_ + (s * 6) + x, obj.y_ + y, tiles[tile_index]);
}
}
}
}
}
void ObjectDrawer::DrawRightwardsDoubled2x2spaced2_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: Doubled 2x2 with spacing (object 0x3C)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 8) {
// Draw doubled 2x2 pattern
for (int y = 0; y < 2; y++) {
for (int x = 0; x < 4; x++) {
int tile_index = y * 4 + x;
WriteTile16(bg, obj.x_ + (s * 6) + x, obj.y_ + y, tiles[tile_index]);
}
}
}
}
}
void ObjectDrawer::DrawRightwardsDecor2x2spaced12_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles) {
// Pattern: 2x2 decoration with large spacing (object 0x3E)
int size = obj.size_ & 0x0F;
for (int s = 0; s < size; s++) {
if (tiles.size() >= 4) {
// Draw 2x2 decoration with 12-tile spacing
WriteTile16(bg, obj.x_ + (s * 14), obj.y_, tiles[0]);
WriteTile16(bg, obj.x_ + (s * 14) + 1, obj.y_, tiles[1]);
WriteTile16(bg, obj.x_ + (s * 14), obj.y_ + 1, tiles[2]);
WriteTile16(bg, obj.x_ + (s * 14) + 1, obj.y_ + 1, tiles[3]);
}
}
}
@@ -187,27 +573,30 @@ void ObjectDrawer::Draw4x4Block(const RoomObject& obj,
void ObjectDrawer::WriteTile16(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
const gfx::Tile16& tile) {
// A Tile16 is 2x2 8x8 tiles, so we write 4 tile entries
printf("[WriteTile16] Writing Tile16 at tile pos (%d,%d) to bitmap\n", tile_x, tile_y);
// Top-left (tile0)
if (IsValidTilePosition(tile_x, tile_y)) {
bg.SetTileAt(tile_x, tile_y, gfx::TileInfoToWord(tile.tile0_));
// Draw directly to bitmap instead of tile buffer to avoid being overwritten
auto& bitmap = bg.bitmap();
if (!bitmap.is_active() || bitmap.width() == 0) {
printf("[WriteTile16] Bitmap not ready: active=%d, width=%d\n", bitmap.is_active(), bitmap.width());
return; // Bitmap not ready
}
// Get graphics data from ROM
if (!rom_ || !rom_->is_loaded()) {
return;
}
// Top-right (tile1)
if (IsValidTilePosition(tile_x + 1, tile_y)) {
bg.SetTileAt(tile_x + 1, tile_y, gfx::TileInfoToWord(tile.tile1_));
}
// Bottom-left (tile2)
if (IsValidTilePosition(tile_x, tile_y + 1)) {
bg.SetTileAt(tile_x, tile_y + 1, gfx::TileInfoToWord(tile.tile2_));
}
// Bottom-right (tile3)
if (IsValidTilePosition(tile_x + 1, tile_y + 1)) {
bg.SetTileAt(tile_x + 1, tile_y + 1, gfx::TileInfoToWord(tile.tile3_));
auto gfx_data = rom_->mutable_graphics_buffer();
if (!gfx_data || gfx_data->empty()) {
return;
}
// Draw each 8x8 tile directly to bitmap
DrawTileToBitmap(bitmap, tile.tile0_, tile_x * 8, tile_y * 8, gfx_data->data());
DrawTileToBitmap(bitmap, tile.tile1_, (tile_x + 1) * 8, tile_y * 8, gfx_data->data());
DrawTileToBitmap(bitmap, tile.tile2_, tile_x * 8, (tile_y + 1) * 8, gfx_data->data());
DrawTileToBitmap(bitmap, tile.tile3_, (tile_x + 1) * 8, (tile_y + 1) * 8, gfx_data->data());
}
bool ObjectDrawer::IsValidTilePosition(int tile_x, int tile_y) const {
@@ -215,6 +604,49 @@ bool ObjectDrawer::IsValidTilePosition(int tile_x, int tile_y) const {
tile_y >= 0 && tile_y < kMaxTilesY;
}
void ObjectDrawer::DrawTileToBitmap(gfx::Bitmap& bitmap, const gfx::TileInfo& tile_info,
int pixel_x, int pixel_y, const uint8_t* tiledata) {
// Draw an 8x8 tile directly to bitmap at pixel coordinates
if (!tiledata) return;
printf("[DrawTileToBitmap] Drawing tile ID=0x%02X at (%d,%d) with palette=%d\n",
tile_info.id_, pixel_x, pixel_y, tile_info.palette_);
// Calculate tile position in graphics sheet (128 pixels wide)
int tile_sheet_x = (tile_info.id_ % 16) * 8; // 16 tiles per row
int tile_sheet_y = (tile_info.id_ / 16) * 8; // Each row is 16 tiles
// Clamp palette to valid range
uint8_t palette_id = tile_info.palette_ & 0x0F;
if (palette_id > 10) palette_id = palette_id % 11;
uint8_t palette_offset = palette_id * 8; // 3BPP: 8 colors per palette
// Draw 8x8 pixels
for (int py = 0; py < 8; py++) {
for (int px = 0; px < 8; px++) {
// Apply mirroring
int src_x = tile_info.horizontal_mirror_ ? (7 - px) : px;
int src_y = tile_info.vertical_mirror_ ? (7 - py) : py;
// Read pixel from graphics sheet
int src_index = (tile_sheet_y + src_y) * 128 + (tile_sheet_x + src_x);
uint8_t pixel_index = tiledata[src_index];
// Apply palette and write to bitmap
uint8_t final_color = pixel_index + palette_offset;
int dest_x = pixel_x + px;
int dest_y = pixel_y + py;
if (dest_x >= 0 && dest_x < bitmap.width() && dest_y >= 0 && dest_y < bitmap.height()) {
int dest_index = dest_y * bitmap.width() + dest_x;
if (dest_index >= 0 && dest_index < static_cast<int>(bitmap.mutable_data().size())) {
bitmap.mutable_data()[dest_index] = final_color;
}
}
}
}
}
} // namespace zelda3
} // namespace yaze

View File

@@ -2,10 +2,13 @@
#define YAZE_APP_ZELDA3_DUNGEON_OBJECT_DRAWER_H
#include <vector>
#include <unordered_map>
#include <functional>
#include "absl/status/status.h"
#include "app/gfx/background_buffer.h"
#include "app/gfx/snes_tile.h"
#include "app/gfx/snes_palette.h"
#include "app/rom.h"
#include "app/zelda3/dungeon/room_object.h"
@@ -17,12 +20,14 @@ namespace zelda3 {
*
* This class interprets object IDs and draws them to BG1/BG2 buffers
* using the patterns extracted from the game's drawing routines.
* Based on ZScream's DungeonObjectData.cs and Subtype1_Draw.cs patterns.
*
* Architecture:
* 1. Load tile data from ROM for the object
* 2. Determine drawing pattern (rightward, downward, diagonal, special)
* 3. Write tiles to BackgroundBuffer according to pattern
* 4. Handle size bytes for repeating patterns
* 2. Look up draw routine ID from object ID mapping
* 3. Execute appropriate draw routine with size/orientation
* 4. Write tiles to BackgroundBuffer according to pattern
* 5. Handle palette coordination and graphics sheet access
*/
class ObjectDrawer {
public:
@@ -33,43 +38,116 @@ class ObjectDrawer {
* @param object The object to draw
* @param bg1 Background layer 1 buffer
* @param bg2 Background layer 2 buffer
* @param palette_group Current palette group for color mapping
* @return Status of the drawing operation
*/
absl::Status DrawObject(const RoomObject& object,
gfx::BackgroundBuffer& bg1,
gfx::BackgroundBuffer& bg2);
gfx::BackgroundBuffer& bg2,
const gfx::PaletteGroup& palette_group);
/**
* @brief Draw all objects in a room
* @param objects Vector of room objects
* @param bg1 Background layer 1 buffer
* @param bg2 Background layer 2 buffer
* @param palette_group Current palette group for color mapping
* @return Status of the drawing operation
*/
absl::Status DrawObjectList(const std::vector<RoomObject>& objects,
gfx::BackgroundBuffer& bg1,
gfx::BackgroundBuffer& bg2);
gfx::BackgroundBuffer& bg2,
const gfx::PaletteGroup& palette_group);
/**
* @brief Get draw routine ID for an object
* @param object_id The object ID to look up
* @return Draw routine ID (0-24) based on ZScream mapping
*/
int GetDrawRoutineId(int16_t object_id) const;
/**
* @brief Initialize draw routine registry
* Must be called before drawing objects
*/
void InitializeDrawRoutines();
/**
* @brief Draw a single tile directly to bitmap
* @param bitmap Target bitmap to draw to
* @param tile_info Tile information (ID, palette, mirroring)
* @param pixel_x X pixel coordinate in bitmap
* @param pixel_y Y pixel coordinate in bitmap
* @param tiledata Source graphics data from ROM
*/
void DrawTileToBitmap(gfx::Bitmap& bitmap, const gfx::TileInfo& tile_info,
int pixel_x, int pixel_y, const uint8_t* tiledata);
private:
// Pattern-specific drawing methods
void DrawRightwards2x2(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const gfx::Tile16& tile);
void DrawDownwards2x2(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const gfx::Tile16& tile);
void DrawDiagonalAcute(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const gfx::Tile16& tile);
void DrawDiagonalGrave(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const gfx::Tile16& tile);
void Draw1x1Solid(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const gfx::Tile16& tile);
void Draw4x4Block(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const gfx::Tile16& tile);
// Draw routine function type
using DrawRoutine = std::function<void(ObjectDrawer*, const RoomObject&, gfx::BackgroundBuffer&,
const std::vector<gfx::Tile16>&)>;
// Core draw routines (based on ZScream's subtype1_routines table)
void DrawRightwards2x2_1to15or32(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwards2x4_1to15or26(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwards2x4spaced4_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwards2x4spaced4_1to16_BothBG(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwards2x2_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawDiagonalAcute_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawDiagonalGrave_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawDiagonalAcute_1to16_BothBG(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawDiagonalGrave_1to16_BothBG(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwards1x2_1to16_plus2(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwardsHasEdge1x1_1to16_plus3(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwardsHasEdge1x1_1to16_plus2(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwardsTopCorners1x2_1to16_plus13(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwardsBottomCorners1x2_1to16_plus13(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void CustomDraw(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwards4x4_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwards1x1Solid_1to16_plus3(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawDoorSwitcherer(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwardsDecor4x4spaced2_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwardsStatue2x3spaced2_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwardsPillar2x4spaced4_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwardsDecor4x3spaced4_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwardsDoubled2x2spaced2_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
void DrawRightwardsDecor2x2spaced12_1to16(const RoomObject& obj, gfx::BackgroundBuffer& bg,
const std::vector<gfx::Tile16>& tiles);
// Utility methods
void WriteTile16(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
const gfx::Tile16& tile);
bool IsValidTilePosition(int tile_x, int tile_y) const;
// Draw routine registry
std::unordered_map<int16_t, int> object_to_routine_map_;
std::vector<DrawRoutine> draw_routines_;
bool routines_initialized_ = false;
Rom* rom_;
// Canvas dimensions in tiles (64x64 = 512x512 pixels)

View File

@@ -6,6 +6,12 @@
#include "absl/strings/str_format.h"
#include "app/zelda3/dungeon/room_object.h"
// ROM addresses for object data (PC addresses, not SNES)
static constexpr int kRoomObjectSubtype1 = 0x0A8000;
static constexpr int kRoomObjectSubtype2 = 0x0A9000;
static constexpr int kRoomObjectSubtype3 = 0x0AA000;
static constexpr int kRoomObjectTileAddress = 0x0AB000;
namespace yaze {
namespace zelda3 {
@@ -203,5 +209,173 @@ int ObjectParser::DetermineSubtype(int16_t object_id) const {
}
}
ObjectDrawInfo ObjectParser::GetObjectDrawInfo(int16_t object_id) const {
ObjectDrawInfo info;
// Map object ID to draw routine based on ZScream's subtype1_routines table
// This is based on the DungeonObjectData.cs mapping from ZScream
if (object_id == 0x00) {
info.draw_routine_id = 0; // RoomDraw_Rightwards2x2_1to15or32
info.routine_name = "Rightwards2x2_1to15or32";
info.tile_count = 4;
info.is_horizontal = true;
}
else if (object_id >= 0x01 && object_id <= 0x02) {
info.draw_routine_id = 1; // RoomDraw_Rightwards2x4_1to15or26
info.routine_name = "Rightwards2x4_1to15or26";
info.tile_count = 8;
info.is_horizontal = true;
}
else if (object_id >= 0x03 && object_id <= 0x04) {
info.draw_routine_id = 2; // RoomDraw_Rightwards2x4spaced4_1to16
info.routine_name = "Rightwards2x4spaced4_1to16";
info.tile_count = 8;
info.is_horizontal = true;
}
else if (object_id >= 0x05 && object_id <= 0x06) {
info.draw_routine_id = 3; // RoomDraw_Rightwards2x4spaced4_1to16_BothBG
info.routine_name = "Rightwards2x4spaced4_1to16_BothBG";
info.tile_count = 8;
info.is_horizontal = true;
info.both_layers = true;
}
else if (object_id >= 0x07 && object_id <= 0x08) {
info.draw_routine_id = 4; // RoomDraw_Rightwards2x2_1to16
info.routine_name = "Rightwards2x2_1to16";
info.tile_count = 4;
info.is_horizontal = true;
}
else if (object_id == 0x09) {
info.draw_routine_id = 5; // RoomDraw_DiagonalAcute_1to16
info.routine_name = "DiagonalAcute_1to16";
info.tile_count = 5;
info.is_horizontal = false;
}
else if (object_id >= 0x0A && object_id <= 0x0B) {
info.draw_routine_id = 6; // RoomDraw_DiagonalGrave_1to16
info.routine_name = "DiagonalGrave_1to16";
info.tile_count = 5;
info.is_horizontal = false;
}
else if (object_id >= 0x15 && object_id <= 0x1F) {
info.draw_routine_id = 7; // RoomDraw_DiagonalAcute_1to16_BothBG
info.routine_name = "DiagonalAcute_1to16_BothBG";
info.tile_count = 5;
info.is_horizontal = false;
info.both_layers = true;
}
else if (object_id >= 0x16 && object_id <= 0x20) {
info.draw_routine_id = 8; // RoomDraw_DiagonalGrave_1to16_BothBG
info.routine_name = "DiagonalGrave_1to16_BothBG";
info.tile_count = 5;
info.is_horizontal = false;
info.both_layers = true;
}
else if (object_id == 0x21) {
info.draw_routine_id = 9; // RoomDraw_Rightwards1x2_1to16_plus2
info.routine_name = "Rightwards1x2_1to16_plus2";
info.tile_count = 2;
info.is_horizontal = true;
}
else if (object_id == 0x22) {
info.draw_routine_id = 10; // RoomDraw_RightwardsHasEdge1x1_1to16_plus3
info.routine_name = "RightwardsHasEdge1x1_1to16_plus3";
info.tile_count = 1;
info.is_horizontal = true;
}
else if (object_id >= 0x23 && object_id <= 0x2E) {
info.draw_routine_id = 11; // RoomDraw_RightwardsHasEdge1x1_1to16_plus2
info.routine_name = "RightwardsHasEdge1x1_1to16_plus2";
info.tile_count = 1;
info.is_horizontal = true;
}
else if (object_id == 0x2F) {
info.draw_routine_id = 12; // RoomDraw_RightwardsTopCorners1x2_1to16_plus13
info.routine_name = "RightwardsTopCorners1x2_1to16_plus13";
info.tile_count = 2;
info.is_horizontal = true;
}
else if (object_id == 0x30) {
info.draw_routine_id = 13; // RoomDraw_RightwardsBottomCorners1x2_1to16_plus13
info.routine_name = "RightwardsBottomCorners1x2_1to16_plus13";
info.tile_count = 2;
info.is_horizontal = true;
}
else if (object_id >= 0x31 && object_id <= 0x32) {
info.draw_routine_id = 14; // CustomDraw
info.routine_name = "CustomDraw";
info.tile_count = 1;
}
else if (object_id == 0x33) {
info.draw_routine_id = 15; // RoomDraw_Rightwards4x4_1to16
info.routine_name = "Rightwards4x4_1to16";
info.tile_count = 16;
info.is_horizontal = true;
}
else if (object_id == 0x34) {
info.draw_routine_id = 16; // RoomDraw_Rightwards1x1Solid_1to16_plus3
info.routine_name = "Rightwards1x1Solid_1to16_plus3";
info.tile_count = 1;
info.is_horizontal = true;
}
else if (object_id == 0x35) {
info.draw_routine_id = 17; // RoomDraw_DoorSwitcherer
info.routine_name = "DoorSwitcherer";
info.tile_count = 1;
}
else if (object_id >= 0x36 && object_id <= 0x37) {
info.draw_routine_id = 18; // RoomDraw_RightwardsDecor4x4spaced2_1to16
info.routine_name = "RightwardsDecor4x4spaced2_1to16";
info.tile_count = 16;
info.is_horizontal = true;
}
else if (object_id == 0x38) {
info.draw_routine_id = 19; // RoomDraw_RightwardsStatue2x3spaced2_1to16
info.routine_name = "RightwardsStatue2x3spaced2_1to16";
info.tile_count = 6;
info.is_horizontal = true;
}
else if (object_id == 0x39 || object_id == 0x3D) {
info.draw_routine_id = 20; // RoomDraw_RightwardsPillar2x4spaced4_1to16
info.routine_name = "RightwardsPillar2x4spaced4_1to16";
info.tile_count = 8;
info.is_horizontal = true;
}
else if (object_id >= 0x3A && object_id <= 0x3B) {
info.draw_routine_id = 21; // RoomDraw_RightwardsDecor4x3spaced4_1to16
info.routine_name = "RightwardsDecor4x3spaced4_1to16";
info.tile_count = 12;
info.is_horizontal = true;
}
else if (object_id == 0x3C) {
info.draw_routine_id = 22; // RoomDraw_RightwardsDoubled2x2spaced2_1to16
info.routine_name = "RightwardsDoubled2x2spaced2_1to16";
info.tile_count = 8;
info.is_horizontal = true;
}
else if (object_id == 0x3E) {
info.draw_routine_id = 23; // RoomDraw_RightwardsDecor2x2spaced12_1to16
info.routine_name = "RightwardsDecor2x2spaced12_1to16";
info.tile_count = 4;
info.is_horizontal = true;
}
else if (object_id >= 0x3F && object_id <= 0x40) {
info.draw_routine_id = 24; // RoomDraw_RightwardsHasEdge1x1_1to16_plus2 (variant)
info.routine_name = "RightwardsHasEdge1x1_1to16_plus2_variant";
info.tile_count = 1;
info.is_horizontal = true;
}
else {
// Default to simple 1x1 solid for unmapped objects
info.draw_routine_id = 16; // Use solid block routine
info.routine_name = "DefaultSolid";
info.tile_count = 1;
info.is_horizontal = true;
}
return info;
}
} // namespace zelda3
} // namespace yaze

View File

@@ -61,6 +61,26 @@ struct ObjectSizeInfo {
repeat_count(1) {}
};
/**
* @brief Draw routine information for object rendering
*/
struct ObjectDrawInfo {
int draw_routine_id; // Which drawing pattern to use (0-24)
int tile_count; // How many tiles this object has
bool is_horizontal; // Orientation
bool is_vertical;
bool both_layers; // Draw to both BG1 and BG2
std::string routine_name; // Human-readable routine name
ObjectDrawInfo()
: draw_routine_id(0),
tile_count(1),
is_horizontal(true),
is_vertical(false),
both_layers(false),
routine_name("Unknown") {}
};
/**
* @brief Direct ROM parser for dungeon objects
*
@@ -109,6 +129,13 @@ class ObjectParser {
* @brief Determine object subtype from ID
*/
int DetermineSubtype(int16_t object_id) const;
/**
* @brief Get draw routine information for an object
* @param object_id The object ID to look up
* @return ObjectDrawInfo containing draw routine details
*/
ObjectDrawInfo GetObjectDrawInfo(int16_t object_id) const;
private:
/**

View File

@@ -756,9 +756,15 @@ absl::Status ObjectRenderer::RenderTileToBitmap(const gfx::Tile16& tile, gfx::Bi
}
void ObjectRenderer::Render8x8Tile(gfx::Bitmap& bitmap, gfx::Bitmap* graphics_sheet, const gfx::TileInfo& tile_info, int x, int y, const gfx::SnesPalette& palette) {
// Calculate tile position within the graphics sheet
// Each graphics sheet is 128x128 pixels (16x16 tiles of 8x8 pixels each)
int tile_x = (tile_info.id_ % 16) * 8;
int tile_y = ((tile_info.id_ % 256) / 16) * 8;
// Get the correct sub-palette for this tile
// Each tile uses a 4-bit palette index (0-7), so we need to extract the right 8-color sub-palette
int palette_offset = tile_info.palette_ * 16; // Each sub-palette is 16 colors (2 bytes each)
for (int py = 0; py < 8; ++py) {
for (int px = 0; px < 8; ++px) {
int final_x = x + px;
@@ -782,7 +788,15 @@ void ObjectRenderer::Render8x8Tile(gfx::Bitmap& bitmap, gfx::Bitmap* graphics_sh
uint8_t color_index = graphics_sheet->at(pixel_index);
if (color_index >= palette.size()) {
// Skip transparent pixels (color 0)
if (color_index == 0) {
continue;
}
// Calculate the final color index in the full palette
int final_color_index = palette_offset + color_index;
if (final_color_index >= static_cast<int>(palette.size())) {
continue;
}
@@ -800,7 +814,7 @@ void ObjectRenderer::Render8x8Tile(gfx::Bitmap& bitmap, gfx::Bitmap* graphics_sh
}
if (render_x >= 0 && render_y >= 0 && render_x < bitmap.width() && render_y < bitmap.height()) {
bitmap.SetPixel(render_x, render_y, palette[color_index]);
bitmap.SetPixel(render_x, render_y, palette[final_color_index]);
}
}
}

View File

@@ -9,6 +9,7 @@
#include "absl/strings/str_cat.h"
#include "app/core/window.h"
#include "app/gfx/arena.h"
#include "app/gfx/snes_palette.h"
#include "app/rom.h"
#include "app/snes.h"
#include "app/zelda3/dungeon/object_drawer.h"
@@ -285,18 +286,23 @@ void Room::CopyRoomGraphicsToBuffer() {
void Room::RenderRoomGraphics() {
CopyRoomGraphicsToBuffer();
// CRITICAL: Load graphics sheets into Arena with actual ROM data
LoadGraphicsSheetsIntoArena();
bg1_buffer_.DrawFloor(rom()->vector(), tile_address,
tile_address_floor, floor1_graphics_);
bg2_buffer_.DrawFloor(rom()->vector(), tile_address,
tile_address_floor, floor2_graphics_);
// Render layout and object tiles to background buffers
RenderObjectsToBackground();
// Draw background tiles (floor, walls, etc.) to buffers
bg1_buffer_.DrawBackground(std::span<uint8_t>(current_gfx16_));
bg2_buffer_.DrawBackground(std::span<uint8_t>(current_gfx16_));
// Render objects ON TOP of background tiles
// This must happen AFTER DrawBackground to avoid overwriting object data
RenderObjectsToBackground();
auto& bg1_bmp = bg1_buffer_.bitmap();
auto& bg2_bmp = bg2_buffer_.bitmap();
@@ -318,23 +324,120 @@ void Room::RenderRoomGraphics() {
// SetPaletteWithTransparent() only extracts 8 colors, which is wrong for dungeons!
bg1_bmp.SetPalette(bg1_palette);
bg2_bmp.SetPalette(bg1_palette);
// Queue texture creation for background buffers
gfx::Arena::Get().QueueTextureCommand(
gfx::Arena::TextureCommandType::CREATE, &bg1_bmp);
gfx::Arena::Get().QueueTextureCommand(
gfx::Arena::TextureCommandType::CREATE, &bg2_bmp);
}
// CRITICAL: Recreate textures with the palette applied!
// TODO: Queue texture for later rendering.
// core::Renderer::Get().RenderBitmap(&bg1_buffer_.bitmap());
// core::Renderer::Get().RenderBitmap(&bg2_buffer_.bitmap());
}
void Room::RenderObjectsToBackground() {
printf("[RenderObjectsToBackground] Starting object rendering\n");
if (!rom_ || !rom_->is_loaded()) {
printf("[RenderObjectsToBackground] ROM not loaded, aborting\n");
return;
}
// Get palette group for object rendering
auto& dungeon_pal_group = rom()->mutable_palette_group()->dungeon_main;
int num_palettes = dungeon_pal_group.size();
int palette_id = palette;
if (palette_id < 0 || palette_id >= num_palettes) {
palette_id = 0;
}
auto room_palette = dungeon_pal_group[palette_id];
auto palette_group_result = gfx::CreatePaletteGroupFromLargePalette(room_palette);
if (!palette_group_result.ok()) {
// Fallback to empty palette group
gfx::PaletteGroup empty_group;
ObjectDrawer drawer(rom_);
drawer.DrawObjectList(tile_objects_, bg1_buffer_, bg2_buffer_, empty_group);
return;
}
auto palette_group = palette_group_result.value();
// Use ObjectDrawer for pattern-based object rendering
// This provides proper wall/object drawing patterns
ObjectDrawer drawer(rom_);
drawer.DrawObjectList(tile_objects_, bg1_buffer_, bg2_buffer_);
auto status = drawer.DrawObjectList(tile_objects_, bg1_buffer_, bg2_buffer_, palette_group);
// Log only failures, not successes
if (!status.ok()) {
printf("[RenderObjectsToBackground] ObjectDrawer failed: %s\n", std::string(status.message()).c_str());
}
}
void Room::LoadGraphicsSheetsIntoArena() {
if (!rom_ || !rom_->is_loaded()) {
return;
}
auto& arena = gfx::Arena::Get();
// For now, create simple placeholder graphics sheets
// This ensures the Room Graphics card has something to display
for (int i = 0; i < 16; i++) {
if (blocks_[i] < 0 || blocks_[i] >= 223) {
continue; // Skip invalid blocks
}
auto& gfx_sheet = arena.gfx_sheets()[blocks_[i]];
// Check if sheet already has data
if (gfx_sheet.is_active() && gfx_sheet.width() > 0) {
continue; // Already loaded
}
try {
// Create a simple placeholder graphics sheet (128x128 pixels)
std::vector<uint8_t> sheet_data(128 * 128, 0);
// Fill with a simple pattern to make it visible
for (int y = 0; y < 128; y++) {
for (int x = 0; x < 128; x++) {
// Create a simple checkerboard pattern
if ((x / 8 + y / 8) % 2 == 0) {
sheet_data[y * 128 + x] = 1; // Light color
} else {
sheet_data[y * 128 + x] = 2; // Dark color
}
}
}
// Create bitmap with the graphics data
gfx::Bitmap sheet_bitmap(128, 128, 8, sheet_data);
// Get room palette and apply to graphics sheet
auto& dungeon_pal_group = rom()->mutable_palette_group()->dungeon_main;
if (palette >= 0 && palette < static_cast<int>(dungeon_pal_group.size())) {
auto room_palette = dungeon_pal_group[palette];
sheet_bitmap.SetPalette(room_palette);
} else {
// Use default palette
gfx::SnesPalette default_palette;
default_palette.AddColor(gfx::SnesColor(0, 0, 0)); // Transparent
default_palette.AddColor(gfx::SnesColor(255, 255, 255)); // White
default_palette.AddColor(gfx::SnesColor(0, 0, 0)); // Black
sheet_bitmap.SetPalette(default_palette);
}
// Replace the graphics sheet in Arena
arena.gfx_sheets()[blocks_[i]] = std::move(sheet_bitmap);
// Queue texture creation for this graphics sheet
gfx::Arena::Get().QueueTextureCommand(
gfx::Arena::TextureCommandType::CREATE,
&arena.gfx_sheets()[blocks_[i]]);
} catch (const std::exception& e) {
// Skip this graphics sheet if creation fails
continue;
}
}
}
void Room::LoadAnimatedGraphics() {

View File

@@ -206,6 +206,7 @@ class Room {
void LoadRoomGraphics(uint8_t entrance_blockset = 0xFF);
void CopyRoomGraphicsToBuffer();
void LoadGraphicsSheetsIntoArena();
void RenderRoomGraphics();
void RenderObjectsToBackground();
void LoadAnimatedGraphics();

View File

@@ -153,14 +153,28 @@ void RoomObject::DrawTile(gfx::Tile16 t, int xx, int yy,
}
void RoomObject::EnsureTilesLoaded() {
if (tiles_loaded_) return;
if (rom_ == nullptr) return;
printf("[EnsureTilesLoaded] Object ID=0x%02X, tiles_loaded=%d\n", id_, tiles_loaded_);
if (tiles_loaded_) {
printf("[EnsureTilesLoaded] Tiles already loaded for object 0x%02X\n", id_);
return;
}
if (rom_ == nullptr) {
printf("[EnsureTilesLoaded] ERROR: ROM not set for object 0x%02X\n", id_);
return;
}
// Try the new parser first - this is more efficient and accurate
if (LoadTilesWithParser().ok()) {
printf("[EnsureTilesLoaded] Trying parser for object 0x%02X\n", id_);
auto parser_status = LoadTilesWithParser();
if (parser_status.ok()) {
printf("[EnsureTilesLoaded] Parser succeeded for object 0x%02X, loaded %zu tiles\n", id_, tiles_.size());
tiles_loaded_ = true;
return;
}
printf("[EnsureTilesLoaded] Parser failed for object 0x%02X: %s\n", id_, parser_status.message().data());
// Fallback to legacy method for compatibility with enhanced validation
auto rom_data = rom_->data();

View File

@@ -0,0 +1,356 @@
#include "gtest/gtest.h"
#include "absl/status/status.h"
#include "app/gfx/background_buffer.h"
#include "app/gfx/snes_palette.h"
#include "app/rom.h"
#include "app/zelda3/dungeon/object_drawer.h"
#include "app/zelda3/dungeon/object_parser.h"
#include "app/zelda3/dungeon/room.h"
#include "app/zelda3/dungeon/room_object.h"
namespace yaze {
namespace zelda3 {
class DungeonRenderingIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
// Create a mock ROM for testing
rom_ = std::make_unique<Rom>();
// Initialize with minimal ROM data for testing
std::vector<uint8_t> mock_rom_data(1024 * 1024, 0); // 1MB mock ROM
rom_->LoadFromData(mock_rom_data);
// Create test rooms
room_0x00_ = CreateTestRoom(0x00); // Link's House
room_0x01_ = CreateTestRoom(0x01); // Another test room
}
void TearDown() override {
rom_.reset();
}
std::unique_ptr<Rom> rom_;
// Create a test room with various objects
Room CreateTestRoom(int room_id) {
Room room(room_id, rom_.get());
// Add some test objects to the room
std::vector<RoomObject> objects;
// Add floor objects (object 0x00)
objects.emplace_back(0x00, 5, 5, 3, 0); // Horizontal floor
objects.emplace_back(0x00, 10, 10, 5, 0); // Another floor section
// Add wall objects (object 0x01)
objects.emplace_back(0x01, 15, 15, 2, 0); // Vertical wall
objects.emplace_back(0x01, 20, 20, 4, 1); // Horizontal wall on BG2
// Add diagonal stairs (object 0x09)
objects.emplace_back(0x09, 25, 25, 6, 0); // Diagonal stairs
// Add solid blocks (object 0x34)
objects.emplace_back(0x34, 30, 30, 1, 0); // Solid block
objects.emplace_back(0x34, 35, 35, 2, 1); // Another solid block on BG2
// Set ROM for all objects
for (auto& obj : objects) {
obj.set_rom(rom_.get());
}
// Add objects to room (this would normally be done by LoadObjects)
for (const auto& obj : objects) {
room.AddObject(obj);
}
return room;
}
// Create a test palette
gfx::SnesPalette CreateTestPalette() {
gfx::SnesPalette palette;
// Add some test colors
palette.AddColor(gfx::SnesColor(0, 0, 0)); // Transparent
palette.AddColor(gfx::SnesColor(255, 0, 0)); // Red
palette.AddColor(gfx::SnesColor(0, 255, 0)); // Green
palette.AddColor(gfx::SnesColor(0, 0, 255)); // Blue
palette.AddColor(gfx::SnesColor(255, 255, 0)); // Yellow
palette.AddColor(gfx::SnesColor(255, 0, 255)); // Magenta
palette.AddColor(gfx::SnesColor(0, 255, 255)); // Cyan
palette.AddColor(gfx::SnesColor(255, 255, 255)); // White
return palette;
}
gfx::PaletteGroup CreateTestPaletteGroup() {
gfx::PaletteGroup group;
group.AddPalette(CreateTestPalette());
return group;
}
private:
Room room_0x00_;
Room room_0x01_;
};
// Test full room rendering with ObjectDrawer
TEST_F(DungeonRenderingIntegrationTest, FullRoomRenderingWorks) {
Room test_room = CreateTestRoom(0x00);
// Test that room has objects
EXPECT_GT(test_room.GetTileObjects().size(), 0);
// Test ObjectDrawer can render the room
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
auto status = drawer.DrawObjectList(test_room.GetTileObjects(),
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test room rendering with different palette configurations
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingWithDifferentPalettes) {
Room test_room = CreateTestRoom(0x00);
ObjectDrawer drawer(rom_.get());
// Test with different palette configurations
std::vector<gfx::PaletteGroup> palette_groups;
// Create multiple palette groups
for (int i = 0; i < 3; ++i) {
palette_groups.push_back(CreateTestPaletteGroup());
}
for (const auto& palette_group : palette_groups) {
auto status = drawer.DrawObjectList(test_room.GetTileObjects(),
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
}
// Test room rendering with objects on different layers
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingWithMultipleLayers) {
Room test_room = CreateTestRoom(0x00);
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Separate objects by layer
std::vector<RoomObject> bg1_objects;
std::vector<RoomObject> bg2_objects;
for (const auto& obj : test_room.GetTileObjects()) {
if (obj.GetLayerValue() == 0) {
bg1_objects.push_back(obj);
} else if (obj.GetLayerValue() == 1) {
bg2_objects.push_back(obj);
}
}
// Render BG1 objects
if (!bg1_objects.empty()) {
auto status = drawer.DrawObjectList(bg1_objects,
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Render BG2 objects
if (!bg2_objects.empty()) {
auto status = drawer.DrawObjectList(bg2_objects,
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
}
// Test room rendering with various object sizes
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingWithVariousObjectSizes) {
Room test_room = CreateTestRoom(0x00);
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Group objects by size
std::map<int, std::vector<RoomObject>> objects_by_size;
for (const auto& obj : test_room.GetTileObjects()) {
objects_by_size[obj.size_].push_back(obj);
}
// Render objects of each size
for (const auto& [size, objects] : objects_by_size) {
auto status = drawer.DrawObjectList(objects,
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
}
// Test room rendering performance
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingPerformance) {
// Create a room with many objects
Room large_room(0x00, rom_.get());
// Add many test objects
for (int i = 0; i < 200; ++i) {
int id = i % 65; // Cycle through object IDs 0-64
int x = (i * 2) % 60; // Spread across buffer
int y = (i * 3) % 60;
int size = (i % 8) + 1; // Size 1-8
int layer = i % 2; // Alternate layers
RoomObject obj(id, x, y, size, layer);
obj.set_rom(rom_.get());
large_room.AddObject(obj);
}
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Time the rendering operation
auto start_time = std::chrono::high_resolution_clock::now();
auto status = drawer.DrawObjectList(large_room.GetTileObjects(),
large_room.bg1_buffer(),
large_room.bg2_buffer(),
palette_group);
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end_time - start_time);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Should complete in reasonable time (less than 2 seconds for 200 objects)
EXPECT_LT(duration.count(), 2000);
std::cout << "Rendered room with 200 objects in " << duration.count() << "ms" << std::endl;
}
// Test room rendering with edge case coordinates
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingWithEdgeCaseCoordinates) {
Room test_room = CreateTestRoom(0x00);
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Add objects at edge coordinates
std::vector<RoomObject> edge_objects;
edge_objects.emplace_back(0x34, 0, 0, 1, 0); // Origin
edge_objects.emplace_back(0x34, 63, 63, 1, 0); // Near buffer edge
edge_objects.emplace_back(0x34, 32, 32, 1, 0); // Center
edge_objects.emplace_back(0x34, 1, 1, 1, 0); // Near origin
edge_objects.emplace_back(0x34, 62, 62, 1, 0); // Near edge
// Set ROM for all objects
for (auto& obj : edge_objects) {
obj.set_rom(rom_.get());
}
auto status = drawer.DrawObjectList(edge_objects,
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test room rendering with mixed object types
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingWithMixedObjectTypes) {
Room test_room = CreateTestRoom(0x00);
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Add various object types
std::vector<RoomObject> mixed_objects;
// Floor objects
mixed_objects.emplace_back(0x00, 5, 5, 3, 0);
mixed_objects.emplace_back(0x01, 10, 10, 2, 0);
// Wall objects
mixed_objects.emplace_back(0x02, 15, 15, 4, 0);
mixed_objects.emplace_back(0x03, 20, 20, 1, 1);
// Diagonal objects
mixed_objects.emplace_back(0x09, 25, 25, 5, 0);
mixed_objects.emplace_back(0x0A, 30, 30, 3, 0);
// Solid objects
mixed_objects.emplace_back(0x34, 35, 35, 1, 0);
mixed_objects.emplace_back(0x33, 40, 40, 2, 1);
// Decorative objects
mixed_objects.emplace_back(0x36, 45, 45, 3, 0);
mixed_objects.emplace_back(0x38, 50, 50, 1, 0);
// Set ROM for all objects
for (auto& obj : mixed_objects) {
obj.set_rom(rom_.get());
}
auto status = drawer.DrawObjectList(mixed_objects,
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test room rendering error handling
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingErrorHandling) {
Room test_room = CreateTestRoom(0x00);
// Test with null ROM
ObjectDrawer null_drawer(nullptr);
auto palette_group = CreateTestPaletteGroup();
auto status = null_drawer.DrawObjectList(test_room.GetTileObjects(),
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kFailedPrecondition);
}
// Test room rendering with invalid object data
TEST_F(DungeonRenderingIntegrationTest, RoomRenderingWithInvalidObjectData) {
Room test_room = CreateTestRoom(0x00);
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Create objects with invalid data
std::vector<RoomObject> invalid_objects;
invalid_objects.emplace_back(0x999, 5, 5, 1, 0); // Invalid object ID
invalid_objects.emplace_back(0x00, -1, -1, 1, 0); // Negative coordinates
invalid_objects.emplace_back(0x00, 100, 100, 1, 0); // Out of bounds coordinates
invalid_objects.emplace_back(0x00, 5, 5, 255, 0); // Maximum size
// Set ROM for all objects
for (auto& obj : invalid_objects) {
obj.set_rom(rom_.get());
}
// Should handle gracefully
auto status = drawer.DrawObjectList(invalid_objects,
test_room.bg1_buffer(),
test_room.bg2_buffer(),
palette_group);
// Should succeed or fail gracefully
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
} // namespace zelda3
} // namespace yaze

View File

@@ -0,0 +1,324 @@
#include "gtest/gtest.h"
#include "absl/status/status.h"
#include "app/gfx/background_buffer.h"
#include "app/gfx/snes_palette.h"
#include "app/rom.h"
#include "app/zelda3/dungeon/object_drawer.h"
#include "app/zelda3/dungeon/object_parser.h"
#include "app/zelda3/dungeon/room_object.h"
namespace yaze {
namespace zelda3 {
class ObjectRenderingTest : public ::testing::Test {
protected:
void SetUp() override {
// Create a mock ROM for testing
rom_ = std::make_unique<Rom>();
// Initialize with minimal ROM data for testing
std::vector<uint8_t> mock_rom_data(1024 * 1024, 0); // 1MB mock ROM
rom_->LoadFromData(mock_rom_data);
}
void TearDown() override {
rom_.reset();
}
std::unique_ptr<Rom> rom_;
gfx::BackgroundBuffer bg1_;
gfx::BackgroundBuffer bg2_;
// Create a test palette
gfx::SnesPalette CreateTestPalette() {
gfx::SnesPalette palette;
// Add some test colors
palette.AddColor(gfx::SnesColor(0, 0, 0)); // Transparent
palette.AddColor(gfx::SnesColor(255, 0, 0)); // Red
palette.AddColor(gfx::SnesColor(0, 255, 0)); // Green
palette.AddColor(gfx::SnesColor(0, 0, 255)); // Blue
palette.AddColor(gfx::SnesColor(255, 255, 0)); // Yellow
palette.AddColor(gfx::SnesColor(255, 0, 255)); // Magenta
palette.AddColor(gfx::SnesColor(0, 255, 255)); // Cyan
palette.AddColor(gfx::SnesColor(255, 255, 255)); // White
return palette;
}
gfx::PaletteGroup CreateTestPaletteGroup() {
gfx::PaletteGroup group;
group.AddPalette(CreateTestPalette());
return group;
}
};
// Test object drawer initialization
TEST_F(ObjectRenderingTest, ObjectDrawerInitializesCorrectly) {
ObjectDrawer drawer(rom_.get());
// Test that drawer can be created without errors
EXPECT_NE(rom_.get(), nullptr);
}
// Test object parser draw routine detection
TEST_F(ObjectRenderingTest, ObjectParserDetectsDrawRoutines) {
ObjectParser parser(rom_.get());
// Test common object IDs and their expected draw routines
auto info_00 = parser.GetObjectDrawInfo(0x00);
EXPECT_EQ(info_00.draw_routine_id, 0);
EXPECT_EQ(info_00.routine_name, "Rightwards2x2_1to15or32");
EXPECT_TRUE(info_00.is_horizontal);
auto info_01 = parser.GetObjectDrawInfo(0x01);
EXPECT_EQ(info_01.draw_routine_id, 1);
EXPECT_EQ(info_01.routine_name, "Rightwards2x4_1to15or26");
EXPECT_TRUE(info_01.is_horizontal);
auto info_09 = parser.GetObjectDrawInfo(0x09);
EXPECT_EQ(info_09.draw_routine_id, 5);
EXPECT_EQ(info_09.routine_name, "DiagonalAcute_1to16");
EXPECT_FALSE(info_09.is_horizontal);
auto info_34 = parser.GetObjectDrawInfo(0x34);
EXPECT_EQ(info_34.draw_routine_id, 16);
EXPECT_EQ(info_34.routine_name, "Rightwards1x1Solid_1to16_plus3");
EXPECT_TRUE(info_34.is_horizontal);
// Test unmapped object defaults to solid block routine
auto info_unknown = parser.GetObjectDrawInfo(0x999);
EXPECT_EQ(info_unknown.draw_routine_id, 16); // Default solid routine
EXPECT_EQ(info_unknown.routine_name, "DefaultSolid");
}
// Test object drawer with various object types
TEST_F(ObjectRenderingTest, ObjectDrawerHandlesVariousObjectTypes) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Test object 0x00 (horizontal floor tile)
RoomObject floor_object(0x00, 10, 10, 3, 0); // ID, X, Y, size, layer
auto status = drawer.DrawObject(floor_object, bg1_, bg2_, palette_group);
// Should succeed even if tiles aren't loaded (graceful handling)
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test object 0x09 (diagonal stairs)
RoomObject stair_object(0x09, 15, 15, 5, 0);
stair_object.set_rom(rom_.get());
status = drawer.DrawObject(stair_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test object 0x34 (solid block)
RoomObject block_object(0x34, 20, 20, 1, 0);
block_object.set_rom(rom_.get());
status = drawer.DrawObject(block_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test object drawer with different layers
TEST_F(ObjectRenderingTest, ObjectDrawerHandlesDifferentLayers) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Test BG1 layer object
RoomObject bg1_object(0x00, 5, 5, 2, 0); // Layer 0 = BG1
bg1_object.set_rom(rom_.get());
auto status = drawer.DrawObject(bg1_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test BG2 layer object
RoomObject bg2_object(0x01, 10, 10, 2, 1); // Layer 1 = BG2
bg2_object.set_rom(rom_.get());
status = drawer.DrawObject(bg2_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test object drawer with size variations
TEST_F(ObjectRenderingTest, ObjectDrawerHandlesSizeVariations) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Test small object
RoomObject small_object(0x00, 5, 5, 1, 0); // Size = 1
small_object.set_rom(rom_.get());
auto status = drawer.DrawObject(small_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test large object
RoomObject large_object(0x00, 10, 10, 15, 0); // Size = 15
large_object.set_rom(rom_.get());
status = drawer.DrawObject(large_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test maximum size object
RoomObject max_object(0x00, 15, 15, 31, 0); // Size = 31 (0x1F)
max_object.set_rom(rom_.get());
status = drawer.DrawObject(max_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test object drawer with edge cases
TEST_F(ObjectRenderingTest, ObjectDrawerHandlesEdgeCases) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Test object at origin
RoomObject origin_object(0x34, 0, 0, 1, 0);
origin_object.set_rom(rom_.get());
auto status = drawer.DrawObject(origin_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test object with zero size
RoomObject zero_size_object(0x34, 10, 10, 0, 0);
zero_size_object.set_rom(rom_.get());
status = drawer.DrawObject(zero_size_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test object with maximum coordinates
RoomObject max_coord_object(0x34, 63, 63, 1, 0); // Near buffer edge
max_coord_object.set_rom(rom_.get());
status = drawer.DrawObject(max_coord_object, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test object drawer with multiple objects
TEST_F(ObjectRenderingTest, ObjectDrawerHandlesMultipleObjects) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
std::vector<RoomObject> objects;
// Create various test objects
objects.emplace_back(0x00, 5, 5, 3, 0); // Horizontal floor
objects.emplace_back(0x01, 10, 10, 2, 0); // Vertical floor
objects.emplace_back(0x09, 15, 15, 4, 0); // Diagonal stairs
objects.emplace_back(0x34, 20, 20, 1, 1); // Solid block on BG2
// Set ROM for all objects
for (auto& obj : objects) {
obj.set_rom(rom_.get());
}
auto status = drawer.DrawObjectList(objects, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test specific draw routines
TEST_F(ObjectRenderingTest, DrawRoutinesWorkCorrectly) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
// Test rightward patterns
RoomObject rightward_obj(0x00, 5, 5, 5, 0);
rightward_obj.set_rom(rom_.get());
auto status = drawer.DrawObject(rightward_obj, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test diagonal patterns
RoomObject diagonal_obj(0x09, 10, 10, 6, 0);
diagonal_obj.set_rom(rom_.get());
status = drawer.DrawObject(diagonal_obj, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Test solid block patterns
RoomObject solid_obj(0x34, 15, 15, 8, 0);
solid_obj.set_rom(rom_.get());
status = drawer.DrawObject(solid_obj, bg1_, bg2_, palette_group);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
}
// Test object drawer error handling
TEST_F(ObjectRenderingTest, ObjectDrawerHandlesErrorsGracefully) {
ObjectDrawer drawer(nullptr); // No ROM
auto palette_group = CreateTestPaletteGroup();
RoomObject test_object(0x00, 5, 5, 1, 0);
auto status = drawer.DrawObject(test_object, bg1_, bg2_, palette_group);
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kFailedPrecondition);
}
// Test object parser with various object IDs
TEST_F(ObjectRenderingTest, ObjectParserHandlesVariousObjectIDs) {
ObjectParser parser(rom_.get());
// Test subtype 1 objects (0x00-0xFF)
for (int id = 0; id <= 0x40; id += 4) { // Test every 4th object
auto info = parser.GetObjectDrawInfo(id);
EXPECT_GE(info.draw_routine_id, 0);
EXPECT_LT(info.draw_routine_id, 25); // Should be within valid range
EXPECT_FALSE(info.routine_name.empty());
}
// Test some specific important objects
std::vector<int16_t> important_objects = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x15, 0x16, 0x21, 0x22, 0x2F, 0x30, 0x31, 0x32,
0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C,
0x3D, 0x3E, 0x3F, 0x40
};
for (int16_t obj_id : important_objects) {
auto info = parser.GetObjectDrawInfo(obj_id);
EXPECT_GE(info.draw_routine_id, 0);
EXPECT_LT(info.draw_routine_id, 25);
EXPECT_FALSE(info.routine_name.empty());
// Verify tile count is reasonable
EXPECT_GT(info.tile_count, 0);
EXPECT_LE(info.tile_count, 64); // Reasonable upper bound
}
}
// Test object drawer performance with many objects
TEST_F(ObjectRenderingTest, ObjectDrawerPerformanceTest) {
ObjectDrawer drawer(rom_.get());
auto palette_group = CreateTestPaletteGroup();
std::vector<RoomObject> objects;
// Create 100 test objects
for (int i = 0; i < 100; ++i) {
int id = i % 65; // Cycle through object IDs 0-64
int x = (i * 2) % 60; // Spread across buffer
int y = (i * 3) % 60;
int size = (i % 8) + 1; // Size 1-8
int layer = i % 2; // Alternate layers
objects.emplace_back(id, x, y, size, layer);
objects.back().set_rom(rom_.get());
}
// Time the drawing operation
auto start_time = std::chrono::high_resolution_clock::now();
auto status = drawer.DrawObjectList(objects, bg1_, bg2_, palette_group);
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end_time - start_time);
EXPECT_TRUE(status.ok() || status.code() == absl::StatusCode::kOk);
// Should complete in reasonable time (less than 1 second for 100 objects)
EXPECT_LT(duration.count(), 1000);
std::cout << "Drew 100 objects in " << duration.count() << "ms" << std::endl;
}
} // namespace zelda3
} // namespace yaze