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 <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 id_ = (uint16_t)(((b2 & 0x01) << 8) + (b1));
66 vertical_mirror_ = (b2 & 0x80) == 0x80;
67 horizontal_mirror_ = (b2 & 0x40) == 0x40;
68 over_ = (b2 & 0x20) == 0x20;
69 palette_ = (b2 >> 2) & 0x07;
70 }
71
72 bool operator==(const TileInfo& other) const {
73 return id_ == other.id_ && over_ == other.over_ &&
76 palette_ == other.palette_;
77 }
78};
79
80uint16_t TileInfoToWord(TileInfo tile_info);
81TileInfo WordToTileInfo(uint16_t word);
82uint16_t TileInfoToShort(TileInfo tile_info);
83TileInfo GetTilesInfo(uint16_t tile);
84
88class Tile32 {
89 public:
90 uint16_t tile0_;
91 uint16_t tile1_;
92 uint16_t tile2_;
93 uint16_t tile3_;
94
95 // Default constructor
96 Tile32() : tile0_(0), tile1_(0), tile2_(0), tile3_(0) {}
97
98 // Parameterized constructor
99 Tile32(uint16_t t0, uint16_t t1, uint16_t t2, uint16_t t3)
100 : tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {}
101
102 // Copy constructor
103 Tile32(const Tile32& other)
104 : tile0_(other.tile0_),
105 tile1_(other.tile1_),
106 tile2_(other.tile2_),
107 tile3_(other.tile3_) {}
108
109 // Constructor from packed value
110 Tile32(uint64_t packedVal) {
111 tile0_ = (uint16_t)packedVal;
112 tile1_ = (uint16_t)(packedVal >> 16);
113 tile2_ = (uint16_t)(packedVal >> 32);
114 tile3_ = (uint16_t)(packedVal >> 48);
115 }
116
117 // Get packed uint64_t representation
118 uint64_t GetPackedValue() const {
119 return static_cast<uint64_t>(tile3_) << 48 |
120 (static_cast<uint64_t>(tile2_) << 32) |
121 (static_cast<uint64_t>(tile1_) << 16) | tile0_;
122 }
123
124 // Equality operator
125 bool operator==(const Tile32& other) const {
126 return tile0_ == other.tile0_ && tile1_ == other.tile1_ &&
127 tile2_ == other.tile2_ && tile3_ == other.tile3_;
128 }
129
130 // Inequality operator
131 bool operator!=(const Tile32& other) const { return !(*this == other); }
132};
133
137class Tile16 {
138 public:
143 std::array<TileInfo, 4> tiles_info;
144
145 Tile16() = default;
147 : tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {
148 tiles_info[0] = tile0_;
149 tiles_info[1] = tile1_;
150 tiles_info[2] = tile2_;
151 tiles_info[3] = tile3_;
152 }
153
154 bool operator==(const Tile16& other) const {
155 return tile0_ == other.tile0_ && tile1_ == other.tile1_ &&
156 tile2_ == other.tile2_ && tile3_ == other.tile3_;
157 }
158
159 bool operator!=(const Tile16& other) const { return !(*this == other); }
160};
161
165class OamTile {
166 public:
167 int x_;
168 int y_;
169 int mx_;
170 int my_;
171 int pal_;
172 uint16_t tile_;
173 OamTile() = default;
174 OamTile(int x, int y, uint16_t tile, int pal, bool upper = false, int mx = 0,
175 int my = 0)
176 : x_(x), y_(y), mx_(mx), my_(my), pal_(pal) {
177 if (upper) {
178 tile_ = (uint16_t)(tile + 512);
179 } else {
180 tile_ = (uint16_t)(tile + 256 + 512);
181 }
182 }
183};
184
186 public:
187 GraphicsBuffer() = default;
188 GraphicsBuffer(uint8_t bpp, const std::vector<uint8_t>& data)
189 : bpp_(bpp), data_(data) {}
190 GraphicsBuffer(uint8_t bpp, std::vector<uint8_t>&& data)
191 : bpp_(bpp), data_(std::move(data)) {}
192
193 uint8_t bpp() const { return bpp_; }
194 const std::vector<uint8_t>& data() const { return data_; }
195
196 void set_bpp(uint8_t bpp) { bpp_ = bpp; }
197 void set_data(const std::vector<uint8_t>& data) { data_ = data; }
198 void to_bpp(uint8_t bpp) {
199 if (bpp_ == bpp) {
200 return;
201 }
203 bpp_ = bpp;
204 }
205
206 // Array-like access via operator[]
207 uint8_t& operator[](size_t index) {
208 if (index >= data_.size()) {
209 throw std::out_of_range("Index out of range");
210 }
211 return data_[index];
212 }
213
214 const uint8_t& operator[](size_t index) const {
215 if (index >= data_.size()) {
216 throw std::out_of_range("Index out of range");
217 }
218 return data_[index];
219 }
220
221 auto begin() { return data_.begin(); }
222 auto end() { return data_.end(); }
223 auto begin() const { return data_.begin(); }
224 auto end() const { return data_.end(); }
225
226 private:
227 uint8_t bpp_;
228 std::vector<uint8_t> data_;
229};
230
231} // namespace gfx
232} // namespace yaze
233
234#endif // YAZE_APP_GFX_SNES_TILE_H
GraphicsBuffer(uint8_t bpp, const std::vector< uint8_t > &data)
Definition snes_tile.h:188
void set_data(const std::vector< uint8_t > &data)
Definition snes_tile.h:197
uint8_t bpp() const
Definition snes_tile.h:193
GraphicsBuffer(uint8_t bpp, std::vector< uint8_t > &&data)
Definition snes_tile.h:190
uint8_t & operator[](size_t index)
Definition snes_tile.h:207
const std::vector< uint8_t > & data() const
Definition snes_tile.h:194
std::vector< uint8_t > data_
Definition snes_tile.h:228
const uint8_t & operator[](size_t index) const
Definition snes_tile.h:214
void set_bpp(uint8_t bpp)
Definition snes_tile.h:196
void to_bpp(uint8_t bpp)
Definition snes_tile.h:198
OamTile(int x, int y, uint16_t tile, int pal, bool upper=false, int mx=0, int my=0)
Definition snes_tile.h:174
Tile16(TileInfo t0, TileInfo t1, TileInfo t2, TileInfo t3)
Definition snes_tile.h:146
std::array< TileInfo, 4 > tiles_info
Definition snes_tile.h:143
bool operator!=(const Tile16 &other) const
Definition snes_tile.h:159
bool operator==(const Tile16 &other) const
Definition snes_tile.h:154
uint16_t tile2_
Definition snes_tile.h:92
Tile32(uint64_t packedVal)
Definition snes_tile.h:110
bool operator!=(const Tile32 &other) const
Definition snes_tile.h:131
uint16_t tile3_
Definition snes_tile.h:93
uint16_t tile0_
Definition snes_tile.h:90
bool operator==(const Tile32 &other) const
Definition snes_tile.h:125
Tile32(uint16_t t0, uint16_t t1, uint16_t t2, uint16_t t3)
Definition snes_tile.h:99
Tile32(const Tile32 &other)
Definition snes_tile.h:103
uint64_t GetPackedValue() const
Definition snes_tile.h:118
uint16_t tile1_
Definition snes_tile.h:91
SNES 16-bit tile metadata container.
Definition snes_tile.h:50
bool operator==(const TileInfo &other) const
Definition snes_tile.h:72
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
Contains classes for handling graphical data.
Definition arena.cc:8
std::vector< uint8_t > LoadSNES4bppGFXToIndexedColorMatrix(std::span< uint8_t > src)
Definition snes_tile.cc:379
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:366
uint16_t TileInfoToWord(TileInfo tile_info)
Definition snes_tile.cc:291
constexpr int kTilesheetDepth
Definition snes_tile.h:18
uint16_t TileInfoToShort(TileInfo tile_info)
Definition snes_tile.cc:323
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:189
std::vector< uint8_t > SnesTo8bppSheet(std::span< uint8_t > sheet, int bpp, int num_sheets)
Definition snes_tile.cc:129
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:354
constexpr uint8_t kGraphicsBitmap[8]
Definition snes_tile.h:20
TileInfo WordToTileInfo(uint16_t word)
Definition snes_tile.cc:308
std::vector< uint8_t > ConvertBpp(std::span< uint8_t > tiles, uint32_t from_bpp, uint32_t to_bpp)
Definition snes_tile.cc:115
Main namespace for the application.
Definition controller.cc:18