Refactor bitmap and tilesheet classes: improve variable naming, remove unused methods, and add tile data mirroring functionality for enhanced clarity and maintainability
This commit is contained in:
@@ -45,36 +45,35 @@ class Tilesheet {
|
||||
// Extracts a tile from the tilesheet
|
||||
Bitmap GetTile(int tileX, int tileY, int bmp_width, int bmp_height) {
|
||||
std::vector<uint8_t> tileData(tile_width_ * tile_height_);
|
||||
int tileDataOffset = 0;
|
||||
int tile_data_offset = 0;
|
||||
bitmap_->Get8x8Tile(CalculateTileIndex(tileX, tileY), tileX, tileY,
|
||||
tileData, tileDataOffset);
|
||||
tileData, tile_data_offset);
|
||||
return Bitmap(bmp_width, bmp_height, bitmap_->depth(), tileData);
|
||||
}
|
||||
|
||||
Bitmap GetTile16(int tile_x, int tile_y) {
|
||||
std::vector<uint8_t> tile_data(tile_width_ * tile_height_, 0x00);
|
||||
int tileDataOffset = 0;
|
||||
bitmap_->Get16x16Tile(tile_x, tile_y, tile_data, tileDataOffset);
|
||||
return Bitmap(16, 16, bitmap_->depth(), tile_data);
|
||||
}
|
||||
|
||||
Bitmap GetTile16(int tile_id) {
|
||||
std::cout << "GetTile16: " << tile_id << std::endl;
|
||||
int tiles_per_row = bitmap_->width() / tile_width_;
|
||||
int tile_x = (tile_id % tiles_per_row) * tile_width_;
|
||||
int tile_y = (tile_id / tiles_per_row) * tile_height_;
|
||||
return GetTile16(tile_x, tile_y);
|
||||
std::cout << "Tile X: " << tile_x << " Tile Y: " << tile_y << std::endl;
|
||||
|
||||
std::vector<uint8_t> tile_data(tile_width_ * tile_height_, 0x00);
|
||||
int tile_data_offset = 0;
|
||||
bitmap_->Get16x16Tile(tile_x, tile_y, tile_data, tile_data_offset);
|
||||
|
||||
return Bitmap(16, 16, bitmap_->depth(), tile_data);
|
||||
}
|
||||
|
||||
// Copy a tile within the tilesheet
|
||||
void CopyTile(int srcX, int srcY, int destX, int destY, bool mirrorX = false,
|
||||
bool mirrorY = false) {
|
||||
auto srcTile = GetTile(srcX, srcY, tile_width_, tile_height_);
|
||||
auto destTileData = srcTile.vector();
|
||||
MirrorTileData(destTileData, mirrorX, mirrorY);
|
||||
WriteTile(destX, destY, destTileData);
|
||||
void CopyTile(int srcX, int srcY, int destX, int destY, bool mirror_x = false,
|
||||
bool mirror_y = false) {
|
||||
auto src_tile = GetTile(srcX, srcY, tile_width_, tile_height_);
|
||||
auto dest_tile_data = src_tile.vector();
|
||||
MirrorTileData(dest_tile_data, mirror_x, mirror_y);
|
||||
WriteTile(destX, destY, dest_tile_data);
|
||||
}
|
||||
|
||||
// Other methods and properties
|
||||
auto bitmap() const { return bitmap_; }
|
||||
auto mutable_bitmap() { return bitmap_; }
|
||||
auto num_tiles() const { return num_tiles_; }
|
||||
@@ -92,80 +91,12 @@ class Tilesheet {
|
||||
}
|
||||
|
||||
std::vector<uint8_t> FetchTileDataFromGraphicsBuffer(
|
||||
const std::vector<uint8_t>& graphics_buffer, int tile_id) {
|
||||
const int tileWidth = 8;
|
||||
const int tileHeight = 8;
|
||||
const int bufferWidth = 128;
|
||||
const int sheetHeight = 32;
|
||||
const std::vector<uint8_t>& graphics_buffer, int tile_id);
|
||||
|
||||
const int tilesPerRow = bufferWidth / tileWidth;
|
||||
const int rowsPerSheet = sheetHeight / tileHeight;
|
||||
const int tilesPerSheet = tilesPerRow * rowsPerSheet;
|
||||
|
||||
// Calculate the position in the graphics_buffer_ based on tile_id
|
||||
std::vector<uint8_t> tile_data(0x40, 0x00);
|
||||
int sheet = (tile_id / tilesPerSheet) % 4 + 212;
|
||||
int positionInSheet = tile_id % tilesPerSheet;
|
||||
int rowInSheet = positionInSheet / tilesPerRow;
|
||||
int columnInSheet = positionInSheet % tilesPerRow;
|
||||
|
||||
// Ensure that the sheet ID is between 212 and 215
|
||||
assert(sheet >= 212 && sheet <= 215);
|
||||
|
||||
// Copy the tile data from the graphics_buffer_ to tile_data
|
||||
for (int y = 0; y < 8; ++y) {
|
||||
for (int x = 0; x < 8; ++x) {
|
||||
// Calculate the position in the graphics_buffer_ based on tile_id
|
||||
|
||||
int srcX = columnInSheet * tileWidth + x;
|
||||
int srcY = (sheet * sheetHeight) + (rowInSheet * tileHeight) + y;
|
||||
|
||||
int src_index = (srcY * bufferWidth) + srcX;
|
||||
int dest_index = y * tileWidth + x;
|
||||
|
||||
tile_data[dest_index] = graphics_buffer[src_index];
|
||||
}
|
||||
}
|
||||
|
||||
return tile_data;
|
||||
}
|
||||
|
||||
void MirrorTileDataVertically(std::vector<uint8_t>& tileData) {
|
||||
std::vector<uint8_t> tile_data_copy = tileData;
|
||||
for (int i = 0; i < 8; ++i) { // For each row
|
||||
for (int j = 0; j < 8; ++j) { // For each column
|
||||
int src_index = i * 8 + j;
|
||||
int dest_index = (7 - i) * 8 + j; // Calculate the mirrored row
|
||||
tile_data_copy[dest_index] = tileData[src_index];
|
||||
}
|
||||
}
|
||||
tileData = tile_data_copy;
|
||||
}
|
||||
|
||||
void MirrorTileDataHorizontally(std::vector<uint8_t>& tileData) {
|
||||
std::vector<uint8_t> tile_data_copy = tileData;
|
||||
for (int i = 0; i < 8; ++i) { // For each row
|
||||
for (int j = 0; j < 8; ++j) { // For each column
|
||||
int src_index = i * 8 + j;
|
||||
int dest_index = i * 8 + (7 - j); // Calculate the mirrored column
|
||||
tile_data_copy[dest_index] = tileData[src_index];
|
||||
}
|
||||
}
|
||||
tileData = tile_data_copy;
|
||||
}
|
||||
|
||||
void MirrorTileData(std::vector<uint8_t>& tileData, bool mirrorX,
|
||||
bool mirrorY) {
|
||||
// Implement logic to mirror tile data horizontally and/or vertically
|
||||
std::vector tile_data_copy = tileData;
|
||||
if (mirrorX) {
|
||||
MirrorTileDataHorizontally(tile_data_copy);
|
||||
}
|
||||
if (mirrorY) {
|
||||
MirrorTileDataVertically(tile_data_copy);
|
||||
}
|
||||
tileData = tile_data_copy;
|
||||
}
|
||||
void MirrorTileDataVertically(std::vector<uint8_t>& tileData);
|
||||
void MirrorTileDataHorizontally(std::vector<uint8_t>& tileData);
|
||||
void MirrorTileData(std::vector<uint8_t>& tileData, bool mirror_x,
|
||||
bool mirror_y);
|
||||
|
||||
void WriteTile(int x, int y, const std::vector<uint8_t>& tileData) {
|
||||
int tileDataOffset = 0;
|
||||
|
||||
Reference in New Issue
Block a user