Fonctionnal save system

This commit is contained in:
Shadow15510 2021-06-04 09:44:57 +02:00
parent a665f12807
commit 7b4d3a58fa
2 changed files with 23 additions and 14 deletions

Binary file not shown.

View File

@ -4,16 +4,24 @@
#include "save.h"
/* BUG
Démarrer le jeu, quitter, revenir sur le jeu, quitter -> la calto plante
=> Premier démarrage du jeu : ok (valeur par défaut bien initialisée)
=> Quand on revient sur le jeu, pas de problème : tout est sauvé correctement (sauvé correctement et chargé correctement)
=> On quitte : bug
*/
// 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
// Sizes of data and init
const int planes_size = sizeof(*current_game->planes) * (NB_PLANES + 1);
struct plane *new_planes[NB_PLANES + 1];
for (int i = 0; i < NB_PLANES; i ++)
@ -31,21 +39,23 @@ void read_save(struct game *current_game)
// If file doesn't exists
if (checkfile == -1)
{
int size = sizeof(*current_game) + planes_size + data_size;
int size = sizeof(struct game) + planes_size + data_size;
BFile_Create(filename, BFile_File, &size);
}
// Loading of the game data
// Loading game data
else
{
fd = BFile_Open(filename, BFile_ReadOnly);
BFile_Read(fd, current_game, sizeof(*current_game), 0);
BFile_Read(fd, current_game, sizeof(struct game), 0);
// Overwritten struct plane* planes
for (int i = 0; i < NB_PLANES; i ++)
{
current_game->planes[i] = new_planes[i];
}
// Loa
for (int i = 0; i < NB_PLANES; i ++)
{
BFile_Read(fd, current_game->planes[i], sizeof(struct plane), -1);
@ -60,20 +70,19 @@ void read_save(struct game *current_game)
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;
const int planes_size = NB_PLANES * sizeof(struct plane);
int size = sizeof(*current_game) + planes_size + data_size;
// Remove the old savefile
BFile_Remove(filename);
// Create a new one
const int data_size = current_game->grid.width * current_game->grid.height;
int size = sizeof(struct game) + NB_PLANES * sizeof(struct plane) + data_size;
BFile_Create(filename, BFile_File, &size);
int fd = BFile_Open(filename, BFile_WriteOnly);
// Write data
BFile_Write(fd, current_game, sizeof(*current_game));
for (int i = 0; current_game->planes[i] ; i ++)
BFile_Write(fd, current_game, sizeof(struct game));
for (int i = 0; i < NB_PLANES ; i ++)
{
BFile_Write(fd, current_game->planes[i], sizeof(struct plane));
}