From 6465486443fc76d81b913d1a02dcebfb1a66955b Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 25 Aug 2024 14:22:52 -0400 Subject: [PATCH] Refactor Bitmap class to use shared_ptr for SDL_Surface objects --- src/app/gfx/bitmap.cc | 25 ++++++++++++------------- src/app/gfx/bitmap.h | 3 +-- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/app/gfx/bitmap.cc b/src/app/gfx/bitmap.cc index 58b24fb4..1b97a62b 100644 --- a/src/app/gfx/bitmap.cc +++ b/src/app/gfx/bitmap.cc @@ -229,7 +229,8 @@ Bitmap::Bitmap(int width, int height, int depth, int data_size) { Create(width, height, depth, std::vector(data_size, 0)); } -void Bitmap::Create(int width, int height, int depth, const std::vector &data) { +void Bitmap::Create(int width, int height, int depth, + const std::vector &data) { Create(width, height, depth, kIndexed, data); } @@ -251,10 +252,10 @@ void Bitmap::Create(int width, int height, int depth, int format, return; } pixel_data_ = data_.data(); - surface_ = std::unique_ptr( + surface_ = std::shared_ptr{ SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_, GetSnesPixelFormat(format)), - SDL_Surface_Deleter()); + SDL_Surface_Deleter{}}; if (surface_ == nullptr) { SDL_Log("SDL_CreateRGBSurfaceWithFormat failed: %s\n", SDL_GetError()); active_ = false; @@ -293,12 +294,10 @@ void Bitmap::CreateTexture(SDL_Renderer *renderer) { SDL_Log("SDL_CreateTextureFromSurface failed: %s\n", SDL_GetError()); } - SDL_Surface *converted_surface = - SDL_ConvertSurfaceFormat(surface_.get(), SDL_PIXELFORMAT_ARGB8888, 0); - if (converted_surface) { - converted_surface_ = std::unique_ptr( - converted_surface, SDL_Surface_Deleter()); - } else { + converted_surface_ = std::shared_ptr{ + SDL_ConvertSurfaceFormat(surface_.get(), SDL_PIXELFORMAT_ARGB8888, 0), + SDL_Surface_Deleter{}}; + if (converted_surface_ == nullptr) { SDL_Log("SDL_ConvertSurfaceFormat failed: %s\n", SDL_GetError()); return; } @@ -361,7 +360,7 @@ absl::Status Bitmap::ApplyPalette(const SnesPalette &palette) { } SDL_UnlockSurface(surface_.get()); - for (int i = 0; i < palette.size(); ++i) { + for (size_t i = 0; i < palette.size(); ++i) { ASSIGN_OR_RETURN(gfx::SnesColor pal_color, palette.GetColor(i)); sdl_palette->colors[i].r = pal_color.rgb().x; sdl_palette->colors[i].g = pal_color.rgb().y; @@ -378,7 +377,7 @@ absl::Status Bitmap::ApplyPaletteFromPaletteGroup(const SnesPalette &palette, auto start_index = palette_id * 8; palette_ = palette.sub_palette(start_index, start_index + 8); SDL_UnlockSurface(surface_.get()); - for (int i = 0; i < palette_.size(); ++i) { + for (size_t i = 0; i < palette_.size(); ++i) { ASSIGN_OR_RETURN(auto pal_color, palette_.GetColor(i)); if (pal_color.is_transparent()) { surface_->format->palette->colors[i].r = 0; @@ -398,7 +397,7 @@ absl::Status Bitmap::ApplyPaletteFromPaletteGroup(const SnesPalette &palette, } absl::Status Bitmap::ApplyPaletteWithTransparent(const SnesPalette &palette, - int index, int length) { + size_t index, int length) { if (index < 0 || index >= palette.size()) { return absl::InvalidArgumentError("Invalid palette index"); } @@ -441,7 +440,7 @@ absl::Status Bitmap::ApplyPaletteWithTransparent(const SnesPalette &palette, void Bitmap::ApplyPalette(const std::vector &palette) { SDL_UnlockSurface(surface_.get()); - for (int i = 0; i < palette.size(); ++i) { + for (size_t i = 0; i < palette.size(); ++i) { surface_->format->palette->colors[i].r = palette[i].r; surface_->format->palette->colors[i].g = palette[i].g; surface_->format->palette->colors[i].b = palette[i].b; diff --git a/src/app/gfx/bitmap.h b/src/app/gfx/bitmap.h index ce853cac..e985e4a8 100644 --- a/src/app/gfx/bitmap.h +++ b/src/app/gfx/bitmap.h @@ -125,7 +125,7 @@ class Bitmap { */ absl::Status ApplyPalette(const SnesPalette &palette); absl::Status ApplyPaletteWithTransparent(const SnesPalette &palette, - int index, int length = 7); + size_t index, int length = 7); void ApplyPalette(const std::vector &palette); absl::Status ApplyPaletteFromPaletteGroup(const SnesPalette &palette, int palette_id); @@ -203,7 +203,6 @@ class Bitmap { int depth_ = 0; int data_size_ = 0; - bool freed_ = false; bool active_ = false; bool modified_ = false; void *texture_pixels = nullptr;