yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
object_drawer.h
Go to the documentation of this file.
1#ifndef YAZE_APP_ZELDA3_DUNGEON_OBJECT_DRAWER_H
2#define YAZE_APP_ZELDA3_DUNGEON_OBJECT_DRAWER_H
3
4#include <functional>
5#include <unordered_map>
6#include <vector>
7
8#include "absl/status/status.h"
12#include "rom/rom.h"
18
19namespace yaze {
20namespace zelda3 {
21
37 public:
38 explicit ObjectDrawer(Rom* rom, int room_id,
39 const uint8_t* room_gfx_buffer = nullptr);
40
51 absl::Status DrawObject(const RoomObject& object, gfx::BackgroundBuffer& bg1,
53 const gfx::PaletteGroup& palette_group,
54 const DungeonState* state = nullptr,
55 gfx::BackgroundBuffer* layout_bg1 = nullptr,
56 gfx::BackgroundBuffer* layout_bg2 = nullptr);
57
58 struct DoorDef {
61 uint8_t position;
62
63 // Helper to get visible render coordinates using DoorPositionManager.
64 std::pair<int, int> GetTileCoords() const {
66 direction);
67 }
68
69 // Helper to get door dimensions
73 };
74
75 // Reset the chest-only and shared chest/big-key-lock room-event indexes.
83 // Room objects 0xC4/0xDB copy the active Floor 1/Floor 2 pattern rather than
84 // reading a normal object tile payload.
85 void SetRoomFloorGraphics(uint8_t floor1, uint8_t floor2) {
86 floor1_graphics_ = floor1 & 0x0F;
87 floor2_graphics_ = floor2 & 0x0F;
89 }
90
101 void DrawDoor(const DoorDef& door, int door_index, gfx::BackgroundBuffer& bg1,
102 gfx::BackgroundBuffer& bg2, const DungeonState* state = nullptr,
103 gfx::BackgroundBuffer* layout_bg1 = nullptr,
104 gfx::BackgroundBuffer* layout_bg2 = nullptr);
105
113 void DrawPotItem(uint8_t item_id, int x, int y, gfx::BackgroundBuffer& bg);
114
130 absl::Status DrawObjectList(const std::vector<RoomObject>& objects,
133 const gfx::PaletteGroup& palette_group,
134 const DungeonState* state = nullptr,
135 gfx::BackgroundBuffer* layout_bg1 = nullptr,
136 bool reset_room_event_indices = true,
137 gfx::BackgroundBuffer* layout_bg2 = nullptr);
138
144 int GetDrawRoutineId(int16_t object_id) const;
145
150 return static_cast<int>(draw_routines_.size());
151 }
152
158
167 void DrawTileToBitmap(gfx::Bitmap& bitmap, const gfx::TileInfo& tile_info,
168 int pixel_x, int pixel_y, const uint8_t* tiledata);
169
184 absl::Status DrawRoomDrawObjectData2x2(uint16_t object_id, int tile_x,
185 int tile_y,
187 uint16_t room_draw_object_data_offset,
190
196 std::pair<int, int> CalculateObjectDimensions(const RoomObject& object);
197
198 struct TileTrace {
199 uint16_t object_id = 0;
200 uint8_t size = 0;
201 uint8_t layer = 0;
202 int16_t x_tile = 0;
203 int16_t y_tile = 0;
204 uint16_t tile_id = 0;
205 uint8_t flags = 0;
206 };
207
208 void SetTraceCollector(std::vector<TileTrace>* collector,
209 bool trace_only = false);
210 void ClearTraceCollector();
211 bool TraceOnly() const { return trace_only_; }
212
213 protected:
214 // Draw routine function type
215 using DrawRoutine = std::function<void(
217 std::span<const gfx::TileInfo>, const DungeonState*)>;
218
219 // Core draw routines (based on ZScream's subtype1_routines table)
220 void DrawChest(const RoomObject& obj, gfx::BackgroundBuffer& bg,
221 std::span<const gfx::TileInfo> tiles,
222 const DungeonState* state = nullptr);
223 void DrawBigChest(const RoomObject& obj, gfx::BackgroundBuffer& bg,
224 std::span<const gfx::TileInfo> tiles,
225 const DungeonState* state = nullptr);
227 std::span<const gfx::TileInfo> tiles,
228 const DungeonState* state = nullptr);
229
230 void DrawNothing(const RoomObject& obj, gfx::BackgroundBuffer& bg,
231 std::span<const gfx::TileInfo> tiles,
232 const DungeonState* state = nullptr);
233 void CustomDraw(const RoomObject& obj, gfx::BackgroundBuffer& bg,
234 std::span<const gfx::TileInfo> tiles,
235 const DungeonState* state = nullptr);
237 std::span<const gfx::TileInfo> tiles,
238 const DungeonState* state = nullptr);
241 std::span<const gfx::TileInfo> tiles,
242 const DungeonState* state = nullptr);
243
244 // Type 3 / Special Routines
246 std::span<const gfx::TileInfo> tiles, int width,
247 int height);
248
249 // Custom Object Routine
251 std::span<const gfx::TileInfo> tiles,
252 const DungeonState* state = nullptr);
254 int tile_y);
255
256 // Utility methods
257 // Execute a DrawRoutineRegistry routine (pure DrawContext function) but render
258 // via ObjectDrawer::WriteTile8 so output hits the bitmap-backed buffers used
259 // by RoomLayerManager compositing.
260 void DrawUsingRegistryRoutine(int routine_id, const RoomObject& obj,
262 std::span<const gfx::TileInfo> tiles,
263 const DungeonState* state);
264 void WriteTile8(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
265 const gfx::TileInfo& tile_info);
266 bool IsValidTilePosition(int tile_x, int tile_y) const;
267
268 // Check if a draw routine should render to both BG1 and BG2
269 // Uses metadata instead of hardcoded routine IDs
270 static bool RoutineDrawsToBothBGs(int routine_id);
271
272 // Record a deferred BG1 reveal instead of mutating the raw BG1 pixels.
273 void MarkBg1RectRevealed(gfx::BackgroundBuffer& bg1, int start_px,
274 int start_py, int pixel_width, int pixel_height);
276 const gfx::TileInfo& tile_info,
277 int pixel_x, int pixel_y,
278 const uint8_t* tiledata);
279 static bool RequiresRectangularBg1Mask(const RoomObject& object);
280
281 // Door indicator fallback when graphics unavailable
282 void DrawDoorIndicator(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
283 int width, int height, DoorType type,
284 DoorDirection direction);
285
286 // Draw routine function array (indexed by routine ID from DrawRoutineRegistry)
287 std::vector<DrawRoutine> draw_routines_;
289
291 uint16_t object_id = 0;
292 uint8_t size = 0;
293 uint8_t layer = 0;
294 };
295
296 void SetTraceContext(const RoomObject& object, RoomObject::LayerType layer);
297 void PushTrace(int tile_x, int tile_y, const gfx::TileInfo& tile_info);
298 static void TraceHookThunk(gfx::BackgroundBuffer* bg, int tile_x, int tile_y,
299 const gfx::TileInfo& tile_info, void* user_data);
300
302 std::vector<TileTrace>* trace_collector_ = nullptr;
303 bool trace_only_ = false;
304
307 mutable int current_chest_index_ = 0;
310 uint8_t floor1_graphics_ = 0;
311 uint8_t floor2_graphics_ = 0;
321 const uint8_t*
322 room_gfx_buffer_; // Room-specific graphics buffer (current_gfx16_)
323
324 // Canvas dimensions in tiles (64x64 = 512x512 pixels)
325 static constexpr int kMaxTilesX = 64;
326 static constexpr int kMaxTilesY = 64;
327};
328
329} // namespace zelda3
330} // namespace yaze
331
332#endif // YAZE_APP_ZELDA3_DUNGEON_OBJECT_DRAWER_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
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:69
SNES 16-bit tile metadata container.
Definition snes_tile.h:52
static std::pair< int, int > PositionToRenderTileCoords(uint8_t position, DoorDirection direction)
Convert encoded position to the top-left tile of the visible door.
Interface for accessing dungeon game state.
Draws dungeon objects to background buffers using game patterns.
int GetDrawRoutineId(int16_t object_id) const
Get draw routine ID for an object.
void WriteTile8(gfx::BackgroundBuffer &bg, int tile_x, int tile_y, const gfx::TileInfo &tile_info)
void DrawTileToBitmap(gfx::Bitmap &bitmap, const gfx::TileInfo &tile_info, int pixel_x, int pixel_y, const uint8_t *tiledata)
Draw a single tile directly to bitmap.
void InitializeDrawRoutines()
Initialize draw routine registry Must be called before drawing objects.
void DrawRightwards4x4_1to16(const RoomObject &obj, gfx::BackgroundBuffer &bg, std::span< const gfx::TileInfo > tiles, const DungeonState *state=nullptr)
int GetDrawRoutineCount() const
Get the total number of registered draw routines.
gfx::BackgroundBuffer * active_mask_source_bg_
void DrawBigChest(const RoomObject &obj, gfx::BackgroundBuffer &bg, std::span< const gfx::TileInfo > tiles, const DungeonState *state=nullptr)
std::vector< TileTrace > * trace_collector_
gfx::BackgroundBuffer * active_layout_bg1_mask_
std::vector< DrawRoutine > draw_routines_
void DrawUsingRegistryRoutine(int routine_id, const RoomObject &obj, gfx::BackgroundBuffer &bg, std::span< const gfx::TileInfo > tiles, const DungeonState *state)
gfx::BackgroundBuffer * registry_secondary_bg_
static bool RequiresRectangularBg1Mask(const RoomObject &object)
static void TraceHookThunk(gfx::BackgroundBuffer *bg, int tile_x, int tile_y, const gfx::TileInfo &tile_info, void *user_data)
void SetRoomFloorGraphics(uint8_t floor1, uint8_t floor2)
void MarkBg1OpaqueTilePixelsRevealed(gfx::BackgroundBuffer &bg1, const gfx::TileInfo &tile_info, int pixel_x, int pixel_y, const uint8_t *tiledata)
void SetBG1RevealMaskSource(gfx::BG1RevealMaskSource source)
std::pair< int, int > CalculateObjectDimensions(const RoomObject &object)
Calculate the dimensions (width, height) of an object in pixels.
void SetTraceContext(const RoomObject &object, RoomObject::LayerType layer)
void MarkBg1RectRevealed(gfx::BackgroundBuffer &bg1, int start_px, int start_py, int pixel_width, int pixel_height)
gfx::BackgroundBuffer * active_object_bg1_mask_
const uint8_t * room_gfx_buffer_
static bool RoutineDrawsToBothBGs(int routine_id)
void DrawNothing(const RoomObject &obj, gfx::BackgroundBuffer &bg, std::span< const gfx::TileInfo > tiles, const DungeonState *state=nullptr)
void DrawDoor(const DoorDef &door, int door_index, gfx::BackgroundBuffer &bg1, gfx::BackgroundBuffer &bg2, const DungeonState *state=nullptr, gfx::BackgroundBuffer *layout_bg1=nullptr, gfx::BackgroundBuffer *layout_bg2=nullptr)
Draw a door to background buffers.
std::function< void( ObjectDrawer *, const RoomObject &, gfx::BackgroundBuffer &, std::span< const gfx::TileInfo >, const DungeonState *)> DrawRoutine
void DrawBigKeyLock(const RoomObject &obj, gfx::BackgroundBuffer &bg, std::span< const gfx::TileInfo > tiles, const DungeonState *state=nullptr)
RoomObject::LayerType registry_primary_layer_
void DrawPotItem(uint8_t item_id, int x, int y, gfx::BackgroundBuffer &bg)
Draw a pot item visualization.
void DrawDoorIndicator(gfx::BackgroundBuffer &bg, int tile_x, int tile_y, int width, int height, DoorType type, DoorDirection direction)
void DrawLargeCanvasObject(const RoomObject &obj, gfx::BackgroundBuffer &bg, std::span< const gfx::TileInfo > tiles, int width, int height)
void PushTrace(int tile_x, int tile_y, const gfx::TileInfo &tile_info)
absl::Status DrawObjectList(const std::vector< RoomObject > &objects, gfx::BackgroundBuffer &bg1, gfx::BackgroundBuffer &bg2, const gfx::PaletteGroup &palette_group, const DungeonState *state=nullptr, gfx::BackgroundBuffer *layout_bg1=nullptr, bool reset_room_event_indices=true, gfx::BackgroundBuffer *layout_bg2=nullptr)
Draw all objects in a room.
void DrawCustomObject(const RoomObject &obj, gfx::BackgroundBuffer &bg, std::span< const gfx::TileInfo > tiles, const DungeonState *state=nullptr)
void DrawRightwardsDecor4x3spaced4_1to16(const RoomObject &obj, gfx::BackgroundBuffer &bg, std::span< const gfx::TileInfo > tiles, const DungeonState *state=nullptr)
static constexpr int kMaxTilesX
void DrawChest(const RoomObject &obj, gfx::BackgroundBuffer &bg, std::span< const gfx::TileInfo > tiles, const DungeonState *state=nullptr)
void CustomDraw(const RoomObject &obj, gfx::BackgroundBuffer &bg, std::span< const gfx::TileInfo > tiles, const DungeonState *state=nullptr)
void DrawMissingCustomObjectPlaceholder(gfx::BackgroundBuffer &bg, int tile_x, int tile_y)
absl::Status DrawObject(const RoomObject &object, gfx::BackgroundBuffer &bg1, gfx::BackgroundBuffer &bg2, const gfx::PaletteGroup &palette_group, const DungeonState *state=nullptr, gfx::BackgroundBuffer *layout_bg1=nullptr, gfx::BackgroundBuffer *layout_bg2=nullptr)
Draw a room object to background buffers.
ObjectDrawer(Rom *rom, int room_id, const uint8_t *room_gfx_buffer=nullptr)
static constexpr int kMaxTilesY
absl::Status DrawRoomDrawObjectData2x2(uint16_t object_id, int tile_x, int tile_y, RoomObject::LayerType layer, uint16_t room_draw_object_data_offset, gfx::BackgroundBuffer &bg1, gfx::BackgroundBuffer &bg2)
Draw a fixed 2x2 (16x16) tile pattern from RoomDrawObjectData.
void SetTraceCollector(std::vector< TileTrace > *collector, bool trace_only=false)
bool IsValidTilePosition(int tile_x, int tile_y) const
gfx::BG1RevealMaskSource bg1_reveal_mask_source_
const gfx::BackgroundBuffer * registry_primary_layout_bg_
RoomObject::LayerType registry_secondary_layer_
constexpr DoorDimensions GetDoorDimensions(DoorDirection dir)
Get door dimensions based on direction.
Definition door_types.h:253
DoorType
Door types from ALTTP.
Definition door_types.h:33
DoorDirection
Door direction on room walls.
Definition door_types.h:18
Represents a group of palettes.
Door dimensions in tiles (8x8 pixel tiles)
Definition door_types.h:221
std::pair< int, int > GetTileCoords() const
DoorDimensions GetDimensions() const