#include "menu.h" #include #include #include "color.h" #include "game/entry.h" #include "utilities.h" #include "maze_generator.h" #include "file.h" #define MO_LOAD_FROM_FILE 1 #define MO_SAVE_TO_FILE 2 #define MO_GENERATE 3 #define MO_START 4 #define MO_QUIT 5 #define MO_INVALID_OPTION 6 int choose_menu_option() { attron(COLOR_PAIR(CI_DEFAULT)); clear(); printw("1. Load from file.\n"); printw("2. Save to file.\n"); printw("3. Random maze.\n"); printw("4. Start the game.\n"); printw("q. Quit.\n"); printw("Choose:\n"); curs_set(1); echo(); refresh(); //char buffer[8]; //wgetnstr(stdscr, buffer, 8); switch (getch()) { case '1': return MO_LOAD_FROM_FILE; break; case '2': return MO_SAVE_TO_FILE; break; case '3': return MO_GENERATE; break; case '4': return MO_START; break; case 'q': return MO_QUIT; break; } return MO_INVALID_OPTION; } int get_file_path(char *path) { attron(COLOR_PAIR(CI_DEFAULT)); clear(); printw("Enter the path of file:\n"); curs_set(1); echo(); wgetnstr(stdscr, path, 256); return *path == 0 ? -1 : 1; } int save_file_path(char *path) { return get_file_path(path); } int open_file_path(char *path) { return get_file_path(path); } void get_size(int *width, int *height) { attron(COLOR_PAIR(CI_DEFAULT)); while (1) { clear(); printw("Enter a size:\n"); curs_set(1); echo(); char buffer[16]; wgetnstr(stdscr, buffer, 16); char *left, *right; if (find_lr_ints(buffer, &left, &right)) { char *end; *width = strtol(left, &end, 10); printw("\nwidth=%d\n", *width); if (*end == '%') { *width = ((double)*width / 100) * getmaxx(stdscr); if (!is_valid_maze_size(*width)) (*width)--; } *height = strtol(right, &end, 10); printw("\nheight=%d\n", *height); if (*end == '%') { *height = ((double)*height / 100) * getmaxy(stdscr); if (!is_valid_maze_size(*height)) (*height)--; } printw("\nwidth=%d\nheight=%d\n", *width, *height); } if (is_valid_maze_size(*width) && is_valid_maze_size(*height)) return; printw("\nWrong size number!\n"); while (getch() != 10) ; } } void main_menu_loop(struct app *app) { while (1) { switch (choose_menu_option()) { case MO_LOAD_FROM_FILE: { char path[256]; if (open_file_path(path)) { app->maze = maze_from_file(path); if (app->maze == NULL) { printw("\nFile is not exsist!\n"); while (getch() != 10) ; } } } break; case MO_SAVE_TO_FILE: { char path[256]; if (save_file_path(path)) { maze_to_file(path, app->maze); } } break; case MO_GENERATE: { app->maze = malloc(sizeof(struct maze)); get_size(&(app->maze->width), &(app->maze->height)); //maze->width = 41; //maze->height = 21; app->maze->map = malloc(sizeof_2d(app->maze->width, app->maze->height, sizeof(int))); init2d((void **)app->maze->map, app->maze->width, app->maze->height, sizeof(int)); generate_maze(app->maze); } break; case MO_START: { if (app->maze == NULL) { printw("\nThere is no maze!\n"); while (getch() != 10) ; } else if (game(app) == GR_QUIT) { return; } } break; case MO_QUIT: { return; } case MO_INVALID_OPTION: { printw("\nError!\n"); while (getch() != 10) ; } break; } //TODO: Handle. } }