From cf13d6bf9ebe2870c328a05cf4692bb799a8c3fe Mon Sep 17 00:00:00 2001 From: Justin Scofield <47263509+scawful@users.noreply.github.com> Date: Sun, 5 Jan 2025 20:55:36 -0500 Subject: [PATCH] Update Bitmap and add Initialize function --- src/app/gfx/bitmap.cc | 17 +++++++++++++---- src/app/gfx/bitmap.h | 12 ++++++------ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/app/gfx/bitmap.cc b/src/app/gfx/bitmap.cc index ddf04741..576d7bae 100644 --- a/src/app/gfx/bitmap.cc +++ b/src/app/gfx/bitmap.cc @@ -218,6 +218,13 @@ Bitmap::Bitmap(int width, int height, int depth, int data_size) { Create(width, height, depth, std::vector(data_size, 0)); } +void Bitmap::Initialize(int width, int height, int depth, std::span& data) { + width_ = width; + height_ = height; + depth_ = depth; + data_ = std::vector(data.begin(), data.end()); +} + void Bitmap::Create(int width, int height, int depth, std::span data) { data_ = std::vector(data.begin(), data.end()); Create(width, height, depth, data_); @@ -239,19 +246,20 @@ void Bitmap::Create(int width, int height, int depth, int format, width_ = width; height_ = height; depth_ = depth; - data_ = data; data_size_ = data.size(); if (data_size_ == 0) { SDL_Log("Data provided to Bitmap is empty.\n"); return; } + data_.reserve(data_size_); + data_ = data; pixel_data_ = data_.data(); surface_ = std::shared_ptr{ SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_, GetSnesPixelFormat(format)), SDL_Surface_Deleter{}}; if (surface_ == nullptr) { - SDL_Log("SDL_CreateRGBSurfaceWithFormat failed: %s\n", SDL_GetError()); + SDL_Log("Bitmap::Create.SDL_CreateRGBSurfaceWithFormat failed: %s\n", SDL_GetError()); active_ = false; return; } @@ -285,14 +293,15 @@ void Bitmap::CreateTexture(SDL_Renderer *renderer) { SDL_TEXTUREACCESS_STREAMING, width_, height_), SDL_Texture_Deleter{}}; if (texture_ == nullptr) { - SDL_Log("SDL_CreateTextureFromSurface failed: %s\n", SDL_GetError()); + SDL_Log("Bitmap::CreateTexture.SDL_CreateTextureFromSurface failed: %s\n", SDL_GetError()); } + texture_pixels = data_.data(); auto 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()); + SDL_Log("Bitmap::CreateTexture.SDL_ConvertSurfaceFormat failed: %s\n", SDL_GetError()); return; } diff --git a/src/app/gfx/bitmap.h b/src/app/gfx/bitmap.h index 002aab63..6b004ea8 100644 --- a/src/app/gfx/bitmap.h +++ b/src/app/gfx/bitmap.h @@ -46,7 +46,7 @@ enum BitmapFormat { /** * @brief Convert SDL_Surface to PNG image data. */ -bool ConvertSurfaceToPNG(SDL_Surface *surface, std::vector &buffer); +bool ConvertSurfaceToPng(SDL_Surface *surface, std::vector &buffer); /** * @brief Convert PNG image data to SDL_Surface. @@ -91,9 +91,11 @@ class Bitmap { void SaveSurfaceToFile(std::string_view filename); - /** - * @brief Creates a bitmap object with the provided graphical data. - */ + void Initialize(int width, int height, int depth, std::span& data); + + void Create(int width, int height, int depth, int data_size) { + Create(width, height, depth, std::vector(data_size, 0)); + } void Create(int width, int height, int depth, std::span data); void Create(int width, int height, int depth, const std::vector &data); @@ -152,7 +154,6 @@ class Bitmap { auto palette() const { return palette_; } auto mutable_palette() { return &palette_; } - auto palette_size() const { return palette_.size(); } int width() const { return width_; } int height() const { return height_; } @@ -161,7 +162,6 @@ class Bitmap { auto data() const { return data_.data(); } auto &mutable_data() { return data_; } auto surface() const { return surface_.get(); } - auto mutable_surface() { return surface_.get(); } auto vector() const { return data_; } auto at(int i) const { return data_[i]; }