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.
This commit is contained in:
scawful
2025-05-08 11:44:22 -04:00
parent 43bf1e04c5
commit fb015523dc
2 changed files with 45 additions and 15 deletions

View File

@@ -5,8 +5,6 @@
#include <stdexcept>
#include <vector>
#include "util/macro.h"
namespace yaze {
namespace gfx {
@@ -128,15 +126,7 @@ std::vector<uint8_t> ConvertBpp(const std::vector<uint8_t>& tiles,
return converted;
}
std::vector<uint8_t> Convert3bppTo4bpp(const std::vector<uint8_t>& tiles) {
return ConvertBpp(tiles, 3, 4);
}
std::vector<uint8_t> Convert4bppTo3bpp(const std::vector<uint8_t>& tiles) {
return ConvertBpp(tiles, 4, 3);
}
std::vector<uint8_t> SnesTo8bppSheet(const std::vector<uint8_t>& sheet, int bpp,
std::vector<uint8_t> SnesTo8bppSheet(std::span<uint8_t> 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<uint8_t>& bitmap,
}
}
void LoadSNES4bppGFXToIndexedColorMatrix(std::span<uint8_t> src,
std::span<uint8_t> 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

View File

@@ -6,6 +6,7 @@
#include <array>
#include <cstdint>
#include <cstring>
#include <span>
#include <stdexcept>
#include <vector>
@@ -19,7 +20,7 @@ constexpr int kTilesheetDepth = 8;
constexpr uint8_t kGraphicsBitmap[8] = {0x80, 0x40, 0x20, 0x10,
0x08, 0x04, 0x02, 0x01};
std::vector<uint8_t> SnesTo8bppSheet(const std::vector<uint8_t>& sheet, int bpp,
std::vector<uint8_t> SnesTo8bppSheet(std::span<uint8_t> sheet, int bpp,
int num_sheets = 1);
std::vector<uint8_t> Bpp8SnesToIndexed(std::vector<uint8_t> data,
uint64_t bpp = 0);
@@ -32,12 +33,12 @@ std::vector<uint8_t> PackBppTile(const snes_tile8& tile, const uint32_t bpp);
std::vector<uint8_t> ConvertBpp(const std::vector<uint8_t>& tiles,
uint32_t from_bpp, uint32_t to_bpp);
std::vector<uint8_t> Convert3bppTo4bpp(const std::vector<uint8_t>& tiles);
std::vector<uint8_t> Convert4bppTo3bpp(const std::vector<uint8_t>& tiles);
void CopyTile8bpp16(int x, int y, int tile, std::vector<uint8_t>& bitmap,
std::vector<uint8_t>& blockset);
void LoadSNES4bppGFXToIndexedColorMatrix(std::span<uint8_t> src,
std::span<uint8_t> dest);
/**
* @brief SNES 16-bit tile metadata container
*