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