Bitmap class additions
This commit is contained in:
@@ -33,6 +33,10 @@ Bitmap::Bitmap(int width, int height, int depth, int data_size) {
|
|||||||
Create(width, height, depth, 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
|
// Pass raw pixel data directly to the surface
|
||||||
void Bitmap::Create(int width, int height, int depth, uchar *data) {
|
void Bitmap::Create(int width, int height, int depth, uchar *data) {
|
||||||
width_ = width;
|
width_ = width;
|
||||||
@@ -62,6 +66,21 @@ void Bitmap::Create(int width, int height, int depth, int size) {
|
|||||||
surface_->pixels = pixel_data_;
|
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.
|
// Creates the texture that will be displayed to the screen.
|
||||||
void Bitmap::CreateTexture(std::shared_ptr<SDL_Renderer> renderer) {
|
void Bitmap::CreateTexture(std::shared_ptr<SDL_Renderer> renderer) {
|
||||||
texture_ = std::unique_ptr<SDL_Texture, sdl_deleter>(
|
texture_ = std::unique_ptr<SDL_Texture, sdl_deleter>(
|
||||||
|
|||||||
@@ -21,9 +21,11 @@ class Bitmap {
|
|||||||
Bitmap() = default;
|
Bitmap() = default;
|
||||||
Bitmap(int width, int height, int depth, uchar *data);
|
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, 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, uchar *data);
|
||||||
void Create(int width, int height, int depth, int data_size);
|
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);
|
void CreateTexture(std::shared_ptr<SDL_Renderer> renderer);
|
||||||
|
|
||||||
@@ -31,19 +33,31 @@ class Bitmap {
|
|||||||
|
|
||||||
absl::StatusOr<std::vector<Bitmap>> CreateTiles();
|
absl::StatusOr<std::vector<Bitmap>> CreateTiles();
|
||||||
absl::Status CreateFromTiles(const std::vector<Bitmap> &tiles);
|
absl::Status CreateFromTiles(const std::vector<Bitmap> &tiles);
|
||||||
|
|
||||||
absl::Status WritePixel(int pos, uchar pixel);
|
absl::Status WritePixel(int pos, uchar pixel);
|
||||||
|
|
||||||
int GetWidth() const { return width_; }
|
int GetWidth() const { return width_; }
|
||||||
int GetHeight() const { return height_; }
|
int GetHeight() const { return height_; }
|
||||||
|
auto GetSize() const { return data_size_; }
|
||||||
auto GetData() const { return pixel_data_; }
|
auto GetData() const { return pixel_data_; }
|
||||||
|
auto GetByte(int i) const { return pixel_data_[i]; }
|
||||||
auto GetTexture() const { return texture_.get(); }
|
auto GetTexture() const { return texture_.get(); }
|
||||||
auto GetSurface() const { return surface_.get(); }
|
auto GetSurface() const { return surface_.get(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct sdl_deleter {
|
struct sdl_deleter {
|
||||||
void operator()(SDL_Texture *p) const { if (p) { SDL_DestroyTexture(p); p = nullptr; } }
|
void operator()(SDL_Texture *p) const {
|
||||||
void operator()(SDL_Surface *p) const { if (p) { SDL_FreeSurface(p); p = nullptr;} }
|
// if (p) {
|
||||||
|
// SDL_DestroyTexture(p);
|
||||||
|
// p = nullptr;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
void operator()(SDL_Surface *p) const {
|
||||||
|
// if (p) {
|
||||||
|
// SDL_FreeSurface(p);
|
||||||
|
// p = nullptr;
|
||||||
|
// }
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int width_ = 0;
|
int width_ = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user