Skip to content

Commit 942d055

Browse files
committed
Update DungeonObjectRenderer experiment
1 parent ed7204b commit 942d055

File tree

3 files changed

+60
-34
lines changed

3 files changed

+60
-34
lines changed

Diff for: src/app/editor/dungeon_editor.cc

+8-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace app {
1515
namespace editor {
1616

1717
void DungeonEditor::Update() {
18-
if (!is_loaded_) {
18+
if (!is_loaded_ && rom()->isLoaded()) {
1919
for (int i = 0; i < 0x100; i++) {
2020
rooms_.emplace_back(zelda3::dungeon::Room(i));
2121
rooms_[i].LoadHeader();
@@ -56,9 +56,6 @@ void DungeonEditor::Update() {
5656
ImGui::TableNextColumn();
5757
DrawDungeonTabView();
5858
ImGui::TableNextColumn();
59-
if (ImGui::Button("dungeon object renderer")) {
60-
object_renderer_.RenderObjectsAsBitmaps();
61-
}
6259
DrawTileSelector();
6360
ImGui::EndTable();
6461
}
@@ -158,7 +155,7 @@ void DungeonEditor::DrawDungeonCanvas(int room_id) {
158155
}
159156

160157
void DungeonEditor::DrawToolset() {
161-
if (ImGui::BeginTable("DWToolset", 9, ImGuiTableFlags_SizingFixedFit,
158+
if (ImGui::BeginTable("DWToolset", 10, ImGuiTableFlags_SizingFixedFit,
162159
ImVec2(0, 0))) {
163160
ImGui::TableSetupColumn("#undoTool");
164161
ImGui::TableSetupColumn("#redoTool");
@@ -196,6 +193,12 @@ void DungeonEditor::DrawToolset() {
196193

197194
ImGui::TableNextColumn();
198195
ImGui::Button(ICON_MD_PEST_CONTROL_RODENT);
196+
197+
ImGui::TableNextColumn();
198+
if (ImGui::Button("Load Dungeon Objects")) {
199+
// object_renderer_.CreateVramFromRoomBlockset();
200+
object_renderer_.RenderObjectsAsBitmaps(*rom());
201+
}
199202
ImGui::EndTable();
200203
}
201204
}

Diff for: src/app/zelda3/dungeon/room.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class Room : public SharedROM {
140140
uint8_t layout = 0;
141141

142142
uint16_t message_id_ = 0;
143-
143+
144144
gfx::Bitmap current_graphics_;
145145

146146
private:

Diff for: src/app/zelda3/dungeon/room_object.h

+51-28
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,76 @@ namespace app {
1919
namespace zelda3 {
2020
namespace dungeon {
2121

22-
class DungeonObjectRenderer : public SharedROM {
22+
class DungeonObjectRenderer {
2323
public:
24-
2524
struct PseudoVram {
2625
std::vector<gfx::Bitmap> sheets;
2726
};
2827

2928
void CreateVramFromRoomBlockset() {
30-
auto bitmap_manager = rom()->BitmapManager();
31-
uint16_t room_id = 0;
32-
auto room_blockset = rom()->room_blockset_ids[room_id];
33-
34-
for (const auto blockset_id : room_blockset) {
35-
auto blockset = bitmap_manager[(uint16_t)blockset_id];
36-
vram_.sheets.push_back(*blockset.get());
37-
}
38-
29+
// auto bitmap_manager = rom()->BitmapManager();
30+
// uint16_t room_id = 0;
31+
// auto room_blockset = rom()->room_blockset_ids[room_id];
32+
33+
// for (const auto blockset_id : room_blockset) {
34+
// auto blockset = bitmap_manager[(uint16_t)blockset_id];
35+
// vram_.sheets.push_back(*blockset.get());
36+
// }
3937
}
4038

41-
void RenderObjectsAsBitmaps() {
39+
void RenderObjectsAsBitmaps(ROM& rom) {
40+
memory_.Initialize(rom.vector());
41+
cpu.Init();
42+
4243
auto subtype1_ptr = core::subtype1_tiles;
43-
auto subtype1_routine_ptr =
44-
core::subtype1_tiles + 0x200; // Where the draw routines start
44+
auto subtype1_routine_ptr = core::subtype1_tiles + 0x200;
45+
std::array<uint16_t, 256> routine_ptrs;
46+
for (int i = 0; i < 256; i++) {
47+
uint16_t actual_ptr = rom.toint16(subtype1_routine_ptr + (i * 2));
48+
routine_ptrs[i] = actual_ptr;
49+
std::cout << std::hex << routine_ptrs[i] << std::endl;
50+
}
51+
52+
int i = 0;
53+
for (const auto routine_ptr : routine_ptrs) {
54+
cpu.PC = routine_ptr - 2;
55+
cpu.PB = 0x00;
56+
57+
auto cycles_to_run = clock_.GetCycleCount();
58+
59+
while (true) {
60+
auto opcode = cpu.FetchByte();
61+
// Fetch and execute an instruction
62+
cpu.ExecuteInstruction(opcode);
63+
64+
// Handle any interrupts, if necessary
65+
cpu.HandleInterrupts();
66+
67+
// Check if the instruction is RTS
68+
if (opcode == 0x60) {
69+
break;
70+
}
71+
i++;
72+
if (i > 50) {
73+
break;
74+
}
75+
}
76+
}
4577

4678
auto subtype2_ptr = core::subtype2_tiles;
4779
auto subtype2_routine_ptr =
4880
core::subtype2_tiles + 0x80; // Where the draw routines start
81+
std::array<uint16_t, 128> subtype2_routine_ptrs;
82+
for (int i = 0; i < 128; i++) {
83+
subtype2_routine_ptrs[i] = subtype2_routine_ptr + i * 2;
84+
}
4985

5086
auto subtype3_ptr = core::subtype3_tiles;
5187
auto subtype3_routine_ptr =
5288
core::subtype3_tiles + 0x100; // Where the draw routines start
53-
54-
auto data = (*rom()).vector();
55-
// Construct a copy of the rooms VRAM
56-
// Jump to the routine that draws the object based on the ID
57-
// Run the routine and get the VRAM data using the CPU and PPU
58-
// Render the VRAM data to a bitmap
59-
60-
memory_.Initialize(data);
61-
cpu.PC = subtype1_routine_ptr;
62-
cpu.JMP(cpu.FetchWord());
63-
auto dest = cpu.PC + 0x10;
64-
while (cpu.PC < dest) {
65-
cpu.ExecuteInstruction(cpu.FetchByte());
66-
}
6789
}
6890

91+
std::vector<uint8_t> rom_data_;
6992
emu::MemoryImpl memory_;
7093
emu::ClockImpl clock_;
7194
emu::CPU cpu{memory_, clock_};

0 commit comments

Comments
 (0)