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.
This commit is contained in:
scawful
2025-05-15 23:01:03 -04:00
parent ab729fc198
commit ceab496226
3 changed files with 7 additions and 8 deletions

View File

@@ -6,7 +6,6 @@
#endif #endif
#include <cstdint> #include <cstdint>
#include <future>
#include "app/gfx/arena.h" #include "app/gfx/arena.h"
#include "app/gfx/snes_palette.h" #include "app/gfx/snes_palette.h"
@@ -230,12 +229,11 @@ void Bitmap::Create(int width, int height, int depth, int format,
width_ = width; width_ = width;
height_ = height; height_ = height;
depth_ = depth; depth_ = depth;
data_size_ = data.size(); if (data.size() == 0) {
if (data_size_ == 0) {
SDL_Log("Data provided to Bitmap is empty.\n"); SDL_Log("Data provided to Bitmap is empty.\n");
return; return;
} }
data_.reserve(data_size_); data_.reserve(data.size());
data_ = data; data_ = data;
pixel_data_ = data_.data(); pixel_data_ = data_.data();
surface_ = Arena::Get().AllocateSurface(width_, height_, depth_, surface_ = Arena::Get().AllocateSurface(width_, height_, depth_,
@@ -263,6 +261,7 @@ void Bitmap::UpdateTexture(SDL_Renderer *renderer) {
CreateTexture(renderer); CreateTexture(renderer);
return; return;
} }
memcpy(surface_->pixels, data_.data(), data_.size());
Arena::Get().UpdateTexture(texture_, surface_); Arena::Get().UpdateTexture(texture_, surface_);
} }
@@ -379,6 +378,7 @@ void Bitmap::WriteToPixel(int position, uint8_t value) {
pixel_data_ = data_.data(); pixel_data_ = data_.data();
} }
pixel_data_[position] = value; pixel_data_[position] = value;
data_[position] = value;
modified_ = true; modified_ = true;
} }

View File

@@ -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_; } int 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_; }
@@ -172,7 +172,6 @@ class Bitmap {
int width_ = 0; int width_ = 0;
int height_ = 0; int height_ = 0;
int depth_ = 0; int depth_ = 0;
int data_size_ = 0;
bool active_ = false; bool active_ = false;
bool modified_ = false; bool modified_ = false;

View File

@@ -40,7 +40,7 @@ class SnesColor {
constexpr SnesColor() constexpr SnesColor()
: rgb_({0.f, 0.f, 0.f, 0.f}), snes_(0), rom_color_({0, 0, 0}) {} : 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; snes_color color;
color.red = static_cast<uint8_t>(val.x) / kColorByteMax; color.red = static_cast<uint8_t>(val.x) / kColorByteMax;
color.green = static_cast<uint8_t>(val.y) / kColorByteMax; color.green = static_cast<uint8_t>(val.y) / kColorByteMax;
@@ -48,7 +48,7 @@ class SnesColor {
snes_ = ConvertRgbToSnes(color); 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); snes_color color = ConvertSnesToRgb(val);
rgb_ = ImVec4(color.red, color.green, color.blue, 0.f); rgb_ = ImVec4(color.red, color.green, color.blue, 0.f);
} }