yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
draw_routine_types.cc
Go to the documentation of this file.
2
5
6namespace yaze {
7namespace zelda3 {
8namespace DrawRoutineUtils {
9
10namespace {
11struct TraceState {
12 TraceHookFn hook = nullptr;
13 void* user_data = nullptr;
14 bool trace_only = false;
15};
16
18} // namespace
19
20void SetTraceHook(TraceHookFn hook, void* user_data, bool trace_only) {
21 g_trace_state.hook = hook;
22 g_trace_state.user_data = user_data;
23 g_trace_state.trace_only = trace_only;
24}
25
27 g_trace_state = TraceState{};
28}
29
30void WriteTile8(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
31 const gfx::TileInfo& tile_info) {
32 // Rows 64-127 reach only a trace hook: the game's upper tilemap ($7E2000)
33 // runs straight into the lower one ($7E4000), so ObjectDrawer moves those
34 // writes to BG2 at row - 64.
35 const bool overflow_row = g_trace_state.hook != nullptr && tile_x >= 0 &&
36 tile_x < 64 && tile_y >= 64 && tile_y < 128;
37 if (!IsValidTilePosition(tile_x, tile_y) && !overflow_row) {
38 return;
39 }
40 if (g_trace_state.hook) {
41 g_trace_state.hook(&bg, tile_x, tile_y, tile_info, g_trace_state.user_data);
42 if (g_trace_state.trace_only || overflow_row) {
43 return;
44 }
45 }
46 // Convert TileInfo to the 16-bit word format and store in tile buffer
47 uint16_t word = gfx::TileInfoToWord(tile_info);
48 bg.SetTileAt(tile_x, tile_y, word);
49}
50
51void DrawBlock2x2(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
52 std::span<const gfx::TileInfo> tiles, int offset) {
53 // Draw a 2x2 block (4 tiles) at the given position
54 // Layout: [0][1]
55 // [2][3]
56 if (offset + 3 >= static_cast<int>(tiles.size()))
57 return;
58
59 WriteTile8(bg, tile_x, tile_y, tiles[offset]);
60 WriteTile8(bg, tile_x + 1, tile_y, tiles[offset + 1]);
61 WriteTile8(bg, tile_x, tile_y + 1, tiles[offset + 2]);
62 WriteTile8(bg, tile_x + 1, tile_y + 1, tiles[offset + 3]);
63}
64
65void DrawBlock2x4(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
66 std::span<const gfx::TileInfo> tiles, int offset) {
67 // Draw a 2x4 block (8 tiles) at the given position
68 // Layout: [0][1]
69 // [2][3]
70 // [4][5]
71 // [6][7]
72 if (offset + 7 >= static_cast<int>(tiles.size()))
73 return;
74
75 for (int row = 0; row < 4; row++) {
76 WriteTile8(bg, tile_x, tile_y + row, tiles[offset + row * 2]);
77 WriteTile8(bg, tile_x + 1, tile_y + row, tiles[offset + row * 2 + 1]);
78 }
79}
80
81void DrawBlock4x2(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
82 std::span<const gfx::TileInfo> tiles, int offset) {
83 // Draw a 4x2 block (8 tiles) at the given position
84 // Layout: [0][1][2][3]
85 // [4][5][6][7]
86 if (offset + 7 >= static_cast<int>(tiles.size()))
87 return;
88
89 for (int col = 0; col < 4; col++) {
90 WriteTile8(bg, tile_x + col, tile_y, tiles[offset + col]);
91 WriteTile8(bg, tile_x + col, tile_y + 1, tiles[offset + 4 + col]);
92 }
93}
94
95void DrawBlock4x4(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
96 std::span<const gfx::TileInfo> tiles, int offset) {
97 // Draw a 4x4 block (16 tiles) at the given position
98 // Layout: [0 ][1 ][2 ][3 ]
99 // [4 ][5 ][6 ][7 ]
100 // [8 ][9 ][10][11]
101 // [12][13][14][15]
102 if (offset + 15 >= static_cast<int>(tiles.size()))
103 return;
104
105 for (int row = 0; row < 4; row++) {
106 for (int col = 0; col < 4; col++) {
107 WriteTile8(bg, tile_x + col, tile_y + row, tiles[offset + row * 4 + col]);
108 }
109 }
110}
111
112} // namespace DrawRoutineUtils
113} // namespace zelda3
114} // namespace yaze
void SetTileAt(int x, int y, uint16_t value)
SNES 16-bit tile metadata container.
Definition snes_tile.h:52
uint16_t TileInfoToWord(TileInfo tile_info)
Definition snes_tile.cc:362
void DrawBlock2x2(gfx::BackgroundBuffer &bg, int tile_x, int tile_y, std::span< const gfx::TileInfo > tiles, int offset)
Draw a 2x2 block of tiles (16x16 pixels)
void DrawBlock2x4(gfx::BackgroundBuffer &bg, int tile_x, int tile_y, std::span< const gfx::TileInfo > tiles, int offset)
Draw a 2x4 block of tiles (16x32 pixels)
void SetTraceHook(TraceHookFn hook, void *user_data, bool trace_only)
void DrawBlock4x2(gfx::BackgroundBuffer &bg, int tile_x, int tile_y, std::span< const gfx::TileInfo > tiles, int offset)
Draw a 4x2 block of tiles (32x16 pixels)
bool IsValidTilePosition(int tile_x, int tile_y)
Check if tile position is within canvas bounds.
void(*)(gfx::BackgroundBuffer *bg, int tile_x, int tile_y, const gfx::TileInfo &tile_info, void *user_data) TraceHookFn
void WriteTile8(gfx::BackgroundBuffer &bg, int tile_x, int tile_y, const gfx::TileInfo &tile_info)
Write an 8x8 tile to the background buffer.
void DrawBlock4x4(gfx::BackgroundBuffer &bg, int tile_x, int tile_y, std::span< const gfx::TileInfo > tiles, int offset)
Draw a 4x4 block of tiles (32x32 pixels)