gfx housekeeping

This commit is contained in:
scawful
2024-08-13 00:50:39 -04:00
parent 9f9edc9666
commit f1f2a7a25f
5 changed files with 25 additions and 26 deletions

View File

@@ -256,10 +256,12 @@ void Bitmap::Reformat(int format) {
GetSnesPixelFormat(format)),
SDL_Surface_Deleter());
surface_->pixels = pixel_data_;
if (!ApplyPalette(palette_).ok()) {
// Some sort of error occurred, throw an exception?
}
active_ = true;
auto apply_palette = ApplyPalette(palette_);
if (!apply_palette.ok()) {
SDL_Log("Failed to apply palette: %s\n", apply_palette.message().data());
active_ = false;
}
}
void Bitmap::CreateTexture(SDL_Renderer *renderer) {
@@ -402,9 +404,7 @@ absl::Status Bitmap::ApplyPaletteWithTransparent(const SnesPalette &palette,
i++;
}
SDL_LockSurface(surface_.get());
if (SDL_GetError() != nullptr) { // Check for SDL errors
return absl::InternalError(absl::StrCat("SDL Error: ", SDL_GetError()));
}
SDL_RETURN_IF_ERROR()
return absl::OkStatus();
}

View File

@@ -161,7 +161,7 @@ class Bitmap {
height_ = 0;
depth_ = 0;
data_size_ = 0;
palette_.Clear();
palette_.clear();
}
auto sdl_palette() {

View File

@@ -40,9 +40,9 @@ uint16_t ConvertRGBtoSNES(const snes_color& color) {
uint16_t ConvertRGBtoSNES(const ImVec4& color) {
snes_color new_color;
new_color.red = color.x * 255;
new_color.green = color.y * 255;
new_color.blue = color.z * 255;
new_color.red = color.x * kColorByteMax;
new_color.green = color.y * kColorByteMax;
new_color.blue = color.z * kColorByteMax;
return ConvertRGBtoSNES(new_color);
}

View File

@@ -20,6 +20,9 @@ std::vector<snes_color> Extract(const char* data, unsigned int offset,
std::vector<char> Convert(const std::vector<snes_color>& palette);
constexpr uint8_t kColorByteMax = 255;
constexpr float kColorByteMaxF = 255.f;
/**
* @brief SNES Color container
*
@@ -35,9 +38,9 @@ class SnesColor {
SnesColor() : rgb_(0.f, 0.f, 0.f, 0.f), snes_(0) {}
explicit SnesColor(const ImVec4 val) : rgb_(val) {
snes_color color;
color.red = val.x / 255;
color.green = val.y / 255;
color.blue = val.z / 255;
color.red = val.x / kColorByteMax;
color.green = val.y / kColorByteMax;
color.blue = val.z / kColorByteMax;
snes_ = ConvertRGBtoSNES(color);
}
explicit SnesColor(const uint16_t val) : snes_(val) {
@@ -45,12 +48,12 @@ class SnesColor {
rgb_ = ImVec4(color.red, color.green, color.blue, 0.f);
}
explicit SnesColor(const snes_color val)
: rgb_(val.red, val.green, val.blue, 255.f),
: rgb_(val.red, val.green, val.blue, kColorByteMaxF),
snes_(ConvertRGBtoSNES(val)),
rom_color_(val) {}
SnesColor(uint8_t r, uint8_t g, uint8_t b) {
rgb_ = ImVec4(r, g, b, 255.f);
rgb_ = ImVec4(r, g, b, kColorByteMaxF);
snes_color color;
color.red = r;
color.green = g;
@@ -62,9 +65,9 @@ class SnesColor {
ImVec4 rgb() const { return rgb_; }
void set_rgb(const ImVec4 val) {
rgb_.x = val.x / 255;
rgb_.y = val.y / 255;
rgb_.z = val.z / 255;
rgb_.x = val.x / kColorByteMax;
rgb_.y = val.y / kColorByteMax;
rgb_.z = val.z / kColorByteMax;
snes_color color;
color.red = val.x;
color.green = val.y;

View File

@@ -1,8 +1,6 @@
#ifndef YAZE_APP_GFX_PALETTE_H
#define YAZE_APP_GFX_PALETTE_H
#include <SDL.h>
#include <cstdint>
#include <cstdlib>
#include <cstring>
@@ -127,9 +125,7 @@ class SnesPalette {
}
void AddColor(const SnesColor& color) { colors.emplace_back(color); }
void AddColor(const snes_color& color) { colors.emplace_back(color); }
void AddColor(uint16_t color) { colors.emplace_back(color); }
absl::StatusOr<SnesColor> GetColor(int i) const {
@@ -141,8 +137,7 @@ class SnesPalette {
auto mutable_color(int i) { return &colors[i]; }
void Clear() { colors.clear(); }
void clear() { colors.clear(); }
auto size() const { return colors.size(); }
auto empty() const { return colors.empty(); }
@@ -156,14 +151,15 @@ class SnesPalette {
void operator()(int i, const SnesColor& color) {
if (i >= colors.size()) {
throw std::out_of_range("SNESPalette: Index out of bounds");
std::cout << SNESPalette: Index out of bounds << std::endl;
}
colors[i] = color;
}
void operator()(int i, const ImVec4& color) {
if (i >= colors.size()) {
throw std::out_of_range("SNESPalette: Index out of bounds");
std::cout << SNESPalette: Index out of bounds << std::endl;
return;
}
colors[i].set_rgb(color);
colors[i].set_modified(true);