jumprun_tile als packed struct für mehr Speicher.
[hackover2013-badge-firmware.git] / badge / jumpnrun / starter.c
1 #include "jumpnrun.h"
2 #include "../ui/menu.h"
3
4 #include <drivers/fatfs/ff.h>
5
6 enum {
7 LEVELFILE_MAX = 12,
8 LEVELDESCRIPTION_MAX = 14,
9 MENU_BUFLEN = LEVELDESCRIPTION_MAX + 1 + LEVELFILE_MAX + 1
10 };
11
12 static uint8_t jumpnrun_pick_level_from_fd(char *buf, size_t *first_visible, size_t *selected, FIL *fd) {
13 unsigned levelcount = 0;
14
15 {
16 char buf[MENU_BUFLEN];
17 while(f_gets(buf, MENU_BUFLEN, fd)) {
18 ++levelcount;
19 }
20 }
21
22 if(FR_OK != f_lseek(fd, 0)) {
23 return JUMPNRUN_ERROR;
24 }
25
26 char menu_buf[levelcount][MENU_BUFLEN];
27 char const *menu_index[levelcount];
28 char const *fnames[levelcount];
29 unsigned i;
30
31 for(i = 0; i < levelcount && f_gets(menu_buf[i], MENU_BUFLEN, fd); ++i) {
32 menu_index[i] = menu_buf[i];
33 char *p;
34 for(p = menu_buf[i]; *p && *p != '|'; ++p)
35 ;
36 if(*p) {
37 *p++ = '\0';
38 }
39 fnames[i] = p;
40 }
41
42 *selected = badge_menu(menu_index, i, first_visible, *selected);
43
44 strcpy(buf, fnames[*selected]);
45 return 0;
46 }
47
48 static uint8_t jumpnrun_pick_level(char *buf, size_t *first_visible, size_t *selected) {
49 FIL fd;
50
51 if(FR_OK != f_open(&fd, "levels.lst", FA_OPEN_EXISTING | FA_READ)) {
52 return JUMPNRUN_ERROR;
53 }
54
55 uint8_t err = jumpnrun_pick_level_from_fd(buf, first_visible, selected, &fd);
56
57 f_close(&fd);
58
59 return err;
60 }
61
62 void jumpnrun_play(void) {
63 char buf[LEVELFILE_MAX + 1];
64 size_t first_visible = 0;
65 size_t selected = 0;
66
67 while(0 == jumpnrun_pick_level(buf, &first_visible, &selected)) {
68 jumpnrun_play_level(buf);
69 }
70 }
This page took 0.048642 seconds and 5 git commands to generate.