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
19 std::array<TileInfo, 4> tiles;
20};
21
30class Tilesheet {
31 public:
32 Tilesheet() = default;
33 Tilesheet(std::shared_ptr<Bitmap> bitmap, int tileWidth, int tileHeight,
35 : bitmap_(std::move(bitmap)),
36 tile_width_(tileWidth),
37 tile_height_(tileHeight),
39
40 void Init(int width, int height, TileType tile_type);
41
42 void ComposeTile16(const std::vector<uint8_t>& graphics_buffer,
43 const TileInfo& top_left, const TileInfo& top_right,
44 const TileInfo& bottom_left, const TileInfo& bottom_right,
45 int sheet_offset = 0);
46 void ModifyTile16(const std::vector<uint8_t>& graphics_buffer,
47 const TileInfo& top_left, const TileInfo& top_right,
48 const TileInfo& bottom_left, const TileInfo& bottom_right, int tile_id,
49 int sheet_offset = 0);
50
51 void ComposeAndPlaceTilePart(const std::vector<uint8_t>& graphics_buffer,
52 const TileInfo& tile_info, int baseX, int baseY);
53
54 // Extracts a tile from the tilesheet
55 Bitmap GetTile(int tileX, int tileY, int bmp_width, int bmp_height) {
56 std::vector<uint8_t> tileData(tile_width_ * tile_height_);
57 int tile_data_offset = 0;
58 bitmap_->Get8x8Tile(CalculateTileIndex(tileX, tileY), tileX, tileY,
59 tileData, tile_data_offset);
60 return Bitmap(bmp_width, bmp_height, bitmap_->depth(), tileData);
61 }
62
63 Bitmap GetTile16(int tile_id) {
64 std::cout << "GetTile16: " << tile_id << std::endl;
65 int tiles_per_row = bitmap_->width() / tile_width_;
66 int tile_x = (tile_id % tiles_per_row) * tile_width_;
67 int tile_y = (tile_id / tiles_per_row) * tile_height_;
68 std::cout << "Tile X: " << tile_x << " Tile Y: " << tile_y << std::endl;
69
70 std::vector<uint8_t> tile_data(tile_width_ * tile_height_, 0x00);
71 int tile_data_offset = 0;
72 bitmap_->Get16x16Tile(tile_x, tile_y, tile_data, tile_data_offset);
73
74 return Bitmap(16, 16, bitmap_->depth(), tile_data);
75 }
76
77 // Copy a tile within the tilesheet
78 void CopyTile(int srcX, int srcY, int destX, int destY, bool mirror_x = false,
79 bool mirror_y = false) {
80 auto src_tile = GetTile(srcX, srcY, tile_width_, tile_height_);
81 auto dest_tile_data = src_tile.vector();
82 MirrorTileData(dest_tile_data, mirror_x, mirror_y);
83 WriteTile(destX, destY, dest_tile_data);
84 }
85
86 auto bitmap() const { return bitmap_; }
87 auto mutable_bitmap() { return bitmap_; }
88 auto num_tiles() const { return num_tiles_; }
89 auto tile_width() const { return tile_width_; }
90 auto tile_height() const { return tile_height_; }
92 auto palette() const { return palette_; }
93 auto tile_type() const { return tile_type_; }
94 auto tile_info() const { return tile_info_; }
95 auto mutable_tile_info() { return tile_info_; }
96 void clear() {
98 internal_data_.clear();
99 tile_info_.clear();
100 bitmap_.reset();
101 num_tiles_ = 0;
102 }
103
104 private:
105 int CalculateTileIndex(int x, int y) {
106 return y * (bitmap_->width() / tile_width_) + x;
107 }
108
109 std::vector<uint8_t> FetchTileDataFromGraphicsBuffer(
110 const std::vector<uint8_t>& graphics_buffer, int tile_id);
111
112 void MirrorTileDataVertically(std::vector<uint8_t>& tileData);
113 void MirrorTileDataHorizontally(std::vector<uint8_t>& tileData);
114 void MirrorTileData(std::vector<uint8_t>& tileData, bool mirror_x,
115 bool mirror_y);
116
117 void WriteTile(int x, int y, const std::vector<uint8_t>& tileData) {
118 int tileDataOffset = 0;
119 bitmap_->Get8x8Tile(CalculateTileIndex(x, y), x, y,
120 const_cast<std::vector<uint8_t>&>(tileData),
121 tileDataOffset);
122 }
123
124 int num_tiles_ = 0;
125 int tile_width_ = 0;
128
131 std::vector<uint8_t> internal_data_;
132 std::vector<InternalTile16> tile_info_;
133 std::shared_ptr<Bitmap> bitmap_;
134};
135
136absl::StatusOr<Tilesheet> CreateTilesheetFromGraphicsBuffer(
137 const uint8_t* graphics_buffer, int width, int height, TileType tile_type,
138 int sheet_id);
139
140} // namespace gfx
141} // namespace app
142} // namespace yaze
143
144#endif // YAZE_APP_GFX_TILESHEET_H
Represents a bitmap image.
Definition bitmap.h:67
int width() const
Definition bitmap.h:172
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
Tile composition of four 8x8 tiles.
Definition snes_tile.h:130
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
auto set_palette(gfx::SnesPalette &palette)
Definition tilesheet.h:91
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
Tilesheet(std::shared_ptr< Bitmap > bitmap, int tileWidth, int tileHeight, TileType tile_type)
Definition tilesheet.h:33
auto tile_width() const
Definition tilesheet.h:89
void WriteTile(int x, int y, const std::vector< uint8_t > &tileData)
Definition tilesheet.h:117
void MirrorTileDataHorizontally(std::vector< uint8_t > &tileData)
Definition tilesheet.cc:188
void Init(int width, int height, TileType tile_type)
Definition tilesheet.cc:49
Bitmap GetTile16(int tile_id)
Definition tilesheet.h:63
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 CopyTile(int srcX, int srcY, int destX, int destY, bool mirror_x=false, bool mirror_y=false)
Definition tilesheet.h:78
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
Bitmap GetTile(int tileX, int tileY, int bmp_width, int bmp_height)
Definition tilesheet.h:55
int CalculateTileIndex(int x, int y)
Definition tilesheet.h:105
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
std::array< TileInfo, 4 > tiles
Definition tilesheet.h:19