Change on save system

This commit is contained in:
Shadow15510 2021-06-03 16:15:29 +02:00
parent 450fd42512
commit 2e869371c7
2 changed files with 20 additions and 5 deletions

Binary file not shown.

View File

@ -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);