yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
background_buffer.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_BACKGROUND_BUFFER_H
2#define YAZE_APP_GFX_BACKGROUND_BUFFER_H
3
4#include <array>
5#include <cstdint>
6#include <optional>
7#include <vector>
8
11
12namespace yaze {
13namespace gfx {
14
15enum class BG1RevealMaskSource : uint8_t {
16 kBG2Layout = 1 << 0,
17 kBG2Objects = 1 << 1,
18};
19
20// Decode the eight 8x8 tiles selected by a dungeon room's floor-graphics
21// nibble. The first four words live in the main table and the second four in
22// the adjacent floor table, matching RoomDraw_DrawFloors in the game.
23std::optional<std::array<TileInfo, 8>> DecodeDungeonFloorTilePattern(
24 const std::vector<uint8_t>& rom_data, int tile_address,
25 int tile_address_floor, uint8_t floor_graphics);
26
28 public:
29 BackgroundBuffer(int width = 512, int height = 512);
30
31 // Buffer manipulation methods
32 void SetTileAt(int x, int y, uint16_t value);
33 uint16_t GetTileAt(int x, int y) const;
34 void ClearTileBuffer();
35 void ClearBuffer();
36
37 // Drawing methods
38 void DrawTile(const TileInfo& tile_info, uint8_t* canvas,
39 const uint8_t* tiledata, int indexoffset);
40 void DrawBackground(std::span<uint8_t> gfx16_data);
41
42 // Floor drawing methods
43 void DrawFloor(const std::vector<uint8_t>& rom_data, int tile_address,
44 int tile_address_floor, uint8_t floor_graphics);
45
46 // Ensure bitmap is initialized before accessing
47 // Call this before using bitmap() if the buffer was created standalone
49
50 // Priority buffer methods for per-tile priority support
51 // SNES Mode 1 uses priority bits to control Z-ordering between layers
53 uint8_t GetPriorityAt(int x, int y) const;
54 void SetPriorityAt(int x, int y, uint8_t priority);
55 const std::vector<uint8_t>& priority_data() const { return priority_buffer_; }
56 std::vector<uint8_t>& mutable_priority_data();
57
58 // Coverage buffer methods for per-pixel "this layer wrote here" tracking.
59 //
60 // This is critical for accurate dungeon compositing: a tilemap entry can
61 // legally be fully transparent (all pixels == 0), and it still overwrites
62 // whatever was previously on that BG. With separate Layout/Object buffers,
63 // we need an explicit coverage mask to distinguish:
64 // - "object layer didn't write here" (fall back to layout), vs
65 // - "object layer wrote transparent here" (clear layout; reveal BG2/backdrop).
67 const std::vector<uint8_t>& coverage_data() const { return coverage_buffer_; }
68 std::vector<uint8_t>& mutable_coverage_data();
69
70 // Per-pixel cross-layer reveal bits carried by raw BG1 target buffers.
71 // Each bit identifies the BG2 source layer that requested the reveal, so
72 // compositing can ignore that request when its owner is hidden while later
73 // BG1 writes can clear only the source bit they supersede.
74 void ClearBG1RevealMask();
76 void ClearBG1RevealMaskRect(BG1RevealMaskSource source, int start_x,
77 int start_y, int width, int height);
78 void SetBG1RevealMaskRect(BG1RevealMaskSource source, int start_x,
79 int start_y, int width, int height);
80 const std::vector<uint8_t>& bg1_reveal_mask_data() const {
82 }
83 std::vector<uint8_t>& mutable_bg1_reveal_mask_data();
84
85 // Accessors
86 std::vector<uint16_t>& buffer() { return buffer_; }
87 const std::vector<uint16_t>& buffer() const { return buffer_; }
88 auto& bitmap() { return bitmap_; }
89 const gfx::Bitmap& bitmap() const { return bitmap_; }
90
91 private:
96
97 std::vector<uint16_t> buffer_;
98 std::vector<uint8_t> priority_buffer_; // Per-pixel priority (0 or 1)
99 std::vector<uint8_t> coverage_buffer_; // Per-pixel coverage (0=unset,1=set)
100 std::vector<uint8_t> bg1_reveal_mask_buffer_; // Per-pixel BG1 reveal mask
104};
105
106} // namespace gfx
107} // namespace yaze
108
109#endif // YAZE_APP_GFX_BACKGROUND_BUFFER_H
void DrawTile(const TileInfo &tile_info, uint8_t *canvas, const uint8_t *tiledata, int indexoffset)
void DrawBackground(std::span< uint8_t > gfx16_data)
const std::vector< uint16_t > & buffer() const
const std::vector< uint8_t > & bg1_reveal_mask_data() const
void SetBG1RevealMaskRect(BG1RevealMaskSource source, int start_x, int start_y, int width, int height)
std::vector< uint8_t > & mutable_bg1_reveal_mask_data()
BackgroundBuffer(int width=512, int height=512)
const std::vector< uint8_t > & coverage_data() const
std::vector< uint8_t > coverage_buffer_
void SetPriorityAt(int x, int y, uint8_t priority)
std::vector< uint8_t > & mutable_priority_data()
std::vector< uint8_t > bg1_reveal_mask_buffer_
uint8_t GetPriorityAt(int x, int y) const
void SetTileAt(int x, int y, uint16_t value)
void DrawFloor(const std::vector< uint8_t > &rom_data, int tile_address, int tile_address_floor, uint8_t floor_graphics)
const gfx::Bitmap & bitmap() const
std::vector< uint8_t > priority_buffer_
std::vector< uint8_t > & mutable_coverage_data()
std::vector< uint16_t > & buffer()
void ClearBG1RevealMaskRect(BG1RevealMaskSource source, int start_x, int start_y, int width, int height)
uint16_t GetTileAt(int x, int y) const
const std::vector< uint8_t > & priority_data() const
std::vector< uint16_t > buffer_
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:69
SNES 16-bit tile metadata container.
Definition snes_tile.h:52
std::optional< std::array< TileInfo, 8 > > DecodeDungeonFloorTilePattern(const std::vector< uint8_t > &rom_data, int tile_address, int tile_address_floor, uint8_t floor_graphics)