From 744ad03be3cf08da27df08de9b73b040b20c3c6e Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 10 Nov 2024 09:52:58 -0500 Subject: [PATCH] Refactor bitmap and tilesheet classes: improve variable naming, remove unused methods, and add tile data mirroring functionality for enhanced clarity and maintainability --- src/app/gfx/bitmap.cc | 18 +------ src/app/gfx/tilesheet.cc | 111 ++++++++++++++++++++++++++++++++------- src/app/gfx/tilesheet.h | 111 ++++++++------------------------------- 3 files changed, 115 insertions(+), 125 deletions(-) diff --git a/src/app/gfx/bitmap.cc b/src/app/gfx/bitmap.cc index 1b97a62b..77b8a36c 100644 --- a/src/app/gfx/bitmap.cc +++ b/src/app/gfx/bitmap.cc @@ -466,22 +466,6 @@ void Bitmap::Get8x8Tile(int tile_index, int x, int y, } } -void Bitmap::Get16x16Tile(int tile_index, int x, int y, - std::vector &tile_data, - int &tile_data_offset) { - int tile_offset = tile_index * (width_ * height_); - for (int i = 0; i < 16; i++) { - int row_offset = tile_offset + ((i / 8) * (width_ * 8)); - for (int j = 0; j < 16; j++) { - int pixel_offset = - row_offset + ((j / 8) * 8) + ((i % 8) * width_) + (j % 8); - int pixel_value = data_[pixel_offset]; - tile_data[tile_data_offset] = pixel_value; - tile_data_offset++; - } - } -} - void Bitmap::Get16x16Tile(int tile_x, int tile_y, std::vector &tile_data, int &tile_data_offset) { @@ -490,7 +474,7 @@ void Bitmap::Get16x16Tile(int tile_x, int tile_y, // Calculate the pixel position in the bitmap int pixel_x = tile_x + tx; int pixel_y = tile_y + ty; - int pixel_offset = pixel_y * width_ + pixel_x; + int pixel_offset = (pixel_y * width_) + pixel_x; int pixel_value = data_[pixel_offset]; // Store the pixel value in the tile data diff --git a/src/app/gfx/tilesheet.cc b/src/app/gfx/tilesheet.cc index 71c8f584..c5991c11 100644 --- a/src/app/gfx/tilesheet.cc +++ b/src/app/gfx/tilesheet.cc @@ -32,12 +32,12 @@ absl::StatusOr CreateTilesheetFromGraphicsBuffer( // Copy the tile data into the tilesheet for (int y = 0; y < 8; ++y) { for (int x = 0; x < 8; ++x) { - int srcIndex = tile_index + (y * 8 + x); - int destX = col * 8 + x; - int destY = row * 8 + y; - int destIndex = (destY * width * 8) + destX; - tilesheet.mutable_bitmap()->mutable_data()[destIndex] = - graphics_buffer[srcIndex]; + int src_index = tile_index + (y * 8 + x); + int dest_x = col * 8 + x; + int dest_y = row * 8 + y; + int dest_index = (dest_y * width * 8) + dest_x; + tilesheet.mutable_bitmap()->mutable_data()[dest_index] = + graphics_buffer[src_index]; } } } @@ -68,14 +68,15 @@ void Tilesheet::ComposeTile16(const std::vector& graphics_buffer, int tiles_per_row = bitmap_->width() / tile_width_; int tile16_row = num_tiles_ / tiles_per_row; int tile16_column = num_tiles_ % tiles_per_row; - int baseX = tile16_column * tile_width_; - int baseY = tile16_row * tile_height_; + int base_x = tile16_column * tile_width_; + int base_y = tile16_row * tile_height_; // Compose and place each part of the Tile16 - ComposeAndPlaceTilePart(graphics_buffer, top_left, baseX, baseY); - ComposeAndPlaceTilePart(graphics_buffer, top_right, baseX + 8, baseY); - ComposeAndPlaceTilePart(graphics_buffer, bottom_left, baseX, baseY + 8); - ComposeAndPlaceTilePart(graphics_buffer, bottom_right, baseX + 8, baseY + 8); + ComposeAndPlaceTilePart(graphics_buffer, top_left, base_x, base_y); + ComposeAndPlaceTilePart(graphics_buffer, top_right, base_x + 8, base_y); + ComposeAndPlaceTilePart(graphics_buffer, bottom_left, base_x, base_y + 8); + ComposeAndPlaceTilePart(graphics_buffer, bottom_right, base_x + 8, + base_y + 8); tile_info_.push_back({top_left, top_right, bottom_left, bottom_right}); @@ -84,7 +85,7 @@ void Tilesheet::ComposeTile16(const std::vector& graphics_buffer, void Tilesheet::ComposeAndPlaceTilePart( const std::vector& graphics_buffer, const TileInfo& tile_info, - int baseX, int baseY) { + int base_x, int base_y) { std::vector tile_data = FetchTileDataFromGraphicsBuffer(graphics_buffer, tile_info.id_); @@ -98,17 +99,91 @@ void Tilesheet::ComposeAndPlaceTilePart( // Place the tile data into the full-size bitmap at the calculated position for (int y = 0; y < 8; ++y) { for (int x = 0; x < 8; ++x) { - int srcIndex = y * 8 + x; - int destX = baseX + x; - int destY = baseY + y; - int destIndex = (destY * bitmap_->width()) + destX; - internal_data_[destIndex] = tile_data[srcIndex]; + int src_index = y * 8 + x; + int dest_x = base_x + x; + int dest_y = base_y + y; + int dest_index = (dest_y * bitmap_->width()) + dest_x; + internal_data_[dest_index] = tile_data[src_index]; } } bitmap_->set_data(internal_data_); } +std::vector Tilesheet::FetchTileDataFromGraphicsBuffer( + const std::vector& graphics_buffer, int tile_id) { + const int tile_width = 8; + const int tile_height = 8; + const int buffer_width = 128; + const int sheet_height = 32; + + const int tiles_per_row = buffer_width / tile_width; + const int rows_per_sheet = sheet_height / tile_height; + const int tiles_per_sheet = tiles_per_row * rows_per_sheet; + + // Calculate the position in the graphics_buffer_ based on tile_id + std::vector tile_data(0x40, 0x00); + int sheet = (tile_id / tiles_per_sheet) % 4 + 212; + int position_in_sheet = tile_id % tiles_per_sheet; + int row_in_sheet = position_in_sheet / tiles_per_row; + int column_in_sheet = position_in_sheet % tiles_per_row; + + // 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 src_x = column_in_sheet * tile_width + x; + int src_y = (sheet * sheet_height) + (row_in_sheet * tile_height) + y; + + int src_index = (src_y * buffer_width) + src_x; + int dest_index = y * tile_width + x; + + tile_data[dest_index] = graphics_buffer[src_index]; + } + } + + return tile_data; +} + +void Tilesheet::MirrorTileDataVertically(std::vector& tile_data) { + std::vector tile_data_copy = tile_data; + 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] = tile_data[src_index]; + } + } + tile_data = tile_data_copy; +} + +void Tilesheet::MirrorTileDataHorizontally(std::vector& tile_data) { + std::vector tile_data_copy = tile_data; + 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] = tile_data[src_index]; + } + } + tile_data = tile_data_copy; +} + +void Tilesheet::MirrorTileData(std::vector& tile_data, bool mirrorX, + bool mirrorY) { + std::vector tile_data_copy = tile_data; + if (mirrorX) { + MirrorTileDataHorizontally(tile_data_copy); + } + if (mirrorY) { + MirrorTileDataVertically(tile_data_copy); + } + tile_data = tile_data_copy; +} + } // namespace gfx } // namespace app } // namespace yaze diff --git a/src/app/gfx/tilesheet.h b/src/app/gfx/tilesheet.h index 5de1e6b4..abcb9273 100644 --- a/src/app/gfx/tilesheet.h +++ b/src/app/gfx/tilesheet.h @@ -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 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 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 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 FetchTileDataFromGraphicsBuffer( - const std::vector& graphics_buffer, int tile_id) { - const int tileWidth = 8; - const int tileHeight = 8; - const int bufferWidth = 128; - const int sheetHeight = 32; + const std::vector& 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 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& tileData) { - std::vector 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& tileData) { - std::vector 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& 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& tileData); + void MirrorTileDataHorizontally(std::vector& tileData); + void MirrorTileData(std::vector& tileData, bool mirror_x, + bool mirror_y); void WriteTile(int x, int y, const std::vector& tileData) { int tileDataOffset = 0;