From 39ac475bc132eadb905246fba3e4b50be0f6314b Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Thu, 8 Jun 2023 22:26:12 +0100 Subject: [PATCH] Switch to graphics for main menu --- src/menu/menu_main.c | 108 +++++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 49 deletions(-) diff --git a/src/menu/menu_main.c b/src/menu/menu_main.c index 16756a2e..6a46548a 100644 --- a/src/menu/menu_main.c +++ b/src/menu/menu_main.c @@ -25,12 +25,16 @@ static FILINFO current_fileinfo; void load_n64_rom() { + console_init(); console_clear(); - char tmp_buffer[280]; - sprintf(tmp_buffer, "sd:/%s", current_fileinfo.fname); + char sd_path_buffer[1024]; + sprintf(sd_path_buffer, "sd:/%s", current_fileinfo.fname); - rom_header_t temp_header = file_read_rom_header(tmp_buffer); + rom_header_t temp_header = file_read_rom_header(sd_path_buffer); + + printf("Loading N64 ROM type...\n"); + printf("%s\n\n", current_fileinfo.fname); printf("ROM title: %s\n\n", temp_header.title); @@ -47,22 +51,24 @@ void load_n64_rom() { uint8_t save_type = rom_db_match_save_type(temp_header); printf("save type: %d\n", save_type); - sprintf(tmp_buffer, "%s.%llu.sav", current_fileinfo.fname, temp_header.checksum); + sprintf(sd_path_buffer, "%s.%llu.sav", current_fileinfo.fname, temp_header.checksum); wait_ms(5000); // wait used for debugging. Can be removed later. - assertf(flashcart_load_save(tmp_buffer, (flashcart_save_type_t)save_type, true) == FLASHCART_OK, "ROM load save error"); + assertf(flashcart_load_save(sd_path_buffer, (flashcart_save_type_t)save_type, true) == FLASHCART_OK, "ROM load save error"); assertf(flashcart_load_rom(current_fileinfo.fname) == FLASHCART_OK, "ROM load error"); } // FIXME: use newlib rather than fatfs to do this! -FRESULT scan_file_path (char *path) { +FRESULT scan_file_path (surface_t *disp, char *path) { FRESULT res; DIR dir; - char sfno[280]; + char sfno[1024]; int counter = 0; //FIXME: we dont account for an empty dir or only one valid file! + int16_t vertical_position = 56; + char str_buffer[1036]; res = f_opendir(&dir, path); @@ -81,20 +87,22 @@ FRESULT scan_file_path (char *path) { } counter ++; - + // TODO: convert these to icons... sprintf(sfno, "| %c%c%c | %10d | %s", ((file_info.fattrib & AM_DIR) ? 'D' : '-'), ((file_info.fattrib & AM_RDO) ? 'R' : '-'), ((file_info.fattrib & AM_HID) ? 'H' : '-'), (int)file_info.fsize, file_info.fname); - + if (scroll_menu_position == counter) { - printf("-> %s\n", sfno); + sprintf(str_buffer, "-> %s\n", sfno); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, str_buffer); current_fileinfo = file_info; } else { - printf(" %s\n", sfno); + sprintf(str_buffer, " %s\n", sfno); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, str_buffer); } } } @@ -105,62 +113,66 @@ FRESULT scan_file_path (char *path) { void menu_main_draw_header(surface_t *disp) { - printf(" FILE MENU\n\n\n\n"); - - // graphics_draw_text(disp, (disp->width / 2) - 36, vertical_start_position, "FILE MENU"); // centre = numchars * font_horizontal_pixels / 2 - // graphics_draw_line(disp,0,30,disp->width,30, 0xff); + graphics_draw_text(disp, (disp->width / 2) - 36, vertical_start_position, "FILE MENU"); // centre = numchars * font_horizontal_pixels / 2 + graphics_draw_line(disp,0,30,disp->width,30, 0xff); } void menu_main_draw_footer(char *dir_path, surface_t *disp) { - // graphics_draw_line(disp,0,disp->height - overscan_vertical_pixels - font_vertical_pixels,disp->width,disp->height - overscan_vertical_pixels - font_vertical_pixels, 0xff); + graphics_draw_line(disp,0,disp->height - overscan_vertical_pixels - font_vertical_pixels,disp->width,disp->height - overscan_vertical_pixels - font_vertical_pixels, 0xff); - printf("\n\nCurrent Directory: %s\n", dir_path); - printf(" File: %d of %d\n\n", scroll_menu_position, items_in_dir); + char str_buffer[1024]; + sprintf(str_buffer, "Current Directory: SD:%s\nFile: %d of %d\n\n", dir_path, scroll_menu_position, items_in_dir); + + graphics_draw_text(disp, (disp->width / 2) - 160,disp->height - overscan_vertical_pixels, str_buffer); // centre = numchars * font_horizontal_pixels / 2 } -void menu_main_refresh (char *dir_path, surface_t *disp) { +void menu_main_refresh (char *dir_path) { - console_clear(); + surface_t *disp = display_get(); + graphics_fill_screen(disp, 0); menu_main_draw_header(disp); - printf(" | DRH | FILE SIZE | FILE NAME\n"); - printf(" |-----|------------|----------\n"); + int16_t vertical_position = 40; + + char str_buffer[1024]; + sprintf(str_buffer, " | DRH | FILE SIZE | FILE NAME\n"); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, str_buffer); + sprintf(str_buffer, " |-----|------------|----------\n"); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, str_buffer); - scan_file_path(dir_path); + scan_file_path(disp, dir_path); menu_main_draw_footer(dir_path, disp); + display_show(disp); + } void menu_main_init (settings_t *settings) { - // TODO: implement nice user interface here - surface_t *disp = display_try_get(); - - console_init(); - console_set_debug(true); - console_clear(); - - char *current_dir = settings->last_state.current_directory; char *last_dir = current_dir; struct controller_data joypad = get_keys_down(); if ((settings->last_state.auto_load_last_rom) && !(joypad.c[0].B)) { // FIXME: the B button on any controller! - printf("Loading last ROM: %s\n", settings->last_rom.rom_path); - assertf(flashcart_load_rom(settings->last_rom.rom_path) == FLASHCART_OK, "ROM load error"); - printf("Loading save: %s, type: %d, writeback: %d\n", settings->last_rom.save_path, settings->last_rom.save_type, settings->last_rom.save_writeback); - assertf(flashcart_load_save(settings->last_rom.save_path, settings->last_rom.save_type, settings->last_rom.save_writeback) == FLASHCART_OK, "Save load error"); + console_init(); + console_clear(); + + printf("Loading last ROM: %s\n", settings->last_rom.rom_path); + assertf(flashcart_load_rom(settings->last_rom.rom_path) == FLASHCART_OK, "ROM load error"); + + printf("Loading save: %s, type: %d, writeback: %d\n", settings->last_rom.save_path, settings->last_rom.save_type, settings->last_rom.save_writeback); + assertf(flashcart_load_save(settings->last_rom.save_path, settings->last_rom.save_type, settings->last_rom.save_writeback) == FLASHCART_OK, "Save load error"); } else { settings->last_state.auto_load_last_rom = false; - menu_main_refresh(current_dir, disp); + menu_main_refresh(current_dir); } for (;;) { @@ -174,7 +186,7 @@ void menu_main_init (settings_t *settings) { else { scroll_menu_position = items_in_dir; } - menu_main_refresh(current_dir, disp); + menu_main_refresh(current_dir); } if (joypad.c[0].down) { @@ -184,14 +196,12 @@ void menu_main_init (settings_t *settings) { else { scroll_menu_position = 1; } - menu_main_refresh(current_dir, disp); + menu_main_refresh(current_dir); } if (joypad.c[0].A) { // TODO: move this to a function and check that the ROM is valid by checking the header... if (str_endswith(current_fileinfo.fname, ".z64") || str_endswith(current_fileinfo.fname, ".n64") || str_endswith(current_fileinfo.fname, ".v64") || str_endswith(current_fileinfo.fname, ".rom")) { - printf("Loading N64 ROM type...\n"); - printf("%s\n", current_fileinfo.fname); load_n64_rom(); @@ -204,31 +214,31 @@ void menu_main_init (settings_t *settings) { scroll_menu_position = 1; last_dir = current_dir; current_dir = current_fileinfo.fname; - menu_main_refresh(current_dir, disp); + menu_main_refresh(current_dir); } else { + + console_init(); + console_clear(); + printf("Failed... Returning to menu...\n"); wait_ms(1000); - menu_main_refresh(current_dir, disp); + menu_main_refresh(current_dir); } } } if (joypad.c[0].B) { - menu_main_refresh(last_dir, disp); + menu_main_refresh(current_dir); current_dir = last_dir; } if (joypad.c[0].start) { // FIXME: the START button on any controller! menu_info(); - menu_main_refresh(current_dir, disp); + menu_main_refresh(current_dir); } if (joypad.c[0].Z) { menu_fileinfo(current_fileinfo); - menu_main_refresh(current_dir, disp); + menu_main_refresh(current_dir); } - if (joypad.c[0].L) { - //load the new File list GUI - //menu_file_scroll(current_fileinfo); - } } // TODO: write menu state to SD card