Bitmap class additions

This commit is contained in:
Justin Scofield
2022-08-27 22:07:18 -05:00
parent b2030be95f
commit b65c7893a3
2 changed files with 36 additions and 3 deletions

View File

@@ -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_Surface, sdl_deleter>(
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<SDL_Renderer> renderer) {
texture_ = std::unique_ptr<SDL_Texture, sdl_deleter>(

View File

@@ -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<SDL_Renderer> renderer);
@@ -31,19 +33,31 @@ class Bitmap {
absl::StatusOr<std::vector<Bitmap>> CreateTiles();
absl::Status CreateFromTiles(const std::vector<Bitmap> &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;