Skip to content

Commit

Permalink
use new/delete instead of malloc and optimize gamedata load
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertBColton committed Feb 16, 2019
1 parent 3086c9f commit 9076bb3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
27 changes: 11 additions & 16 deletions CompilerSource/backend/GameData.cpp
Expand Up @@ -71,25 +71,20 @@ ImageData loadImageData(const std::string &filePath, int &errorc) {
return ImageData(0, 0, 0, 0);
}

unsigned ih,iw;
const int bitmap_size = pngwidth*pngheight*4;
unsigned char* bitmap = new unsigned char[bitmap_size](); // Initialize to zero.

for (ih = 0; ih < pngheight; ih++) {
unsigned tmp = ih*pngwidth*4;
for (iw = 0; iw < pngwidth; iw++) {
bitmap[tmp+0] = image[4*pngwidth*ih+iw*4+2];
bitmap[tmp+1] = image[4*pngwidth*ih+iw*4+1];
bitmap[tmp+2] = image[4*pngwidth*ih+iw*4+0];
bitmap[tmp+3] = image[4*pngwidth*ih+iw*4+3];
tmp+=4;
const unsigned pitch = pngwidth * 4;
for (unsigned i = 0; i < pngheight; i++) {
const unsigned row = i*pitch;
for (unsigned j = 0; j < pngwidth; j++) {
const unsigned px = row+j*4;
unsigned char temp = image[px+2];
image[px+2] = image[px+0];
image[px+0] = temp;
}
}

free(image);

int dataSize = bitmap_size;
const unsigned char* data = zlib_compress(bitmap, dataSize);
int dataSize = pngwidth*pngheight*4;
const unsigned char* data = zlib_compress(image, dataSize);
delete[] image;

errorc = 0;
return ImageData(pngwidth, pngheight, data, dataSize);;
Expand Down
Expand Up @@ -62,7 +62,7 @@ unsigned char* image_load_png(string filename, unsigned int* width, unsigned int
}
}

free(image);
delete[] image;
*width = pngwidth;
*height = pngheight;
*fullwidth = widfull;
Expand Down
3 changes: 1 addition & 2 deletions shared/libpng-util/libpng-util.cpp
Expand Up @@ -18,7 +18,6 @@
#include <string>

#include <png.h>
#include <stdlib.h> // for malloc/free

unsigned libpng_encode32_file(const unsigned char* image, const unsigned w, const unsigned h, const char* filename) {
FILE *fp = fopen(filename, "wb");
Expand Down Expand Up @@ -103,7 +102,7 @@ unsigned libpng_decode32_file(unsigned char** out, unsigned* w, unsigned* h, con

png_bytep image;
size_t pitch = sizeof(png_byte) * 4 * (*w); // number of bytes in a row
image = (png_bytep)malloc(pitch * (*h));
image = new png_byte[pitch * (*h)];

for (size_t y = 0; y < (*h); y++) {
png_read_row(png, (png_bytep)&image[pitch * y], NULL);
Expand Down

0 comments on commit 9076bb3

Please sign in to comment.