From ceab4962261552ec69395b18288c56bb847025de Mon Sep 17 00:00:00 2001 From: scawful Date: Thu, 15 May 2025 23:01:03 -0400 Subject: [PATCH] Refactor Bitmap class for improved data handling - Removed unused data_size_ member and updated size() method to return the size of data_ directly. - Simplified data size checks in Create method to enhance clarity. - Updated UpdateTexture method to use memcpy for pixel data assignment, improving performance. - Ensured WriteToPixel method updates both pixel_data_ and data_ for consistency in modifications. --- src/app/gfx/bitmap.cc | 8 ++++---- src/app/gfx/bitmap.h | 3 +-- src/app/gfx/snes_color.h | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/app/gfx/bitmap.cc b/src/app/gfx/bitmap.cc index a4ad7342..8e29f8af 100644 --- a/src/app/gfx/bitmap.cc +++ b/src/app/gfx/bitmap.cc @@ -6,7 +6,6 @@ #endif #include -#include #include "app/gfx/arena.h" #include "app/gfx/snes_palette.h" @@ -230,12 +229,11 @@ void Bitmap::Create(int width, int height, int depth, int format, width_ = width; height_ = height; depth_ = depth; - data_size_ = data.size(); - if (data_size_ == 0) { + if (data.size() == 0) { SDL_Log("Data provided to Bitmap is empty.\n"); return; } - data_.reserve(data_size_); + data_.reserve(data.size()); data_ = data; pixel_data_ = data_.data(); surface_ = Arena::Get().AllocateSurface(width_, height_, depth_, @@ -263,6 +261,7 @@ void Bitmap::UpdateTexture(SDL_Renderer *renderer) { CreateTexture(renderer); return; } + memcpy(surface_->pixels, data_.data(), data_.size()); Arena::Get().UpdateTexture(texture_, surface_); } @@ -379,6 +378,7 @@ void Bitmap::WriteToPixel(int position, uint8_t value) { pixel_data_ = data_.data(); } pixel_data_[position] = value; + data_[position] = value; modified_ = true; } diff --git a/src/app/gfx/bitmap.h b/src/app/gfx/bitmap.h index e4f59330..e38ea450 100644 --- a/src/app/gfx/bitmap.h +++ b/src/app/gfx/bitmap.h @@ -151,7 +151,7 @@ class Bitmap { int width() const { return width_; } int height() const { return height_; } int depth() const { return depth_; } - int size() const { return data_size_; } + int size() const { return data_.size(); } const uint8_t *data() const { return data_.data(); } std::vector &mutable_data() { return data_; } SDL_Surface *surface() const { return surface_; } @@ -172,7 +172,6 @@ class Bitmap { int width_ = 0; int height_ = 0; int depth_ = 0; - int data_size_ = 0; bool active_ = false; bool modified_ = false; diff --git a/src/app/gfx/snes_color.h b/src/app/gfx/snes_color.h index 3adc862c..6be8860a 100644 --- a/src/app/gfx/snes_color.h +++ b/src/app/gfx/snes_color.h @@ -40,7 +40,7 @@ class SnesColor { constexpr SnesColor() : rgb_({0.f, 0.f, 0.f, 0.f}), snes_(0), rom_color_({0, 0, 0}) {} - constexpr explicit SnesColor(const ImVec4 val) : rgb_(val) { + explicit SnesColor(const ImVec4 val) : rgb_(val) { snes_color color; color.red = static_cast(val.x) / kColorByteMax; color.green = static_cast(val.y) / kColorByteMax; @@ -48,7 +48,7 @@ class SnesColor { snes_ = ConvertRgbToSnes(color); } - constexpr explicit SnesColor(const uint16_t val) : snes_(val) { + explicit SnesColor(const uint16_t val) : snes_(val) { snes_color color = ConvertSnesToRgb(val); rgb_ = ImVec4(color.red, color.green, color.blue, 0.f); }