31 lines
806 B
C
31 lines
806 B
C
// chunkIO.c
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include "chunkIO.h"
|
|
|
|
bool SaveChunk(const Chunk *chunk) {
|
|
char filename[128];
|
|
snprintf(filename, sizeof(filename), "saves/chunk-%d-%d.dat", chunk->x, chunk->z);
|
|
|
|
FILE *file = fopen(filename, "wb");
|
|
if (!file) return false;
|
|
|
|
fwrite(chunk->blocks, sizeof(Block), CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z, file);
|
|
fclose(file);
|
|
return true;
|
|
}
|
|
|
|
bool LoadChunk(Chunk *chunk) {
|
|
char filename[128];
|
|
snprintf(filename, sizeof(filename), "saves/chunk-%d-%d.dat", chunk->x, chunk->z);
|
|
|
|
FILE *file = fopen(filename, "rb");
|
|
if (!file) return false;
|
|
|
|
fread(chunk->blocks, sizeof(Block), CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z, file);
|
|
fclose(file);
|
|
return true;
|
|
}
|