Plague-fx/src/save.c

62 lines
1.6 KiB
C

#include <gint/bfile.h>
#include <gint/defs/types.h>
#include <gint/std/stdlib.h>
#include "save.h"
// Name of the savefile
static const uint16_t *filename = u"\\\\fls0\\Plague.sav";
void read_save(struct game *current_game)
{
struct BFile_FileInfo fileInfo;
int fd, handle;
uint16_t foundpath[30];
// 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 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);
BFile_Read(fd, current_game, sizeof(current_game), 0);
BFile_Read(fd, data, data_size, -1);
BFile_Close(fd);
current_game->grid.data = data;
}
}
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, data_size);
// Close file
BFile_Close(fd);
}