From a6277ecd5c1c18d69473aacdee70aa23fdace683 Mon Sep 17 00:00:00 2001 From: Justin Scofield <47263509+scawful@users.noreply.github.com> Date: Sat, 3 Sep 2022 22:45:32 -0500 Subject: [PATCH] improve bitmap --- src/app/gfx/bitmap.cc | 32 +++++++++++++++++++++++--------- src/app/gfx/bitmap.h | 18 +++++++++++++----- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/app/gfx/bitmap.cc b/src/app/gfx/bitmap.cc index 94b9ea6f..e1be16f4 100644 --- a/src/app/gfx/bitmap.cc +++ b/src/app/gfx/bitmap.cc @@ -43,10 +43,10 @@ void Bitmap::Create(int width, int height, int depth, uchar *data) { height_ = height; depth_ = depth; pixel_data_ = data; - surface_ = std::unique_ptr( + surface_ = std::unique_ptr( SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_, SDL_PIXELFORMAT_INDEX8), - sdl_deleter()); + SDL_Surface_Deleter()); GrayscalePalette(surface_->format->palette); surface_->pixels = pixel_data_; } @@ -57,10 +57,10 @@ void Bitmap::Create(int width, int height, int depth, int size) { height_ = height; depth_ = depth; data_size_ = size; - surface_ = std::unique_ptr( + surface_ = std::unique_ptr( SDL_CreateRGBSurfaceWithFormat(0, width, height, depth, SDL_PIXELFORMAT_INDEX8), - sdl_deleter()); + SDL_Surface_Deleter()); GrayscalePalette(surface_->format->palette); pixel_data_ = (uchar *)SDL_malloc(size); surface_->pixels = pixel_data_; @@ -73,19 +73,33 @@ void Bitmap::Create(int width, int height, int depth, uchar *data, int size) { depth_ = depth; pixel_data_ = data; data_size_ = size; - surface_ = std::unique_ptr( + surface_ = std::unique_ptr( SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_, SDL_PIXELFORMAT_INDEX8), - sdl_deleter()); - GrayscalePalette(surface_->format->palette); + SDL_Surface_Deleter()); surface_->pixels = pixel_data_; + GrayscalePalette(surface_->format->palette); +} + +void Bitmap::Create(int width, int height, int depth, Bytes data) { + width_ = width; + height_ = height; + depth_ = depth; + data_ = data; + pixel_data_ = data_.data(); + surface_ = std::unique_ptr( + SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_, + SDL_PIXELFORMAT_INDEX8), + SDL_Surface_Deleter()); + surface_->pixels = pixel_data_; + GrayscalePalette(surface_->format->palette); } // Creates the texture that will be displayed to the screen. void Bitmap::CreateTexture(std::shared_ptr renderer) { - texture_ = std::unique_ptr( + texture_ = std::shared_ptr{ SDL_CreateTextureFromSurface(renderer.get(), surface_.get()), - sdl_deleter()); + SDL_Texture_Deleter{}}; } // Convert SNESPalette to SDL_Palette for surface. diff --git a/src/app/gfx/bitmap.h b/src/app/gfx/bitmap.h index 2270f5ab..2afe5b8f 100644 --- a/src/app/gfx/bitmap.h +++ b/src/app/gfx/bitmap.h @@ -26,6 +26,7 @@ class Bitmap { void Create(int width, int height, int depth, uchar *data); void Create(int width, int height, int depth, int data_size); void Create(int width, int height, int depth, uchar *data, int data_size); + void Create(int width, int height, int depth, Bytes data); void CreateTexture(std::shared_ptr renderer); @@ -45,14 +46,21 @@ class Bitmap { auto GetSurface() const { return surface_.get(); } private: - struct sdl_deleter { + struct SDL_Texture_Deleter { void operator()(SDL_Texture *p) const { - SDL_DestroyTexture(p); - p = nullptr; + if (p != nullptr) { + SDL_DestroyTexture(p); + p = nullptr; + } } + }; + + struct SDL_Surface_Deleter { void operator()(SDL_Surface *p) const { - SDL_FreeSurface(p); - p = nullptr; + if (p != nullptr) { + SDL_FreeSurface(p); + p = nullptr; + } } };