Enhance Bitmap class with error handling and code improvements
- Introduced BitmapError class for more specific error handling in the Bitmap class. - Updated data checks to use `empty()` for clarity and consistency. - Improved pixel data handling by ensuring proper type usage and modifying loop constructs for better performance. - Refactored constructor and method implementations for improved readability and maintainability.
This commit is contained in:
@@ -6,6 +6,8 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <span>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "app/gfx/arena.h"
|
#include "app/gfx/arena.h"
|
||||||
#include "app/gfx/snes_palette.h"
|
#include "app/gfx/snes_palette.h"
|
||||||
@@ -178,7 +180,11 @@ void ConvertPngToSurface(const std::vector<uint8_t> &png_data,
|
|||||||
|
|
||||||
#endif // YAZE_LIB_PNG
|
#endif // YAZE_LIB_PNG
|
||||||
|
|
||||||
// Utility functions
|
class BitmapError : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
using std::runtime_error::runtime_error;
|
||||||
|
};
|
||||||
|
|
||||||
Uint32 GetSnesPixelFormat(int format) {
|
Uint32 GetSnesPixelFormat(int format) {
|
||||||
switch (format) {
|
switch (format) {
|
||||||
case 0:
|
case 0:
|
||||||
@@ -187,8 +193,9 @@ Uint32 GetSnesPixelFormat(int format) {
|
|||||||
return SNES_PIXELFORMAT_4BPP;
|
return SNES_PIXELFORMAT_4BPP;
|
||||||
case 2:
|
case 2:
|
||||||
return SNES_PIXELFORMAT_8BPP;
|
return SNES_PIXELFORMAT_8BPP;
|
||||||
|
default:
|
||||||
|
return SDL_PIXELFORMAT_INDEX8;
|
||||||
}
|
}
|
||||||
return SDL_PIXELFORMAT_INDEX8;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Bitmap::Bitmap(int width, int height, int depth,
|
Bitmap::Bitmap(int width, int height, int depth,
|
||||||
@@ -202,8 +209,8 @@ Bitmap::Bitmap(int width, int height, int depth,
|
|||||||
: width_(width),
|
: width_(width),
|
||||||
height_(height),
|
height_(height),
|
||||||
depth_(depth),
|
depth_(depth),
|
||||||
data_(data),
|
palette_(palette),
|
||||||
palette_(palette) {
|
data_(data) {
|
||||||
Create(width, height, depth, data);
|
Create(width, height, depth, data);
|
||||||
SetPalette(palette);
|
SetPalette(palette);
|
||||||
}
|
}
|
||||||
@@ -229,7 +236,7 @@ void Bitmap::Create(int width, int height, int depth, int format,
|
|||||||
width_ = width;
|
width_ = width;
|
||||||
height_ = height;
|
height_ = height;
|
||||||
depth_ = depth;
|
depth_ = depth;
|
||||||
if (data.size() == 0) {
|
if (data.empty()) {
|
||||||
SDL_Log("Data provided to Bitmap is empty.\n");
|
SDL_Log("Data provided to Bitmap is empty.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -299,17 +306,17 @@ void Bitmap::UpdateTextureData() {
|
|||||||
|
|
||||||
void Bitmap::SetPalette(const SnesPalette &palette) {
|
void Bitmap::SetPalette(const SnesPalette &palette) {
|
||||||
if (surface_ == nullptr) {
|
if (surface_ == nullptr) {
|
||||||
throw std::runtime_error("Surface is null. Palette not applied");
|
throw BitmapError("Surface is null. Palette not applied");
|
||||||
}
|
}
|
||||||
if (surface_->format == nullptr || surface_->format->palette == nullptr) {
|
if (surface_->format == nullptr || surface_->format->palette == nullptr) {
|
||||||
throw std::runtime_error(
|
throw BitmapError(
|
||||||
"Surface format or palette is null. Palette not applied.");
|
"Surface format or palette is null. Palette not applied.");
|
||||||
}
|
}
|
||||||
palette_ = palette;
|
palette_ = palette;
|
||||||
|
|
||||||
SDL_Palette *sdl_palette = surface_->format->palette;
|
SDL_Palette *sdl_palette = surface_->format->palette;
|
||||||
if (sdl_palette == nullptr) {
|
if (sdl_palette == nullptr) {
|
||||||
throw std::runtime_error("Failed to get SDL palette");
|
throw BitmapError("Failed to get SDL palette");
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_UnlockSurface(surface_);
|
SDL_UnlockSurface(surface_);
|
||||||
@@ -325,7 +332,7 @@ void Bitmap::SetPalette(const SnesPalette &palette) {
|
|||||||
|
|
||||||
void Bitmap::SetPaletteWithTransparent(const SnesPalette &palette, size_t index,
|
void Bitmap::SetPaletteWithTransparent(const SnesPalette &palette, size_t index,
|
||||||
int length) {
|
int length) {
|
||||||
if (index < 0 || index >= palette.size()) {
|
if (index >= palette.size()) {
|
||||||
throw std::invalid_argument("Invalid palette index");
|
throw std::invalid_argument("Invalid palette index");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,21 +345,21 @@ void Bitmap::SetPaletteWithTransparent(const SnesPalette &palette, size_t index,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (surface_ == nullptr) {
|
if (surface_ == nullptr) {
|
||||||
throw std::runtime_error("Surface is null. Palette not applied");
|
throw BitmapError("Surface is null. Palette not applied");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto start_index = index * 7;
|
auto start_index = index * 7;
|
||||||
palette_ = palette.sub_palette(start_index, start_index + 7);
|
palette_ = palette.sub_palette(start_index, start_index + 7);
|
||||||
std::vector<ImVec4> colors;
|
std::vector<ImVec4> colors;
|
||||||
colors.push_back(ImVec4(0, 0, 0, 0));
|
colors.push_back(ImVec4(0, 0, 0, 0));
|
||||||
for (int i = start_index; i < start_index + 7; ++i) {
|
for (size_t i = start_index; i < start_index + 7; ++i) {
|
||||||
auto pal_color = palette[i];
|
auto &pal_color = palette[i];
|
||||||
colors.push_back(pal_color.rgb());
|
colors.push_back(pal_color.rgb());
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_UnlockSurface(surface_);
|
SDL_UnlockSurface(surface_);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (auto &each : colors) {
|
for (const auto &each : colors) {
|
||||||
surface_->format->palette->colors[i].r = each.x;
|
surface_->format->palette->colors[i].r = each.x;
|
||||||
surface_->format->palette->colors[i].g = each.y;
|
surface_->format->palette->colors[i].g = each.y;
|
||||||
surface_->format->palette->colors[i].b = each.z;
|
surface_->format->palette->colors[i].b = each.z;
|
||||||
@@ -410,7 +417,7 @@ void Bitmap::Get8x8Tile(int tile_index, int x, int y,
|
|||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
for (int j = 0; j < 8; j++) {
|
for (int j = 0; j < 8; j++) {
|
||||||
int pixel_offset = tile_offset + (tile_y + i) * width_ + tile_x + j;
|
int pixel_offset = tile_offset + (tile_y + i) * width_ + tile_x + j;
|
||||||
int pixel_value = data_[pixel_offset];
|
uint8_t pixel_value = data_[pixel_offset];
|
||||||
tile_data[tile_data_offset] = pixel_value;
|
tile_data[tile_data_offset] = pixel_value;
|
||||||
tile_data_offset++;
|
tile_data_offset++;
|
||||||
}
|
}
|
||||||
@@ -426,10 +433,11 @@ void Bitmap::Get16x16Tile(int tile_x, int tile_y,
|
|||||||
int pixel_x = tile_x + tx;
|
int pixel_x = tile_x + tx;
|
||||||
int pixel_y = tile_y + ty;
|
int pixel_y = tile_y + ty;
|
||||||
int pixel_offset = (pixel_y * width_) + pixel_x;
|
int pixel_offset = (pixel_y * width_) + pixel_x;
|
||||||
int pixel_value = data_[pixel_offset];
|
uint8_t pixel_value = data_[pixel_offset];
|
||||||
|
|
||||||
// Store the pixel value in the tile data
|
// Store the pixel value in the tile data
|
||||||
tile_data[tile_data_offset++] = pixel_value;
|
tile_data_offset++;
|
||||||
|
tile_data[tile_data_offset] = pixel_value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ class Bitmap {
|
|||||||
int width() const { return width_; }
|
int width() const { return width_; }
|
||||||
int height() const { return height_; }
|
int height() const { return height_; }
|
||||||
int depth() const { return depth_; }
|
int depth() const { return depth_; }
|
||||||
int size() const { return data_.size(); }
|
auto size() const { return data_.size(); }
|
||||||
const uint8_t *data() const { return data_.data(); }
|
const uint8_t *data() const { return data_.data(); }
|
||||||
std::vector<uint8_t> &mutable_data() { return data_; }
|
std::vector<uint8_t> &mutable_data() { return data_; }
|
||||||
SDL_Surface *surface() const { return surface_; }
|
SDL_Surface *surface() const { return surface_; }
|
||||||
|
|||||||
Reference in New Issue
Block a user