Collapse gamesave to save space in memory

This commit is contained in:
Simon Robertshaw 2012-07-28 02:23:21 +01:00
parent 385552ceab
commit aa774771a0
4 changed files with 118 additions and 16 deletions

View File

@ -25,7 +25,10 @@ gravityEnable(save.gravityEnable),
paused(save.paused),
gravityMode(save.gravityMode),
airMode(save.airMode),
signs(save.signs)
signs(save.signs),
expanded(save.expanded),
hasOriginalData(save.hasOriginalData),
originalData(save.originalData)
{
blockMap = NULL;
blockMapPtr = NULL;
@ -34,14 +37,21 @@ signs(save.signs)
fanVelY = NULL;
fanVelYPtr = NULL;
particles = NULL;
if(save.expanded)
{
setSize(save.blockWidth, save.blockHeight);
setSize(save.blockWidth, save.blockHeight);
copy(save.particles, save.particles+NPART, particles);
copy(save.blockMapPtr, save.blockMapPtr+(blockHeight*blockWidth), blockMapPtr);
copy(save.fanVelXPtr, save.fanVelXPtr+(blockHeight*blockWidth), fanVelXPtr);
copy(save.fanVelYPtr, save.fanVelYPtr+(blockHeight*blockWidth), fanVelYPtr);
}
else
{
blockWidth = save.blockWidth;
blockHeight = save.blockHeight;
}
particlesCount = save.particlesCount;
copy(save.particles, save.particles+NPART, particles);
copy(save.blockMapPtr, save.blockMapPtr+(blockHeight*blockWidth), blockMapPtr);
copy(save.fanVelXPtr, save.fanVelXPtr+(blockHeight*blockWidth), fanVelXPtr);
copy(save.fanVelYPtr, save.fanVelYPtr+(blockHeight*blockWidth), fanVelYPtr);
}
GameSave::GameSave(int width, int height)
@ -54,6 +64,8 @@ GameSave::GameSave(int width, int height)
fanVelYPtr = NULL;
particles = NULL;
hasOriginalData = false;
expanded = true;
setSize(width, height);
}
@ -70,13 +82,20 @@ GameSave::GameSave(std::vector<char> data)
fanVelYPtr = NULL;
particles = NULL;
try{
read(&data[0], data.size());
} catch (ParseException& e) {
expanded = false;
hasOriginalData = true;
originalData = data;
try
{
Expand();
}
catch(ParseException & e)
{
std::cout << e.what() << std::endl;
this->~GameSave(); //Free any allocated memory
throw;
}
Collapse();
}
GameSave::GameSave(std::vector<unsigned char> data)
@ -92,13 +111,20 @@ GameSave::GameSave(std::vector<unsigned char> data)
fanVelYPtr = NULL;
particles = NULL;
try{
read((char*)(&data[0]), data.size());
} catch (ParseException& e) {
expanded = false;
hasOriginalData = true;
originalData = std::vector<char>(data.begin(), data.end());
try
{
Expand();
}
catch(ParseException & e)
{
std::cout << e.what() << std::endl;
this->~GameSave(); //Free any allocated memory
throw;
}
Collapse();
}
GameSave::GameSave(char * data, int dataSize)
@ -114,13 +140,77 @@ GameSave::GameSave(char * data, int dataSize)
fanVelYPtr = NULL;
particles = NULL;
try{
read(data, dataSize);
} catch (ParseException& e) {
expanded = true;
hasOriginalData = true;
originalData = std::vector<char>(data, data+dataSize);
try
{
Expand();
}
catch(ParseException & e)
{
std::cout << e.what() << std::endl;
this->~GameSave(); //Free any allocated memory
throw;
}
//Collapse();
}
bool GameSave::Collapsed()
{
return !expanded;
}
void GameSave::Expand()
{
if(hasOriginalData && !expanded)
{
expanded = true;
read(&originalData[0], originalData.size());
}
}
void GameSave::Collapse()
{
if(expanded && hasOriginalData)
{
expanded = false;
if(particles)
{
delete[] particles;
particles = NULL;
}
if(blockMap)
{
delete[] blockMap;
blockMap = NULL;
}
if(blockMapPtr)
{
delete[] blockMapPtr;
blockMapPtr = NULL;
}
if(fanVelX)
{
delete[] fanVelX;
fanVelX = NULL;
}
if(fanVelXPtr)
{
delete[] fanVelXPtr;
fanVelXPtr = NULL;
}
if(fanVelY)
{
delete[] fanVelY;
fanVelY = NULL;
}
if(fanVelYPtr)
{
delete[] fanVelYPtr;
fanVelYPtr = NULL;
}
}
}
void GameSave::read(char * data, int dataSize)

View File

@ -65,6 +65,10 @@ public:
void setSize(int width, int height);
char * Serialise(int & dataSize);
void Transform(matrix2d transform, vector2d translate);
void Expand();
void Collapse();
bool Collapsed();
inline GameSave& operator << (Particle v)
{
@ -83,10 +87,14 @@ public:
}
private:
bool expanded;
bool hasOriginalData;
float * fanVelXPtr;
float * fanVelYPtr;
unsigned char * blockMapPtr;
std::vector<char> originalData;
void read(char * data, int dataSize);
void readOPS(char * data, int dataLength);
void readPSv(char * data, int dataLength);

View File

@ -41,6 +41,7 @@ Thumbnail * SaveRenderer::Render(GameSave * save)
Thumbnail * tempThumb;
width = save->blockWidth;
height = save->blockHeight;
bool doCollapse = save->Collapsed();
g->Acquire();
g->Clear();
@ -112,6 +113,8 @@ Thumbnail * SaveRenderer::Render(GameSave * save)
free(pData);
#endif
}
if(doCollapse)
save->Collapse();
g->Release();
return tempThumb;
}

View File

@ -21,6 +21,7 @@ int Simulation::Load(int fullX, int fullY, GameSave * save)
int blockX, blockY, x, y, r;
if(!save) return 0;
save->Expand();
//Align to blockMap
blockX = fullX/CELL;