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