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 srcIndex = tile_index + (y * 8 + x);
36 int destX = col * 8 + x;
37 int destY = row * 8 + y;
38 int destIndex = (destY * width * 8) + destX;
39 tilesheet.mutable_bitmap()->mutable_data()[destIndex] =
40 graphics_buffer[srcIndex];
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) {
67 // Calculate the base position for this Tile16 in the full-size bitmap
68 int tiles_per_row = bitmap_->width() / tile_width_;
69 int tile16_row = num_tiles_ / tiles_per_row;
70 int tile16_column = num_tiles_ % tiles_per_row;
71 int baseX = tile16_column * tile_width_;
72 int baseY = tile16_row * tile_height_;
73
74 // Compose and place each part of the Tile16
75 ComposeAndPlaceTilePart(graphics_buffer, top_left, baseX, baseY);
76 ComposeAndPlaceTilePart(graphics_buffer, top_right, baseX + 8, baseY);
77 ComposeAndPlaceTilePart(graphics_buffer, bottom_left, baseX, baseY + 8);
78 ComposeAndPlaceTilePart(graphics_buffer, bottom_right, baseX + 8, baseY + 8);
79
80 tile_info_.push_back({top_left, top_right, bottom_left, bottom_right});
81
82 num_tiles_++;
83}
84
86 const std::vector<uint8_t>& graphics_buffer, const TileInfo& tile_info,
87 int baseX, int baseY) {
88 std::vector<uint8_t> tile_data =
89 FetchTileDataFromGraphicsBuffer(graphics_buffer, tile_info.id_);
90
91 if (tile_info.vertical_mirror_) {
92 MirrorTileDataVertically(tile_data);
93 }
94 if (tile_info.horizontal_mirror_) {
96 }
97
98 // Place the tile data into the full-size bitmap at the calculated position
99 for (int y = 0; y < 8; ++y) {
100 for (int x = 0; x < 8; ++x) {
101 int srcIndex = y * 8 + x;
102 int destX = baseX + x;
103 int destY = baseY + y;
104 int destIndex = (destY * bitmap_->width()) + destX;
105 internal_data_[destIndex] = tile_data[srcIndex];
106 }
107 }
108
109 bitmap_->set_data(internal_data_);
110}
111
112} // namespace gfx
113} // namespace app
114} // namespace yaze
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
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
void ComposeAndPlaceTilePart(const std::vector< uint8_t > &graphics_buffer, const TileInfo &tile_info, int baseX, int baseY)
Definition tilesheet.cc:85
void MirrorTileDataHorizontally(std::vector< uint8_t > &tileData)
Definition tilesheet.h:145
void Init(int width, int height, TileType tile_type)
Definition tilesheet.cc:49
void MirrorTileDataVertically(std::vector< uint8_t > &tileData)
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)
Definition tilesheet.cc:62
std::vector< uint8_t > internal_data_
Definition tilesheet.h:178
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