2 #include "../ui/menu.h"
3 #include "../ui/browser.h"
5 #include <drivers/fatfs/ff.h>
7 #define CREDITS_FNAME "credits.txt"
8 #define POSITION_FNAME "selected.dat"
9 #define PROGRESS_FNAME "progress.dat"
13 LEVELDESCRIPTION_MAX
= 14,
14 MENU_BUFLEN
= LEVELDESCRIPTION_MAX
+ 1 + LEVELFILE_MAX
+ 1
24 static uint8_t read_byte_from_file(char const *fname
) {
28 if(FR_OK
== f_chdir(JUMPNRUN_PATH
) &&
29 FR_OK
== f_open(&fd
, fname
, FA_OPEN_EXISTING
| FA_READ
)) {
31 f_read(&fd
, &x
, sizeof(x
), &bytes
);
38 static void save_byte_to_file(char const *fname
, uint8_t x
) {
41 if(FR_OK
== f_chdir(JUMPNRUN_PATH
) &&
42 FR_OK
== f_open(&fd
, fname
, FA_CREATE_ALWAYS
| FA_WRITE
)) {
44 f_write(&fd
, &x
, sizeof(x
), &bytes
);
49 static uint8_t jumpnrun_load_selected(void ) { return read_byte_from_file(POSITION_FNAME
); }
50 static void jumpnrun_save_selected(uint8_t selected
) { save_byte_to_file (POSITION_FNAME
, selected
); }
52 static uint8_t jumpnrun_load_progress(void ) { return read_byte_from_file(PROGRESS_FNAME
); }
53 static void jumpnrun_save_progress(uint8_t progress
) { save_byte_to_file (PROGRESS_FNAME
, progress
); }
55 static uint8_t jumpnrun_pick_level_from_fd(char *buf
, uint8_t *first_visible
, uint8_t *selected
, uint8_t progress
, FIL
*fd
) {
56 unsigned levelcount
= 0;
59 char buf
[MENU_BUFLEN
];
60 while(f_gets(buf
, MENU_BUFLEN
, fd
) && levelcount
<= progress
) {
65 if(FR_OK
!= f_lseek(fd
, 0)) {
66 return JUMPNRUN_ERROR
;
69 uint8_t menulen
= levelcount
+ (levelcount
<= progress
) + 1;
71 char menu_buf
[menulen
][MENU_BUFLEN
];
72 char const *menu_index
[menulen
];
73 char const *fnames
[menulen
];
76 for(i
= 0; i
< levelcount
&& f_gets(menu_buf
[i
], MENU_BUFLEN
, fd
); ++i
) {
77 menu_index
[i
] = menu_buf
[i
];
79 for(p
= menu_buf
[i
]; *p
&& *p
!= '|'; ++p
)
87 uint8_t creditspos
= -1;
90 if(levelcount
<= progress
) {
92 strcpy(menu_buf
[creditspos
], "Credits");
93 menu_index
[creditspos
] = menu_buf
[creditspos
];
98 strcpy(menu_buf
[exitpos
], "Zurück");
99 menu_index
[exitpos
] = menu_buf
[exitpos
];
101 uint8_t choice
= badge_menu(menu_index
, exitpos
+ 1, first_visible
, *selected
);
103 if(choice
== exitpos
) {
109 if(choice
== creditspos
) {
110 return CHOICE_CREDITS
;
113 strncpy(buf
, fnames
[*selected
], LEVELFILE_MAX
);
114 buf
[LEVELFILE_MAX
] = '\0';
119 static uint8_t jumpnrun_pick_level(char *buf
, uint8_t *first_visible
, uint8_t *selected
, uint8_t progress
) {
122 if(FR_OK
!= f_chdir(JUMPNRUN_PATH
) ||
123 FR_OK
!= f_open(&fd
, "levels.lst", FA_OPEN_EXISTING
| FA_READ
)) {
124 return JUMPNRUN_ERROR
;
127 uint8_t err
= jumpnrun_pick_level_from_fd(buf
, first_visible
, selected
, progress
, &fd
);
134 void jumpnrun_play(void) {
135 char buf
[LEVELFILE_MAX
+ 1];
136 uint8_t selected
= jumpnrun_load_selected();
137 uint8_t progress
= jumpnrun_load_progress();
138 uint8_t first_visible
= selected
;
140 uint8_t oldselected
= selected
;
143 if(oldselected
!= selected
) {
144 jumpnrun_save_selected((uint8_t) selected
);
145 oldselected
= selected
;
148 choice
= jumpnrun_pick_level(buf
, &first_visible
, &selected
, progress
);
152 if(JUMPNRUN_WON
== jumpnrun_play_level(buf
) && selected
== progress
) {
153 selected
= ++progress
;
154 jumpnrun_save_progress(progress
);
158 badge_browse_textfile(CREDITS_FNAME
);
161 } while(choice
!= CHOICE_EXIT
);
This page took 0.062373 seconds and 5 git commands to generate.