yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
snes_tile.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_SNES_TILE_H
2#define YAZE_APP_GFX_SNES_TILE_H
3
4#include <yaze.h>
5
6#include <array>
7#include <cstdint>
8#include <cstring>
9#include <span>
10#include <stdexcept>
11#include <vector>
12
13namespace yaze {
14namespace gfx {
15
16constexpr int kTilesheetWidth = 128;
17constexpr int kTilesheetHeight = 32;
18constexpr int kTilesheetDepth = 8;
19
20constexpr uint8_t kGraphicsBitmap[8] = {0x80, 0x40, 0x20, 0x10,
21 0x08, 0x04, 0x02, 0x01};
22
23std::vector<uint8_t> SnesTo8bppSheet(std::span<uint8_t> sheet, int bpp,
24 int num_sheets = 1);
25std::vector<uint8_t> IndexedToSnesSheet(std::span<const uint8_t> sheet,
26 int bpp, int num_sheets = 1);
27std::vector<uint8_t> Bpp8SnesToIndexed(std::vector<uint8_t> data,
28 uint64_t bpp = 0);
29
30snes_tile8 UnpackBppTile(std::span<uint8_t> data, const uint32_t offset,
31 const uint32_t bpp);
32
33std::vector<uint8_t> PackBppTile(const snes_tile8& tile, const uint32_t bpp);
34
35std::vector<uint8_t> ConvertBpp(std::span<uint8_t> tiles, uint32_t from_bpp,
36 uint32_t to_bpp);
37
38void CopyTile8bpp16(int x, int y, int tile, std::vector<uint8_t>& bitmap,
39 std::vector<uint8_t>& blockset);
40
41std::vector<uint8_t> LoadSNES4bppGFXToIndexedColorMatrix(
42 std::span<uint8_t> src);
43
52class TileInfo {
53 public:
54 uint16_t id_;
55 uint8_t palette_;
56 bool over_;
59 TileInfo() = default;
60 TileInfo(uint16_t id, uint8_t palette, bool v, bool h, bool o)
61 : id_(id),
62 over_(o),
65 palette_(palette) {}
66 TileInfo(uint8_t b1, uint8_t b2) {
67 // SNES tilemap word format: vhopppcc cccccccc
68 // b1 = low byte (bits 0-7 of tile ID)
69 // b2 = high byte (bits 8-9 of tile ID in bits 0-1, palette in bits 2-4, flags in 5-7)
70 id_ = (uint16_t)(((b2 & 0x03) << 8) | b1); // 10-bit tile ID (bits 0-9)
71 vertical_mirror_ = (b2 & 0x80) == 0x80; // bit 15
72 horizontal_mirror_ = (b2 & 0x40) == 0x40; // bit 14
73 over_ = (b2 & 0x20) == 0x20; // bit 13 (priority)
74 palette_ = (b2 >> 2) & 0x07; // bits 10-12
75 }
76
77 bool operator==(const TileInfo& other) const {
78 return id_ == other.id_ && over_ == other.over_ &&
81 palette_ == other.palette_;
82 }
83};
84
85uint16_t TileInfoToWord(TileInfo tile_info);
86TileInfo WordToTileInfo(uint16_t word);
87uint16_t TileInfoToShort(TileInfo tile_info);
88TileInfo GetTilesInfo(uint16_t tile);
89
93class Tile32 {
94 public:
95 uint16_t tile0_;
96 uint16_t tile1_;
97 uint16_t tile2_;
98 uint16_t tile3_;
99
100 // Default constructor
101 Tile32() : tile0_(0), tile1_(0), tile2_(0), tile3_(0) {}
102
103 // Parameterized constructor
104 Tile32(uint16_t t0, uint16_t t1, uint16_t t2, uint16_t t3)
105 : tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {}
106
107 // Copy constructor
108 Tile32(const Tile32& other)
109 : tile0_(other.tile0_),
110 tile1_(other.tile1_),
111 tile2_(other.tile2_),
112 tile3_(other.tile3_) {}
113
114 // Constructor from packed value
115 Tile32(uint64_t packedVal) {
116 tile0_ = (uint16_t)packedVal;
117 tile1_ = (uint16_t)(packedVal >> 16);
118 tile2_ = (uint16_t)(packedVal >> 32);
119 tile3_ = (uint16_t)(packedVal >> 48);
120 }
121
122 // Get packed uint64_t representation
123 uint64_t GetPackedValue() const {
124 return static_cast<uint64_t>(tile3_) << 48 |
125 (static_cast<uint64_t>(tile2_) << 32) |
126 (static_cast<uint64_t>(tile1_) << 16) | tile0_;
127 }
128
129 // Equality operator
130 bool operator==(const Tile32& other) const {
131 return tile0_ == other.tile0_ && tile1_ == other.tile1_ &&
132 tile2_ == other.tile2_ && tile3_ == other.tile3_;
133 }
134
135 // Inequality operator
136 bool operator!=(const Tile32& other) const { return !(*this == other); }
137};
138
142class Tile16 {
143 public:
148 std::array<TileInfo, 4> tiles_info;
149
150 Tile16() = default;
152 : tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {
153 tiles_info[0] = tile0_;
154 tiles_info[1] = tile1_;
155 tiles_info[2] = tile2_;
156 tiles_info[3] = tile3_;
157 }
158
159 bool operator==(const Tile16& other) const {
160 return tile0_ == other.tile0_ && tile1_ == other.tile1_ &&
161 tile2_ == other.tile2_ && tile3_ == other.tile3_;
162 }
163
164 bool operator!=(const Tile16& other) const { return !(*this == other); }
165};
166
170class OamTile {
171 public:
172 int x_;
173 int y_;
174 int mx_;
175 int my_;
176 int pal_;
177 uint16_t tile_;
178 OamTile() = default;
179 OamTile(int x, int y, uint16_t tile, int pal, bool upper = false, int mx = 0,
180 int my = 0)
181 : x_(x), y_(y), mx_(mx), my_(my), pal_(pal) {
182 if (upper) {
183 tile_ = (uint16_t)(tile + 512);
184 } else {
185 tile_ = (uint16_t)(tile + 256 + 512);
186 }
187 }
188};
189
191 public:
192 GraphicsBuffer() = default;
193 GraphicsBuffer(uint8_t bpp, const std::vector<uint8_t>& data)
194 : bpp_(bpp), data_(data) {}
195 GraphicsBuffer(uint8_t bpp, std::vector<uint8_t>&& data)
196 : bpp_(bpp), data_(std::move(data)) {}
197
198 uint8_t bpp() const { return bpp_; }
199 const std::vector<uint8_t>& data() const { return data_; }
200
201 void set_bpp(uint8_t bpp) { bpp_ = bpp; }
202 void set_data(const std::vector<uint8_t>& data) { data_ = data; }
203 void to_bpp(uint8_t bpp) {
204 if (bpp_ == bpp) {
205 return;
206 }
208 bpp_ = bpp;
209 }
210
211 // Array-like access via operator[]
212 uint8_t& operator[](size_t index) {
213 if (index >= data_.size()) {
214 throw std::out_of_range("Index out of range");
215 }
216 return data_[index];
217 }
218
219 const uint8_t& operator[](size_t index) const {
220 if (index >= data_.size()) {
221 throw std::out_of_range("Index out of range");
222 }
223 return data_[index];
224 }
225
226 auto begin() { return data_.begin(); }
227 auto end() { return data_.end(); }
228 auto begin() const { return data_.begin(); }
229 auto end() const { return data_.end(); }
230
231 private:
232 uint8_t bpp_;
233 std::vector<uint8_t> data_;
234};
235
236} // namespace gfx
237} // namespace yaze
238
239#endif // YAZE_APP_GFX_SNES_TILE_H
GraphicsBuffer(uint8_t bpp, const std::vector< uint8_t > &data)
Definition snes_tile.h:193
void set_data(const std::vector< uint8_t > &data)
Definition snes_tile.h:202
uint8_t bpp() const
Definition snes_tile.h:198
GraphicsBuffer(uint8_t bpp, std::vector< uint8_t > &&data)
Definition snes_tile.h:195
uint8_t & operator[](size_t index)
Definition snes_tile.h:212
const std::vector< uint8_t > & data() const
Definition snes_tile.h:199
std::vector< uint8_t > data_
Definition snes_tile.h:233
const uint8_t & operator[](size_t index) const
Definition snes_tile.h:219
void set_bpp(uint8_t bpp)
Definition snes_tile.h:201
void to_bpp(uint8_t bpp)
Definition snes_tile.h:203
Object Attribute Memory tile abstraction container.
Definition snes_tile.h:170
OamTile(int x, int y, uint16_t tile, int pal, bool upper=false, int mx=0, int my=0)
Definition snes_tile.h:179
Tile composition of four 8x8 tiles.
Definition snes_tile.h:142
Tile16(TileInfo t0, TileInfo t1, TileInfo t2, TileInfo t3)
Definition snes_tile.h:151
std::array< TileInfo, 4 > tiles_info
Definition snes_tile.h:148
bool operator!=(const Tile16 &other) const
Definition snes_tile.h:164
bool operator==(const Tile16 &other) const
Definition snes_tile.h:159
Tile composition of four 16x16 tiles.
Definition snes_tile.h:93
uint16_t tile2_
Definition snes_tile.h:97
Tile32(uint64_t packedVal)
Definition snes_tile.h:115
bool operator!=(const Tile32 &other) const
Definition snes_tile.h:136
uint16_t tile3_
Definition snes_tile.h:98
uint16_t tile0_
Definition snes_tile.h:95
bool operator==(const Tile32 &other) const
Definition snes_tile.h:130
Tile32(uint16_t t0, uint16_t t1, uint16_t t2, uint16_t t3)
Definition snes_tile.h:104
Tile32(const Tile32 &other)
Definition snes_tile.h:108
uint64_t GetPackedValue() const
Definition snes_tile.h:123
uint16_t tile1_
Definition snes_tile.h:96
SNES 16-bit tile metadata container.
Definition snes_tile.h:52
bool operator==(const TileInfo &other) const
Definition snes_tile.h:77
TileInfo(uint8_t b1, uint8_t b2)
Definition snes_tile.h:66
TileInfo(uint16_t id, uint8_t palette, bool v, bool h, bool o)
Definition snes_tile.h:60
std::vector< uint8_t > LoadSNES4bppGFXToIndexedColorMatrix(std::span< uint8_t > src)
Definition snes_tile.cc:449
constexpr int kTilesheetHeight
Definition snes_tile.h:17
constexpr int kTilesheetWidth
Definition snes_tile.h:16
void CopyTile8bpp16(int x, int y, int tile, std::vector< uint8_t > &bitmap, std::vector< uint8_t > &blockset)
Definition snes_tile.cc:436
uint16_t TileInfoToWord(TileInfo tile_info)
Definition snes_tile.cc:361
constexpr int kTilesheetDepth
Definition snes_tile.h:18
uint16_t TileInfoToShort(TileInfo tile_info)
Definition snes_tile.cc:393
std::vector< uint8_t > PackBppTile(const snes_tile8 &tile, const uint32_t bpp)
Definition snes_tile.cc:73
std::vector< uint8_t > Bpp8SnesToIndexed(std::vector< uint8_t > data, uint64_t bpp)
Definition snes_tile.cc:259
std::vector< uint8_t > SnesTo8bppSheet(std::span< uint8_t > sheet, int bpp, int num_sheets)
Definition snes_tile.cc:132
snes_tile8 UnpackBppTile(std::span< uint8_t > data, const uint32_t offset, const uint32_t bpp)
Definition snes_tile.cc:24
TileInfo GetTilesInfo(uint16_t tile)
Definition snes_tile.cc:424
constexpr uint8_t kGraphicsBitmap[8]
Definition snes_tile.h:20
TileInfo WordToTileInfo(uint16_t word)
Definition snes_tile.cc:378
std::vector< uint8_t > IndexedToSnesSheet(std::span< const uint8_t > sheet, int bpp, int num_sheets)
Definition snes_tile.cc:203
std::vector< uint8_t > ConvertBpp(std::span< uint8_t > tiles, uint32_t from_bpp, uint32_t to_bpp)
Definition snes_tile.cc:118
8x8 SNES tile data
Definition yaze.h:287
Yet Another Zelda3 Editor (YAZE) - Public C API.