I am trying to replicate a chunk loading system, in C, for my own minecraft like game, though I don't know how. Once chunks are loaded, are they placed in an array, or are there a bunch of variables for each loaded chunk?
In my game, once I load chunks using fread
, what should I use to store them? My problem for a two dimensional array for storing chunks, is that the loaded chunks might be separate and not start at zero. Are there any suggestions for storing loaded chunks?
I have a code fragment, solely so you guys will not yell at me for not including code:
char ChunkFilePath[strlen(CurrentWorld)+32];
sprintf(ChunkFilePath, "%s/ChunkData/%ld %ld", CurrentWorld, X, Z);
FILE *ChunkFile;
if ((ChunkFile = fopen(ChunkFilePath, "r"))) {
//chunk file exists, Read Chunk file to ???
}
if ((ChunkFile = fopen(ChunkFilePath, "w")) == NULL) {
fputs("Error Loading Chunk (File Not Found)", stderr);
return;
}
/*Chunk does not exist, but any other errors would have been caught
at this point, so Generate chunk, write generated results to ???*/
Note: The game I am trying to make is purely for my personal use, not for any commercial use.
Edit:
The problem is just an array will not work. Suppose there is a player at 1000000 and another one at -1000000 They would only render 6 chunks around them, but if they shared an array, all the 2000000 blocks worth of chunks that aren't loaded would be taking up memory. I would refer to chunks like LoadedChunk[1000]. How can I handle that?