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