2020-08-27 00:31:22 +02:00
|
|
|
#include <gint/bfile.h>
|
|
|
|
#include <gint/gint.h>
|
|
|
|
#include "define.h"
|
2020-08-28 00:36:42 +02:00
|
|
|
#include "drawlevel.h"
|
2020-08-27 00:31:22 +02:00
|
|
|
int retcode;
|
2021-03-22 00:18:54 +01:00
|
|
|
|
2021-03-21 23:39:09 +01:00
|
|
|
int id_leveltab = 0;
|
|
|
|
int times[LEVEL_MAX];
|
|
|
|
static const uint16_t *filepath = u"\\\\fls0\\AST3.sav";
|
2020-08-28 00:36:42 +02:00
|
|
|
|
2021-03-21 23:39:09 +01:00
|
|
|
/* savefile() write the time of all the player's times in the savefile.
|
|
|
|
* savefile() is long to execute. That's why it must be call once per
|
|
|
|
* new record only to avoid loading times. */
|
2020-08-27 00:31:22 +02:00
|
|
|
void savefile(void)
|
|
|
|
{
|
|
|
|
int descriptor;
|
|
|
|
|
|
|
|
descriptor = BFile_Open(filepath, BFile_WriteOnly);
|
2021-03-21 23:39:09 +01:00
|
|
|
BFile_Write(descriptor, times, sizeof(times));
|
2020-08-27 00:31:22 +02:00
|
|
|
BFile_Close(descriptor);
|
|
|
|
}
|
|
|
|
|
2021-03-21 23:39:09 +01:00
|
|
|
/* loadfile() is only called by the function loadtime() because
|
|
|
|
* loadtime() set the value of "id_leveltab". It store in the global retcode
|
|
|
|
* the time of the player. This function is called once per level in the
|
|
|
|
* level selection menu to avoid loading time. */
|
2020-08-27 00:31:22 +02:00
|
|
|
void loadfile(void)
|
|
|
|
{
|
|
|
|
int descriptor;
|
|
|
|
descriptor = BFile_Open(filepath, BFile_ReadOnly);
|
2021-03-21 23:39:09 +01:00
|
|
|
BFile_Read(descriptor, &retcode,
|
|
|
|
sizeof(times[0]), sizeof(times[0]) * id_leveltab);
|
2020-08-27 00:31:22 +02:00
|
|
|
BFile_Close(descriptor);
|
|
|
|
}
|
|
|
|
|
2021-03-21 23:39:09 +01:00
|
|
|
/* savetimes() is call when the player has reached the end of a level.
|
|
|
|
* If his time is better than the save time, it will call the function
|
|
|
|
* savefile() else, nothing append to avoid loading time. */
|
2020-08-27 00:31:22 +02:00
|
|
|
void savetimes(float framelevel, int id_level)
|
|
|
|
{
|
2021-03-21 23:39:09 +01:00
|
|
|
if(times[id_level - 1] > (int)(framelevel / FPS * 100) ||
|
|
|
|
times[id_level - 1] == 0)
|
2020-08-27 00:31:22 +02:00
|
|
|
{
|
2021-03-21 23:39:09 +01:00
|
|
|
times[id_level - 1] = (int)(framelevel / FPS * 100);
|
2020-08-28 00:36:42 +02:00
|
|
|
draw_end((int)framelevel, id_level, 1);
|
2020-08-27 00:31:22 +02:00
|
|
|
gint_switch(savefile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 23:39:09 +01:00
|
|
|
/* restore() is call when the app is load, it will create the save file
|
|
|
|
* if it doesn't exist. If the file exist, it will read all times from
|
|
|
|
* the save and store them in the global times[] */
|
|
|
|
void restore(void)
|
2020-08-27 00:31:22 +02:00
|
|
|
{
|
|
|
|
struct BFile_FileInfo fileInfo;
|
|
|
|
int handle;
|
|
|
|
uint16_t foundpath[30];
|
2021-03-21 23:39:09 +01:00
|
|
|
int size = sizeof(times);
|
2020-08-27 00:31:22 +02:00
|
|
|
int descriptor;
|
|
|
|
char checkfile = BFile_FindFirst(filepath, &handle, foundpath, &fileInfo);
|
|
|
|
BFile_FindClose(handle);
|
2021-03-21 23:39:09 +01:00
|
|
|
if(checkfile == -1)
|
|
|
|
BFile_Create(filepath, BFile_File, &size);
|
|
|
|
else {
|
2020-08-27 00:31:22 +02:00
|
|
|
descriptor = BFile_Open(filepath, BFile_ReadOnly);
|
2021-03-21 23:39:09 +01:00
|
|
|
BFile_Read(descriptor, times, sizeof(times), 0);
|
2020-08-27 00:31:22 +02:00
|
|
|
BFile_Close(descriptor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 23:39:09 +01:00
|
|
|
/* loadtime() is changing the current value of the global "id_leveltab" to
|
|
|
|
* set it to the current level then it execute loadfile and read and
|
|
|
|
* stock in the global retcode 4 bytes at the place 4*level (times are in
|
|
|
|
* 4 bytes) */
|
|
|
|
int loadtime(int idlevel)
|
2020-08-27 00:31:22 +02:00
|
|
|
{
|
2021-03-21 23:39:09 +01:00
|
|
|
id_leveltab = idlevel;
|
2020-08-27 00:31:22 +02:00
|
|
|
gint_switch(loadfile);
|
|
|
|
return (int)retcode;
|
|
|
|
}
|