yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
tilesheet.cc
Go to the documentation of this file.
1#include "app/gfx/tilesheet.h"
2
3#include <memory>
4#include <vector>
5
6#include "app/gfx/bitmap.h"
9#include "app/gfx/snes_tile.h"
10
11namespace yaze {
12namespace app {
13namespace gfx {
14
15absl::StatusOr<Tilesheet> CreateTilesheetFromGraphicsBuffer(
16 const uint8_t* graphics_buffer, int width, int height, TileType tile_type,
17 int sheet_id) {
18 Tilesheet tilesheet;
19
20 // Calculate the offset in the graphics buffer based on the sheet ID
21 int sheet_offset = sheet_id * width * height;
22
23 // Initialize the tilesheet with the specified width, height, and tile type
24 tilesheet.Init(width, height, tile_type);
25
26 // Iterate over the tiles in the sheet and copy them into the tilesheet
27 for (int row = 0; row < height; ++row) {
28 for (int col = 0; col < width; ++col) {
29 // Calculate the index of the current tile in the graphics buffer
30 int tile_index = sheet_offset + (row * width + col) * 64;
31
32 // Copy the tile data into the tilesheet
33 for (int y = 0; y < 8; ++y) {
34 for (int x = 0; x < 8; ++x) {
35 int src_index = tile_index + (y * 8 + x);
36 int dest_x = col * 8 + x;
37 int dest_y = row * 8 + y;
38 int dest_index = (dest_y * width * 8) + dest_x;
39 tilesheet.mutable_bitmap()->mutable_data()[dest_index] =
40 graphics_buffer[src_index];
41 }
42 }
43 }
44 }
45
46 return tilesheet;
47}
48
49void Tilesheet::Init(int width, int height, TileType tile_type) {
50 bitmap_ = std::make_shared<Bitmap>(width, height, 8, 0x20000);
51 internal_data_.resize(0x20000);
54 tile_width_ = 8;
55 tile_height_ = 8;
56 } else {
57 tile_width_ = 16;
58 tile_height_ = 16;
59 }
60}
61
62void Tilesheet::ComposeTile16(const std::vector<uint8_t>& graphics_buffer,
63 const TileInfo& top_left,
64 const TileInfo& top_right,
65 const TileInfo& bottom_left,
66 const TileInfo& bottom_right, int sheet_offset) {
67 sheet_offset_ = sheet_offset;
68 // Calculate the base position for this Tile16 in the full-size bitmap
69 int tiles_per_row = bitmap_->width() / tile_width_;
70 int tile16_row = num_tiles_ / tiles_per_row;
71 int tile16_column = num_tiles_ % tiles_per_row;
72 int base_x = tile16_column * tile_width_;
73 int base_y = tile16_row * tile_height_;
74
75 // Compose and place each part of the Tile16
76 ComposeAndPlaceTilePart(graphics_buffer, top_left, base_x, base_y);
77 ComposeAndPlaceTilePart(graphics_buffer, top_right, base_x + 8, base_y);
78 ComposeAndPlaceTilePart(graphics_buffer, bottom_left, base_x, base_y + 8);
79 ComposeAndPlaceTilePart(graphics_buffer, bottom_right, base_x + 8,
80 base_y + 8);
81
82 tile_info_.push_back({top_left, top_right, bottom_left, bottom_right});
83
84 num_tiles_++;
85}
86
87void Tilesheet::ModifyTile16(const std::vector<uint8_t>& graphics_buffer,
88 const TileInfo& top_left,
89 const TileInfo& top_right,
90 const TileInfo& bottom_left,
91 const TileInfo& bottom_right, int tile_id,
92 int sheet_offset) {
93 sheet_offset_ = sheet_offset;
94 // Calculate the base position for this Tile16 in the full-size bitmap
95 int tiles_per_row = bitmap_->width() / tile_width_;
96 int tile16_row = tile_id / tiles_per_row;
97 int tile16_column = tile_id % tiles_per_row;
98 int base_x = tile16_column * tile_width_;
99 int base_y = tile16_row * tile_height_;
100
101 // Compose and place each part of the Tile16
102 ComposeAndPlaceTilePart(graphics_buffer, top_left, base_x, base_y);
103 ComposeAndPlaceTilePart(graphics_buffer, top_right, base_x + 8, base_y);
104 ComposeAndPlaceTilePart(graphics_buffer, bottom_left, base_x, base_y + 8);
105 ComposeAndPlaceTilePart(graphics_buffer, bottom_right, base_x + 8,
106 base_y + 8);
107
108 tile_info_[tile_id] = {top_left, top_right, bottom_left, bottom_right};
109}
110
112 const std::vector<uint8_t>& graphics_buffer, const TileInfo& tile_info,
113 int base_x, int base_y) {
114 std::vector<uint8_t> tile_data =
115 FetchTileDataFromGraphicsBuffer(graphics_buffer, tile_info.id_);
116
117 if (tile_info.vertical_mirror_) {
118 MirrorTileDataVertically(tile_data);
119 }
120 if (tile_info.horizontal_mirror_) {
122 }
123
124 // Place the tile data into the full-size bitmap at the calculated position
125 for (int y = 0; y < 8; ++y) {
126 for (int x = 0; x < 8; ++x) {
127 int src_index = y * 8 + x;
128 int dest_x = base_x + x;
129 int dest_y = base_y + y;
130 int dest_index = (dest_y * bitmap_->width()) + dest_x;
131 internal_data_[dest_index] = tile_data[src_index];
132 }
133 }
134
135 bitmap_->set_data(internal_data_);
136}
137
139 const std::vector<uint8_t>& graphics_buffer, int tile_id) {
140 const int tile_width = 8;
141 const int tile_height = 8;
142 const int buffer_width = 128;
143 const int sheet_height = 32;
144
145 const int tiles_per_row = buffer_width / tile_width;
146 const int rows_per_sheet = sheet_height / tile_height;
147 const int tiles_per_sheet = tiles_per_row * rows_per_sheet;
148
149 // Calculate the position in the graphics_buffer_ based on tile_id
150 std::vector<uint8_t> tile_data(0x40, 0x00);
151 int sheet = (tile_id / tiles_per_sheet) % 4 + sheet_offset_;
152 int position_in_sheet = tile_id % tiles_per_sheet;
153 int row_in_sheet = position_in_sheet / tiles_per_row;
154 int column_in_sheet = position_in_sheet % tiles_per_row;
155
156 // Ensure that the sheet ID is between 212 and 215 if using full gfx buffer
157 assert(sheet >= sheet_offset_ && sheet <= sheet_offset_ + 3);
158
159 // Copy the tile data from the graphics_buffer_ to tile_data
160 for (int y = 0; y < 8; ++y) {
161 for (int x = 0; x < 8; ++x) {
162 // Calculate the position in the graphics_buffer_ based on tile_id
163 int src_x = column_in_sheet * tile_width + x;
164 int src_y = (sheet * sheet_height) + (row_in_sheet * tile_height) + y;
165
166 int src_index = (src_y * buffer_width) + src_x;
167 int dest_index = y * tile_width + x;
168
169 tile_data[dest_index] = graphics_buffer[src_index];
170 }
171 }
172
173 return tile_data;
174}
175
176void Tilesheet::MirrorTileDataVertically(std::vector<uint8_t>& tile_data) {
177 std::vector<uint8_t> tile_data_copy = tile_data;
178 for (int i = 0; i < 8; ++i) { // For each row
179 for (int j = 0; j < 8; ++j) { // For each column
180 int src_index = i * 8 + j;
181 int dest_index = (7 - i) * 8 + j; // Calculate the mirrored row
182 tile_data_copy[dest_index] = tile_data[src_index];
183 }
184 }
185 tile_data = tile_data_copy;
186}
187
188void Tilesheet::MirrorTileDataHorizontally(std::vector<uint8_t>& tile_data) {
189 std::vector<uint8_t> tile_data_copy = tile_data;
190 for (int i = 0; i < 8; ++i) { // For each row
191 for (int j = 0; j < 8; ++j) { // For each column
192 int src_index = i * 8 + j;
193 int dest_index = i * 8 + (7 - j); // Calculate the mirrored column
194 tile_data_copy[dest_index] = tile_data[src_index];
195 }
196 }
197 tile_data = tile_data_copy;
198}
199
200void Tilesheet::MirrorTileData(std::vector<uint8_t>& tile_data, bool mirrorX,
201 bool mirrorY) {
202 std::vector tile_data_copy = tile_data;
203 if (mirrorX) {
204 MirrorTileDataHorizontally(tile_data_copy);
205 }
206 if (mirrorY) {
207 MirrorTileDataVertically(tile_data_copy);
208 }
209 tile_data = tile_data_copy;
210}
211
212} // namespace gfx
213} // namespace app
214} // namespace yaze
SNES 16-bit tile metadata container.
Definition snes_tile.h:50
Represents a tilesheet, which is a collection of tiles stored in a bitmap.
Definition tilesheet.h:30
std::vector< InternalTile16 > tile_info_
Definition tilesheet.h:132
std::vector< uint8_t > FetchTileDataFromGraphicsBuffer(const std::vector< uint8_t > &graphics_buffer, int tile_id)
Definition tilesheet.cc:138
void ComposeAndPlaceTilePart(const std::vector< uint8_t > &graphics_buffer, const TileInfo &tile_info, int baseX, int baseY)
Definition tilesheet.cc:111
auto tile_width() const
Definition tilesheet.h:89
void MirrorTileDataHorizontally(std::vector< uint8_t > &tileData)
Definition tilesheet.cc:188
void Init(int width, int height, TileType tile_type)
Definition tilesheet.cc:49
void MirrorTileDataVertically(std::vector< uint8_t > &tileData)
Definition tilesheet.cc:176
void MirrorTileData(std::vector< uint8_t > &tileData, bool mirror_x, bool mirror_y)
Definition tilesheet.cc:200
auto tile_height() const
Definition tilesheet.h:90
void ModifyTile16(const std::vector< uint8_t > &graphics_buffer, const TileInfo &top_left, const TileInfo &top_right, const TileInfo &bottom_left, const TileInfo &bottom_right, int tile_id, int sheet_offset=0)
Definition tilesheet.cc:87
std::vector< uint8_t > internal_data_
Definition tilesheet.h:131
std::shared_ptr< Bitmap > bitmap_
Definition tilesheet.h:133
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, int sheet_offset=0)
Definition tilesheet.cc:62
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:22