Each chunk has 2 16x16x16 unsigned char arrays called position and data.
I have two functions below a read and write.
Somehow between them the data becomes corrupt.
Is "(char)Chunk->position[j][k];" truncating my data?
The data array contains values from 0 to 255.
When it converts them from unsigned chars to chars is it losing the data? (even though thier converted back " newChunk->data[j][k] = (unsigned char)*pLoc;" )
After the readChunk routine runs I can look at the data[] and see that all of the values are 205.
Someone please point out my mistake.
Thanks,
Walker
bool file::writeChunk(chunk* Chunk)
{
if (Chunk->requiresWriting == false)
{
return false;
}
std::stringstream name;
std::string temp;
signed int x = Chunk->absolutePos.x;
signed int y = Chunk->absolutePos.y;
signed int z = Chunk->absolutePos.z;
ofstream fileStream;
////Open File to save in
name.str(std::string());
name << "save/" << world0->name << "x" << x << "x" << y << "x" << z << ".txt";
temp = name.str();
fileStream.open(temp.c_str(), ios::out|ios::binary);
if (fileStream.is_open())
{
////Make the 2 pointers
char *pStoreMemory;
char *pLoc;
////Allocate memory
pStoreMemory = new char [16*16*16*2];
pLoc = pStoreMemory;
////Write needed data to memory
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
for (int k = 0; k < 16; k++)
{
*pLoc = (char)Chunk->position[j][k];
pLoc++;
}
}
}
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
for (int k = 0; k < 16; k++)
{
*pLoc = (char)Chunk->data[j][k];
pLoc++;
}
}
}
////Copy memory to file
fileStream.write(pStoreMemory,16*16*16*2);
fileStream.close();
delete[] pStoreMemory;
//cout << "Done!\n";
}
else
{
//cout << "/!\\ Cannot open " << temp.c_str() << "\n";
}
delete Chunk;
return true;
}
chunk* file::readChunk(signed int x,signed int y,signed int z)
{
std::stringstream name;
std::string temp;
chunk* newChunk;
ifstream fileStreamRead;
////Open File to load from
name.str(std::string());
name << "save/" << world0->name << "x" << x << "x" << y << "x" << z << ".txt";
temp = name.str();
fileStreamRead.open(temp.c_str(), ios::in|ios::binary);
if (fileStreamRead.is_open())
{
iVertex3d position;
position.x = x;
position.y = y;
position.z = z;
newChunk = new chunk(position);
////Make the 2 pointers
char *pStoreMemory;
char *pLoc;
////Allocate memory
pStoreMemory = new char [16*16*16*2];
pLoc = pStoreMemory;
////Copy memory to file
fileStreamRead.read(pStoreMemory,16*16*16*2);
////read needed data from memory
//if ((unsigned char)*pLoc != 48)
{
//newChunk->empty = false;
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
for (int k = 0; k < 16; k++)
{
newChunk->position[j][k] = (unsigned char)*pLoc;
// /!\ Add to watch Queue if block has potential to move
switch (*pLoc)
{
case steam:
case smoke:
case water1:
case water2:
case water3:
case water4:
case water5:
case lava1:
case lava2:
case lava3:
case lava4:
case lava5:
case oil1:
case oil2:
case oil3:
case oil4:
case oil5:
case fire:
newChunk->liquidBufferAdd(i,j,k);
break;
}
pLoc++;
}
}
}
}
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
for (int k = 0; k < 16; k++)
{
newChunk->data[j][k] = (unsigned char)*pLoc;
}
}
}
fileStreamRead.close();
delete[] pStoreMemory;
return newChunk;
}
else
{
//cout << "/!\\ Cannot open " << temp.c_str() << "\n";
iVertex3d position;
position.x = x;
position.y = y;
position.z = z;
newChunk = new chunk(position);
generateChunk(newChunk);
return newChunk;
}
}