From c39bf81f1820600053271e037891e614f67c9646 Mon Sep 17 00:00:00 2001 From: Wintermute Date: Mon, 21 Oct 2013 20:27:24 +0200 Subject: [PATCH] Levelauswahl auf Badge. --- Makefile | 1 + badge/jumpnrun/jumpnrun.c | 2 +- badge/jumpnrun/jumpnrun.h | 3 +- badge/jumpnrun/starter.c | 70 +++++++++++++++++++++++++++++++++++++++ badge/main.c | 4 ++- drivers/fatfs/ffconf.h | 2 +- mock/badge_main_loop.c | 2 +- 7 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 badge/jumpnrun/starter.c diff --git a/Makefile b/Makefile index c649bd7..b6d9f6a 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,7 @@ SRCS = \ badge/jumpnrun/player.c \ badge/jumpnrun/render.c \ badge/jumpnrun/shots.c \ + badge/jumpnrun/starter.c \ badge/jumpnrun/stats.c \ badge/jumpnrun/tiles.c \ badge/ui/display.c \ diff --git a/badge/jumpnrun/jumpnrun.c b/badge/jumpnrun/jumpnrun.c index e859503..0ebc9df 100644 --- a/badge/jumpnrun/jumpnrun.c +++ b/badge/jumpnrun/jumpnrun.c @@ -237,7 +237,7 @@ void jumpnrun_level_tick(jumpnrun_level *lv, } } -uint8_t jumpnrun_play(char const *lvname) { +uint8_t jumpnrun_play_level(char const *lvname) { jumpnrun_level lv; JUMPNRUN_LEVEL_LOAD(lv, lvname); diff --git a/badge/jumpnrun/jumpnrun.h b/badge/jumpnrun/jumpnrun.h index 5c4024b..7890889 100644 --- a/badge/jumpnrun/jumpnrun.h +++ b/badge/jumpnrun/jumpnrun.h @@ -30,6 +30,7 @@ void jumpnrun_apply_gravity(vec2d *inertia); void jumpnrun_passive_movement(vec2d *inertia); badge_sprite const *jumpnrun_hacker_symbol(void); -uint8_t jumpnrun_play(char const *lvname); +uint8_t jumpnrun_play_level(char const *lvname); +void jumpnrun_play(void); #endif diff --git a/badge/jumpnrun/starter.c b/badge/jumpnrun/starter.c new file mode 100644 index 0000000..ca98dc0 --- /dev/null +++ b/badge/jumpnrun/starter.c @@ -0,0 +1,70 @@ +#include "jumpnrun.h" +#include "../ui/menu.h" + +#include + +enum { + LEVELFILE_MAX = 12, + LEVELDESCRIPTION_MAX = 14, + MENU_BUFLEN = LEVELDESCRIPTION_MAX + 1 + LEVELFILE_MAX + 1 +}; + +static uint8_t jumpnrun_pick_level_from_fd(char *buf, size_t *first_visible, size_t *selected, FIL *fd) { + unsigned levelcount = 0; + + { + char buf[MENU_BUFLEN]; + while(f_gets(buf, MENU_BUFLEN, fd)) { + ++levelcount; + } + } + + if(FR_OK != f_lseek(fd, 0)) { + return JUMPNRUN_ERROR; + } + + char menu_buf[levelcount][MENU_BUFLEN]; + char const *menu_index[levelcount]; + char const *fnames[levelcount]; + unsigned i; + + for(i = 0; i < levelcount && f_gets(menu_buf[i], MENU_BUFLEN, fd); ++i) { + menu_index[i] = menu_buf[i]; + char *p; + for(p = menu_buf[i]; *p && *p != '|'; ++p) + ; + if(*p) { + *p++ = '\0'; + } + fnames[i] = p; + } + + *selected = badge_menu(menu_index, i, first_visible, *selected); + + strcpy(buf, fnames[*selected]); + return 0; +} + +static uint8_t jumpnrun_pick_level(char *buf, size_t *first_visible, size_t *selected) { + FIL fd; + + if(FR_OK != f_open(&fd, "levels.lst", FA_OPEN_EXISTING | FA_READ)) { + return JUMPNRUN_ERROR; + } + + uint8_t err = jumpnrun_pick_level_from_fd(buf, first_visible, selected, &fd); + + f_close(&fd); + + return err; +} + +void jumpnrun_play(void) { + char buf[LEVELFILE_MAX + 1]; + size_t first_visible = 0; + size_t selected = 0; + + while(0 == jumpnrun_pick_level(buf, &first_visible, &selected)) { + jumpnrun_play_level(buf); + } +} diff --git a/badge/main.c b/badge/main.c index 25a4356..852cb6b 100644 --- a/badge/main.c +++ b/badge/main.c @@ -243,8 +243,10 @@ int main(void) badge_event_start(); + jumpnrun_play(); + for(;;) { - if(JUMPNRUN_ERROR == jumpnrun_play("smb.lvl")) { + if(JUMPNRUN_ERROR == jumpnrun_play_level("smb.lvl")) { break; } } diff --git a/drivers/fatfs/ffconf.h b/drivers/fatfs/ffconf.h index 1ffe18b..440605e 100644 --- a/drivers/fatfs/ffconf.h +++ b/drivers/fatfs/ffconf.h @@ -37,7 +37,7 @@ / 3: f_lseek is removed in addition to level 2. */ -#define _USE_STRFUNC 0 /* 0, 1 or 2 */ +#define _USE_STRFUNC 1 /* 0, 1 or 2 */ /* To enable string functions, set _USE_STRFUNC to 1 or 2. */ diff --git a/mock/badge_main_loop.c b/mock/badge_main_loop.c index 86a72b1..41d4113 100644 --- a/mock/badge_main_loop.c +++ b/mock/badge_main_loop.c @@ -38,6 +38,6 @@ void badge_main_loop(void) { char lvname[256]; sprintf(lvname, "../badge/jumpnrun/%s.lvl", menu[choice]); - jumpnrun_play(lvname); + jumpnrun_play_level(lvname); } } -- 2.20.1