Update Bitmap and add Initialize function
This commit is contained in:
@@ -218,6 +218,13 @@ Bitmap::Bitmap(int width, int height, int depth, int data_size) {
|
|||||||
Create(width, height, depth, std::vector<uint8_t>(data_size, 0));
|
Create(width, height, depth, std::vector<uint8_t>(data_size, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Bitmap::Initialize(int width, int height, int depth, std::span<uint8_t>& data) {
|
||||||
|
width_ = width;
|
||||||
|
height_ = height;
|
||||||
|
depth_ = depth;
|
||||||
|
data_ = std::vector<uint8_t>(data.begin(), data.end());
|
||||||
|
}
|
||||||
|
|
||||||
void Bitmap::Create(int width, int height, int depth, std::span<uint8_t> data) {
|
void Bitmap::Create(int width, int height, int depth, std::span<uint8_t> data) {
|
||||||
data_ = std::vector<uint8_t>(data.begin(), data.end());
|
data_ = std::vector<uint8_t>(data.begin(), data.end());
|
||||||
Create(width, height, depth, data_);
|
Create(width, height, depth, data_);
|
||||||
@@ -239,19 +246,20 @@ void Bitmap::Create(int width, int height, int depth, int format,
|
|||||||
width_ = width;
|
width_ = width;
|
||||||
height_ = height;
|
height_ = height;
|
||||||
depth_ = depth;
|
depth_ = depth;
|
||||||
data_ = data;
|
|
||||||
data_size_ = data.size();
|
data_size_ = data.size();
|
||||||
if (data_size_ == 0) {
|
if (data_size_ == 0) {
|
||||||
SDL_Log("Data provided to Bitmap is empty.\n");
|
SDL_Log("Data provided to Bitmap is empty.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
data_.reserve(data_size_);
|
||||||
|
data_ = data;
|
||||||
pixel_data_ = data_.data();
|
pixel_data_ = data_.data();
|
||||||
surface_ = std::shared_ptr<SDL_Surface>{
|
surface_ = std::shared_ptr<SDL_Surface>{
|
||||||
SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_,
|
SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_,
|
||||||
GetSnesPixelFormat(format)),
|
GetSnesPixelFormat(format)),
|
||||||
SDL_Surface_Deleter{}};
|
SDL_Surface_Deleter{}};
|
||||||
if (surface_ == nullptr) {
|
if (surface_ == nullptr) {
|
||||||
SDL_Log("SDL_CreateRGBSurfaceWithFormat failed: %s\n", SDL_GetError());
|
SDL_Log("Bitmap::Create.SDL_CreateRGBSurfaceWithFormat failed: %s\n", SDL_GetError());
|
||||||
active_ = false;
|
active_ = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -285,14 +293,15 @@ void Bitmap::CreateTexture(SDL_Renderer *renderer) {
|
|||||||
SDL_TEXTUREACCESS_STREAMING, width_, height_),
|
SDL_TEXTUREACCESS_STREAMING, width_, height_),
|
||||||
SDL_Texture_Deleter{}};
|
SDL_Texture_Deleter{}};
|
||||||
if (texture_ == nullptr) {
|
if (texture_ == nullptr) {
|
||||||
SDL_Log("SDL_CreateTextureFromSurface failed: %s\n", SDL_GetError());
|
SDL_Log("Bitmap::CreateTexture.SDL_CreateTextureFromSurface failed: %s\n", SDL_GetError());
|
||||||
}
|
}
|
||||||
|
texture_pixels = data_.data();
|
||||||
|
|
||||||
auto converted_surface_ = std::shared_ptr<SDL_Surface>{
|
auto converted_surface_ = std::shared_ptr<SDL_Surface>{
|
||||||
SDL_ConvertSurfaceFormat(surface_.get(), SDL_PIXELFORMAT_ARGB8888, 0),
|
SDL_ConvertSurfaceFormat(surface_.get(), SDL_PIXELFORMAT_ARGB8888, 0),
|
||||||
SDL_Surface_Deleter{}};
|
SDL_Surface_Deleter{}};
|
||||||
if (converted_surface_ == nullptr) {
|
if (converted_surface_ == nullptr) {
|
||||||
SDL_Log("SDL_ConvertSurfaceFormat failed: %s\n", SDL_GetError());
|
SDL_Log("Bitmap::CreateTexture.SDL_ConvertSurfaceFormat failed: %s\n", SDL_GetError());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ enum BitmapFormat {
|
|||||||
/**
|
/**
|
||||||
* @brief Convert SDL_Surface to PNG image data.
|
* @brief Convert SDL_Surface to PNG image data.
|
||||||
*/
|
*/
|
||||||
bool ConvertSurfaceToPNG(SDL_Surface *surface, std::vector<uint8_t> &buffer);
|
bool ConvertSurfaceToPng(SDL_Surface *surface, std::vector<uint8_t> &buffer);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Convert PNG image data to SDL_Surface.
|
* @brief Convert PNG image data to SDL_Surface.
|
||||||
@@ -91,9 +91,11 @@ class Bitmap {
|
|||||||
|
|
||||||
void SaveSurfaceToFile(std::string_view filename);
|
void SaveSurfaceToFile(std::string_view filename);
|
||||||
|
|
||||||
/**
|
void Initialize(int width, int height, int depth, std::span<uint8_t>& data);
|
||||||
* @brief Creates a bitmap object with the provided graphical data.
|
|
||||||
*/
|
void Create(int width, int height, int depth, int data_size) {
|
||||||
|
Create(width, height, depth, std::vector<uint8_t>(data_size, 0));
|
||||||
|
}
|
||||||
void Create(int width, int height, int depth, std::span<uint8_t> data);
|
void Create(int width, int height, int depth, std::span<uint8_t> data);
|
||||||
void Create(int width, int height, int depth,
|
void Create(int width, int height, int depth,
|
||||||
const std::vector<uint8_t> &data);
|
const std::vector<uint8_t> &data);
|
||||||
@@ -152,7 +154,6 @@ class Bitmap {
|
|||||||
|
|
||||||
auto palette() const { return palette_; }
|
auto palette() const { return palette_; }
|
||||||
auto mutable_palette() { return &palette_; }
|
auto mutable_palette() { return &palette_; }
|
||||||
auto palette_size() const { return palette_.size(); }
|
|
||||||
|
|
||||||
int width() const { return width_; }
|
int width() const { return width_; }
|
||||||
int height() const { return height_; }
|
int height() const { return height_; }
|
||||||
@@ -161,7 +162,6 @@ class Bitmap {
|
|||||||
auto data() const { return data_.data(); }
|
auto data() const { return data_.data(); }
|
||||||
auto &mutable_data() { return data_; }
|
auto &mutable_data() { return data_; }
|
||||||
auto surface() const { return surface_.get(); }
|
auto surface() const { return surface_.get(); }
|
||||||
auto mutable_surface() { return surface_.get(); }
|
|
||||||
|
|
||||||
auto vector() const { return data_; }
|
auto vector() const { return data_; }
|
||||||
auto at(int i) const { return data_[i]; }
|
auto at(int i) const { return data_[i]; }
|
||||||
|
|||||||
Reference in New Issue
Block a user