From 184777ed122493df62a4093242b2a0a081345d9e Mon Sep 17 00:00:00 2001 From: jacob1 Date: Sun, 23 Apr 2017 22:44:31 -0400 Subject: [PATCH] GameSave.cpp 2D array cleanup --- src/client/GameSave.cpp | 98 +++++++---------------------------------- src/client/GameSave.h | 3 -- 2 files changed, 16 insertions(+), 85 deletions(-) diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp index 9959c34bb..077bed0e7 100644 --- a/src/client/GameSave.cpp +++ b/src/client/GameSave.cpp @@ -36,11 +36,11 @@ originalData(save.originalData) setSize(save.blockWidth, save.blockHeight); std::copy(save.particles, save.particles+NPART, particles); - std::copy(save.blockMapPtr, save.blockMapPtr+(blockHeight*blockWidth), blockMapPtr); - std::copy(save.fanVelXPtr, save.fanVelXPtr+(blockHeight*blockWidth), fanVelXPtr); - std::copy(save.fanVelYPtr, save.fanVelYPtr+(blockHeight*blockWidth), fanVelYPtr); for (int j = 0; j < blockHeight; j++) { + std::copy(save.blockMap[j], save.blockMap[j]+blockWidth, blockMap[j]); + std::copy(save.fanVelX[j], save.fanVelX[j]+blockWidth, fanVelX[j]); + std::copy(save.fanVelY[j], save.fanVelY[j]+blockWidth, fanVelY[j]); std::copy(save.pressure[j], save.pressure[j]+blockWidth, pressure[j]); std::copy(save.velocityX[j], save.velocityX[j]+blockWidth, velocityX[j]); std::copy(save.velocityY[j], save.velocityY[j]+blockWidth, velocityY[j]); @@ -144,11 +144,8 @@ GameSave::GameSave(char * data, int dataSize) void GameSave::InitData() { blockMap = NULL; - blockMapPtr = NULL; fanVelX = NULL; - fanVelXPtr = NULL; fanVelY = NULL; - fanVelYPtr = NULL; particles = NULL; pressure = NULL; velocityX = NULL; @@ -245,23 +242,9 @@ void GameSave::setSize(int newWidth, int newHeight) particlesCount = 0; particles = new Particle[NPART]; - blockMapPtr = new unsigned char[blockHeight*blockWidth]; - std::fill(blockMapPtr, blockMapPtr+(blockHeight*blockWidth), 0); - fanVelXPtr = new float[(blockHeight)*(blockWidth)]; - std::fill(fanVelXPtr, fanVelXPtr+((blockHeight)*(blockWidth)), 0.0f); - fanVelYPtr = new float[(blockHeight)*(blockWidth)]; - std::fill(fanVelYPtr, fanVelYPtr+((blockHeight)*(blockWidth)), 0.0f); - - blockMap = new unsigned char*[blockHeight]; - for(int y = 0; y < blockHeight; y++) - blockMap[y] = &blockMapPtr[y*blockWidth]; - fanVelX = new float*[blockHeight]; - for(int y = 0; y < blockHeight; y++) - fanVelX[y] = &fanVelXPtr[y*(blockWidth)]; - fanVelY = new float*[blockHeight]; - for(int y = 0; y < blockHeight; y++) - fanVelY[y] = &fanVelYPtr[y*blockWidth]; - + blockMap = Allocate2DArray(blockWidth, blockHeight, 0); + fanVelX = Allocate2DArray(blockWidth, blockHeight, 0.0f); + fanVelY = Allocate2DArray(blockWidth, blockHeight, 0.0f); pressure = Allocate2DArray(blockWidth, blockHeight, 0.0f); velocityX = Allocate2DArray(blockWidth, blockHeight, 0.0f); velocityY = Allocate2DArray(blockWidth, blockHeight, 0.0f); @@ -319,26 +302,9 @@ void GameSave::Transform(matrix2d transform, vector2d translate) float ** velocityXNew; float ** velocityYNew; - float * fanVelXPtrNew; - float * fanVelYPtrNew; - unsigned char * blockMapPtrNew; - - blockMapPtrNew = new unsigned char[newBlockHeight*newBlockWidth]; - std::fill(blockMapPtrNew, blockMapPtrNew+(newBlockHeight*newBlockWidth), 0); - fanVelXPtrNew = new float[newBlockHeight*newBlockWidth]; - std::fill(fanVelXPtrNew, fanVelXPtrNew+(newBlockHeight*newBlockWidth), 0.0f); - fanVelYPtrNew = new float[(newBlockHeight)*(newBlockWidth)]; - std::fill(fanVelYPtrNew, fanVelYPtrNew+(newBlockHeight*newBlockWidth), 0.0f); - - blockMapNew = new unsigned char*[newBlockHeight]; - for(int y = 0; y < newBlockHeight; y++) - blockMapNew[y] = &blockMapPtrNew[y*newBlockWidth]; - fanVelXNew = new float*[newBlockHeight]; - for(int y = 0; y < newBlockHeight; y++) - fanVelXNew[y] = &fanVelXPtrNew[y*newBlockWidth]; - fanVelYNew = new float*[newBlockHeight]; - for(int y = 0; y < newBlockHeight; y++) - fanVelYNew[y] = &fanVelYPtrNew[y*newBlockWidth]; + blockMapNew = Allocate2DArray(newBlockWidth, newBlockHeight, 0); + fanVelXNew = Allocate2DArray(newBlockWidth, newBlockHeight, 0.0f); + fanVelYNew = Allocate2DArray(newBlockWidth, newBlockHeight, 0.0f); pressureNew = Allocate2DArray(newBlockWidth, newBlockHeight, 0.0f); velocityXNew = Allocate2DArray(newBlockWidth, newBlockHeight, 0.0f); velocityYNew = Allocate2DArray(newBlockWidth, newBlockHeight, 0.0f); @@ -404,6 +370,9 @@ void GameSave::Transform(matrix2d transform, vector2d translate) for (int j = 0; j < blockHeight; j++) { + delete[] blockMap[j]; + delete[] fanVelX[j]; + delete[] fanVelY[j]; delete[] pressure[j]; delete[] velocityX[j]; delete[] velocityY[j]; @@ -419,20 +388,12 @@ void GameSave::Transform(matrix2d transform, vector2d translate) delete[] velocityX; delete[] velocityY; - delete[] blockMapPtr; - delete[] fanVelXPtr; - delete[] fanVelYPtr; - blockMap = blockMapNew; fanVelX = fanVelXNew; fanVelY = fanVelYNew; pressure = pressureNew; velocityX = velocityXNew; velocityY = velocityYNew; - - blockMapPtr = (unsigned char*)blockMapPtrNew; - fanVelXPtr = (float*)fanVelXPtrNew; - fanVelYPtr = (float*)fanVelYPtrNew; } void bson_error_handler(const char *err) @@ -2464,41 +2425,14 @@ void GameSave::Deallocate2DArray(T ***array, int blockHeight) void GameSave::dealloc() { - if(particles) + 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; - } + Deallocate2DArray(&blockMap, blockHeight); + Deallocate2DArray(&fanVelX, blockHeight); + Deallocate2DArray(&fanVelY, blockHeight); Deallocate2DArray(&pressure, blockHeight); Deallocate2DArray(&velocityX, blockHeight); Deallocate2DArray(&velocityY, blockHeight); diff --git a/src/client/GameSave.h b/src/client/GameSave.h index 48f38e8ff..dfd7b36e3 100644 --- a/src/client/GameSave.h +++ b/src/client/GameSave.h @@ -94,9 +94,6 @@ public: private: bool expanded; bool hasOriginalData; - float * fanVelXPtr; - float * fanVelYPtr; - unsigned char * blockMapPtr; std::vector originalData;