yaze 0.2.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 <snes.h>
5
6#include <array>
7#include <cstdint>
8#include <cstring>
9#include <stdexcept>
10#include <vector>
11
12namespace yaze {
13namespace gfx {
14
15constexpr int kTilesheetWidth = 128;
16constexpr int kTilesheetHeight = 32;
17constexpr int kTilesheetDepth = 8;
18
19constexpr uint8_t kGraphicsBitmap[8] = {0x80, 0x40, 0x20, 0x10,
20 0x08, 0x04, 0x02, 0x01};
21
22std::vector<uint8_t> SnesTo8bppSheet(const std::vector<uint8_t>& sheet, int bpp,
23 int num_sheets = 1);
24std::vector<uint8_t> Bpp8SnesToIndexed(std::vector<uint8_t> data,
25 uint64_t bpp = 0);
26
27snes_tile8 UnpackBppTile(const std::vector<uint8_t>& data,
28 const uint32_t offset, const uint32_t bpp);
29
30std::vector<uint8_t> PackBppTile(const snes_tile8& tile, const uint32_t bpp);
31
32std::vector<uint8_t> ConvertBpp(const std::vector<uint8_t>& tiles,
33 uint32_t from_bpp, uint32_t to_bpp);
34
35std::vector<uint8_t> Convert3bppTo4bpp(const std::vector<uint8_t>& tiles);
36std::vector<uint8_t> Convert4bppTo3bpp(const std::vector<uint8_t>& tiles);
37
38void CopyTile8bpp16(int x, int y, int tile, std::vector<uint8_t>& bitmap,
39 std::vector<uint8_t>& blockset);
40
49class TileInfo {
50 public:
51 uint16_t id_;
52 uint8_t palette_;
53 bool over_;
56 TileInfo() = default;
57 TileInfo(uint16_t id, uint8_t palette, bool v, bool h, bool o)
58 : id_(id),
59 over_(o),
62 palette_(palette) {}
63
64 bool operator==(const TileInfo& other) const {
65 return id_ == other.id_ && over_ == other.over_ &&
68 palette_ == other.palette_;
69 }
70};
71
72uint16_t TileInfoToWord(TileInfo tile_info);
73TileInfo WordToTileInfo(uint16_t word);
74uint16_t TileInfoToShort(TileInfo tile_info);
75TileInfo GetTilesInfo(uint16_t tile);
76
80class Tile32 {
81 public:
82 uint16_t tile0_;
83 uint16_t tile1_;
84 uint16_t tile2_;
85 uint16_t tile3_;
86
87 // Default constructor
88 Tile32() : tile0_(0), tile1_(0), tile2_(0), tile3_(0) {}
89
90 // Parameterized constructor
91 Tile32(uint16_t t0, uint16_t t1, uint16_t t2, uint16_t t3)
92 : tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {}
93
94 // Copy constructor
95 Tile32(const Tile32& other)
96 : tile0_(other.tile0_),
97 tile1_(other.tile1_),
98 tile2_(other.tile2_),
99 tile3_(other.tile3_) {}
100
101 // Constructor from packed value
102 Tile32(uint64_t packedVal) {
103 tile0_ = (uint16_t)packedVal;
104 tile1_ = (uint16_t)(packedVal >> 16);
105 tile2_ = (uint16_t)(packedVal >> 32);
106 tile3_ = (uint16_t)(packedVal >> 48);
107 }
108
109 // Get packed uint64_t representation
110 uint64_t GetPackedValue() const {
111 return static_cast<uint64_t>(tile3_) << 48 |
112 (static_cast<uint64_t>(tile2_) << 32) |
113 (static_cast<uint64_t>(tile1_) << 16) | tile0_;
114 }
115
116 // Equality operator
117 bool operator==(const Tile32& other) const {
118 return tile0_ == other.tile0_ && tile1_ == other.tile1_ &&
119 tile2_ == other.tile2_ && tile3_ == other.tile3_;
120 }
121
122 // Inequality operator
123 bool operator!=(const Tile32& other) const { return !(*this == other); }
124};
125
129class Tile16 {
130 public:
135 std::array<TileInfo, 4> tiles_info;
136
137 Tile16() = default;
139 : tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {
140 tiles_info[0] = tile0_;
141 tiles_info[1] = tile1_;
142 tiles_info[2] = tile2_;
143 tiles_info[3] = tile3_;
144 }
145
146 bool operator==(const Tile16& other) const {
147 return tile0_ == other.tile0_ && tile1_ == other.tile1_ &&
148 tile2_ == other.tile2_ && tile3_ == other.tile3_;
149 }
150
151 bool operator!=(const Tile16& other) const { return !(*this == other); }
152};
153
157class OamTile {
158 public:
159 int x_;
160 int y_;
161 int mx_;
162 int my_;
163 int pal_;
164 uint16_t tile_;
165 OamTile() = default;
166 OamTile(int x, int y, uint16_t tile, int pal, bool upper = false, int mx = 0,
167 int my = 0)
168 : x_(x), y_(y), mx_(mx), my_(my), pal_(pal) {
169 if (upper) {
170 tile_ = (uint16_t)(tile + 512);
171 } else {
172 tile_ = (uint16_t)(tile + 256 + 512);
173 }
174 }
175};
176
178 public:
179 GraphicsBuffer() = default;
180 GraphicsBuffer(uint8_t bpp, const std::vector<uint8_t>& data)
181 : bpp_(bpp), data_(data) {}
182 GraphicsBuffer(uint8_t bpp, std::vector<uint8_t>&& data)
183 : bpp_(bpp), data_(std::move(data)) {}
184
185 uint8_t bpp() const { return bpp_; }
186 const std::vector<uint8_t>& data() const { return data_; }
187
188 void set_bpp(uint8_t bpp) { bpp_ = bpp; }
189 void set_data(const std::vector<uint8_t>& data) { data_ = data; }
190 void to_bpp(uint8_t bpp) {
191 if (bpp_ == bpp) {
192 return;
193 }
195 bpp_ = bpp;
196 }
197
198 // Array-like access via operator[]
199 uint8_t& operator[](size_t index) {
200 if (index >= data_.size()) {
201 throw std::out_of_range("Index out of range");
202 }
203 return data_[index];
204 }
205
206 const uint8_t& operator[](size_t index) const {
207 if (index >= data_.size()) {
208 throw std::out_of_range("Index out of range");
209 }
210 return data_[index];
211 }
212
213 auto begin() { return data_.begin(); }
214 auto end() { return data_.end(); }
215 auto begin() const { return data_.begin(); }
216 auto end() const { return data_.end(); }
217
218 private:
219 uint8_t bpp_;
220 std::vector<uint8_t> data_;
221};
222
223} // namespace gfx
224
225} // namespace yaze
226
227#endif // YAZE_APP_GFX_SNES_TILE_H
GraphicsBuffer(uint8_t bpp, const std::vector< uint8_t > &data)
Definition snes_tile.h:180
void set_data(const std::vector< uint8_t > &data)
Definition snes_tile.h:189
uint8_t bpp() const
Definition snes_tile.h:185
GraphicsBuffer(uint8_t bpp, std::vector< uint8_t > &&data)
Definition snes_tile.h:182
uint8_t & operator[](size_t index)
Definition snes_tile.h:199
const std::vector< uint8_t > & data() const
Definition snes_tile.h:186
std::vector< uint8_t > data_
Definition snes_tile.h:220
const uint8_t & operator[](size_t index) const
Definition snes_tile.h:206
void set_bpp(uint8_t bpp)
Definition snes_tile.h:188
void to_bpp(uint8_t bpp)
Definition snes_tile.h:190
OamTile(int x, int y, uint16_t tile, int pal, bool upper=false, int mx=0, int my=0)
Definition snes_tile.h:166
Tile16(TileInfo t0, TileInfo t1, TileInfo t2, TileInfo t3)
Definition snes_tile.h:138
std::array< TileInfo, 4 > tiles_info
Definition snes_tile.h:135
bool operator!=(const Tile16 &other) const
Definition snes_tile.h:151
bool operator==(const Tile16 &other) const
Definition snes_tile.h:146
uint16_t tile2_
Definition snes_tile.h:84
Tile32(uint64_t packedVal)
Definition snes_tile.h:102
bool operator!=(const Tile32 &other) const
Definition snes_tile.h:123
uint16_t tile3_
Definition snes_tile.h:85
uint16_t tile0_
Definition snes_tile.h:82
bool operator==(const Tile32 &other) const
Definition snes_tile.h:117
Tile32(uint16_t t0, uint16_t t1, uint16_t t2, uint16_t t3)
Definition snes_tile.h:91
Tile32(const Tile32 &other)
Definition snes_tile.h:95
uint64_t GetPackedValue() const
Definition snes_tile.h:110
uint16_t tile1_
Definition snes_tile.h:83
SNES 16-bit tile metadata container.
Definition snes_tile.h:49
bool operator==(const TileInfo &other) const
Definition snes_tile.h:64
TileInfo(uint16_t id, uint8_t palette, bool v, bool h, bool o)
Definition snes_tile.h:57
Contains classes for handling graphical data.
Definition bitmap.cc:16
constexpr int kTilesheetHeight
Definition snes_tile.h:16
constexpr int kTilesheetWidth
Definition snes_tile.h:15
void CopyTile8bpp16(int x, int y, int tile, std::vector< uint8_t > &bitmap, std::vector< uint8_t > &blockset)
Definition snes_tile.cc:388
uint16_t TileInfoToWord(TileInfo tile_info)
Definition snes_tile.cc:313
constexpr int kTilesheetDepth
Definition snes_tile.h:17
uint16_t TileInfoToShort(TileInfo tile_info)
Definition snes_tile.cc:345
std::vector< uint8_t > PackBppTile(const snes_tile8 &tile, const uint32_t bpp)
Definition snes_tile.cc:82
std::vector< uint8_t > Bpp8SnesToIndexed(std::vector< uint8_t > data, uint64_t bpp)
Definition snes_tile.cc:211
std::vector< uint8_t > ConvertBpp(const std::vector< uint8_t > &tiles, uint32_t from_bpp, uint32_t to_bpp)
Definition snes_tile.cc:129
TileInfo GetTilesInfo(uint16_t tile)
Definition snes_tile.cc:376
constexpr uint8_t kGraphicsBitmap[8]
Definition snes_tile.h:19
TileInfo WordToTileInfo(uint16_t word)
Definition snes_tile.cc:330
snes_tile8 UnpackBppTile(const std::vector< uint8_t > &data, const uint32_t offset, const uint32_t bpp)
Definition snes_tile.cc:25
std::vector< uint8_t > Convert3bppTo4bpp(const std::vector< uint8_t > &tiles)
Definition snes_tile.cc:143
std::vector< uint8_t > Convert4bppTo3bpp(const std::vector< uint8_t > &tiles)
Definition snes_tile.cc:147
std::vector< uint8_t > SnesTo8bppSheet(const std::vector< uint8_t > &sheet, int bpp, int num_sheets)
Definition snes_tile.cc:151
Main namespace for the application.
Definition controller.cc:18