From f712474efe4c939195ca99924febd76631f35d25 Mon Sep 17 00:00:00 2001 From: scawful Date: Fri, 12 Apr 2024 00:34:06 -0400 Subject: [PATCH] Add gfx::Tilesheet implementations to source file --- src/CMakeLists.txt | 1 + src/app/gfx/tilesheet.cc | 114 +++++++++++++++++++++++++++++++++++++++ src/app/gfx/tilesheet.h | 67 ++++------------------- 3 files changed, 125 insertions(+), 57 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dbf55f5f..702b7b40 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,6 +29,7 @@ set( app/gfx/snes_palette.cc app/gfx/snes_tile.cc app/gfx/snes_color.cc + app/gfx/tilesheet.cc ) set( diff --git a/src/app/gfx/tilesheet.cc b/src/app/gfx/tilesheet.cc index e69de29b..71c8f584 100644 --- a/src/app/gfx/tilesheet.cc +++ b/src/app/gfx/tilesheet.cc @@ -0,0 +1,114 @@ +#include "app/gfx/tilesheet.h" + +#include +#include + +#include "app/gfx/bitmap.h" +#include "app/gfx/snes_color.h" +#include "app/gfx/snes_palette.h" +#include "app/gfx/snes_tile.h" + +namespace yaze { +namespace app { +namespace gfx { + +absl::StatusOr CreateTilesheetFromGraphicsBuffer( + const uint8_t* graphics_buffer, int width, int height, TileType tile_type, + int sheet_id) { + Tilesheet tilesheet; + + // Calculate the offset in the graphics buffer based on the sheet ID + int sheet_offset = sheet_id * width * height; + + // Initialize the tilesheet with the specified width, height, and tile type + tilesheet.Init(width, height, tile_type); + + // Iterate over the tiles in the sheet and copy them into the tilesheet + for (int row = 0; row < height; ++row) { + for (int col = 0; col < width; ++col) { + // Calculate the index of the current tile in the graphics buffer + int tile_index = sheet_offset + (row * width + col) * 64; + + // 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]; + } + } + } + } + + return tilesheet; +} + +void Tilesheet::Init(int width, int height, TileType tile_type) { + bitmap_ = std::make_shared(width, height, 8, 0x20000); + internal_data_.resize(0x20000); + tile_type_ = tile_type; + if (tile_type_ == TileType::Tile8) { + tile_width_ = 8; + tile_height_ = 8; + } else { + tile_width_ = 16; + tile_height_ = 16; + } +} + +void Tilesheet::ComposeTile16(const std::vector& graphics_buffer, + const TileInfo& top_left, + const TileInfo& top_right, + const TileInfo& bottom_left, + const TileInfo& bottom_right) { + // Calculate the base position for this Tile16 in the full-size bitmap + 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_; + + // 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); + + tile_info_.push_back({top_left, top_right, bottom_left, bottom_right}); + + num_tiles_++; +} + +void Tilesheet::ComposeAndPlaceTilePart( + const std::vector& graphics_buffer, const TileInfo& tile_info, + int baseX, int baseY) { + std::vector tile_data = + FetchTileDataFromGraphicsBuffer(graphics_buffer, tile_info.id_); + + if (tile_info.vertical_mirror_) { + MirrorTileDataVertically(tile_data); + } + if (tile_info.horizontal_mirror_) { + MirrorTileDataHorizontally(tile_data); + } + + // 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]; + } + } + + bitmap_->set_data(internal_data_); +} + +} // namespace gfx +} // namespace app +} // namespace yaze diff --git a/src/app/gfx/tilesheet.h b/src/app/gfx/tilesheet.h index 81ca60a2..621f2efe 100644 --- a/src/app/gfx/tilesheet.h +++ b/src/app/gfx/tilesheet.h @@ -1,10 +1,13 @@ #ifndef YAZE_APP_GFX_TILESHEET_H #define YAZE_APP_GFX_TILESHEET_H + #include #include #include "app/gfx/bitmap.h" +#include "app/gfx/snes_color.h" #include "app/gfx/snes_palette.h" +#include "app/gfx/snes_tile.h" namespace yaze { namespace app { @@ -22,68 +25,14 @@ class Tilesheet { tile_height_(tileHeight), tile_type_(tile_type) {} - void Init(int width, int height, TileType tile_type) { - bitmap_ = std::make_shared(width, height, 8, 0x20000); - internal_data_.resize(0x20000); - tile_type_ = tile_type; - if (tile_type_ == TileType::Tile8) { - tile_width_ = 8; - tile_height_ = 8; - } else { - tile_width_ = 16; - tile_height_ = 16; - } - } + void Init(int width, int height, TileType tile_type); void ComposeTile16(const std::vector& graphics_buffer, const TileInfo& top_left, const TileInfo& top_right, - const TileInfo& bottom_left, - const TileInfo& bottom_right) { - // Calculate the base position for this Tile16 in the full-size bitmap - 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_; - - // 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); - - tile_info_.push_back({top_left, top_right, bottom_left, bottom_right}); - - num_tiles_++; - } + const TileInfo& bottom_left, const TileInfo& bottom_right); void ComposeAndPlaceTilePart(const std::vector& graphics_buffer, - const TileInfo& tile_info, int baseX, - int baseY) { - std::vector tile_data = - FetchTileDataFromGraphicsBuffer(graphics_buffer, tile_info.id_); - - if (tile_info.vertical_mirror_) { - MirrorTileDataVertically(tile_data); - } - if (tile_info.horizontal_mirror_) { - MirrorTileDataHorizontally(tile_data); - } - - // 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]; - } - } - - bitmap_->set_data(internal_data_); - } + const TileInfo& tile_info, int baseX, int baseY); // Extracts a tile from the tilesheet Bitmap GetTile(int tileX, int tileY, int bmp_width, int bmp_height) { @@ -230,6 +179,10 @@ class Tilesheet { TileType tile_type_; }; +absl::StatusOr CreateTilesheetFromGraphicsBuffer( + const uint8_t* graphics_buffer, int width, int height, TileType tile_type, + int sheet_id); + } // namespace gfx } // namespace app } // namespace yaze