47 lines
1.2 KiB
C
47 lines
1.2 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];
|
|
|
|
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);
|
|
|
|
char checkfile = BFile_FindFirst(filename, &handle, foundpath, &fileInfo);
|
|
BFile_FindClose(handle);
|
|
|
|
if (checkfile == -1) BFile_Create(filename, BFile_File, &size);
|
|
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);
|
|
|
|
// Write data
|
|
BFile_Write(fd, current_game, sizeof(current_game));
|
|
BFile_Write(fd, current_game->grid.data, sizeof(current_game->grid.data));
|
|
|
|
// Close file
|
|
BFile_Close(fd);
|
|
} |