Save system

This commit is contained in:
Shadow15510 2021-06-03 15:48:38 +02:00
parent 27ab144ea7
commit 450fd42512
5 changed files with 65 additions and 6 deletions

View File

@ -16,6 +16,7 @@ set(SOURCES
src/mutation_engine.c
src/data.c
src/epidemic_engine.c
src/save.c
# ...
)
# Shared assets, fx-9860G-only assets and fx-CG-50-only assets

Binary file not shown.

View File

@ -17,7 +17,7 @@
#include "core.h"
#include "display_engine.h"
#include "mutation_engine.h"
#include "save.h"
// title_screen : display the title screen
static void title_screen(void);
@ -41,6 +41,7 @@ int main(void)
struct plane plane_4 = {104, 20, 3, 104, 50, 104, 20};
struct plane plane_5 = {68, 44, 4, 34, 44, 68, 44};
struct game current_game =
{
.contagion = 0,
@ -68,17 +69,15 @@ int main(void)
/* allocate memory */
current_game.grid.data = calloc(current_game.grid.width * current_game.grid.height, sizeof(uint8_t));
if (current_game.grid.data == NULL)
{
const char *msg[5] = {"CALLOC", "FAILED", "", "", ""};
message(msg);
}
read_save(&current_game);
current_game.grid.data[95 + 20 * current_game.grid.width] = 1;
current_game.humans[0] = (current_game.grid.width * current_game.grid.height) - 1;
main_loop(&current_game);
write_save(&current_game);
/* free memory */
free(current_game.grid.data);

47
src/save.c Normal file
View File

@ -0,0 +1,47 @@
#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);
}

12
src/save.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef _SAVE_H
#define _SAVE_H
#include "core.h"
// read_save : read or create a new save
void read_save(struct game *current_game);
// write_save : write the savefile
void write_save(const struct game *current_game);
#endif /* _SAVE_H */