yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
snes_tile.cc
Go to the documentation of this file.
1#include "snes_tile.h"
2
3#include <cassert>
4#include <cstdint>
5#include <stdexcept>
6#include <vector>
7
9
10namespace yaze {
11namespace app {
12namespace gfx {
13
14// Bit set for object priority
15constexpr ushort TilePriorityBit = 0x2000;
16
17// Bit set for object hflip
18constexpr ushort TileHFlipBit = 0x4000;
19
20// Bit set for object vflip
21constexpr ushort TileVFlipBit = 0x8000;
22
23// Bits used for tile name
24constexpr ushort TileNameMask = 0x03FF;
25
26tile8 UnpackBppTile(const std::vector<uint8_t>& data, const uint32_t offset,
27 const uint32_t bpp) {
28 tile8 tile;
29 assert(bpp >= 1 && bpp <= 8);
30 unsigned int bpp_pos[8]; // More for conveniance and readibility
31 for (int col = 0; col < 8; col++) {
32 for (int row = 0; row < 8; row++) {
33 if (bpp == 1) {
34 tile.data[col * 8 + row] = (data[offset + col] >> (7 - row)) & 0x01;
35 continue;
36 }
37 /* SNES bpp format interlace each byte of the first 2 bitplanes.
38 * | byte 1 of first bitplane | byte 1 of second bitplane |
39 * | byte 2 of first bitplane | byte 2 of second bitplane | ..
40 */
41 bpp_pos[0] = offset + col * 2;
42 bpp_pos[1] = offset + col * 2 + 1;
43 char mask = 1 << (7 - row);
44 tile.data[col * 8 + row] = (data[bpp_pos[0]] & mask) == mask;
45 tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[1]] & mask) == mask)
46 << 1;
47 if (bpp == 3) {
48 // When we have 3 bitplanes, the bytes for the third bitplane are after
49 // the 16 bytes of the 2 bitplanes.
50 bpp_pos[2] = offset + 16 + col;
51 tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[2]] & mask) == mask)
52 << 2;
53 }
54 if (bpp >= 4) {
55 // For 4 bitplanes, the 2 added bitplanes are interlaced like the first
56 // two.
57 bpp_pos[2] = offset + 16 + col * 2;
58 bpp_pos[3] = offset + 16 + col * 2 + 1;
59 tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[2]] & mask) == mask)
60 << 2;
61 tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[3]] & mask) == mask)
62 << 3;
63 }
64 if (bpp == 8) {
65 bpp_pos[4] = offset + 32 + col * 2;
66 bpp_pos[5] = offset + 32 + col * 2 + 1;
67 bpp_pos[6] = offset + 48 + col * 2;
68 bpp_pos[7] = offset + 48 + col * 2 + 1;
69 tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[4]] & mask) == mask)
70 << 4;
71 tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[5]] & mask) == mask)
72 << 5;
73 tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[6]] & mask) == mask)
74 << 6;
75 tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[7]] & mask) == mask)
76 << 7;
77 }
78 }
79 }
80 return tile;
81}
82
83std::vector<uint8_t> PackBppTile(const tile8& tile, const uint32_t bpp) {
84 // Allocate memory for output data
85 std::vector<uint8_t> output(bpp * 8, 0); // initialized with 0
86 unsigned maxcolor = 2 << bpp;
87
88 // Iterate over all columns and rows of the tile
89 for (unsigned int col = 0; col < 8; col++) {
90 for (unsigned int row = 0; row < 8; row++) {
91 uint8_t color = tile.data[col * 8 + row];
92 if (color > maxcolor) {
93 throw std::invalid_argument("Invalid color value.");
94 }
95
96 // 1bpp format
97 if (bpp == 1) output[col] += (uint8_t)((color & 1) << (7 - row));
98
99 // 2bpp format
100 if (bpp >= 2) {
101 output[col * 2] += (uint8_t)((color & 1) << (7 - row));
102 output[col * 2 + 1] +=
103 (uint8_t)((uint8_t)((color & 2) == 2) << (7 - row));
104 }
105
106 // 3bpp format
107 if (bpp == 3)
108 output[16 + col] += (uint8_t)(((color & 4) == 4) << (7 - row));
109
110 // 4bpp format
111 if (bpp >= 4) {
112 output[16 + col * 2] += (uint8_t)(((color & 4) == 4) << (7 - row));
113 output[16 + col * 2 + 1] += (uint8_t)(((color & 8) == 8) << (7 - row));
114 }
115
116 // 8bpp format
117 if (bpp == 8) {
118 output[32 + col * 2] += (uint8_t)(((color & 16) == 16) << (7 - row));
119 output[32 + col * 2 + 1] +=
120 (uint8_t)(((color & 32) == 32) << (7 - row));
121 output[48 + col * 2] += (uint8_t)(((color & 64) == 64) << (7 - row));
122 output[48 + col * 2 + 1] +=
123 (uint8_t)(((color & 128) == 128) << (7 - row));
124 }
125 }
126 }
127 return output;
128}
129
130std::vector<uint8_t> ConvertBpp(const std::vector<uint8_t>& tiles,
131 uint32_t from_bpp, uint32_t to_bpp) {
132 unsigned int nb_tile = tiles.size() / (from_bpp * 8);
133 std::vector<uint8_t> converted(nb_tile * to_bpp * 8);
134
135 for (unsigned int i = 0; i < nb_tile; i++) {
136 tile8 tile = UnpackBppTile(tiles, i * from_bpp * 8, from_bpp);
137 std::vector<uint8_t> packed_tile = PackBppTile(tile, to_bpp);
138 std::memcpy(converted.data() + i * to_bpp * 8, packed_tile.data(),
139 to_bpp * 8);
140 }
141 return converted;
142}
143
144std::vector<uint8_t> Convert3bppTo4bpp(const std::vector<uint8_t>& tiles) {
145 return ConvertBpp(tiles, 3, 4);
146}
147
148std::vector<uint8_t> Convert4bppTo3bpp(const std::vector<uint8_t>& tiles) {
149 return ConvertBpp(tiles, 4, 3);
150}
151
152std::vector<uint8_t> SnesTo8bppSheet(const std::vector<uint8_t>& sheet,
153 int bpp) {
154 int xx = 0; // positions where we are at on the sheet
155 int yy = 0;
156 int pos = 0;
157 int ypos = 0;
158 int num_tiles = 64;
159 int buffer_size = 0x1000;
160 if (bpp == 2) {
161 bpp = 16;
162 num_tiles = 128;
163 buffer_size = 0x2000;
164 } else if (bpp == 3) {
165 bpp = 24;
166 } else if (bpp == 4) {
167 bpp = 32;
168 buffer_size = 0x4000;
169 } else if (bpp == 8) {
170 bpp = 64;
171 }
172 std::vector<uint8_t> sheet_buffer_out(buffer_size);
173
174 for (int i = 0; i < num_tiles; i++) { // for each tiles, 16 per line
175 for (int y = 0; y < 8; y++) { // for each line
176 for (int x = 0; x < 8; x++) { //[0] + [1] + [16]
177 auto b1 = (sheet[(y * 2) + (bpp * pos)] & (kGraphicsBitmap[x]));
178 auto b2 = (sheet[((y * 2) + (bpp * pos)) + 1] & (kGraphicsBitmap[x]));
179 auto b3 = (sheet[(16 + y) + (bpp * pos)] & (kGraphicsBitmap[x]));
180 unsigned char b = 0;
181 if (b1 != 0) {
182 b |= 1;
183 }
184 if (b2 != 0) {
185 b |= 2;
186 }
187 if (b3 != 0 && bpp != 16) {
188 b |= 4;
189 }
190 sheet_buffer_out[x + xx + (y * 128) + (yy * 1024)] = b;
191 }
192 }
193 pos++;
194 ypos++;
195 xx += 8;
196 if (ypos >= 16) {
197 yy++;
198 xx = 0;
199 ypos = 0;
200 }
201 }
202 return sheet_buffer_out;
203}
204
205std::vector<uint8_t> Bpp8SnesToIndexed(std::vector<uint8_t> data,
206 uint64_t bpp) {
207 // 3BPP
208 // [r0,bp1],[r0,bp2],[r1,bp1],[r1,bp2],[r2,bp1],[r2,bp2],[r3,bp1],[r3,bp2]
209 // [r4,bp1],[r4,bp2],[r5,bp1],[r5,bp2],[r6,bp1],[r6,bp2],[r7,bp1],[r7,bp2]
210 // [r0,bp3],[r0,bp4],[r1,bp3],[r1,bp4],[r2,bp3],[r2,bp4],[r3,bp3],[r3,bp4]
211 // [r4,bp3],[r4,bp4],[r5,bp3],[r5,bp4],[r6,bp3],[r6,bp4],[r7,bp3],[r7,bp4]
212 // [r0,bp5],[r0,bp6],[r1,bp5],[r1,bp6],[r2,bp5],[r2,bp6],[r3,bp5],[r3,bp6]
213 // [r4,bp5],[r4,bp6],[r5,bp5],[r5,bp6],[r6,bp5],[r6,bp6],[r7,bp5],[r7,bp6]
214 // [r0,bp7],[r0,bp8],[r1,bp7],[r1,bp8],[r2,bp7],[r2,bp8],[r3,bp7],[r3,bp8]
215 // [r4,bp7],[r4,bp8],[r5,bp7],[r5,bp8],[r6,bp7],[r6,bp8],[r7,bp7],[r7,bp8]
216
217 // 16 tiles = 1024 bytes
218 auto buffer = std::vector<uint8_t>(data.size());
219 std::vector<std::vector<uint8_t>> bitmap_data;
220 bitmap_data.resize(0x80);
221 for (auto& each : bitmap_data) {
222 each.reserve(0x800);
223 }
224 int yy = 0;
225 int xx = 0;
226 int pos = 0;
227
228 const uint16_t sheet_width = 128;
229
230 // 64 = 4096 bytes
231 // 16 = 1024?
232 int ypos = 0;
233 // for each tiles //16 per lines
234 for (int i = 0; i < 4096; i++) {
235 // for each lines
236 for (int y = 0; y < 8; y++) {
237 //[0] + [1] + [16]
238 for (int x = 0; x < 8; x++) {
239 const uint16_t bitmask[] = {0x80, 0x40, 0x20, 0x10,
240 0x08, 0x04, 0x02, 0x01};
241 auto b1 = (data[(y * 2) + ((bpp * 8) * pos)] & (bitmask[x]));
242 auto b2 = (data[((y * 2) + ((bpp * 8) * pos)) + 1] & (bitmask[x]));
243 auto b3 = (data[(y * 2) + ((bpp * 8) * pos) + 16] & (bitmask[x]));
244 auto b4 = (data[(y * 2) + ((bpp * 8) * pos) + 17] & (bitmask[x]));
245 auto b5 = (data[(y * 2) + ((bpp * 8) * pos) + 32] & (bitmask[x]));
246 auto b6 = (data[(y * 2) + ((bpp * 8) * pos) + 33] & (bitmask[x]));
247 auto b7 = (data[(y * 2) + ((bpp * 8) * pos) + 48] & (bitmask[x]));
248 auto b8 = (data[(y * 2) + ((bpp * 8) * pos) + 49] & (bitmask[x]));
249
250 auto b = 0;
251 if (b1 != 0) {
252 b |= 1;
253 }
254 if (b2 != 0) {
255 b |= 2;
256 }
257 if (bpp >= 4) {
258 if (b3 != 0) {
259 b |= 4;
260 }
261 if (b4 != 0) {
262 b |= 8;
263 }
264 }
265 if (bpp >= 8) {
266 if (b5 != 0) {
267 b |= 0x10;
268 }
269 if (b6 != 0) {
270 b |= 0x20;
271 }
272 if (b7 != 0) {
273 b |= 0x40;
274 }
275 if (b8 != 0) {
276 b |= 0x80;
277 }
278 }
279 // bitmap_data[((x + xx) * sheet_width) + y + (yy * 8)] = b;
280 bitmap_data[x + xx][y + (yy * 8)] = b;
281 }
282 }
283 pos++;
284 ypos++;
285 xx += 8;
286 if (ypos >= 16) {
287 yy++;
288 xx = 0;
289 ypos = 0;
290 }
291 }
292
293 int n = 0;
294
295 for (int y = 0; y < (data.size() / 64); y++) {
296 for (int x = 0; x < sheet_width; x++) { // 128 assumption
297 if (n < data.size()) {
298 // buffer[n] = bitmap_data[(x * sheet_width) + y];
299 buffer[n] = bitmap_data[x][y];
300 n++;
301 }
302 }
303 }
304 return buffer;
305}
306
307uint16_t TileInfoToWord(TileInfo tile_info) {
308 uint16_t result = 0;
309
310 // Copy the id_ value
311 result |= tile_info.id_ & 0x3FF; // ids are 10 bits
312
313 // Set the vertical_mirror_, horizontal_mirror_, and over_ flags
314 result |= (tile_info.vertical_mirror_ ? 1 : 0) << 15;
315 result |= (tile_info.horizontal_mirror_ ? 1 : 0) << 14;
316 result |= (tile_info.over_ ? 1 : 0) << 13;
317
318 // Set the palette_
319 result |= (tile_info.palette_ & 0x07) << 10; // palettes are 3 bits
320
321 return result;
322}
323
324TileInfo WordToTileInfo(uint16_t word) {
325 // Extract the id_ value
326 uint16_t id = word & 0x3FF; // ids are 10 bits
327
328 // Extract the vertical_mirror_, horizontal_mirror_, and over_ flags
329 bool vertical_mirror = (word >> 15) & 0x01;
330 bool horizontal_mirror = (word >> 14) & 0x01;
331 bool over = (word >> 13) & 0x01;
332
333 // Extract the palette_
334 uint8_t palette = (word >> 10) & 0x07; // palettes are 3 bits
335
336 return TileInfo(id, palette, vertical_mirror, horizontal_mirror, over);
337}
338
339uint16_t TileInfoToShort(TileInfo tile_info) {
340 // uint16_t result = 0;
341
342 // Copy the id_ value
343 // result |= tile_info.id_ & 0x3FF; // ids are 10 bits
344
345 // Set the vertical_mirror_, horizontal_mirror_, and over_ flags
346 // result |= (tile_info.vertical_mirror_ ? 1 : 0) << 10;
347 // result |= (tile_info.horizontal_mirror_ ? 1 : 0) << 11;
348 // result |= (tile_info.over_ ? 1 : 0) << 12;
349
350 // Set the palette_
351 // result |= (tile_info.palette_ & 0x07) << 13; // palettes are 3 bits
352
353 uint16_t value = 0;
354 // vhopppcc cccccccc
355 if (tile_info.over_) {
356 value |= TilePriorityBit;
357 }
358 if (tile_info.horizontal_mirror_) {
359 value |= TileHFlipBit;
360 }
361 if (tile_info.vertical_mirror_) {
362 value |= TileVFlipBit;
363 }
364 value |= (uint16_t)((tile_info.palette_ << 10) & 0x1C00);
365 value |= (uint16_t)(tile_info.id_ & TileNameMask);
366
367 return value;
368}
369
370TileInfo GetTilesInfo(uint16_t tile) {
371 // vhopppcc cccccccc
372 uint16_t tid = (uint16_t)(tile & TileNameMask);
373 uint8_t p = (uint8_t)((tile >> 10) & 0x07);
374
375 bool o = ((tile & TilePriorityBit) == TilePriorityBit);
376 bool h = ((tile & TileHFlipBit) == TileHFlipBit);
377 bool v = ((tile & TileVFlipBit) == TileVFlipBit);
378
379 return TileInfo(tid, p, v, h, o);
380}
381
382} // namespace gfx
383} // namespace app
384} // namespace yaze
SNES 16-bit tile metadata container.
Definition snes_tile.h:52
unsigned short ushort
Definition constants.h:119
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:324
constexpr ushort TilePriorityBit
Definition snes_tile.cc:15
std::vector< uint8_t > PackBppTile(const tile8 &tile, const uint32_t bpp)
Definition snes_tile.cc:83
constexpr ushort TileHFlipBit
Definition snes_tile.cc:18
std::vector< uint8_t > Bpp8SnesToIndexed(std::vector< uint8_t > data, uint64_t bpp)
Definition snes_tile.cc:205
std::vector< uint8_t > SnesTo8bppSheet(const std::vector< uint8_t > &sheet, int bpp)
Definition snes_tile.cc:152
uint16_t TileInfoToShort(TileInfo tile_info)
Definition snes_tile.cc:339
tile8 UnpackBppTile(const std::vector< uint8_t > &data, const uint32_t offset, const uint32_t bpp)
Definition snes_tile.cc:26
constexpr ushort TileVFlipBit
Definition snes_tile.cc:21
constexpr uint8_t kGraphicsBitmap[8]
Definition snes_tile.h:18
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 ushort TileNameMask
Definition snes_tile.cc:24
uint16_t TileInfoToWord(TileInfo tile_info)
Definition snes_tile.cc:307
TileInfo GetTilesInfo(uint16_t tile)
Definition snes_tile.cc:370
Definition common.cc:21