From 14796097e8873b38c9f77e03586006c558388621 Mon Sep 17 00:00:00 2001 From: Shadow15510 Date: Thu, 3 Jun 2021 17:32:16 +0200 Subject: [PATCH] Fix bugs --- src/save.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/save.c b/src/save.c index 2089edb..01e4070 100644 --- a/src/save.c +++ b/src/save.c @@ -14,6 +14,9 @@ void read_save(struct game *current_game) uint16_t foundpath[30]; // Sizes of data + const int planes_size = sizeof(*current_game->planes); + struct plane *planes = current_game->planes; + const int data_size = current_game->grid.width * current_game->grid.height; uint8_t *data = current_game->grid.data; @@ -24,7 +27,7 @@ void read_save(struct game *current_game) // If file doesn't exists if (checkfile == -1) { - int size = sizeof(*current_game) + data_size; + int size = sizeof(*current_game) + planes_size + data_size; BFile_Create(filename, BFile_File, &size); } @@ -32,9 +35,14 @@ void read_save(struct game *current_game) else { fd = BFile_Open(filename, BFile_ReadOnly); + BFile_Read(fd, current_game, sizeof(*current_game), 0); + Bfile_Read(fd, planes, planes_size, -1); BFile_Read(fd, data, data_size, -1); + BFile_Close(fd); + + current_game->planes = planes; current_game->grid.data = data; } } @@ -47,13 +55,15 @@ void write_save(const struct game *current_game) // Create a new savefile const int data_size = current_game->grid.width * current_game->grid.height; - int size = sizeof(*current_game) + data_size; + const int planes_size = sizeof(*current_game->planes); + int size = sizeof(*current_game) + planes_size + 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->planes, planes_size); BFile_Write(fd, current_game->grid.data, data_size); // Close file