0645487291e549a1f4f623303a1cf57b883a994f
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 strcpy(buf
, fnames
[*selected
]);
83 static uint8_t jumpnrun_pick_level(char *buf
, size_t *first_visible
, size_t *selected
, uint8_t progress
) {
86 if(FR_OK
!= f_chdir(JUMPNRUN_PATH
) ||
87 FR_OK
!= f_open(&fd
, "levels.lst", FA_OPEN_EXISTING
| FA_READ
)) {
88 return JUMPNRUN_ERROR
;
91 uint8_t err
= jumpnrun_pick_level_from_fd(buf
, first_visible
, selected
, progress
, &fd
);
98 void jumpnrun_play(void) {
99 char buf
[LEVELFILE_MAX
+ 1];
100 size_t first_visible
= 0;
102 uint8_t progress
= jumpnrun_load_progress();
104 while(0 == jumpnrun_pick_level(buf
, &first_visible
, &selected
, progress
)) {
105 if(JUMPNRUN_WON
== jumpnrun_play_level(buf
) && selected
== progress
) {
106 selected
= ++progress
;
107 jumpnrun_save_progress(progress
);
This page took 0.040503 seconds and 3 git commands to generate.