yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
tilesheet.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_TILESHEET_H
2#define YAZE_APP_GFX_TILESHEET_H
3
4#include <memory>
5#include <vector>
6
7#include "app/gfx/bitmap.h"
10#include "app/gfx/snes_tile.h"
11
12namespace yaze {
13namespace app {
14namespace gfx {
15
16enum class TileType { Tile8, Tile16 };
17
26class Tilesheet {
27 public:
28 Tilesheet() = default;
29 Tilesheet(std::shared_ptr<Bitmap> bitmap, int tileWidth, int tileHeight,
31 : bitmap_(std::move(bitmap)),
32 tile_width_(tileWidth),
33 tile_height_(tileHeight),
35
36 void Init(int width, int height, TileType tile_type);
37
38 void ComposeTile16(const std::vector<uint8_t>& graphics_buffer,
39 const TileInfo& top_left, const TileInfo& top_right,
40 const TileInfo& bottom_left, const TileInfo& bottom_right);
41
42 void ComposeAndPlaceTilePart(const std::vector<uint8_t>& graphics_buffer,
43 const TileInfo& tile_info, int baseX, int baseY);
44
45 // Extracts a tile from the tilesheet
46 Bitmap GetTile(int tileX, int tileY, int bmp_width, int bmp_height) {
47 std::vector<uint8_t> tileData(tile_width_ * tile_height_);
48 int tileDataOffset = 0;
49 bitmap_->Get8x8Tile(CalculateTileIndex(tileX, tileY), tileX, tileY,
50 tileData, tileDataOffset);
51 return Bitmap(bmp_width, bmp_height, bitmap_->depth(), tileData);
52 }
53
54 Bitmap GetTile16(int tile_x, int tile_y) {
55 std::vector<uint8_t> tile_data(tile_width_ * tile_height_, 0x00);
56 int tileDataOffset = 0;
57 bitmap_->Get16x16Tile(tile_x, tile_y, tile_data, tileDataOffset);
58 return Bitmap(16, 16, bitmap_->depth(), tile_data);
59 }
60
61 Bitmap GetTile16(int tile_id) {
62 int tiles_per_row = bitmap_->width() / tile_width_;
63 int tile_x = (tile_id % tiles_per_row) * tile_width_;
64 int tile_y = (tile_id / tiles_per_row) * tile_height_;
65 return GetTile16(tile_x, tile_y);
66 }
67
68 // Copy a tile within the tilesheet
69 void CopyTile(int srcX, int srcY, int destX, int destY, bool mirrorX = false,
70 bool mirrorY = false) {
71 auto srcTile = GetTile(srcX, srcY, tile_width_, tile_height_);
72 auto destTileData = srcTile.vector();
73 MirrorTileData(destTileData, mirrorX, mirrorY);
74 WriteTile(destX, destY, destTileData);
75 }
76
77 // Other methods and properties
78 auto bitmap() const { return bitmap_; }
79 auto mutable_bitmap() { return bitmap_; }
80 auto num_tiles() const { return num_tiles_; }
81 auto tile_width() const { return tile_width_; }
82 auto tile_height() const { return tile_height_; }
84 auto palette() const { return palette_; }
85 auto tile_type() const { return tile_type_; }
86 auto tile_info() const { return tile_info_; }
87 auto mutable_tile_info() { return tile_info_; }
88
89 private:
90 int CalculateTileIndex(int x, int y) {
91 return y * (bitmap_->width() / tile_width_) + x;
92 }
93
94 std::vector<uint8_t> FetchTileDataFromGraphicsBuffer(
95 const std::vector<uint8_t>& graphics_buffer, int tile_id) {
96 const int tileWidth = 8;
97 const int tileHeight = 8;
98 const int bufferWidth = 128;
99 const int sheetHeight = 32;
100
101 const int tilesPerRow = bufferWidth / tileWidth;
102 const int rowsPerSheet = sheetHeight / tileHeight;
103 const int tilesPerSheet = tilesPerRow * rowsPerSheet;
104
105 // Calculate the position in the graphics_buffer_ based on tile_id
106 std::vector<uint8_t> tile_data(0x40, 0x00);
107 int sheet = (tile_id / tilesPerSheet) % 4 + 212;
108 int positionInSheet = tile_id % tilesPerSheet;
109 int rowInSheet = positionInSheet / tilesPerRow;
110 int columnInSheet = positionInSheet % tilesPerRow;
111
112 // Ensure that the sheet ID is between 212 and 215
113 assert(sheet >= 212 && sheet <= 215);
114
115 // Copy the tile data from the graphics_buffer_ to tile_data
116 for (int y = 0; y < 8; ++y) {
117 for (int x = 0; x < 8; ++x) {
118 // Calculate the position in the graphics_buffer_ based on tile_id
119
120 int srcX = columnInSheet * tileWidth + x;
121 int srcY = (sheet * sheetHeight) + (rowInSheet * tileHeight) + y;
122
123 int src_index = (srcY * bufferWidth) + srcX;
124 int dest_index = y * tileWidth + x;
125
126 tile_data[dest_index] = graphics_buffer[src_index];
127 }
128 }
129
130 return tile_data;
131 }
132
133 void MirrorTileDataVertically(std::vector<uint8_t>& tileData) {
134 std::vector<uint8_t> tile_data_copy = tileData;
135 for (int i = 0; i < 8; ++i) { // For each row
136 for (int j = 0; j < 8; ++j) { // For each column
137 int src_index = i * 8 + j;
138 int dest_index = (7 - i) * 8 + j; // Calculate the mirrored row
139 tile_data_copy[dest_index] = tileData[src_index];
140 }
141 }
142 tileData = tile_data_copy;
143 }
144
145 void MirrorTileDataHorizontally(std::vector<uint8_t>& tileData) {
146 std::vector<uint8_t> tile_data_copy = tileData;
147 for (int i = 0; i < 8; ++i) { // For each row
148 for (int j = 0; j < 8; ++j) { // For each column
149 int src_index = i * 8 + j;
150 int dest_index = i * 8 + (7 - j); // Calculate the mirrored column
151 tile_data_copy[dest_index] = tileData[src_index];
152 }
153 }
154 tileData = tile_data_copy;
155 }
156
157 void MirrorTileData(std::vector<uint8_t>& tileData, bool mirrorX,
158 bool mirrorY) {
159 // Implement logic to mirror tile data horizontally and/or vertically
160 std::vector tile_data_copy = tileData;
161 if (mirrorX) {
162 MirrorTileDataHorizontally(tile_data_copy);
163 }
164 if (mirrorY) {
165 MirrorTileDataVertically(tile_data_copy);
166 }
167 tileData = tile_data_copy;
168 }
169
170 void WriteTile(int x, int y, const std::vector<uint8_t>& tileData) {
171 int tileDataOffset = 0;
172 bitmap_->Get8x8Tile(CalculateTileIndex(x, y), x, y,
173 const_cast<std::vector<uint8_t>&>(tileData),
174 tileDataOffset);
175 }
176
178 std::vector<uint8_t> internal_data_;
179 std::shared_ptr<Bitmap> bitmap_;
181 std::array<TileInfo, 4> tiles;
182 };
183 std::vector<InternalTile16> tile_info_;
184 int num_tiles_ = 0;
185 int tile_width_ = 0;
188};
189
190absl::StatusOr<Tilesheet> CreateTilesheetFromGraphicsBuffer(
191 const uint8_t* graphics_buffer, int width, int height, TileType tile_type,
192 int sheet_id);
193
194} // namespace gfx
195} // namespace app
196} // namespace yaze
197
198#endif // YAZE_APP_GFX_TILESHEET_H
Represents a bitmap image.
Definition bitmap.h:70
int width() const
Definition bitmap.h:179
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
Tile composition of four 8x8 tiles.
Definition snes_tile.h:133
SNES 16-bit tile metadata container.
Definition snes_tile.h:52
Represents a tilesheet, which is a collection of tiles stored in a bitmap.
Definition tilesheet.h:26
auto set_palette(gfx::SnesPalette &palette)
Definition tilesheet.h:83
std::vector< InternalTile16 > tile_info_
Definition tilesheet.h:183
std::vector< uint8_t > FetchTileDataFromGraphicsBuffer(const std::vector< uint8_t > &graphics_buffer, int tile_id)
Definition tilesheet.h:94
gfx::SnesPalette palette_
Definition tilesheet.h:177
void ComposeAndPlaceTilePart(const std::vector< uint8_t > &graphics_buffer, const TileInfo &tile_info, int baseX, int baseY)
Definition tilesheet.cc:85
Tilesheet(std::shared_ptr< Bitmap > bitmap, int tileWidth, int tileHeight, TileType tile_type)
Definition tilesheet.h:29
auto tile_width() const
Definition tilesheet.h:81
void WriteTile(int x, int y, const std::vector< uint8_t > &tileData)
Definition tilesheet.h:170
void MirrorTileDataHorizontally(std::vector< uint8_t > &tileData)
Definition tilesheet.h:145
void Init(int width, int height, TileType tile_type)
Definition tilesheet.cc:49
Bitmap GetTile16(int tile_x, int tile_y)
Definition tilesheet.h:54
Bitmap GetTile16(int tile_id)
Definition tilesheet.h:61
void MirrorTileDataVertically(std::vector< uint8_t > &tileData)
Definition tilesheet.h:133
auto tile_height() const
Definition tilesheet.h:82
void MirrorTileData(std::vector< uint8_t > &tileData, bool mirrorX, bool mirrorY)
Definition tilesheet.h:157
void ComposeTile16(const std::vector< uint8_t > &graphics_buffer, const TileInfo &top_left, const TileInfo &top_right, const TileInfo &bottom_left, const TileInfo &bottom_right)
Definition tilesheet.cc:62
Bitmap GetTile(int tileX, int tileY, int bmp_width, int bmp_height)
Definition tilesheet.h:46
int CalculateTileIndex(int x, int y)
Definition tilesheet.h:90
std::vector< uint8_t > internal_data_
Definition tilesheet.h:178
void CopyTile(int srcX, int srcY, int destX, int destY, bool mirrorX=false, bool mirrorY=false)
Definition tilesheet.h:69
std::shared_ptr< Bitmap > bitmap_
Definition tilesheet.h:179
absl::StatusOr< Tilesheet > CreateTilesheetFromGraphicsBuffer(const uint8_t *graphics_buffer, int width, int height, TileType tile_type, int sheet_id)
Definition tilesheet.cc:15
Definition common.cc:21
std::array< TileInfo, 4 > tiles
Definition tilesheet.h:181