cleanup bitmap class

This commit is contained in:
scawful
2024-05-28 23:49:45 -04:00
parent b48af5313b
commit 00e82140e0
2 changed files with 123 additions and 135 deletions

View File

@@ -49,8 +49,8 @@ void PngReadCallback(png_structp png_ptr, png_bytep outBytes,
} // namespace } // namespace
bool ConvertSurfaceToPNG(SDL_Surface *surface, std::vector<uint8_t> &buffer) { bool ConvertSurfaceToPNG(SDL_Surface *surface, std::vector<uint8_t> &buffer) {
png_structp png_ptr = png_create_write_struct("1.6.40", NULL, NULL, png_structp png_ptr = png_create_write_struct("1.6.40", NULL, NULL, NULL);
NULL); if (!png_ptr) { if (!png_ptr) {
SDL_Log("Failed to create PNG write struct"); SDL_Log("Failed to create PNG write struct");
return false; return false;
} }
@@ -77,8 +77,7 @@ bool ConvertSurfaceToPNG(SDL_Surface *surface, std::vector<uint8_t> &buffer) {
int i = 0; int i = 0;
SDL_Palette *pal; SDL_Palette *pal;
if (surface->format->BytesPerPixel > 0 && if (surface->format->BytesPerPixel > 0 &&
surface->format->BytesPerPixel <= 8 && (pal = surface->format->BytesPerPixel <= 8 && (pal = surface->format->palette)) {
surface->format->palette)) {
SDL_Log("Writing PNG image with palette"); SDL_Log("Writing PNG image with palette");
colortype |= PNG_COLOR_MASK_PALETTE; colortype |= PNG_COLOR_MASK_PALETTE;
pal_ptr = (png_colorp)malloc(pal->ncolors * sizeof(png_color)); pal_ptr = (png_colorp)malloc(pal->ncolors * sizeof(png_color));
@@ -230,10 +229,8 @@ void Bitmap::CreateTexture(SDL_Renderer *renderer) {
SDL_LockTexture(texture_.get(), nullptr, (void **)&texture_pixels, SDL_LockTexture(texture_.get(), nullptr, (void **)&texture_pixels,
&converted_surface_->pitch); &converted_surface_->pitch);
memcpy(texture_pixels, converted_surface_->pixels, memcpy(texture_pixels, converted_surface_->pixels,
converted_surface_->h * converted_surface_->pitch); converted_surface_->h * converted_surface_->pitch);
SDL_UnlockTexture(texture_.get()); SDL_UnlockTexture(texture_.get());
} }
@@ -249,8 +246,6 @@ void Bitmap::UpdateTexture(SDL_Renderer *renderer, bool use_sdl_update) {
SDL_LockTexture(texture_.get(), nullptr, (void **)&texture_pixels, SDL_LockTexture(texture_.get(), nullptr, (void **)&texture_pixels,
&converted_surface_->pitch); &converted_surface_->pitch);
try {
if (use_sdl_update) { if (use_sdl_update) {
SDL_UpdateTexture(texture_.get(), nullptr, converted_surface_->pixels, SDL_UpdateTexture(texture_.get(), nullptr, converted_surface_->pixels,
converted_surface_->pitch); converted_surface_->pitch);
@@ -258,10 +253,6 @@ void Bitmap::UpdateTexture(SDL_Renderer *renderer, bool use_sdl_update) {
memcpy(texture_pixels, converted_surface_->pixels, memcpy(texture_pixels, converted_surface_->pixels,
converted_surface_->h * converted_surface_->pitch); converted_surface_->h * converted_surface_->pitch);
} }
} catch (const std::exception &e) {
SDL_Log("Exception: %s\n", e.what());
}
SDL_UnlockTexture(texture_.get()); SDL_UnlockTexture(texture_.get());
} }
@@ -272,8 +263,6 @@ void Bitmap::CreateTexture(std::shared_ptr<SDL_Renderer> renderer) {
} }
void Bitmap::UpdateTexture(std::shared_ptr<SDL_Renderer> renderer) { void Bitmap::UpdateTexture(std::shared_ptr<SDL_Renderer> renderer) {
// SDL_DestroyTexture(texture_.get());
// texture_ = nullptr;
texture_ = std::shared_ptr<SDL_Texture>{ texture_ = std::shared_ptr<SDL_Texture>{
SDL_CreateTextureFromSurface(renderer.get(), surface_.get()), SDL_CreateTextureFromSurface(renderer.get(), surface_.get()),
SDL_Texture_Deleter{}}; SDL_Texture_Deleter{}};

View File

@@ -253,7 +253,6 @@ class Bitmap {
struct SDL_Surface_Deleter { struct SDL_Surface_Deleter {
void operator()(SDL_Surface *p) const { void operator()(SDL_Surface *p) const {
if (p != nullptr) { if (p != nullptr) {
p->pixels = nullptr;
SDL_FreeSurface(p); SDL_FreeSurface(p);
p = nullptr; p = nullptr;
} }