Refactor tile structures and functions to use snes_tile8; introduce GraphicsBuffer class for better data management

This commit is contained in:
scawful
2024-11-13 08:52:12 -05:00
parent 2fd1b1fe94
commit d13407c8e2
3 changed files with 100 additions and 22 deletions

View File

@@ -23,9 +23,9 @@ constexpr ushort TileVFlipBit = 0x8000;
// Bits used for tile name
constexpr ushort TileNameMask = 0x03FF;
tile8 UnpackBppTile(const std::vector<uint8_t>& data, const uint32_t offset,
snes_tile8 UnpackBppTile(const std::vector<uint8_t>& data, const uint32_t offset,
const uint32_t bpp) {
tile8 tile;
snes_tile8 tile;
assert(bpp >= 1 && bpp <= 8);
unsigned int bpp_pos[8]; // More for conveniance and readibility
for (int col = 0; col < 8; col++) {
@@ -80,7 +80,7 @@ tile8 UnpackBppTile(const std::vector<uint8_t>& data, const uint32_t offset,
return tile;
}
std::vector<uint8_t> PackBppTile(const tile8& tile, const uint32_t bpp) {
std::vector<uint8_t> PackBppTile(const snes_tile8& tile, const uint32_t bpp) {
// Allocate memory for output data
std::vector<uint8_t> output(bpp * 8, 0); // initialized with 0
unsigned maxcolor = 2 << bpp;
@@ -133,7 +133,7 @@ std::vector<uint8_t> ConvertBpp(const std::vector<uint8_t>& tiles,
std::vector<uint8_t> converted(nb_tile * to_bpp * 8);
for (unsigned int i = 0; i < nb_tile; i++) {
tile8 tile = UnpackBppTile(tiles, i * from_bpp * 8, from_bpp);
snes_tile8 tile = UnpackBppTile(tiles, i * from_bpp * 8, from_bpp);
std::vector<uint8_t> packed_tile = PackBppTile(tile, to_bpp);
std::memcpy(converted.data() + i * to_bpp * 8, packed_tile.data(),
to_bpp * 8);
@@ -401,4 +401,4 @@ void CopyTile8bpp16(int x, int y, int tile, std::vector<uint8_t>& bitmap,
} // namespace gfx
} // namespace app
} // namespace yaze
} // namespace yaze

View File

@@ -5,7 +5,7 @@
#include <cstring>
#include <vector>
#include "app/core/constants.h"
#include "incl/snes_tile.h"
namespace yaze {
namespace app {
@@ -23,17 +23,10 @@ std::vector<uint8_t> SnesTo8bppSheet(const std::vector<uint8_t>& sheet, int bpp,
std::vector<uint8_t> Bpp8SnesToIndexed(std::vector<uint8_t> data,
uint64_t bpp = 0);
struct tile8 {
uint32_t id;
char data[64];
uint32_t palette_id;
};
using tile8 = struct tile8;
snes_tile8 UnpackBppTile(const std::vector<uint8_t>& data,
const uint32_t offset, const uint32_t bpp);
tile8 UnpackBppTile(const std::vector<uint8_t>& data, const uint32_t offset,
const uint32_t bpp);
std::vector<uint8_t> PackBppTile(const tile8& tile, const uint32_t bpp);
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);
@@ -78,7 +71,6 @@ class TileInfo {
uint16_t TileInfoToWord(TileInfo tile_info);
TileInfo WordToTileInfo(uint16_t word);
uint16_t TileInfoToShort(TileInfo tile_info);
TileInfo GetTilesInfo(uint16_t tile);
/**
@@ -139,15 +131,15 @@ class Tile16 {
TileInfo tile1_;
TileInfo tile2_;
TileInfo tile3_;
std::vector<TileInfo> tiles_info;
std::array<TileInfo, 4> tiles_info;
Tile16() = default;
Tile16(TileInfo t0, TileInfo t1, TileInfo t2, TileInfo t3)
: tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {
tiles_info.push_back(tile0_);
tiles_info.push_back(tile1_);
tiles_info.push_back(tile2_);
tiles_info.push_back(tile3_);
tiles_info[0] = tile0_;
tiles_info[1] = tile1_;
tiles_info[2] = tile2_;
tiles_info[3] = tile3_;
}
bool operator==(const Tile16& other) const {
@@ -181,6 +173,52 @@ class OamTile {
}
};
class GraphicsBuffer {
public:
GraphicsBuffer() = default;
GraphicsBuffer(uint8_t bpp, const std::vector<uint8_t>& data)
: bpp_(bpp), data_(data) {}
GraphicsBuffer(uint8_t bpp, std::vector<uint8_t>&& data)
: bpp_(bpp), data_(std::move(data)) {}
uint8_t bpp() const { return bpp_; }
const std::vector<uint8_t>& data() const { return data_; }
void set_bpp(uint8_t bpp) { bpp_ = bpp; }
void set_data(const std::vector<uint8_t>& data) { data_ = data; }
void to_bpp(uint8_t bpp) {
if (bpp_ == bpp) {
return;
}
data_ = ConvertBpp(data_, bpp_, bpp);
bpp_ = bpp;
}
// Array-like access via operator[]
uint8_t& operator[](size_t index) {
if (index >= data_.size()) {
throw std::out_of_range("Index out of range");
}
return data_[index];
}
const uint8_t& operator[](size_t index) const {
if (index >= data_.size()) {
throw std::out_of_range("Index out of range");
}
return data_[index];
}
auto begin() { return data_.begin(); }
auto end() { return data_.end(); }
auto begin() const { return data_.begin(); }
auto end() const { return data_.end(); }
private:
uint8_t bpp_;
std::vector<uint8_t> data_;
};
} // namespace gfx
} // namespace app
} // namespace yaze

40
src/incl/snes_tile.h Normal file
View File

@@ -0,0 +1,40 @@
#ifndef YAZE_INCL_SNES_TILE_H
#define YAZE_INCL_SNES_TILE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
typedef struct snes_tile8 {
uint32_t id;
uint32_t palette_id;
uint8_t data[64];
} snes_tile8;
typedef struct snes_tile_info {
uint16_t id;
uint8_t palette;
bool priority;
bool vertical_mirror;
bool horizontal_mirror;
} snes_tile_info;
typedef struct snes_tile16 {
snes_tile_info tiles[4];
} snes_tile16;
typedef struct snes_tile32 {
uint16_t t0;
uint16_t t1;
uint16_t t2;
uint16_t t3;
} snes_tile32;
#ifdef __cplusplus
}
#endif
#endif