Save system
This commit is contained in:
parent
27ab144ea7
commit
450fd42512
|
@ -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
|
||||
|
|
BIN
Plague.g1a
BIN
Plague.g1a
Binary file not shown.
11
src/main.c
11
src/main.c
|
@ -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(¤t_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(¤t_game);
|
||||
|
||||
write_save(¤t_game);
|
||||
|
||||
/* free memory */
|
||||
free(current_game.grid.data);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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 */
|
Loading…
Reference in New Issue