From d13407c8e260964d5ddeb8149f68acc1e17c5d31 Mon Sep 17 00:00:00 2001 From: scawful Date: Wed, 13 Nov 2024 08:52:12 -0500 Subject: [PATCH] Refactor tile structures and functions to use snes_tile8; introduce GraphicsBuffer class for better data management --- src/app/gfx/snes_tile.cc | 10 +++--- src/app/gfx/snes_tile.h | 72 ++++++++++++++++++++++++++++++---------- src/incl/snes_tile.h | 40 ++++++++++++++++++++++ 3 files changed, 100 insertions(+), 22 deletions(-) create mode 100644 src/incl/snes_tile.h diff --git a/src/app/gfx/snes_tile.cc b/src/app/gfx/snes_tile.cc index 33ef51bc..940fd6c5 100644 --- a/src/app/gfx/snes_tile.cc +++ b/src/app/gfx/snes_tile.cc @@ -23,9 +23,9 @@ constexpr ushort TileVFlipBit = 0x8000; // Bits used for tile name constexpr ushort TileNameMask = 0x03FF; -tile8 UnpackBppTile(const std::vector& data, const uint32_t offset, +snes_tile8 UnpackBppTile(const std::vector& 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& data, const uint32_t offset, return tile; } -std::vector PackBppTile(const tile8& tile, const uint32_t bpp) { +std::vector PackBppTile(const snes_tile8& tile, const uint32_t bpp) { // Allocate memory for output data std::vector output(bpp * 8, 0); // initialized with 0 unsigned maxcolor = 2 << bpp; @@ -133,7 +133,7 @@ std::vector ConvertBpp(const std::vector& tiles, std::vector 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 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& bitmap, } // namespace gfx } // namespace app -} // namespace yaze \ No newline at end of file +} // namespace yaze diff --git a/src/app/gfx/snes_tile.h b/src/app/gfx/snes_tile.h index fca3bf32..c2fa2977 100644 --- a/src/app/gfx/snes_tile.h +++ b/src/app/gfx/snes_tile.h @@ -5,7 +5,7 @@ #include #include -#include "app/core/constants.h" +#include "incl/snes_tile.h" namespace yaze { namespace app { @@ -23,17 +23,10 @@ std::vector SnesTo8bppSheet(const std::vector& sheet, int bpp, std::vector Bpp8SnesToIndexed(std::vector 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& data, + const uint32_t offset, const uint32_t bpp); -tile8 UnpackBppTile(const std::vector& data, const uint32_t offset, - const uint32_t bpp); - -std::vector PackBppTile(const tile8& tile, const uint32_t bpp); +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); @@ -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 tiles_info; + std::array 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& data) + : bpp_(bpp), data_(data) {} + GraphicsBuffer(uint8_t bpp, std::vector&& data) + : bpp_(bpp), data_(std::move(data)) {} + + uint8_t bpp() const { return bpp_; } + const std::vector& data() const { return data_; } + + void set_bpp(uint8_t bpp) { bpp_ = bpp; } + void set_data(const std::vector& 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 data_; +}; + } // namespace gfx } // namespace app } // namespace yaze diff --git a/src/incl/snes_tile.h b/src/incl/snes_tile.h new file mode 100644 index 00000000..922bbf4d --- /dev/null +++ b/src/incl/snes_tile.h @@ -0,0 +1,40 @@ +#ifndef YAZE_INCL_SNES_TILE_H +#define YAZE_INCL_SNES_TILE_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +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