Levelauswahl auf Badge.
authorWintermute <wintermute@hannover.ccc.de>
Mon, 21 Oct 2013 18:27:24 +0000 (20:27 +0200)
committerWintermute <wintermute@hannover.ccc.de>
Mon, 21 Oct 2013 18:27:24 +0000 (20:27 +0200)
Makefile
badge/jumpnrun/jumpnrun.c
badge/jumpnrun/jumpnrun.h
badge/jumpnrun/starter.c [new file with mode: 0644]
badge/main.c
drivers/fatfs/ffconf.h
mock/badge_main_loop.c

index c649bd7..b6d9f6a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -42,6 +42,7 @@ SRCS = \
   badge/jumpnrun/player.c \\r
   badge/jumpnrun/render.c \\r
   badge/jumpnrun/shots.c \\r
+  badge/jumpnrun/starter.c \\r
   badge/jumpnrun/stats.c \\r
   badge/jumpnrun/tiles.c \\r
   badge/ui/display.c \\r
index e859503..0ebc9df 100644 (file)
@@ -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);
index 5c4024b..7890889 100644 (file)
@@ -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 (file)
index 0000000..ca98dc0
--- /dev/null
@@ -0,0 +1,70 @@
+#include "jumpnrun.h"
+#include "../ui/menu.h"
+
+#include <drivers/fatfs/ff.h>
+
+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);
+  }
+}
index 25a4356..852cb6b 100644 (file)
@@ -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;
     }
   }
index 1ffe18b..440605e 100644 (file)
@@ -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. */
 
 
index 86a72b1..41d4113 100644 (file)
@@ -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);
   }
 }
This page took 0.027506 seconds and 4 git commands to generate.