diff --git a/Plague.g1a b/Plague.g1a index 1672daa..d0e11ee 100644 Binary files a/Plague.g1a and b/Plague.g1a differ diff --git a/src/save.c b/src/save.c index 73ba249..d08c43c 100644 --- a/src/save.c +++ b/src/save.c @@ -14,14 +14,22 @@ void read_save(struct game *current_game) int fd, handle; uint16_t foundpath[30]; - int size = sizeof(current_game) + sizeof(current_game->grid.data); - const int data_size = sizeof(uint8_t) * current_game->grid.width * current_game->grid.height; - uint8_t *data = malloc(data_size); + // Sizes of data + const int data_size = current_game->grid.width * current_game->grid.height; + uint8_t *data = current_game->grid.data; + // Check if the savefile exists char checkfile = BFile_FindFirst(filename, &handle, foundpath, &fileInfo); BFile_FindClose(handle); - if (checkfile == -1) BFile_Create(filename, BFile_File, &size); + // If file doesn't exists + if (checkfile == -1) + { + int size = sizeof(current_game) + data_size; + BFile_Create(filename, BFile_File, &size); + } + + // Loading of the game data else { fd = BFile_Open(filename, BFile_ReadOnly); @@ -38,9 +46,16 @@ void write_save(const struct game *current_game) // Open file int fd = BFile_Open(filename, BFile_WriteOnly); + // Create a new savefile + const int data_size = current_game->grid.width * current_game->grid.height; + int size = sizeof(current_game) + data_size; + + BFile_Remove(filename); + BFile_Create(filename, BFile_File, &size); + // Write data BFile_Write(fd, current_game, sizeof(current_game)); - BFile_Write(fd, current_game->grid.data, sizeof(current_game->grid.data)); + BFile_Write(fd, current_game->grid.data, data_size); // Close file BFile_Close(fd);