From f1f2a7a25f5560dadd942b0ff7538a1959dd3e89 Mon Sep 17 00:00:00 2001 From: scawful Date: Tue, 13 Aug 2024 00:50:39 -0400 Subject: [PATCH] gfx housekeeping --- src/app/gfx/bitmap.cc | 12 ++++++------ src/app/gfx/bitmap.h | 2 +- src/app/gfx/snes_color.cc | 6 +++--- src/app/gfx/snes_color.h | 19 +++++++++++-------- src/app/gfx/snes_palette.h | 12 ++++-------- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/app/gfx/bitmap.cc b/src/app/gfx/bitmap.cc index 47044121..b67a2b79 100644 --- a/src/app/gfx/bitmap.cc +++ b/src/app/gfx/bitmap.cc @@ -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(); } diff --git a/src/app/gfx/bitmap.h b/src/app/gfx/bitmap.h index 601e9f7e..1a3ab29d 100644 --- a/src/app/gfx/bitmap.h +++ b/src/app/gfx/bitmap.h @@ -161,7 +161,7 @@ class Bitmap { height_ = 0; depth_ = 0; data_size_ = 0; - palette_.Clear(); + palette_.clear(); } auto sdl_palette() { diff --git a/src/app/gfx/snes_color.cc b/src/app/gfx/snes_color.cc index f2dfb239..ee55fbd1 100644 --- a/src/app/gfx/snes_color.cc +++ b/src/app/gfx/snes_color.cc @@ -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); } diff --git a/src/app/gfx/snes_color.h b/src/app/gfx/snes_color.h index 8ac5e764..7e448803 100644 --- a/src/app/gfx/snes_color.h +++ b/src/app/gfx/snes_color.h @@ -20,6 +20,9 @@ std::vector Extract(const char* data, unsigned int offset, std::vector Convert(const std::vector& 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; diff --git a/src/app/gfx/snes_palette.h b/src/app/gfx/snes_palette.h index e01cc865..e5d57a01 100644 --- a/src/app/gfx/snes_palette.h +++ b/src/app/gfx/snes_palette.h @@ -1,8 +1,6 @@ #ifndef YAZE_APP_GFX_PALETTE_H #define YAZE_APP_GFX_PALETTE_H -#include - #include #include #include @@ -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 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);