From b65c7893a34a27a17cbe34d2a1ecb064cfeacf95 Mon Sep 17 00:00:00 2001 From: Justin Scofield <47263509+scawful@users.noreply.github.com> Date: Sat, 27 Aug 2022 22:07:18 -0500 Subject: [PATCH] Bitmap class additions --- src/app/gfx/bitmap.cc | 19 +++++++++++++++++++ src/app/gfx/bitmap.h | 20 +++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/app/gfx/bitmap.cc b/src/app/gfx/bitmap.cc index 5b9a778d..94b9ea6f 100644 --- a/src/app/gfx/bitmap.cc +++ b/src/app/gfx/bitmap.cc @@ -33,6 +33,10 @@ Bitmap::Bitmap(int width, int height, int depth, int data_size) { Create(width, height, depth, data_size); } +Bitmap::Bitmap(int width, int height, int depth, uchar *data, int data_size) { + Create(width, height, depth, data, data_size); +} + // Pass raw pixel data directly to the surface void Bitmap::Create(int width, int height, int depth, uchar *data) { width_ = width; @@ -62,6 +66,21 @@ void Bitmap::Create(int width, int height, int depth, int size) { surface_->pixels = pixel_data_; } +// Pass raw pixel data directly to the surface +void Bitmap::Create(int width, int height, int depth, uchar *data, int size) { + width_ = width; + height_ = height; + depth_ = depth; + pixel_data_ = data; + data_size_ = size; + surface_ = std::unique_ptr( + SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_, + SDL_PIXELFORMAT_INDEX8), + sdl_deleter()); + GrayscalePalette(surface_->format->palette); + surface_->pixels = pixel_data_; +} + // Creates the texture that will be displayed to the screen. void Bitmap::CreateTexture(std::shared_ptr renderer) { texture_ = std::unique_ptr( diff --git a/src/app/gfx/bitmap.h b/src/app/gfx/bitmap.h index ac6a6c64..d16eb674 100644 --- a/src/app/gfx/bitmap.h +++ b/src/app/gfx/bitmap.h @@ -21,9 +21,11 @@ class Bitmap { Bitmap() = default; Bitmap(int width, int height, int depth, uchar *data); Bitmap(int width, int height, int depth, int data_size); + Bitmap(int width, int height, int depth, uchar *data, int data_size); 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 CreateTexture(std::shared_ptr renderer); @@ -31,19 +33,31 @@ class Bitmap { absl::StatusOr> CreateTiles(); absl::Status CreateFromTiles(const std::vector &tiles); - + absl::Status WritePixel(int pos, uchar pixel); int GetWidth() const { return width_; } int GetHeight() const { return height_; } + auto GetSize() const { return data_size_; } auto GetData() const { return pixel_data_; } + auto GetByte(int i) const { return pixel_data_[i]; } auto GetTexture() const { return texture_.get(); } auto GetSurface() const { return surface_.get(); } private: struct sdl_deleter { - void operator()(SDL_Texture *p) const { if (p) { SDL_DestroyTexture(p); p = nullptr; } } - void operator()(SDL_Surface *p) const { if (p) { SDL_FreeSurface(p); p = nullptr;} } + void operator()(SDL_Texture *p) const { + // if (p) { + // SDL_DestroyTexture(p); + // p = nullptr; + // } + } + void operator()(SDL_Surface *p) const { + // if (p) { + // SDL_FreeSurface(p); + // p = nullptr; + // } + } }; int width_ = 0;