Change on save system
This commit is contained in:
parent
450fd42512
commit
2e869371c7
BIN
Plague.g1a
BIN
Plague.g1a
Binary file not shown.
25
src/save.c
25
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);
|
||||
|
|
Loading…
Reference in New Issue