2 #include "../ui/menu.h"
4 #include <drivers/fatfs/ff.h>
8 LEVELDESCRIPTION_MAX
= 14,
9 MENU_BUFLEN
= LEVELDESCRIPTION_MAX
+ 1 + LEVELFILE_MAX
+ 1
12 #define PROGRESS_FNAME "progress.dat"
14 static uint8_t jumpnrun_load_progress(void) {
19 if(FR_OK
== f_chdir(JUMPNRUN_PATH
) &&
20 FR_OK
== f_open(&fd
, PROGRESS_FNAME
, FA_OPEN_EXISTING
| FA_READ
)) {
22 f_read(&fd
, &progress
, sizeof(progress
), &bytes
);
29 static void jumpnrun_save_progress(uint8_t progress
) {
32 if(FR_OK
== f_chdir(JUMPNRUN_PATH
) &&
33 FR_OK
== f_open(&fd
, PROGRESS_FNAME
, FA_CREATE_ALWAYS
| FA_WRITE
)) {
35 f_write(&fd
, &progress
, sizeof(progress
), &bytes
);
40 static uint8_t jumpnrun_pick_level_from_fd(char *buf
, size_t *first_visible
, size_t *selected
, uint8_t progress
, FIL
*fd
) {
41 unsigned levelcount
= 0;
44 char buf
[MENU_BUFLEN
];
45 while(f_gets(buf
, MENU_BUFLEN
, fd
) && levelcount
<= progress
) {
50 if(FR_OK
!= f_lseek(fd
, 0)) {
51 return JUMPNRUN_ERROR
;
54 char menu_buf
[levelcount
+ 1][MENU_BUFLEN
];
55 char const *menu_index
[levelcount
+ 1];
56 char const *fnames
[levelcount
+ 1];
59 for(i
= 0; i
< levelcount
&& f_gets(menu_buf
[i
], MENU_BUFLEN
, fd
); ++i
) {
60 menu_index
[i
] = menu_buf
[i
];
62 for(p
= menu_buf
[i
]; *p
&& *p
!= '|'; ++p
)
70 strcpy(menu_buf
[i
], "exit");
71 menu_index
[i
] = menu_buf
[i
];
72 size_t choice
= badge_menu(menu_index
, i
+ 1, first_visible
, *selected
);
74 if(choice
== levelcount
) {
79 strncpy(buf
, fnames
[*selected
], LEVELFILE_MAX
);
80 buf
[LEVELFILE_MAX
] = '\0';
84 static uint8_t jumpnrun_pick_level(char *buf
, size_t *first_visible
, size_t *selected
, uint8_t progress
) {
87 if(FR_OK
!= f_chdir(JUMPNRUN_PATH
) ||
88 FR_OK
!= f_open(&fd
, "levels.lst", FA_OPEN_EXISTING
| FA_READ
)) {
89 return JUMPNRUN_ERROR
;
92 uint8_t err
= jumpnrun_pick_level_from_fd(buf
, first_visible
, selected
, progress
, &fd
);
99 void jumpnrun_play(void) {
100 char buf
[LEVELFILE_MAX
+ 1];
101 size_t first_visible
= 0;
103 uint8_t progress
= jumpnrun_load_progress();
105 while(0 == jumpnrun_pick_level(buf
, &first_visible
, &selected
, progress
)) {
106 if(JUMPNRUN_WON
== jumpnrun_play_level(buf
) && selected
== progress
) {
107 selected
= ++progress
;
108 jumpnrun_save_progress(progress
);
This page took 0.046406 seconds and 5 git commands to generate.