improve bitmap

This commit is contained in:
Justin Scofield
2022-09-03 22:45:32 -05:00
parent 285dfe183f
commit a6277ecd5c
2 changed files with 36 additions and 14 deletions

View File

@@ -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<SDL_Surface, sdl_deleter>(
surface_ = std::unique_ptr<SDL_Surface, SDL_Surface_Deleter>(
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<SDL_Surface, sdl_deleter>(
surface_ = std::unique_ptr<SDL_Surface, SDL_Surface_Deleter>(
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<SDL_Surface, sdl_deleter>(
surface_ = std::unique_ptr<SDL_Surface, SDL_Surface_Deleter>(
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_Surface, SDL_Surface_Deleter>(
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<SDL_Renderer> renderer) {
texture_ = std::unique_ptr<SDL_Texture, sdl_deleter>(
texture_ = std::shared_ptr<SDL_Texture>{
SDL_CreateTextureFromSurface(renderer.get(), surface_.get()),
sdl_deleter());
SDL_Texture_Deleter{}};
}
// Convert SNESPalette to SDL_Palette for surface.

View File

@@ -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<SDL_Renderer> 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;
}
}
};