From fb015523dca6ecb418f17290fd37810165928fd4 Mon Sep 17 00:00:00 2001 From: scawful Date: Thu, 8 May 2025 11:44:22 -0400 Subject: [PATCH] Refactor SNES tile handling and introduce new graphics loading functionality - Removed unused conversion functions for 3bpp to 4bpp and vice versa to streamline code. - Updated SnesTo8bppSheet function to use std::span for improved performance and flexibility. - Added LoadSNES4bppGFXToIndexedColorMatrix function to handle loading graphics data into an indexed color matrix, enhancing graphics management capabilities. --- src/app/gfx/snes_tile.cc | 51 +++++++++++++++++++++++++++++++--------- src/app/gfx/snes_tile.h | 9 +++---- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/app/gfx/snes_tile.cc b/src/app/gfx/snes_tile.cc index 4e8fab7f..e0465106 100644 --- a/src/app/gfx/snes_tile.cc +++ b/src/app/gfx/snes_tile.cc @@ -5,8 +5,6 @@ #include #include -#include "util/macro.h" - namespace yaze { namespace gfx { @@ -128,15 +126,7 @@ std::vector ConvertBpp(const std::vector& tiles, return converted; } -std::vector Convert3bppTo4bpp(const std::vector& tiles) { - return ConvertBpp(tiles, 3, 4); -} - -std::vector Convert4bppTo3bpp(const std::vector& tiles) { - return ConvertBpp(tiles, 4, 3); -} - -std::vector SnesTo8bppSheet(const std::vector& sheet, int bpp, +std::vector SnesTo8bppSheet(std::span sheet, int bpp, int num_sheets) { int xx = 0; // positions where we are at on the sheet int yy = 0; @@ -386,5 +376,44 @@ void CopyTile8bpp16(int x, int y, int tile, std::vector& bitmap, } } +void LoadSNES4bppGFXToIndexedColorMatrix(std::span src, + std::span dest) { + uint8_t b0; + uint8_t b1; + uint8_t b2; + uint8_t b3; + int res; + int mul; + int yAdder = 0; + int srcIndex; + int destX; + int destY; + int destIndex; + int mainIndexLimit = src.size() / 32; + for (int mainIndex = 0; mainIndex <= mainIndexLimit; mainIndex += 32) { + srcIndex = (mainIndex << 5); + if (srcIndex + 31 >= src.size()) return; + destX = mainIndex & 0x0F; + destY = mainIndex >> 4; + destIndex = ((destY << 7) + destX) << 3; + if (destIndex + 903 >= dest.size()) return; + for (int i = 0; i < 16; i += 2) { + mul = 1; + b0 = src[srcIndex + i]; + b1 = src[srcIndex + i + 1]; + b2 = src[srcIndex + i + 16]; + b3 = src[srcIndex + i + 17]; + for (int j = 0; j < 8; j++) { + res = ((b0 & mul) | ((b1 & mul) << 1) | ((b2 & mul) << 2) | + ((b3 & mul) << 3)) >> + j; + dest[destIndex + (7 - j) + yAdder] = res; + mul <<= 1; + } + yAdder += 128; + } + } +} + } // namespace gfx } // namespace yaze diff --git a/src/app/gfx/snes_tile.h b/src/app/gfx/snes_tile.h index 7137990f..651d9f94 100644 --- a/src/app/gfx/snes_tile.h +++ b/src/app/gfx/snes_tile.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -19,7 +20,7 @@ constexpr int kTilesheetDepth = 8; constexpr uint8_t kGraphicsBitmap[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01}; -std::vector SnesTo8bppSheet(const std::vector& sheet, int bpp, +std::vector SnesTo8bppSheet(std::span sheet, int bpp, int num_sheets = 1); std::vector Bpp8SnesToIndexed(std::vector data, uint64_t bpp = 0); @@ -32,12 +33,12 @@ std::vector PackBppTile(const snes_tile8& tile, const uint32_t bpp); std::vector ConvertBpp(const std::vector& tiles, uint32_t from_bpp, uint32_t to_bpp); -std::vector Convert3bppTo4bpp(const std::vector& tiles); -std::vector Convert4bppTo3bpp(const std::vector& tiles); - void CopyTile8bpp16(int x, int y, int tile, std::vector& bitmap, std::vector& blockset); +void LoadSNES4bppGFXToIndexedColorMatrix(std::span src, + std::span dest); + /** * @brief SNES 16-bit tile metadata container *