Skip to content

Commit

Permalink
destructor added
Browse files Browse the repository at this point in the history
  • Loading branch information
LordEidi authored Apr 12, 2021
1 parent 88e6fc9 commit 946baad
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions 32blit/graphics/surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ namespace blit {
Surface::Surface(uint8_t *data, const PixelFormat &format, const Size &bounds) : data(data), bounds(bounds), format(format) {
init();
}

Surface::~Surface()
{
if (_bNewData) delete[] data;
if (_bNewPalette) delete[] palette;
}

/**
* Loads a packed or raw image asset into a `Surface`
Expand Down Expand Up @@ -641,6 +647,7 @@ namespace blit {
if (format == PixelFormat::P || !is_raw) {
// load palette
ret->palette = new Pen[256];
ret->_bNewPalette = true;
file.read(offset, palette_entry_count * 4, (char *)ret->palette);
offset += palette_entry_count * 4;
}
Expand All @@ -650,15 +657,21 @@ namespace blit {
ret->data = (uint8_t *)file.get_ptr() + offset;
else {
if(!ret->data)
ret->data = new uint8_t[needed_size];
{
ret->data = new uint8_t[needed_size];
ret->_bNewData = true;
}
file.read(offset, image.width * image.height * pixel_format_stride[image.format], (char *)ret->data);
}

return ret;
}

if(!ret->data)
ret->data = new uint8_t[needed_size];
{
ret->data = new uint8_t[needed_size];
ret->_bNewData = true;
}

// avoid allocating if in flash
const uint8_t *image_data, *end;
Expand Down Expand Up @@ -755,6 +768,7 @@ namespace blit {
// unpacked, no longer needed
delete[] ret->palette;
ret->palette = nullptr;
ret->_bNewPalette = false;
}

if (!file.get_ptr()) {
Expand Down

0 comments on commit 946baad

Please sign in to comment.