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
50 absl::Status DrawObject(const RoomObject& object, gfx::BackgroundBuffer& bg1,
52 const gfx::PaletteGroup& palette_group,
53 const DungeonState* state = nullptr,
54 gfx::BackgroundBuffer* layout_bg1 = nullptr);
55
56 struct DoorDef {
59 uint8_t position;
60
61 // Helper to get visible render coordinates using DoorPositionManager.
62 std::pair<int, int> GetTileCoords() const {
64 direction);
65 }
66
67 // Helper to get door dimensions
71 };
72
73 // Reset the chest-only and shared chest/big-key-lock room-event indexes.
78 void SetAllowTrackCornerAliases(bool allow) {
80 }
84
91 void DrawDoor(const DoorDef& door, int door_index, gfx::BackgroundBuffer& bg1,
93 const DungeonState* state = nullptr);
94
102 void DrawPotItem(uint8_t item_id, int x, int y, gfx::BackgroundBuffer& bg);
103
116 absl::Status DrawObjectList(const std::vector<RoomObject>& objects,
119 const gfx::PaletteGroup& palette_group,
120 const DungeonState* state = nullptr,
121 gfx::BackgroundBuffer* layout_bg1 = nullptr,
122 bool reset_room_event_indices = true);
123
129 int GetDrawRoutineId(int16_t object_id) const;
130
135 return static_cast<int>(draw_routines_.size());
136 }
137
143
152 void DrawTileToBitmap(gfx::Bitmap& bitmap, const gfx::TileInfo& tile_info,
153 int pixel_x, int pixel_y, const uint8_t* tiledata);
154
169 absl::Status DrawRoomDrawObjectData2x2(uint16_t object_id, int tile_x,
170 int tile_y,
172 uint16_t room_draw_object_data_offset,
175
181 std::pair<int, int> CalculateObjectDimensions(const RoomObject& object);
182
183 struct TileTrace {
184 uint16_t object_id = 0;
185 uint8_t size = 0;
186 uint8_t layer = 0;
187 int16_t x_tile = 0;
188 int16_t y_tile = 0;
189 uint16_t tile_id = 0;
190 uint8_t flags = 0;
191 };
192
193 void SetTraceCollector(std::vector<TileTrace>* collector,
194 bool trace_only = false);
195 void ClearTraceCollector();
196 bool TraceOnly() const { return trace_only_; }
197
198 protected:
199 // Draw routine function type
200 using DrawRoutine = std::function<void(
202 std::span<const gfx::TileInfo>, const DungeonState*)>;
203
204 // Core draw routines (based on ZScream's subtype1_routines table)
205 void DrawChest(const RoomObject& obj, gfx::BackgroundBuffer& bg,
206 std::span<const gfx::TileInfo> tiles,
207 const DungeonState* state = nullptr);
208 void DrawBigChest(const RoomObject& obj, gfx::BackgroundBuffer& bg,
209 std::span<const gfx::TileInfo> tiles,
210 const DungeonState* state = nullptr);
212 std::span<const gfx::TileInfo> tiles,
213 const DungeonState* state = nullptr);
214
215 void DrawNothing(const RoomObject& obj, gfx::BackgroundBuffer& bg,
216 std::span<const gfx::TileInfo> tiles,
217 const DungeonState* state = nullptr);
218 void CustomDraw(const RoomObject& obj, gfx::BackgroundBuffer& bg,
219 std::span<const gfx::TileInfo> tiles,
220 const DungeonState* state = nullptr);
222 std::span<const gfx::TileInfo> tiles,
223 const DungeonState* state = nullptr);
226 std::span<const gfx::TileInfo> tiles,
227 const DungeonState* state = nullptr);
228
229 // Type 3 / Special Routines
231 std::span<const gfx::TileInfo> tiles, int width,
232 int height);
233
234 // Custom Object Routine
236 std::span<const gfx::TileInfo> tiles,
237 const DungeonState* state = nullptr);
239 int tile_y);
240
241 // Utility methods
242 // Execute a DrawRoutineRegistry routine (pure DrawContext function) but render
243 // via ObjectDrawer::WriteTile8 so output hits the bitmap-backed buffers used
244 // by RoomLayerManager compositing.
245 void DrawUsingRegistryRoutine(int routine_id, const RoomObject& obj,
247 std::span<const gfx::TileInfo> tiles,
248 const DungeonState* state);
249 void WriteTile8(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
250 const gfx::TileInfo& tile_info);
251 bool IsValidTilePosition(int tile_x, int tile_y) const;
252
253 // Check if a draw routine should render to both BG1 and BG2
254 // Uses metadata instead of hardcoded routine IDs
255 static bool RoutineDrawsToBothBGs(int routine_id);
256
257 // Record a deferred BG1 reveal instead of mutating the raw BG1 pixels.
258 void MarkBg1RectRevealed(gfx::BackgroundBuffer& bg1, int start_px,
259 int start_py, int pixel_width, int pixel_height);
261 const gfx::TileInfo& tile_info,
262 int pixel_x, int pixel_y,
263 const uint8_t* tiledata);
264 static bool RequiresRectangularBg1Mask(const RoomObject& object);
265
266 // Door indicator fallback when graphics unavailable
267 void DrawDoorIndicator(gfx::BackgroundBuffer& bg, int tile_x, int tile_y,
268 int width, int height, DoorType type,
269 DoorDirection direction);
270
271 // Draw routine function array (indexed by routine ID from DrawRoutineRegistry)
272 std::vector<DrawRoutine> draw_routines_;
274
276 uint16_t object_id = 0;
277 uint8_t size = 0;
278 uint8_t layer = 0;
279 };
280
281 void SetTraceContext(const RoomObject& object, RoomObject::LayerType layer);
282 void PushTrace(int tile_x, int tile_y, const gfx::TileInfo& tile_info);
283 static void TraceHookThunk(gfx::BackgroundBuffer* bg, int tile_x, int tile_y,
284 const gfx::TileInfo& tile_info, void* user_data);
285
287 std::vector<TileTrace>* trace_collector_ = nullptr;
288 bool trace_only_ = false;
289
292 mutable int current_chest_index_ = 0;
303 const uint8_t*
304 room_gfx_buffer_; // Room-specific graphics buffer (current_gfx16_)
305
306 // Canvas dimensions in tiles (64x64 = 512x512 pixels)
307 static constexpr int kMaxTilesX = 64;
308 static constexpr int kMaxTilesY = 64;
309};
310
311} // namespace zelda3
312} // namespace yaze
313
314#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:67
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 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)
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)
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)
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)
Draw all objects in a room.
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)
ObjectDrawer(Rom *rom, int room_id, const uint8_t *room_gfx_buffer=nullptr)
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)
Draw a room object to background buffers.
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)
void SetAllowTrackCornerAliases(bool allow)
bool IsValidTilePosition(int tile_x, int tile_y) const
gfx::BG1RevealMaskSource bg1_reveal_mask_source_
RoomObject::LayerType registry_secondary_layer_
constexpr DoorDimensions GetDoorDimensions(DoorDirection dir)
Get door dimensions based on direction.
Definition door_types.h:235
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