yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
draw_routine_types.h
Go to the documentation of this file.
1#ifndef YAZE_ZELDA3_DUNGEON_DRAW_ROUTINES_DRAW_ROUTINE_TYPES_H
2#define YAZE_ZELDA3_DUNGEON_DRAW_ROUTINES_DRAW_ROUTINE_TYPES_H
3
4#include <cstdint>
5#include <functional>
6#include <initializer_list>
7#include <span>
8#include <string>
9
12#include "rom/rom.h"
13
14namespace yaze {
15namespace zelda3 {
16
17// Forward declarations
18class DungeonState;
19struct RoomObject;
20
28 gfx::BackgroundBuffer& target_bg; // Primary buffer to draw to
29 const RoomObject& object; // Object being drawn
30 std::span<const gfx::TileInfo> tiles; // Tile data for the object
31 const DungeonState* state; // Dungeon state (chest states, etc.)
32 Rom* rom; // ROM for additional data lookup
33 int room_id; // Current room ID
34 const uint8_t* room_gfx_buffer; // Room-specific graphics buffer
35 gfx::BackgroundBuffer* secondary_bg; // Secondary BG for dual-layer routines (nullable)
36
37 // Canvas dimensions
38 static constexpr int kMaxTilesX = 64;
39 static constexpr int kMaxTilesY = 64;
40
41 // Helper to check if dual-BG drawing is available
42 bool HasSecondaryBG() const { return secondary_bg != nullptr; }
43};
44
51using DrawRoutineFn = std::function<void(const DrawContext& ctx)>;
52
57 int id; // Routine ID (0-39)
58 std::string name; // Human-readable name
59 DrawRoutineFn function; // The actual draw function
60 bool draws_to_both_bgs; // If true, draws to BG1 and BG2
61 int base_width; // Base width in tiles (0 = variable)
62 int base_height; // Base height in tiles (0 = variable)
63
64 // Minimum number of tiles required for this routine to render correctly.
65 // When > 0, ObjectDrawer::DrawObject will skip the routine if the object's
66 // tile payload is smaller, preventing out-of-bounds access in fixed-size
67 // draw patterns. Routines with variable tile counts should leave this at 0.
68 int min_tiles = 0;
69
70 // Category for organization
71 enum class Category {
72 Special, // Chest, Nothing, Custom, DoorSwitcher
73 Rightwards, // Horizontal extension patterns
74 Downwards, // Vertical extension patterns
75 Diagonal, // Diagonal patterns
76 Corner // Corner and special shape patterns
77 };
79};
80
84namespace DrawRoutineUtils {
85
87 void (*)(int tile_x, int tile_y, const gfx::TileInfo& tile_info,
88 void* user_data);
89
90// Optional trace hook for validation/testing (used by ObjectDrawer).
91void SetTraceHook(TraceHookFn hook, void* user_data, bool trace_only);
92void ClearTraceHook();
93
94inline uint16_t TileIdAt(const gfx::BackgroundBuffer& bg, int tile_x,
95 int tile_y) {
96 return bg.GetTileAt(tile_x, tile_y) & 0x03FF;
97}
98
99inline bool ExistingTileMatchesAny(const gfx::BackgroundBuffer& bg, int tile_x,
100 int tile_y,
101 std::initializer_list<uint16_t> tile_ids) {
102 const uint16_t existing_tile_id = TileIdAt(bg, tile_x, tile_y);
103 for (const uint16_t tile_id : tile_ids) {
104 if (existing_tile_id == tile_id) {
105 return true;
106 }
107 }
108 return false;
109}
110
114inline bool IsValidTilePosition(int tile_x, int tile_y) {
115 return tile_x >= 0 && tile_x < DrawContext::kMaxTilesX && tile_y >= 0 &&
117}
118
127void WriteTile8(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
128 const gfx::TileInfo& tile_info);
129
133void DrawBlock2x2(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
134 std::span<const gfx::TileInfo> tiles, int offset = 0);
135
139void DrawBlock2x4(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
140 std::span<const gfx::TileInfo> tiles, int offset = 0);
141
145void DrawBlock4x2(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
146 std::span<const gfx::TileInfo> tiles, int offset = 0);
147
151void DrawBlock4x4(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
152 std::span<const gfx::TileInfo> tiles, int offset = 0);
153
154} // namespace DrawRoutineUtils
155
156} // namespace zelda3
157} // namespace yaze
158
159#endif // YAZE_ZELDA3_DUNGEON_DRAW_ROUTINES_DRAW_ROUTINE_TYPES_H
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:28
uint16_t GetTileAt(int x, int y) const
SNES 16-bit tile metadata container.
Definition snes_tile.h:52
Interface for accessing dungeon game state.
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)
uint16_t TileIdAt(const gfx::BackgroundBuffer &bg, int tile_x, int tile_y)
void(*)(int tile_x, int tile_y, const gfx::TileInfo &tile_info, void *user_data) TraceHookFn
bool ExistingTileMatchesAny(const gfx::BackgroundBuffer &bg, int tile_x, int tile_y, std::initializer_list< uint16_t > tile_ids)
bool IsValidTilePosition(int tile_x, int tile_y)
Check if tile position is within canvas bounds.
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)
std::function< void(const DrawContext &ctx)> DrawRoutineFn
Function signature for a draw routine.
Context passed to draw routines containing all necessary state.
static constexpr int kMaxTilesY
std::span< const gfx::TileInfo > tiles
gfx::BackgroundBuffer & target_bg
static constexpr int kMaxTilesX
gfx::BackgroundBuffer * secondary_bg
const DungeonState * state
Metadata about a draw routine.