2 #include "../ui/menu.h"
3 #include "../ui/browser.h"
5 #include <drivers/fatfs/ff.h>
7 #define CREDITSFILE "credits.txt"
11 LEVELDESCRIPTION_MAX
= 14,
12 MENU_BUFLEN
= LEVELDESCRIPTION_MAX
+ 1 + LEVELFILE_MAX
+ 1
22 #define PROGRESS_FNAME "progress.dat"
24 static uint8_t jumpnrun_load_progress(void) {
29 if(FR_OK
== f_chdir(JUMPNRUN_PATH
) &&
30 FR_OK
== f_open(&fd
, PROGRESS_FNAME
, FA_OPEN_EXISTING
| FA_READ
)) {
32 f_read(&fd
, &progress
, sizeof(progress
), &bytes
);
39 static void jumpnrun_save_progress(uint8_t progress
) {
42 if(FR_OK
== f_chdir(JUMPNRUN_PATH
) &&
43 FR_OK
== f_open(&fd
, PROGRESS_FNAME
, FA_CREATE_ALWAYS
| FA_WRITE
)) {
45 f_write(&fd
, &progress
, sizeof(progress
), &bytes
);
50 static uint8_t jumpnrun_pick_level_from_fd(char *buf
, size_t *first_visible
, size_t *selected
, uint8_t progress
, FIL
*fd
) {
51 unsigned levelcount
= 0;
54 char buf
[MENU_BUFLEN
];
55 while(f_gets(buf
, MENU_BUFLEN
, fd
) && levelcount
<= progress
) {
60 if(FR_OK
!= f_lseek(fd
, 0)) {
61 return JUMPNRUN_ERROR
;
64 size_t menulen
= levelcount
+ (levelcount
<= progress
) + 1;
66 char menu_buf
[menulen
][MENU_BUFLEN
];
67 char const *menu_index
[menulen
];
68 char const *fnames
[menulen
];
71 for(i
= 0; i
< levelcount
&& f_gets(menu_buf
[i
], MENU_BUFLEN
, fd
); ++i
) {
72 menu_index
[i
] = menu_buf
[i
];
74 for(p
= menu_buf
[i
]; *p
&& *p
!= '|'; ++p
)
82 size_t creditspos
= -1;
85 if(levelcount
<= progress
) {
87 strcpy(menu_buf
[creditspos
], "Credits");
88 menu_index
[creditspos
] = menu_buf
[creditspos
];
93 strcpy(menu_buf
[exitpos
], "Zurück");
94 menu_index
[exitpos
] = menu_buf
[exitpos
];
96 size_t choice
= badge_menu(menu_index
, exitpos
+ 1, first_visible
, *selected
);
98 if(choice
== exitpos
) {
104 if(choice
== creditspos
) {
105 return CHOICE_CREDITS
;
108 strncpy(buf
, fnames
[*selected
], LEVELFILE_MAX
);
109 buf
[LEVELFILE_MAX
] = '\0';
114 static uint8_t jumpnrun_pick_level(char *buf
, size_t *first_visible
, size_t *selected
, uint8_t progress
) {
117 if(FR_OK
!= f_chdir(JUMPNRUN_PATH
) ||
118 FR_OK
!= f_open(&fd
, "levels.lst", FA_OPEN_EXISTING
| FA_READ
)) {
119 return JUMPNRUN_ERROR
;
122 uint8_t err
= jumpnrun_pick_level_from_fd(buf
, first_visible
, selected
, progress
, &fd
);
129 void jumpnrun_play(void) {
130 char buf
[LEVELFILE_MAX
+ 1];
131 size_t first_visible
= 0;
133 uint8_t progress
= jumpnrun_load_progress();
137 choice
= jumpnrun_pick_level(buf
, &first_visible
, &selected
, progress
);
141 if(JUMPNRUN_WON
== jumpnrun_play_level(buf
) && selected
== progress
) {
142 selected
= ++progress
;
143 jumpnrun_save_progress(progress
);
147 badge_browse_textfile(CREDITSFILE
);
150 } while(choice
!= CHOICE_EXIT
);
This page took 0.049216 seconds and 5 git commands to generate.