From 72ef0d0536655c7629c2128200a33a54e141db9d Mon Sep 17 00:00:00 2001 From: scawful Date: Sat, 18 Nov 2023 20:05:32 -0500 Subject: [PATCH] Update Bitmap fns --- src/CMakeLists.txt | 4 +-- src/app/core/pipeline.cc | 2 +- src/app/gfx/bitmap.cc | 55 ++++++++++++++++++++---------- src/app/gfx/bitmap.h | 11 +++--- src/app/zelda3/screen/inventory.cc | 4 +-- 5 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2995a9be..d12ac21e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,11 +11,11 @@ set( app/editor/dungeon_editor.cc app/editor/graphics_editor.cc app/editor/master_editor.cc - app/editor/music_editor.cc app/editor/overworld_editor.cc - app/editor/palette_editor.cc app/editor/screen_editor.cc app/editor/sprite_editor.cc + app/editor/resources/music_editor.cc + app/editor/resources/palette_editor.cc app/editor/modules/assembly_editor.cc app/editor/modules/tile16_editor.cc app/editor/modules/gfx_group_editor.cc diff --git a/src/app/core/pipeline.cc b/src/app/core/pipeline.cc index 5e6a3c7d..59b6b203 100644 --- a/src/app/core/pipeline.cc +++ b/src/app/core/pipeline.cc @@ -111,7 +111,7 @@ void BitmapCanvasPipeline(gui::Canvas& canvas, const gfx::Bitmap& bitmap, void BuildAndRenderBitmapPipeline(int width, int height, int depth, Bytes data, ROM& z3_rom, gfx::Bitmap& bitmap, gfx::SNESPalette& palette) { - bitmap.Create(width, height, depth, data); + PRINT_IF_ERROR(bitmap.InitializeFromData(width, height, depth, data)); bitmap.ApplyPalette(palette); z3_rom.RenderBitmap(&bitmap); } diff --git a/src/app/gfx/bitmap.cc b/src/app/gfx/bitmap.cc index 9536dd8e..03ebc3bf 100644 --- a/src/app/gfx/bitmap.cc +++ b/src/app/gfx/bitmap.cc @@ -104,24 +104,6 @@ void Bitmap::Create(int width, int height, int depth, const Bytes &data) { GrayscalePalette(surface_->format->palette); } -void Bitmap::CreateFromSurface(SDL_Surface *surface) { - active_ = true; - width_ = surface->w; - height_ = surface->h; - depth_ = 8; - pixel_data_ = static_cast(surface->pixels); - surface_ = std::unique_ptr( - SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_, - SDL_PIXELFORMAT_INDEX8), - SDL_Surface_Deleter()); - surface_->pixels = pixel_data_; -} - -void Bitmap::Apply(Bytes data) { - pixel_data_ = data.data(); - data_ = data; -} - // Creates the texture that will be displayed to the screen. void Bitmap::CreateTexture(std::shared_ptr renderer) { texture_ = std::shared_ptr{ @@ -177,6 +159,43 @@ void Bitmap::ApplyPalette(const std::vector &palette) { SDL_LockSurface(surface_.get()); } +absl::Status Bitmap::InitializeFromData(uint32_t width, uint32_t height, + uint32_t depth, const Bytes &data) { + if (width == 0 || height == 0 || depth == 0) { + return absl::InvalidArgumentError( + absl::StrCat("Invalid arguments: width: ", width, ", height: ", height, + ", depth: ", depth)); + } + + active_ = true; + width_ = width; + height_ = height; + depth_ = depth; + data_ = data; + + surface_ = std::unique_ptr( + SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_, + SDL_PIXELFORMAT_INDEX8), + SDL_Surface_Deleter()); + + if (surface_ == nullptr) { + return absl::InternalError("Failed to create surface."); + } + + surface_->pixels = data_.data(); + GrayscalePalette(surface_->format->palette); + return absl::OkStatus(); +} + +void Bitmap::ReserveData(uint32_t width, uint32_t height, uint32_t depth, + uint32_t size) { + width_ = width; + height_ = height; + depth_ = depth; + data_.reserve(size); + pixel_data_ = data_.data(); +} + } // namespace gfx } // namespace app } // namespace yaze diff --git a/src/app/gfx/bitmap.h b/src/app/gfx/bitmap.h index 812c573d..4e3a4a24 100644 --- a/src/app/gfx/bitmap.h +++ b/src/app/gfx/bitmap.h @@ -22,8 +22,6 @@ class Bitmap { Bitmap() = default; Bitmap(int width, int height, int depth, int data_size); - // Bitmap(int width, int height, int depth, Bytes data); - Bitmap(int width, int height, int depth, const Bytes &data) : width_(width), height_(height), depth_(depth), data_(data) { CreateTextureFromData(); @@ -48,14 +46,15 @@ class Bitmap { void Create(int width, int height, int depth, int data_size); void Create(int width, int height, int depth, const Bytes &data); + absl::Status InitializeFromData(uint32_t width, uint32_t height, + uint32_t depth, const Bytes &data); + void ReserveData(uint32_t width, uint32_t height, uint32_t depth, + uint32_t size); + [[deprecated]] void Create(int width, int height, int depth, uchar *data); [[deprecated]] void Create(int width, int height, int depth, uchar *data, int data_size); - void CreateFromSurface(SDL_Surface *surface); - - void Apply(Bytes data); - void CreateTexture(std::shared_ptr renderer); void UpdateTexture(std::shared_ptr renderer); diff --git a/src/app/zelda3/screen/inventory.cc b/src/app/zelda3/screen/inventory.cc index eed8511e..a46c7d21 100644 --- a/src/app/zelda3/screen/inventory.cc +++ b/src/app/zelda3/screen/inventory.cc @@ -5,7 +5,6 @@ #include "app/gui/canvas.h" #include "app/rom.h" - namespace yaze { namespace app { namespace zelda3 { @@ -62,7 +61,8 @@ void Inventory::Create() { i++; } } - bitmap_.Create(256, 256, 128, data_); + + PRINT_IF_ERROR(bitmap_.InitializeFromData(256, 256, 8, data_)) bitmap_.ApplyPalette(palette_); rom()->RenderBitmap(&bitmap_); }