change some free/malloc's back to delete/new's

This commit is contained in:
jacob1
2013-01-12 21:06:52 -05:00
parent 0b6418b78d
commit 41751da619
4 changed files with 109 additions and 111 deletions

View File

@@ -259,7 +259,7 @@ void GameSave::setSize(int newWidth, int newHeight)
{ {
this->blockWidth = newWidth; this->blockWidth = newWidth;
this->blockHeight = newHeight; this->blockHeight = newHeight;
particlesCount = 0; particlesCount = 0;
particles = new Particle[NPART]; particles = new Particle[NPART];
@@ -286,7 +286,7 @@ std::vector<char> GameSave::Serialise()
int dataSize; int dataSize;
char * data = Serialise(dataSize); char * data = Serialise(dataSize);
std::vector<char> dataVect(data, data+dataSize); std::vector<char> dataVect(data, data+dataSize);
free(data); delete data;
return dataVect; return dataVect;
} }
@@ -438,54 +438,54 @@ void GameSave::readOPS(char * data, int dataLength)
int savedVersion = inputData[4]; int savedVersion = inputData[4];
bson b; bson b;
bson_iterator iter; bson_iterator iter;
//Block sizes //Block sizes
blockX = 0; blockX = 0;
blockY = 0; blockY = 0;
blockW = inputData[6]; blockW = inputData[6];
blockH = inputData[7]; blockH = inputData[7];
//Full size, normalised //Full size, normalised
fullX = blockX*CELL; fullX = blockX*CELL;
fullY = blockY*CELL; fullY = blockY*CELL;
fullW = blockW*CELL; fullW = blockW*CELL;
fullH = blockH*CELL; fullH = blockH*CELL;
//From newer version //From newer version
if(savedVersion > SAVE_VERSION) if(savedVersion > SAVE_VERSION)
throw ParseException(ParseException::WrongVersion, "Save from newer version"); throw ParseException(ParseException::WrongVersion, "Save from newer version");
//Incompatible cell size //Incompatible cell size
if(inputData[5] > CELL) if(inputData[5] > CELL)
throw ParseException(ParseException::InvalidDimensions, "Incorrect CELL size"); throw ParseException(ParseException::InvalidDimensions, "Incorrect CELL size");
//Too large/off screen //Too large/off screen
if(blockX+blockW > XRES/CELL || blockY+blockH > YRES/CELL) if(blockX+blockW > XRES/CELL || blockY+blockH > YRES/CELL)
throw ParseException(ParseException::InvalidDimensions, "Save too large"); throw ParseException(ParseException::InvalidDimensions, "Save too large");
setSize(blockW, blockH); setSize(blockW, blockH);
bsonDataLen = ((unsigned)inputData[8]); bsonDataLen = ((unsigned)inputData[8]);
bsonDataLen |= ((unsigned)inputData[9]) << 8; bsonDataLen |= ((unsigned)inputData[9]) << 8;
bsonDataLen |= ((unsigned)inputData[10]) << 16; bsonDataLen |= ((unsigned)inputData[10]) << 16;
bsonDataLen |= ((unsigned)inputData[11]) << 24; bsonDataLen |= ((unsigned)inputData[11]) << 24;
bsonData = (unsigned char*)malloc(bsonDataLen+1); bsonData = (unsigned char*)malloc(bsonDataLen+1);
if(!bsonData) if(!bsonData)
throw ParseException(ParseException::InternalError, "Unable to allocate memory"); throw ParseException(ParseException::InternalError, "Unable to allocate memory");
//Make sure bsonData is null terminated, since all string functions need null terminated strings //Make sure bsonData is null terminated, since all string functions need null terminated strings
//(bson_iterator_key returns a pointer into bsonData, which is then used with strcmp) //(bson_iterator_key returns a pointer into bsonData, which is then used with strcmp)
bsonData[bsonDataLen] = 0; bsonData[bsonDataLen] = 0;
if (BZ2_bzBuffToBuffDecompress((char*)bsonData, &bsonDataLen, (char*)(inputData+12), inputDataLen-12, 0, 0)) if (BZ2_bzBuffToBuffDecompress((char*)bsonData, &bsonDataLen, (char*)(inputData+12), inputDataLen-12, 0, 0))
throw ParseException(ParseException::Corrupt, "Unable to decompress"); throw ParseException(ParseException::Corrupt, "Unable to decompress");
bson_init_data(&b, (char*)bsonData); bson_init_data(&b, (char*)bsonData);
bson_iterator_init(&iter, &b); bson_iterator_init(&iter, &b);
std::vector<sign> tempSigns; std::vector<sign> tempSigns;
while(bson_iterator_next(&iter)) while(bson_iterator_next(&iter))
{ {
if(strcmp(bson_iterator_key(&iter), "signs")==0) if(strcmp(bson_iterator_key(&iter), "signs")==0)
@@ -502,7 +502,7 @@ void GameSave::readOPS(char * data, int dataLength)
{ {
bson_iterator signiter; bson_iterator signiter;
bson_iterator_subiterator(&subiter, &signiter); bson_iterator_subiterator(&subiter, &signiter);
sign tempSign("", 0, 0, sign::Left); sign tempSign("", 0, 0, sign::Left);
while(bson_iterator_next(&signiter)) while(bson_iterator_next(&signiter))
{ {
@@ -681,7 +681,7 @@ void GameSave::readOPS(char * data, int dataLength)
} }
} }
} }
//Read wall and fan data //Read wall and fan data
if(wallData) if(wallData)
{ {
@@ -743,7 +743,7 @@ void GameSave::readOPS(char * data, int dataLength)
} }
} }
} }
//Read particle data //Read particle data
if(partsData && partsPosData) if(partsData && partsPosData)
{ {
@@ -778,7 +778,7 @@ void GameSave::readOPS(char * data, int dataLength)
{ {
goto fail; goto fail;
} }
//i+3 because we have 4 bytes of required fields (type (1), descriptor (2), temp (1)) //i+3 because we have 4 bytes of required fields (type (1), descriptor (2), temp (1))
if (i+3 >= partsDataLen) if (i+3 >= partsDataLen)
goto fail; goto fail;
@@ -796,19 +796,19 @@ void GameSave::readOPS(char * data, int dataLength)
if(newIndex < 0 || newIndex >= NPART) if(newIndex < 0 || newIndex >= NPART)
goto fail; goto fail;
//Store partsptr index+1 for this saved particle index (0 means not loaded) //Store partsptr index+1 for this saved particle index (0 means not loaded)
partsSimIndex[partsCount++] = newIndex+1; partsSimIndex[partsCount++] = newIndex+1;
//Clear the particle, ready for our new properties //Clear the particle, ready for our new properties
memset(&(particles[newIndex]), 0, sizeof(Particle)); memset(&(particles[newIndex]), 0, sizeof(Particle));
//Required fields //Required fields
particles[newIndex].type = partsData[i]; particles[newIndex].type = partsData[i];
particles[newIndex].x = x; particles[newIndex].x = x;
particles[newIndex].y = y; particles[newIndex].y = y;
i+=3; i+=3;
//Read temp //Read temp
if(fieldDescriptor & 0x01) if(fieldDescriptor & 0x01)
{ {
@@ -823,7 +823,7 @@ void GameSave::readOPS(char * data, int dataLength)
tempTemp = (char)partsData[i++]; tempTemp = (char)partsData[i++];
particles[newIndex].temp = tempTemp+294.15f; particles[newIndex].temp = tempTemp+294.15f;
} }
//Read life //Read life
if(fieldDescriptor & 0x02) if(fieldDescriptor & 0x02)
{ {
@@ -836,7 +836,7 @@ void GameSave::readOPS(char * data, int dataLength)
particles[newIndex].life |= (((unsigned)partsData[i++]) << 8); particles[newIndex].life |= (((unsigned)partsData[i++]) << 8);
} }
} }
//Read tmp //Read tmp
if(fieldDescriptor & 0x08) if(fieldDescriptor & 0x08)
{ {
@@ -856,7 +856,7 @@ void GameSave::readOPS(char * data, int dataLength)
} }
} }
} }
//Read ctype //Read ctype
if(fieldDescriptor & 0x20) if(fieldDescriptor & 0x20)
{ {
@@ -871,7 +871,7 @@ void GameSave::readOPS(char * data, int dataLength)
particles[newIndex].ctype |= (((unsigned)partsData[i++]) << 8); particles[newIndex].ctype |= (((unsigned)partsData[i++]) << 8);
} }
} }
//Read dcolour //Read dcolour
if(fieldDescriptor & 0x40) if(fieldDescriptor & 0x40)
{ {
@@ -881,21 +881,21 @@ void GameSave::readOPS(char * data, int dataLength)
particles[newIndex].dcolour |= (((unsigned)partsData[i++]) << 8); particles[newIndex].dcolour |= (((unsigned)partsData[i++]) << 8);
particles[newIndex].dcolour |= ((unsigned)partsData[i++]); particles[newIndex].dcolour |= ((unsigned)partsData[i++]);
} }
//Read vx //Read vx
if(fieldDescriptor & 0x80) if(fieldDescriptor & 0x80)
{ {
if(i >= partsDataLen) goto fail; if(i >= partsDataLen) goto fail;
particles[newIndex].vx = (partsData[i++]-127.0f)/16.0f; particles[newIndex].vx = (partsData[i++]-127.0f)/16.0f;
} }
//Read vy //Read vy
if(fieldDescriptor & 0x100) if(fieldDescriptor & 0x100)
{ {
if(i >= partsDataLen) goto fail; if(i >= partsDataLen) goto fail;
particles[newIndex].vy = (partsData[i++]-127.0f)/16.0f; particles[newIndex].vy = (partsData[i++]-127.0f)/16.0f;
} }
//Read tmp2 //Read tmp2
if(fieldDescriptor & 0x400) if(fieldDescriptor & 0x400)
{ {
@@ -1008,20 +1008,20 @@ void GameSave::readPSv(char * data, int dataLength)
int bx0=0, by0=0, bw, bh, w, h, y0 = 0, x0 = 0; int bx0=0, by0=0, bw, bh, w, h, y0 = 0, x0 = 0;
int nf=0, new_format = 0, ttv = 0; int nf=0, new_format = 0, ttv = 0;
int *fp = (int *)malloc(NPART*sizeof(int)); int *fp = (int *)malloc(NPART*sizeof(int));
std::vector<sign> tempSigns; std::vector<sign> tempSigns;
char tempSignText[255]; char tempSignText[255];
sign tempSign("", 0, 0, sign::Left); sign tempSign("", 0, 0, sign::Left);
//Gol data used to read older saves //Gol data used to read older saves
int goltype[NGOL]; int goltype[NGOL];
int grule[NGOL+1][10]; int grule[NGOL+1][10];
int golRulesCount; int golRulesCount;
int * golRulesT = LoadGOLRules(golRulesCount); int * golRulesT = LoadGOLRules(golRulesCount);
memcpy(grule, golRulesT, sizeof(int) * (golRulesCount*10)); memcpy(grule, golRulesT, sizeof(int) * (golRulesCount*10));
free(golRulesT); free(golRulesT);
int golTypesCount; int golTypesCount;
int * golTypesT = LoadGOLTypes(golTypesCount); int * golTypesT = LoadGOLTypes(golTypesCount);
memcpy(goltype, golTypesT, sizeof(int) * (golTypesCount)); memcpy(goltype, golTypesT, sizeof(int) * (golTypesCount));
@@ -1031,10 +1031,10 @@ void GameSave::readPSv(char * data, int dataLength)
try try
{ {
//New file header uses PSv, replacing fuC. This is to detect if the client uses a new save format for temperatures //New file header uses PSv, replacing fuC. This is to detect if the client uses a new save format for temperatures
//This creates a problem for old clients, that display and "corrupt" error instead of a "newer version" error //This creates a problem for old clients, that display and "corrupt" error instead of a "newer version" error
if (dataLength<16) if (dataLength<16)
throw ParseException(ParseException::Corrupt, "No save data"); throw ParseException(ParseException::Corrupt, "No save data");
if (!(c[2]==0x43 && c[1]==0x75 && c[0]==0x66) && !(c[2]==0x76 && c[1]==0x53 && c[0]==0x50)) if (!(c[2]==0x43 && c[1]==0x75 && c[0]==0x66) && !(c[2]==0x76 && c[1]==0x53 && c[0]==0x50))
@@ -1045,7 +1045,7 @@ void GameSave::readPSv(char * data, int dataLength)
if (c[4]>SAVE_VERSION) if (c[4]>SAVE_VERSION)
throw ParseException(ParseException::WrongVersion, "Save from newer version"); throw ParseException(ParseException::WrongVersion, "Save from newer version");
ver = c[4]; ver = c[4];
if (ver<34) if (ver<34)
{ {
legacyEnable = 1; legacyEnable = 1;
@@ -1070,7 +1070,7 @@ void GameSave::readPSv(char * data, int dataLength)
} }
} }
} }
bw = c[6]; bw = c[6];
bh = c[7]; bh = c[7];
if (bx0+bw > XRES/CELL) if (bx0+bw > XRES/CELL)
@@ -1081,7 +1081,7 @@ void GameSave::readPSv(char * data, int dataLength)
bx0 = 0; bx0 = 0;
if (by0 < 0) if (by0 < 0)
by0 = 0; by0 = 0;
if (c[5]!=CELL || bx0+bw>XRES/CELL || by0+bh>YRES/CELL) if (c[5]!=CELL || bx0+bw>XRES/CELL || by0+bh>YRES/CELL)
throw ParseException(ParseException::InvalidDimensions, "Save too large"); throw ParseException(ParseException::InvalidDimensions, "Save too large");
i = (unsigned)c[8]; i = (unsigned)c[8];
@@ -1091,9 +1091,9 @@ void GameSave::readPSv(char * data, int dataLength)
d = (unsigned char *)malloc(i); d = (unsigned char *)malloc(i);
if (!d) if (!d)
throw ParseException(ParseException::Corrupt, "Cannot allocate memory"); throw ParseException(ParseException::Corrupt, "Cannot allocate memory");
setSize(bw, bh); setSize(bw, bh);
int bzStatus = 0; int bzStatus = 0;
if (bzStatus = BZ2_bzBuffToBuffDecompress((char *)d, (unsigned *)&i, (char *)(c+12), dataLength-12, 0, 0)) if (bzStatus = BZ2_bzBuffToBuffDecompress((char *)d, (unsigned *)&i, (char *)(c+12), dataLength-12, 0, 0))
{ {
@@ -1106,22 +1106,22 @@ void GameSave::readPSv(char * data, int dataLength)
#ifdef DEBUG #ifdef DEBUG
std::cout << "Parsing " << dataLength << " bytes of data, version " << ver << std::endl; std::cout << "Parsing " << dataLength << " bytes of data, version " << ver << std::endl;
#endif #endif
if (dataLength < bw*bh) if (dataLength < bw*bh)
throw ParseException(ParseException::Corrupt, "Save data corrupt (missing data)"); throw ParseException(ParseException::Corrupt, "Save data corrupt (missing data)");
// normalize coordinates // normalize coordinates
x0 = bx0*CELL; x0 = bx0*CELL;
y0 = by0*CELL; y0 = by0*CELL;
w = bw *CELL; w = bw *CELL;
h = bh *CELL; h = bh *CELL;
if (ver<46) { if (ver<46) {
gravityMode = 0; gravityMode = 0;
airMode = 0; airMode = 0;
} }
m = (int *)calloc(XRES*YRES, sizeof(int)); m = (int *)calloc(XRES*YRES, sizeof(int));
// load the required air state // load the required air state
for (y=by0; y<by0+bh; y++) for (y=by0; y<by0+bh; y++)
for (x=bx0; x<bx0+bw; x++) for (x=bx0; x<bx0+bw; x++)
@@ -1162,7 +1162,7 @@ void GameSave::readPSv(char * data, int dataLength)
blockMap[y][x]=WL_EHOLE; blockMap[y][x]=WL_EHOLE;
else if (blockMap[y][x]==13) else if (blockMap[y][x]==13)
blockMap[y][x]=WL_ALLOWGAS; blockMap[y][x]=WL_ALLOWGAS;
if (blockMap[y][x]==O_WL_WALLELEC) if (blockMap[y][x]==O_WL_WALLELEC)
blockMap[y][x]=WL_WALLELEC; blockMap[y][x]=WL_WALLELEC;
else if (blockMap[y][x]==O_WL_EWALL) else if (blockMap[y][x]==O_WL_EWALL)
@@ -1196,7 +1196,7 @@ void GameSave::readPSv(char * data, int dataLength)
else if (blockMap[y][x]==O_WL_ALLOWENERGY) else if (blockMap[y][x]==O_WL_ALLOWENERGY)
blockMap[y][x]=WL_ALLOWENERGY; blockMap[y][x]=WL_ALLOWENERGY;
} }
p++; p++;
} }
for (y=by0; y<by0+bh; y++) for (y=by0; y<by0+bh; y++)
@@ -1215,7 +1215,7 @@ void GameSave::readPSv(char * data, int dataLength)
throw ParseException(ParseException::Corrupt, "Not enough data at line " MTOS(__LINE__) " in " MTOS(__FILE__)); throw ParseException(ParseException::Corrupt, "Not enough data at line " MTOS(__LINE__) " in " MTOS(__FILE__));
fanVelY[y][x] = (d[p++]-127.0f)/64.0f; fanVelY[y][x] = (d[p++]-127.0f)/64.0f;
} }
// load the particle map // load the particle map
i = 0; i = 0;
k = 0; k = 0;
@@ -1250,7 +1250,7 @@ void GameSave::readPSv(char * data, int dataLength)
particlesCount = ++k; particlesCount = ++k;
} }
} }
// load particle properties // load particle properties
for (j=0; j<w*h; j++) for (j=0; j<w*h; j++)
{ {
@@ -1479,7 +1479,7 @@ void GameSave::readPSv(char * data, int dataLength)
if (fabs(particles[i-1].vx)>0.0f || fabs(particles[i-1].vy)>0.0f) if (fabs(particles[i-1].vx)>0.0f || fabs(particles[i-1].vy)>0.0f)
particles[i-1].flags |= FLAG_MOVABLE; particles[i-1].flags |= FLAG_MOVABLE;
} }
if (ver<48 && (ty==OLD_PT_WIND || (ty==PT_BRAY&&particles[i-1].life==0))) if (ver<48 && (ty==OLD_PT_WIND || (ty==PT_BRAY&&particles[i-1].life==0)))
{ {
// Replace invisible particles with something sensible and add decoration to hide it // Replace invisible particles with something sensible and add decoration to hide it
@@ -1559,7 +1559,7 @@ void GameSave::readPSv(char * data, int dataLength)
} }
} }
} }
if (p >= dataLength) if (p >= dataLength)
goto version1; goto version1;
j = d[p++]; j = d[p++];
@@ -1586,7 +1586,7 @@ void GameSave::readPSv(char * data, int dataLength)
tempSigns.push_back(tempSign); tempSigns.push_back(tempSign);
p += x; p += x;
} }
for (i = 0; i < tempSigns.size(); i++) for (i = 0; i < tempSigns.size(); i++)
{ {
if(signs.size() == MAXSIGNS) if(signs.size() == MAXSIGNS)
@@ -1614,7 +1614,7 @@ void GameSave::readPSv(char * data, int dataLength)
} }
throw; throw;
} }
version1: version1:
if (m) if (m)
{ {
@@ -1651,17 +1651,17 @@ char * GameSave::serialiseOPS(int & dataLength)
//Get coords in blocks //Get coords in blocks
blockX = 0;//orig_x0/CELL; blockX = 0;//orig_x0/CELL;
blockY = 0;//orig_y0/CELL; blockY = 0;//orig_y0/CELL;
//Snap full coords to block size //Snap full coords to block size
fullX = blockX*CELL; fullX = blockX*CELL;
fullY = blockY*CELL; fullY = blockY*CELL;
//Original size + offset of original corner from snapped corner, rounded up by adding CELL-1 //Original size + offset of original corner from snapped corner, rounded up by adding CELL-1
blockW = blockWidth;//(blockWidth-fullX+CELL-1)/CELL; blockW = blockWidth;//(blockWidth-fullX+CELL-1)/CELL;
blockH = blockHeight;//(blockHeight-fullY+CELL-1)/CELL; blockH = blockHeight;//(blockHeight-fullY+CELL-1)/CELL;
fullW = blockW*CELL; fullW = blockW*CELL;
fullH = blockH*CELL; fullH = blockH*CELL;
//Copy fan and wall data //Copy fan and wall data
wallData = (unsigned char*)malloc(blockW*blockH); wallData = (unsigned char*)malloc(blockW*blockH);
wallDataLen = blockW*blockH; wallDataLen = blockW*blockH;
@@ -1697,7 +1697,7 @@ char * GameSave::serialiseOPS(int & dataLength)
free(wallData); free(wallData);
wallData = NULL; wallData = NULL;
} }
//Index positions of all particles, using linked lists //Index positions of all particles, using linked lists
//partsPosFirstMap is pmap for the first particle in each position //partsPosFirstMap is pmap for the first particle in each position
//partsPosLastMap is pmap for the last particle in each position //partsPosLastMap is pmap for the last particle in each position
@@ -1731,7 +1731,7 @@ char * GameSave::serialiseOPS(int & dataLength)
partsPosCount[y*fullW + x]++; partsPosCount[y*fullW + x]++;
} }
} }
//Store number of particles in each position //Store number of particles in each position
partsPosData = (unsigned char*)malloc(fullW*fullH*3); partsPosData = (unsigned char*)malloc(fullW*fullH*3);
partsPosDataLen = 0; partsPosDataLen = 0;
@@ -1745,7 +1745,7 @@ char * GameSave::serialiseOPS(int & dataLength)
partsPosData[partsPosDataLen++] = (posCount&0x000000FF); partsPosData[partsPosDataLen++] = (posCount&0x000000FF);
} }
} }
//Copy parts data //Copy parts data
/* Field descriptor format: /* Field descriptor format:
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
@@ -1762,27 +1762,27 @@ char * GameSave::serialiseOPS(int & dataLength)
{ {
//Find the first particle in this position //Find the first particle in this position
i = partsPosFirstMap[y*fullW + x]; i = partsPosFirstMap[y*fullW + x];
//Loop while there is a pmap entry //Loop while there is a pmap entry
while (i) while (i)
{ {
unsigned short fieldDesc = 0; unsigned short fieldDesc = 0;
int fieldDescLoc = 0, tempTemp, vTemp; int fieldDescLoc = 0, tempTemp, vTemp;
//Turn pmap entry into a particles index //Turn pmap entry into a particles index
i = i>>8; i = i>>8;
//Store saved particle index+1 for this partsptr index (0 means not saved) //Store saved particle index+1 for this partsptr index (0 means not saved)
partsSaveIndex[i] = (partsCount++) + 1; partsSaveIndex[i] = (partsCount++) + 1;
//Type (required) //Type (required)
partsData[partsDataLen++] = particles[i].type; partsData[partsDataLen++] = particles[i].type;
elementCount[particles[i].type]++; elementCount[particles[i].type]++;
//Location of the field descriptor //Location of the field descriptor
fieldDescLoc = partsDataLen++; fieldDescLoc = partsDataLen++;
partsDataLen++; partsDataLen++;
//Extra Temperature (2nd byte optional, 1st required), 1 to 2 bytes //Extra Temperature (2nd byte optional, 1st required), 1 to 2 bytes
//Store temperature as an offset of 21C(294.15K) or go into a 16byte int and store the whole thing //Store temperature as an offset of 21C(294.15K) or go into a 16byte int and store the whole thing
if(fabs(particles[i].temp-294.15f)<127) if(fabs(particles[i].temp-294.15f)<127)
@@ -1797,7 +1797,7 @@ char * GameSave::serialiseOPS(int & dataLength)
partsData[partsDataLen++] = tempTemp; partsData[partsDataLen++] = tempTemp;
partsData[partsDataLen++] = tempTemp >> 8; partsData[partsDataLen++] = tempTemp >> 8;
} }
//Life (optional), 1 to 2 bytes //Life (optional), 1 to 2 bytes
if(particles[i].life) if(particles[i].life)
{ {
@@ -1809,7 +1809,7 @@ char * GameSave::serialiseOPS(int & dataLength)
partsData[partsDataLen++] = particles[i].life >> 8; partsData[partsDataLen++] = particles[i].life >> 8;
} }
} }
//Tmp (optional), 1 to 2 bytes //Tmp (optional), 1 to 2 bytes
if(particles[i].tmp) if(particles[i].tmp)
{ {
@@ -1827,7 +1827,7 @@ char * GameSave::serialiseOPS(int & dataLength)
} }
} }
} }
//Ctype (optional), 1 or 4 bytes //Ctype (optional), 1 or 4 bytes
if(particles[i].ctype) if(particles[i].ctype)
{ {
@@ -1841,7 +1841,7 @@ char * GameSave::serialiseOPS(int & dataLength)
partsData[partsDataLen++] = (particles[i].ctype&0x0000FF00)>>8; partsData[partsDataLen++] = (particles[i].ctype&0x0000FF00)>>8;
} }
} }
//Dcolour (optional), 4 bytes //Dcolour (optional), 4 bytes
if(particles[i].dcolour && (particles[i].dcolour & 0xFF000000)) if(particles[i].dcolour && (particles[i].dcolour & 0xFF000000))
{ {
@@ -1851,7 +1851,7 @@ char * GameSave::serialiseOPS(int & dataLength)
partsData[partsDataLen++] = (particles[i].dcolour&0x0000FF00)>>8; partsData[partsDataLen++] = (particles[i].dcolour&0x0000FF00)>>8;
partsData[partsDataLen++] = (particles[i].dcolour&0x000000FF); partsData[partsDataLen++] = (particles[i].dcolour&0x000000FF);
} }
//VX (optional), 1 byte //VX (optional), 1 byte
if(fabs(particles[i].vx) > 0.001f) if(fabs(particles[i].vx) > 0.001f)
{ {
@@ -1861,7 +1861,7 @@ char * GameSave::serialiseOPS(int & dataLength)
if (vTemp>255) vTemp=255; if (vTemp>255) vTemp=255;
partsData[partsDataLen++] = vTemp; partsData[partsDataLen++] = vTemp;
} }
//VY (optional), 1 byte //VY (optional), 1 byte
if(fabs(particles[i].vy) > 0.001f) if(fabs(particles[i].vy) > 0.001f)
{ {
@@ -1871,7 +1871,7 @@ char * GameSave::serialiseOPS(int & dataLength)
if (vTemp>255) vTemp=255; if (vTemp>255) vTemp=255;
partsData[partsDataLen++] = vTemp; partsData[partsDataLen++] = vTemp;
} }
//Tmp2 (optional), 1 or 2 bytes //Tmp2 (optional), 1 or 2 bytes
if(particles[i].tmp2) if(particles[i].tmp2)
{ {
@@ -1883,11 +1883,11 @@ char * GameSave::serialiseOPS(int & dataLength)
partsData[partsDataLen++] = particles[i].tmp2 >> 8; partsData[partsDataLen++] = particles[i].tmp2 >> 8;
} }
} }
//Write the field descriptor; //Write the field descriptor;
partsData[fieldDescLoc] = fieldDesc; partsData[fieldDescLoc] = fieldDesc;
partsData[fieldDescLoc+1] = fieldDesc>>8; partsData[fieldDescLoc+1] = fieldDesc>>8;
//Get the pmap entry for the next particle in the same position //Get the pmap entry for the next particle in the same position
i = partsPosLink[i]; i = partsPosLink[i];
} }
@@ -1940,7 +1940,7 @@ char * GameSave::serialiseOPS(int & dataLength)
free(partsData); free(partsData);
partsData = NULL; partsData = NULL;
} }
bson_init(&b); bson_init(&b);
bson_append_bool(&b, "waterEEnabled", waterEEnabled); bson_append_bool(&b, "waterEEnabled", waterEEnabled);
bson_append_bool(&b, "legacyEnable", legacyEnable); bson_append_bool(&b, "legacyEnable", legacyEnable);
@@ -1948,7 +1948,7 @@ char * GameSave::serialiseOPS(int & dataLength)
bson_append_bool(&b, "paused", paused); bson_append_bool(&b, "paused", paused);
bson_append_int(&b, "gravityMode", gravityMode); bson_append_int(&b, "gravityMode", gravityMode);
bson_append_int(&b, "airMode", airMode); bson_append_int(&b, "airMode", airMode);
//bson_append_int(&b, "leftSelectedElement", sl); //bson_append_int(&b, "leftSelectedElement", sl);
//bson_append_int(&b, "rightSelectedElement", sr); //bson_append_int(&b, "rightSelectedElement", sr);
//bson_append_int(&b, "activeMenu", active_menu); //bson_append_int(&b, "activeMenu", active_menu);
@@ -2000,12 +2000,12 @@ char * GameSave::serialiseOPS(int & dataLength)
#ifdef DEBUG #ifdef DEBUG
bson_print(&b); bson_print(&b);
#endif #endif
finalData = (unsigned char *)bson_data(&b); finalData = (unsigned char *)bson_data(&b);
finalDataLen = bson_size(&b); finalDataLen = bson_size(&b);
outputDataLen = finalDataLen*2+12; outputDataLen = finalDataLen*2+12;
outputData = (unsigned char *)malloc(outputDataLen); outputData = new unsigned char[outputDataLen];
outputData[0] = 'O'; outputData[0] = 'O';
outputData[1] = 'P'; outputData[1] = 'P';
outputData[2] = 'S'; outputData[2] = 'S';
@@ -2018,7 +2018,7 @@ char * GameSave::serialiseOPS(int & dataLength)
outputData[9] = finalDataLen >> 8; outputData[9] = finalDataLen >> 8;
outputData[10] = finalDataLen >> 16; outputData[10] = finalDataLen >> 16;
outputData[11] = finalDataLen >> 24; outputData[11] = finalDataLen >> 24;
if (BZ2_bzBuffToBuffCompress((char*)(outputData+12), &outputDataLen, (char*)finalData, bson_size(&b), 9, 0, 0) != BZ_OK) if (BZ2_bzBuffToBuffCompress((char*)(outputData+12), &outputDataLen, (char*)finalData, bson_size(&b), 9, 0, 0) != BZ_OK)
{ {
puts("Save Error\n"); puts("Save Error\n");
@@ -2027,12 +2027,12 @@ char * GameSave::serialiseOPS(int & dataLength)
outputData = NULL; outputData = NULL;
goto fin; goto fin;
} }
#ifdef DEBUG #ifdef DEBUG
printf("compressed data: %d\n", outputDataLen); printf("compressed data: %d\n", outputDataLen);
#endif #endif
dataLength = outputDataLen + 12; dataLength = outputDataLen + 12;
fin: fin:
bson_destroy(&b); bson_destroy(&b);
if(partsData) if(partsData)
@@ -2047,7 +2047,7 @@ fin:
free(partsSaveIndex); free(partsSaveIndex);
if (soapLinkData) if (soapLinkData)
free(soapLinkData); free(soapLinkData);
return (char*)outputData; return (char*)outputData;
} }

View File

@@ -141,7 +141,7 @@ char * Graphics::GenerateGradient(pixel * colours, float * points, int pointcoun
temp = points[j-1]; temp = points[j-1];
points[j-1] = points[j]; points[j-1] = points[j];
points[j] = temp; points[j] = temp;
ptemp = colours[j-1]; ptemp = colours[j-1];
colours[j-1] = colours[j]; colours[j-1] = colours[j];
colours[j] = ptemp; colours[j] = ptemp;
@@ -178,7 +178,7 @@ void *Graphics::ptif_pack(pixel *src, int w, int h, int *result_size){
unsigned char *blue_chan = (unsigned char*)calloc(1, w*h); unsigned char *blue_chan = (unsigned char*)calloc(1, w*h);
unsigned char *data = (unsigned char*)malloc(((w*h)*3)+8); unsigned char *data = (unsigned char*)malloc(((w*h)*3)+8);
unsigned char *result = (unsigned char*)malloc(((w*h)*3)+8); unsigned char *result = (unsigned char*)malloc(((w*h)*3)+8);
for(cx = 0; cx<w; cx++){ for(cx = 0; cx<w; cx++){
for(cy = 0; cy<h; cy++){ for(cy = 0; cy<h; cy++){
red_chan[w*(cy)+(cx)] = PIXR(src[w*(cy)+(cx)]); red_chan[w*(cy)+(cx)] = PIXR(src[w*(cy)+(cx)]);
@@ -186,14 +186,14 @@ void *Graphics::ptif_pack(pixel *src, int w, int h, int *result_size){
blue_chan[w*(cy)+(cx)] = PIXB(src[w*(cy)+(cx)]); blue_chan[w*(cy)+(cx)] = PIXB(src[w*(cy)+(cx)]);
} }
} }
memcpy(data, red_chan, w*h); memcpy(data, red_chan, w*h);
memcpy(data+(w*h), green_chan, w*h); memcpy(data+(w*h), green_chan, w*h);
memcpy(data+((w*h)*2), blue_chan, w*h); memcpy(data+((w*h)*2), blue_chan, w*h);
free(red_chan); free(red_chan);
free(green_chan); free(green_chan);
free(blue_chan); free(blue_chan);
result[0] = 'P'; result[0] = 'P';
result[1] = 'T'; result[1] = 'T';
result[2] = 'i'; result[2] = 'i';
@@ -202,15 +202,15 @@ void *Graphics::ptif_pack(pixel *src, int w, int h, int *result_size){
result[5] = w>>8; result[5] = w>>8;
result[6] = h; result[6] = h;
result[7] = h>>8; result[7] = h>>8;
i -= 8; i -= 8;
if(BZ2_bzBuffToBuffCompress((char *)(result+8), (unsigned *)&i, (char *)data, datalen, 9, 0, 0) != 0){ if(BZ2_bzBuffToBuffCompress((char *)(result+8), (unsigned *)&i, (char *)data, datalen, 9, 0, 0) != 0){
free(data); free(data);
free(result); free(result);
return NULL; return NULL;
} }
*result_size = i+8; *result_size = i+8;
free(data); free(data);
return result; return result;
@@ -234,14 +234,14 @@ pixel *Graphics::ptif_unpack(void *datain, int size, int *w, int *h){
} }
width = data[4]|(data[5]<<8); width = data[4]|(data[5]<<8);
height = data[6]|(data[7]<<8); height = data[6]|(data[7]<<8);
i = (width*height)*3; i = (width*height)*3;
undata = (unsigned char*)calloc(1, (width*height)*3); undata = (unsigned char*)calloc(1, (width*height)*3);
red_chan = (unsigned char*)calloc(1, width*height); red_chan = (unsigned char*)calloc(1, width*height);
green_chan = (unsigned char*)calloc(1, width*height); green_chan = (unsigned char*)calloc(1, width*height);
blue_chan = (unsigned char *)calloc(1, width*height); blue_chan = (unsigned char *)calloc(1, width*height);
result = (pixel *)calloc(width*height, PIXELSIZE); result = (pixel *)calloc(width*height, PIXELSIZE);
resCode = BZ2_bzBuffToBuffDecompress((char *)undata, (unsigned *)&i, (char *)(data+8), size-8, 0, 0); resCode = BZ2_bzBuffToBuffDecompress((char *)undata, (unsigned *)&i, (char *)(data+8), size-8, 0, 0);
if (resCode){ if (resCode){
printf("Decompression failure, %d\n", resCode); printf("Decompression failure, %d\n", resCode);
@@ -264,13 +264,13 @@ pixel *Graphics::ptif_unpack(void *datain, int size, int *w, int *h){
memcpy(red_chan, undata, width*height); memcpy(red_chan, undata, width*height);
memcpy(green_chan, undata+(width*height), width*height); memcpy(green_chan, undata+(width*height), width*height);
memcpy(blue_chan, undata+((width*height)*2), width*height); memcpy(blue_chan, undata+((width*height)*2), width*height);
for(cx = 0; cx<width; cx++){ for(cx = 0; cx<width; cx++){
for(cy = 0; cy<height; cy++){ for(cy = 0; cy<height; cy++){
result[width*(cy)+(cx)] = PIXRGB(red_chan[width*(cy)+(cx)], green_chan[width*(cy)+(cx)], blue_chan[width*(cy)+(cx)]); result[width*(cy)+(cx)] = PIXRGB(red_chan[width*(cy)+(cx)], green_chan[width*(cy)+(cx)], blue_chan[width*(cy)+(cx)]);
} }
} }
*w = width; *w = width;
*h = height; *h = height;
free(red_chan); free(red_chan);
@@ -284,7 +284,7 @@ pixel *Graphics::resample_img_nn(pixel * src, int sw, int sh, int rw, int rh)
{ {
int y, x; int y, x;
pixel *q = NULL; pixel *q = NULL;
q = (pixel *)malloc(rw*rh*PIXELSIZE); q = new pixel[rw*rh];
for (y=0; y<rh; y++) for (y=0; y<rh; y++)
for (x=0; x<rw; x++){ for (x=0; x<rw; x++){
q[rw*y+x] = src[sw*(y*sh/rh)+(x*sw/rw)]; q[rw*y+x] = src[sw*(y*sh/rh)+(x*sw/rw)];
@@ -317,8 +317,8 @@ pixel *Graphics::resample_img(pixel *src, int sw, int sh, int rw, int rh)
samples[i] = new float[sourceWidth]; samples[i] = new float[sourceWidth];
} }
unsigned char * resultImage = (unsigned char*)malloc((resultHeight * resultPitch) * sizeof(unsigned char)); unsigned char * resultImage = new unsigned char[resultHeight * resultPitch];
memset(resultImage, 0, (resultHeight * resultPitch) * sizeof(unsigned char)); std::fill(resultImage, resultImage + (resultHeight*resultPitch), 0);
//Resample time //Resample time
int resultY = 0; int resultY = 0;
@@ -336,21 +336,21 @@ pixel *Graphics::resample_img(pixel *src, int sw, int sh, int rw, int rh)
} }
//Put channel sample data into resampler //Put channel sample data into resampler
for (int c = 0; c < PIXELCHANNELS; c++) for (int c = 0; c < PIXELCHANNELS; c++)
{ {
if (!resamplers[c]->put_line(&samples[c][0])) if (!resamplers[c]->put_line(&samples[c][0]))
{ {
printf("Out of memory!\n"); printf("Out of memory!\n");
return NULL; return NULL;
} }
} }
//Perform resample and Copy components from resampler result samples to image buffer //Perform resample and Copy components from resampler result samples to image buffer
for ( ; ; ) for ( ; ; )
{ {
int comp_index; int comp_index;
for (comp_index = 0; comp_index < PIXELCHANNELS; comp_index++) for (comp_index = 0; comp_index < PIXELCHANNELS; comp_index++)
{ {
const float* resultSamples = resamplers[comp_index]->get_line(); const float* resultSamples = resamplers[comp_index]->get_line();
if (!resultSamples) if (!resultSamples)
break; break;
@@ -364,9 +364,9 @@ pixel *Graphics::resample_img(pixel *src, int sw, int sh, int rw, int rh)
*resultPixel = (unsigned char)c; *resultPixel = (unsigned char)c;
resultPixel += PIXELSIZE; resultPixel += PIXELSIZE;
} }
} }
if (comp_index < PIXELCHANNELS) if (comp_index < PIXELCHANNELS)
break; break;
resultY++; resultY++;
} }
@@ -440,7 +440,7 @@ pixel *Graphics::resample_img(pixel *src, int sw, int sh, int rw, int rh)
(int)(((((float)PIXR(tl))*(1.0f-fxc))+(((float)PIXR(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXR(bl))*(1.0f-fxc))+(((float)PIXR(br))*(fxc)))*(fyc)), (int)(((((float)PIXR(tl))*(1.0f-fxc))+(((float)PIXR(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXR(bl))*(1.0f-fxc))+(((float)PIXR(br))*(fxc)))*(fyc)),
(int)(((((float)PIXG(tl))*(1.0f-fxc))+(((float)PIXG(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXG(bl))*(1.0f-fxc))+(((float)PIXG(br))*(fxc)))*(fyc)), (int)(((((float)PIXG(tl))*(1.0f-fxc))+(((float)PIXG(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXG(bl))*(1.0f-fxc))+(((float)PIXG(br))*(fxc)))*(fyc)),
(int)(((((float)PIXB(tl))*(1.0f-fxc))+(((float)PIXB(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXB(bl))*(1.0f-fxc))+(((float)PIXB(br))*(fxc)))*(fyc)) (int)(((((float)PIXB(tl))*(1.0f-fxc))+(((float)PIXB(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXB(bl))*(1.0f-fxc))+(((float)PIXB(br))*(fxc)))*(fyc))
); );
} }
} else { } else {
//Stairstepping //Stairstepping
@@ -483,7 +483,7 @@ pixel *Graphics::resample_img(pixel *src, int sw, int sh, int rw, int rh)
(int)(((((float)PIXR(tl))*(1.0f-fxc))+(((float)PIXR(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXR(bl))*(1.0f-fxc))+(((float)PIXR(br))*(fxc)))*(fyc)), (int)(((((float)PIXR(tl))*(1.0f-fxc))+(((float)PIXR(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXR(bl))*(1.0f-fxc))+(((float)PIXR(br))*(fxc)))*(fyc)),
(int)(((((float)PIXG(tl))*(1.0f-fxc))+(((float)PIXG(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXG(bl))*(1.0f-fxc))+(((float)PIXG(br))*(fxc)))*(fyc)), (int)(((((float)PIXG(tl))*(1.0f-fxc))+(((float)PIXG(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXG(bl))*(1.0f-fxc))+(((float)PIXG(br))*(fxc)))*(fyc)),
(int)(((((float)PIXB(tl))*(1.0f-fxc))+(((float)PIXB(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXB(bl))*(1.0f-fxc))+(((float)PIXB(br))*(fxc)))*(fyc)) (int)(((((float)PIXB(tl))*(1.0f-fxc))+(((float)PIXB(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXB(bl))*(1.0f-fxc))+(((float)PIXB(br))*(fxc)))*(fyc))
); );
} }
free(oq); free(oq);
oq = q; oq = q;
@@ -1116,4 +1116,4 @@ VideoBuffer Graphics::DumpFrame()
std::copy(vid, vid+((XRES+BARSIZE)*(YRES+MENUSIZE)), newBuffer.Buffer); std::copy(vid, vid+((XRES+BARSIZE)*(YRES+MENUSIZE)), newBuffer.Buffer);
return newBuffer; return newBuffer;
#endif #endif
} }

View File

@@ -196,7 +196,7 @@ PreviewView::PreviewView():
} }
void PreviewView::AttachController(PreviewController * controller) void PreviewView::AttachController(PreviewController * controller)
{ {
c = controller; c = controller;
saveIDTextbox->SetText(format::NumberToString<int>(c->SaveID())); saveIDTextbox->SetText(format::NumberToString<int>(c->SaveID()));
} }
@@ -411,7 +411,7 @@ void PreviewView::NotifySaveChanged(PreviewModel * sender)
float factorY = ((float)YRES/2)/((float)savePreview->Size.Y); float factorY = ((float)YRES/2)/((float)savePreview->Size.Y);
float scaleFactor = factorY < factorX ? factorY : factorX; float scaleFactor = factorY < factorX ? factorY : factorX;
savePreview->Data = Graphics::resample_img(oldData, savePreview->Size.X, savePreview->Size.Y, savePreview->Size.X*scaleFactor, savePreview->Size.Y*scaleFactor); savePreview->Data = Graphics::resample_img(oldData, savePreview->Size.X, savePreview->Size.Y, savePreview->Size.X*scaleFactor, savePreview->Size.Y*scaleFactor);
free(oldData); delete oldData;
savePreview->Size.X *= scaleFactor; savePreview->Size.X *= scaleFactor;
savePreview->Size.Y *= scaleFactor; savePreview->Size.Y *= scaleFactor;
} }

View File

@@ -16,7 +16,7 @@ Thumbnail::Thumbnail(const Thumbnail & thumb):
//Ensure the actual thumbnail data is copied //Ensure the actual thumbnail data is copied
if(thumb.Data) if(thumb.Data)
{ {
Data = (pixel *)malloc((thumb.Size.X*thumb.Size.Y) * PIXELSIZE); Data = new pixel[thumb.Size.X*thumb.Size.Y];
memcpy(Data, thumb.Data, (thumb.Size.X*thumb.Size.Y) * PIXELSIZE); memcpy(Data, thumb.Data, (thumb.Size.X*thumb.Size.Y) * PIXELSIZE);
} }
else else
@@ -33,7 +33,7 @@ Thumbnail::Thumbnail(int _id, int _datestamp, pixel * _data, ui::Point _size):
{ {
if(_data) if(_data)
{ {
Data = (pixel *)malloc((_size.X*_size.Y) * PIXELSIZE); Data = new pixel[_size.X*_size.Y];
memcpy(Data, _data, (_size.X*_size.Y) * PIXELSIZE); memcpy(Data, _data, (_size.X*_size.Y) * PIXELSIZE);
} }
else else
@@ -69,14 +69,12 @@ void Thumbnail::Resize(ui::Point newSize)
Data = Graphics::resample_img(thumbData, Size.X, Size.Y, Size.X * scaleFactor, Size.Y * scaleFactor); Data = Graphics::resample_img(thumbData, Size.X, Size.Y, Size.X * scaleFactor, Size.Y * scaleFactor);
Size.X *= scaleFactor; Size.X *= scaleFactor;
Size.Y *= scaleFactor; Size.Y *= scaleFactor;
free(thumbData); delete thumbData;
} }
} }
Thumbnail::~Thumbnail() Thumbnail::~Thumbnail()
{ {
if(Data) if(Data)
{ delete Data;
free(Data);
}
} }