yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
object_renderer.cc
Go to the documentation of this file.
2
3namespace yaze {
4namespace zelda3 {
5
6void DungeonObjectRenderer::LoadObject(uint32_t routine_ptr,
7 std::array<uint8_t, 16>& sheet_ids) {
8 vram_.sheets = sheet_ids;
9
10 rom_data_ = rom()->vector();
11 // Prepare the CPU and memory environment
12 memory_.Initialize(rom_data_);
13
14 // Configure the object based on the fetched information
16
17 // Run the CPU emulation for the object's draw routines
18 RenderObject(routine_ptr);
19}
20
22 cpu.A = 0x03D8;
23 cpu.X = 0x03D8;
24 cpu.DB = 0x7E;
25 // VRAM target destinations
26 cpu.WriteLong(0xBF, 0x7E2000);
27 cpu.WriteLong(0xCB, 0x7E2080);
28 cpu.WriteLong(0xC2, 0x7E2002);
29 cpu.WriteLong(0xCE, 0x7E2082);
30 cpu.SetAccumulatorSize(false);
31 cpu.SetIndexSize(false);
32}
33
61void DungeonObjectRenderer::RenderObject(uint32_t routine_ptr) {
62 cpu.PB = 0x01;
63 cpu.PC = routine_ptr;
64
65 // Set up initial state for object drawing
66 cpu.Y = 0; // Start at the beginning of the tilemap
67 cpu.D = 0x7E; // Direct page register for memory access
68
69 // Push return address to stack
70 cpu.PushLong(0x01 << 16 | 0xFFFF); // Push a dummy return address
71
72 // Set up a maximum instruction count to prevent infinite loops
73 const int MAX_INSTRUCTIONS = 10000;
74 int instruction_count = 0;
75
76 // Execute instructions until we hit a return instruction or max count
77 while (instruction_count < MAX_INSTRUCTIONS) {
78 uint8_t opcode = cpu.ReadByte(cpu.PB << 16 | cpu.PC);
79
80 // Check for RTS (Return from Subroutine) instruction
81 if (opcode == 0x60) {
82 // Execute the RTS instruction
83 cpu.ExecuteInstruction(opcode);
84 break; // Exit the loop after RTS
85 }
86
87 // Execute the instruction
88 cpu.ExecuteInstruction(opcode);
89 instruction_count++;
90 }
91
92 // If we hit the max instruction count, log a warning
93 if (instruction_count >= MAX_INSTRUCTIONS) {
94 std::cerr << "Warning: Object rendering hit maximum instruction count"
95 << std::endl;
96 }
97
99}
100
101// In the underworld, this holds a copy of the entire BG tilemap for
102// Layer 1 (BG2) in TILEMAPA
103// Layer 2 (BG1) in TILEMAPB
105 // Initialize the tilemap with zeros
106 tilemap_.resize(0x2000, 0);
107
108 // Iterate over tilemap in memory to read tile IDs
109 for (int tile_index = 0; tile_index < 512; tile_index++) {
110 // Read the tile ID from memory
111 uint16_t tile_id = memory_.ReadWord(0x7E2000 + tile_index * 2);
112
113 // Skip empty tiles (0x0000)
114 if (tile_id == 0) continue;
115
116 // Calculate sheet number (each sheet contains 32 tiles)
117 int sheet_number = tile_id / 32;
118
119 // Ensure sheet number is valid
120 if (sheet_number >= vram_.sheets.size()) {
121 std::cerr << "Warning: Invalid sheet number " << sheet_number
122 << std::endl;
123 continue;
124 }
125
126 // Calculate position in the tilemap
127 int tile_x = (tile_index % 32) * 8;
128 int tile_y = (tile_index / 32) * 8;
129
130 // Get the graphics sheet
132 vram_.sheets[sheet_number]);
133
134 // Calculate the offset in the tilemap
135 int tilemap_offset = tile_y * 256 + tile_x;
136
137 // Copy the tile from the graphics sheet to the tilemap
138 sheet.Get8x8Tile(tile_id % 32, 0, 0, tilemap_, tilemap_offset);
139 }
140
141 // Create the bitmap from the tilemap
142 bitmap_.Create(256, 256, 8, tilemap_);
143}
144
146 size_t transparent_index) {
147 // Apply the palette to the bitmap
148 bitmap_.SetPaletteWithTransparent(palette, transparent_index);
149
150 // Store the palette in the VRAM structure for future reference
151 vram_.palettes.clear();
152 vram_.palettes.push_back(palette);
153}
154
155} // namespace zelda3
156} // namespace yaze
static GraphicsSheetManager & GetInstance()
Definition rom.h:272
auto rom()
Definition rom.h:384
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
void UpdateObjectBitmap()
Updates the bitmap with the rendered object.
void RenderObject(uint32_t routine_ptr)
Executes the object drawing routine.
void SetPalette(const gfx::SnesPalette &palette, size_t transparent_index=0)
Sets the palette for the rendered object.
void LoadObject(uint32_t routine_ptr, std::array< uint8_t, 16 > &sheet_ids)
Loads and renders a dungeon object.
void ConfigureObject()
Configures the CPU state for object rendering.
Zelda 3 specific classes and functions.
Main namespace for the application.
Definition controller.cc:18