rename ApplyPalette with SetPalette

This commit is contained in:
scawful
2025-03-02 17:06:46 -05:00
parent a7d933eb14
commit c99a4b0bc4
11 changed files with 106 additions and 112 deletions

View File

@@ -271,7 +271,7 @@ void Bitmap::Reformat(int format) {
SDL_Surface_Deleter());
surface_->pixels = pixel_data_;
active_ = true;
auto apply_palette = ApplyPalette(palette_);
auto apply_palette = SetPalette(palette_);
if (!apply_palette.ok()) {
SDL_Log("Failed to apply palette: %s\n", apply_palette.message().data());
active_ = false;
@@ -326,7 +326,7 @@ void Bitmap::UpdateTexture(SDL_Renderer *renderer) {
SDL_UnlockTexture(texture_.get());
}
absl::Status Bitmap::ApplyPalette(const SnesPalette &palette) {
absl::Status Bitmap::SetPalette(const SnesPalette &palette) {
if (surface_ == nullptr) {
return absl::FailedPreconditionError(
"Surface is null. Palette not applied");
@@ -355,8 +355,8 @@ absl::Status Bitmap::ApplyPalette(const SnesPalette &palette) {
return absl::OkStatus();
}
absl::Status Bitmap::ApplyPaletteFromPaletteGroup(const SnesPalette &palette,
int palette_id) {
absl::Status Bitmap::SetPaletteFromPaletteGroup(const SnesPalette &palette,
int palette_id) {
auto start_index = palette_id * 8;
palette_ = palette.sub_palette(start_index, start_index + 8);
SDL_UnlockSurface(surface_.get());
@@ -379,8 +379,8 @@ absl::Status Bitmap::ApplyPaletteFromPaletteGroup(const SnesPalette &palette,
return absl::OkStatus();
}
absl::Status Bitmap::ApplyPaletteWithTransparent(const SnesPalette &palette,
size_t index, int length) {
absl::Status Bitmap::SetPaletteWithTransparent(const SnesPalette &palette,
size_t index, int length) {
if (index < 0 || index >= palette.size()) {
return absl::InvalidArgumentError("Invalid palette index");
}
@@ -421,7 +421,7 @@ absl::Status Bitmap::ApplyPaletteWithTransparent(const SnesPalette &palette,
return absl::OkStatus();
}
void Bitmap::ApplyPalette(const std::vector<SDL_Color> &palette) {
void Bitmap::SetPalette(const std::vector<SDL_Color> &palette) {
SDL_UnlockSurface(surface_.get());
for (size_t i = 0; i < palette.size(); ++i) {
surface_->format->palette->colors[i].r = palette[i].r;

View File

@@ -3,9 +3,9 @@
#include <SDL.h>
#include <span>
#include <cstdint>
#include <memory>
#include <span>
#include "absl/status/status.h"
#include "app/core/platform/sdl_deleter.h"
@@ -81,7 +81,7 @@ class Bitmap {
data_(data),
palette_(palette) {
Create(width, height, depth, data);
if (!ApplyPalette(palette).ok()) {
if (!SetPalette(palette).ok()) {
std::cerr << "Error applying palette in bitmap constructor." << std::endl;
}
}
@@ -121,12 +121,12 @@ class Bitmap {
/**
* @brief Copy color data from the SnesPalette into the SDL_Palette
*/
absl::Status ApplyPalette(const SnesPalette &palette);
absl::Status ApplyPaletteWithTransparent(const SnesPalette &palette,
size_t index, int length = 7);
void ApplyPalette(const std::vector<SDL_Color> &palette);
absl::Status ApplyPaletteFromPaletteGroup(const SnesPalette &palette,
int palette_id);
absl::Status SetPalette(const SnesPalette &palette);
absl::Status SetPaletteWithTransparent(const SnesPalette &palette,
size_t index, int length = 7);
void SetPalette(const std::vector<SDL_Color> &palette);
absl::Status SetPaletteFromPaletteGroup(const SnesPalette &palette,
int palette_id);
void Get8x8Tile(int tile_index, int x, int y, std::vector<uint8_t> &tile_data,
int &tile_data_offset);

View File

@@ -41,38 +41,30 @@ snes_tile8 UnpackBppTile(const std::vector<uint8_t>& data,
bpp_pos[1] = offset + col * 2 + 1;
char mask = 1 << (7 - row);
tile.data[col * 8 + row] = (data[bpp_pos[0]] & mask) == mask;
tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[1]] & mask) == mask)
<< 1;
tile.data[col * 8 + row] |= ((data[bpp_pos[1]] & mask) == mask) << 1;
if (bpp == 3) {
// When we have 3 bitplanes, the bytes for the third bitplane are after
// the 16 bytes of the 2 bitplanes.
bpp_pos[2] = offset + 16 + col;
tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[2]] & mask) == mask)
<< 2;
tile.data[col * 8 + row] |= ((data[bpp_pos[2]] & mask) == mask) << 2;
}
if (bpp >= 4) {
// For 4 bitplanes, the 2 added bitplanes are interlaced like the first
// two.
bpp_pos[2] = offset + 16 + col * 2;
bpp_pos[3] = offset + 16 + col * 2 + 1;
tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[2]] & mask) == mask)
<< 2;
tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[3]] & mask) == mask)
<< 3;
tile.data[col * 8 + row] |= ((data[bpp_pos[2]] & mask) == mask) << 2;
tile.data[col * 8 + row] |= ((data[bpp_pos[3]] & mask) == mask) << 3;
}
if (bpp == 8) {
bpp_pos[4] = offset + 32 + col * 2;
bpp_pos[5] = offset + 32 + col * 2 + 1;
bpp_pos[6] = offset + 48 + col * 2;
bpp_pos[7] = offset + 48 + col * 2 + 1;
tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[4]] & mask) == mask)
<< 4;
tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[5]] & mask) == mask)
<< 5;
tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[6]] & mask) == mask)
<< 6;
tile.data[col * 8 + row] |= (uint8_t)((data[bpp_pos[7]] & mask) == mask)
<< 7;
tile.data[col * 8 + row] |= ((data[bpp_pos[4]] & mask) == mask) << 4;
tile.data[col * 8 + row] |= ((data[bpp_pos[5]] & mask) == mask) << 5;
tile.data[col * 8 + row] |= ((data[bpp_pos[6]] & mask) == mask) << 6;
tile.data[col * 8 + row] |= ((data[bpp_pos[7]] & mask) == mask) << 7;
}
}
}
@@ -97,29 +89,25 @@ std::vector<uint8_t> PackBppTile(const snes_tile8& tile, const uint32_t bpp) {
// 2bpp format
if (bpp >= 2) {
output[col * 2] += (uint8_t)((color & 1) << (7 - row));
output[col * 2 + 1] +=
(uint8_t)((uint8_t)((color & 2) == 2) << (7 - row));
output[col * 2] += ((color & 1) << (7 - row));
output[col * 2 + 1] += (((color & 2) == 2) << (7 - row));
}
// 3bpp format
if (bpp == 3)
output[16 + col] += (uint8_t)(((color & 4) == 4) << (7 - row));
if (bpp == 3) output[16 + col] += (((color & 4) == 4) << (7 - row));
// 4bpp format
if (bpp >= 4) {
output[16 + col * 2] += (uint8_t)(((color & 4) == 4) << (7 - row));
output[16 + col * 2 + 1] += (uint8_t)(((color & 8) == 8) << (7 - row));
output[16 + col * 2] += (((color & 4) == 4) << (7 - row));
output[16 + col * 2 + 1] += (((color & 8) == 8) << (7 - row));
}
// 8bpp format
if (bpp == 8) {
output[32 + col * 2] += (uint8_t)(((color & 16) == 16) << (7 - row));
output[32 + col * 2 + 1] +=
(uint8_t)(((color & 32) == 32) << (7 - row));
output[48 + col * 2] += (uint8_t)(((color & 64) == 64) << (7 - row));
output[48 + col * 2 + 1] +=
(uint8_t)(((color & 128) == 128) << (7 - row));
output[32 + col * 2] += (((color & 16) == 16) << (7 - row));
output[32 + col * 2 + 1] += (((color & 32) == 32) << (7 - row));
output[48 + col * 2] += (((color & 64) == 64) << (7 - row));
output[48 + col * 2 + 1] += (((color & 128) == 128) << (7 - row));
}
}
}
@@ -399,5 +387,4 @@ void CopyTile8bpp16(int x, int y, int tile, std::vector<uint8_t>& bitmap,
}
} // namespace gfx
} // namespace yaze