Merge branch 'master' of gitlab:wintermute/hackover2013-badge-firmware
[hackover2013-badge-firmware.git] / badge / jumpnrun / starter.c
index 146e85c..d35c5fa 100644 (file)
@@ -15,9 +15,12 @@ static uint8_t jumpnrun_load_progress(void) {
   uint8_t progress = 0;
   FIL fd;
 
-  if(FR_OK == f_open(&fd, PROGRESS_FNAME, FA_OPEN_EXISTING | FA_READ)) {
+
+  if(FR_OK == f_chdir(JUMPNRUN_PATH) &&
+     FR_OK == f_open(&fd, PROGRESS_FNAME, FA_OPEN_EXISTING | FA_READ)) {
     UINT bytes;
     f_read(&fd, &progress, sizeof(progress), &bytes);
+    f_close(&fd);
   }
 
   return progress;
@@ -26,9 +29,11 @@ static uint8_t jumpnrun_load_progress(void) {
 static void jumpnrun_save_progress(uint8_t progress) {
   FIL fd;
 
-  if(FR_OK == f_open(&fd, PROGRESS_FNAME, FA_CREATE_NEW | FA_WRITE)) {
+  if(FR_OK == f_chdir(JUMPNRUN_PATH) &&
+     FR_OK == f_open(&fd, PROGRESS_FNAME, FA_CREATE_ALWAYS | FA_WRITE)) {
     UINT bytes;
     f_write(&fd, &progress, sizeof(progress), &bytes);
+    f_close(&fd);
   }
 }
 
@@ -46,9 +51,9 @@ static uint8_t jumpnrun_pick_level_from_fd(char *buf, size_t *first_visible, siz
     return JUMPNRUN_ERROR;
   }
 
-  char menu_buf[levelcount][MENU_BUFLEN];
-  char const *menu_index[levelcount];
-  char const *fnames[levelcount];
+  char menu_buf[levelcount + 1][MENU_BUFLEN];
+  char const *menu_index[levelcount + 1];
+  char const *fnames[levelcount + 1];
   unsigned i;
 
   for(i = 0; i < levelcount && f_gets(menu_buf[i], MENU_BUFLEN, fd); ++i) {
@@ -62,16 +67,25 @@ static uint8_t jumpnrun_pick_level_from_fd(char *buf, size_t *first_visible, siz
     fnames[i] = p;
   }
 
-  *selected = badge_menu(menu_index, i, first_visible, *selected);
+  strcpy(menu_buf[i], "exit");
+  menu_index[i] = menu_buf[i];
+  size_t choice = badge_menu(menu_index, i + 1, first_visible, *selected);
+
+  if(choice == levelcount) {
+    return 1; // exit
+  }
 
-  strcpy(buf, fnames[*selected]);
+  *selected = choice;
+  strncpy(buf, fnames[*selected], LEVELFILE_MAX);
+  buf[LEVELFILE_MAX] = '\0';
   return 0;
 }
 
 static uint8_t jumpnrun_pick_level(char *buf, size_t *first_visible, size_t *selected, uint8_t progress) {
   FIL fd;
 
-  if(FR_OK != f_open(&fd, "levels.lst", FA_OPEN_EXISTING | FA_READ)) {
+  if(FR_OK != f_chdir(JUMPNRUN_PATH) ||
+     FR_OK != f_open(&fd, "levels.lst", FA_OPEN_EXISTING | FA_READ)) {
     return JUMPNRUN_ERROR;
   }
 
@@ -90,7 +104,7 @@ void jumpnrun_play(void) {
 
   while(0 == jumpnrun_pick_level(buf, &first_visible, &selected, progress)) {
     if(JUMPNRUN_WON == jumpnrun_play_level(buf) && selected == progress) {
-      ++progress;
+      selected = ++progress;
       jumpnrun_save_progress(progress);
     }
   }
This page took 0.019436 seconds and 4 git commands to generate.