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) {
18 if(FR_OK
== f_open(&fd
, PROGRESS_FNAME
, FA_OPEN_EXISTING
| FA_READ
)) {
20 f_read(&fd
, &progress
, sizeof(progress
), &bytes
);
27 static void jumpnrun_save_progress(uint8_t progress
) {
30 if(FR_OK
== f_open(&fd
, PROGRESS_FNAME
, FA_CREATE_ALWAYS
| FA_WRITE
)) {
32 f_write(&fd
, &progress
, sizeof(progress
), &bytes
);
37 static uint8_t jumpnrun_pick_level_from_fd(char *buf
, size_t *first_visible
, size_t *selected
, uint8_t progress
, FIL
*fd
) {
38 unsigned levelcount
= 0;
41 char buf
[MENU_BUFLEN
];
42 while(f_gets(buf
, MENU_BUFLEN
, fd
) && levelcount
<= progress
) {
47 if(FR_OK
!= f_lseek(fd
, 0)) {
48 return JUMPNRUN_ERROR
;
51 char menu_buf
[levelcount
+ 1][MENU_BUFLEN
];
52 char const *menu_index
[levelcount
+ 1];
53 char const *fnames
[levelcount
+ 1];
56 for(i
= 0; i
< levelcount
&& f_gets(menu_buf
[i
], MENU_BUFLEN
, fd
); ++i
) {
57 menu_index
[i
] = menu_buf
[i
];
59 for(p
= menu_buf
[i
]; *p
&& *p
!= '|'; ++p
)
67 strcpy(menu_buf
[i
], "exit");
68 menu_index
[i
] = menu_buf
[i
];
69 size_t choice
= badge_menu(menu_index
, i
+ 1, first_visible
, *selected
);
71 if(choice
== levelcount
) {
76 strcpy(buf
, fnames
[*selected
]);
80 static uint8_t jumpnrun_pick_level(char *buf
, size_t *first_visible
, size_t *selected
, uint8_t progress
) {
83 if(FR_OK
!= f_open(&fd
, "levels.lst", FA_OPEN_EXISTING
| FA_READ
)) {
84 return JUMPNRUN_ERROR
;
87 uint8_t err
= jumpnrun_pick_level_from_fd(buf
, first_visible
, selected
, progress
, &fd
);
94 void jumpnrun_play(void) {
95 char buf
[LEVELFILE_MAX
+ 1];
96 size_t first_visible
= 0;
98 uint8_t progress
= jumpnrun_load_progress();
100 while(0 == jumpnrun_pick_level(buf
, &first_visible
, &selected
, progress
)) {
101 if(JUMPNRUN_WON
== jumpnrun_play_level(buf
) && selected
== progress
) {
102 selected
= ++progress
;
103 jumpnrun_save_progress(progress
);
This page took 0.05672 seconds and 5 git commands to generate.