From 8bc896265f5192571d95683aaa7dfc1288d1e149 Mon Sep 17 00:00:00 2001 From: scawful Date: Tue, 23 Sep 2025 22:01:43 -0400 Subject: [PATCH] Refactor BackgroundBuffer tile access methods for improved validation and performance - Updated SetTileAt and GetTileAt methods to enhance boundary checks, ensuring valid tile coordinates are used. - Simplified buffer resizing logic in DrawBackground to accurately reflect the number of tiles based on width and height. - Improved tile drawing logic to prevent unnecessary operations when the tile value is 0xFFFF, enhancing rendering efficiency. --- src/app/gfx/background_buffer.cc | 35 ++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/app/gfx/background_buffer.cc b/src/app/gfx/background_buffer.cc index c983b238..0d53e3d2 100644 --- a/src/app/gfx/background_buffer.cc +++ b/src/app/gfx/background_buffer.cc @@ -17,16 +17,18 @@ BackgroundBuffer::BackgroundBuffer(int width, int height) } void BackgroundBuffer::SetTileAt(int x, int y, uint16_t value) { - if (x >= 0 && x < width_ / 8 && y >= 0 && y < height_ / 8) { - buffer_[y * (width_ / 8) + x] = value; - } + if (x < 0 || y < 0) return; + int tiles_w = width_ / 8; + int tiles_h = height_ / 8; + if (x >= tiles_w || y >= tiles_h) return; + buffer_[y * tiles_w + x] = value; } uint16_t BackgroundBuffer::GetTileAt(int x, int y) const { - if (x >= 0 && x < width_ / 8 && y >= 0 && y < height_ / 8) { - return buffer_[y * (width_ / 8) + x]; - } - return 0; + int tiles_w = width_ / 8; + int tiles_h = height_ / 8; + if (x < 0 || y < 0 || x >= tiles_w || y >= tiles_h) return 0; + return buffer_[y * tiles_w + x]; } void BackgroundBuffer::ClearBuffer() { std::ranges::fill(buffer_, 0); } @@ -55,18 +57,21 @@ void BackgroundBuffer::DrawTile(const TileInfo& tile, uint8_t* canvas, void BackgroundBuffer::DrawBackground(std::span gfx16_data) { // Initialize bitmap bitmap_.Create(width_, height_, 8, std::vector(width_ * height_, 0)); - if (buffer_.size() < width_ * height_) { - buffer_.resize(width_ * height_); + int tiles_w = width_ / 8; + int tiles_h = height_ / 8; + if ((int)buffer_.size() < tiles_w * tiles_h) { + buffer_.resize(tiles_w * tiles_h); } // For each tile on the tile buffer - for (int yy = 0; yy < (height_ / 8) * (width_ / 8); yy += (width_ / 8)) { - for (int xx = 0; xx < width_ / 8; xx++) { + for (int yy = 0; yy < tiles_h; yy++) { + for (int xx = 0; xx < tiles_w; xx++) { + uint16_t word = buffer_[xx + yy * tiles_w]; // Prevent draw if tile == 0xFFFF since it's 0 indexed - if (buffer_[xx + yy] != 0xFFFF) { - auto tile = gfx::GetTilesInfo(buffer_[xx + yy]); - DrawTile(tile, bitmap_.mutable_data().data(), gfx16_data.data(), 0); - } + if (word == 0xFFFF) continue; + auto tile = gfx::WordToTileInfo(word); + DrawTile(tile, bitmap_.mutable_data().data(), gfx16_data.data(), + (yy * 512) + (xx * 8)); } } }