Tilemap data is stored in a 2D array.
It is saved using the following pseudocodecode
FUNCTION
void SAVEChunk::save_to_file() {
OPEN FILE
FOR I, J, IN 2D// ARRAYFILENAME
WRITE 2D_ARRAY[I, J] TO FILEstd::string filename = std::to_string(chunk_seed);
WRITE
std::fstream file;
file.open(filename + ".chunk", std::ios::out);
for (int i = 0; i < Constants::BIOMESIZE; i++) {
for (int j = 0; j < Constants::BIOMESIZE; j++) {
file << std::to_string(chunk[i][j]);
file << ",";
TO FILE }
CLOSE FILE }
END
FUNCTION file.close();
}
And it is loaded using the following pseudocodecode
FUNCTION
void LOADChunk::load_from_file() {
OPEN FILE // FILENAME
READ DATA // The chunk will be located at hash(chunk_x, chunk_y) ^ seed = chunk_seed
SPLIT DATA BY GIVEN DELIMITERstd::string INTOfilename 1D= ARRAYstd::to_string(chunk_seed);
FOR I, J // Data within file
std::string data;
std::fstream file;
file.open(filename + ".chunk", INstd::ios::in);
2D DATA ARRAY file >> data;
2D_ARRAY[I file.close();
std::vector<std::string> tokens = split(data, J]',');
for (unsigned int i = SPLITTED_DATA[I0; i < Constants::BIOMESIZE; i++) {
for (unsigned int j = 0; j < Constants::BIOMESIZE; j++) {
chunk[i][j] = std::stoi(tokens[i + Jj * 2D_ARRAY_DIMENSION]Constants::BIOMESIZE]);
}
}
}
Assuming a square 2D array.
Tilemaps are loaded using the loading function when the program is started up and displayed.
Tilemaps are saved using the saving function when the program is exited.
They do not display the same way each time.
I have pinpointed that the problem is in the saving function.
This is because I commented out the saving part and when I was only loading the same files it worked and had the same output each time.
It appears as though the saving function rotates the tilemap by 180 degrees each time I save it.
How can this be fixed?

