main.c (3934B)
1 #include <ctype.h> 2 #include <curses.h> 3 #include <locale.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <unistd.h> 7 8 #include "display.h" 9 #include "file.h" 10 #include "game.h" 11 #include "logic.h" 12 #include "utils.h" 13 #include "window.h" 14 15 extern window_T MAIN_w; 16 window_T menu_w; 17 18 void settings(char *pass, int index) { 19 struct imenu_T imenu_items[] = { 20 { "Number of rows", 9, isdigit, NULL}, 21 {"Number of columns", 9, isdigit, NULL}, 22 }; 23 int imenu_items_s = sizeof(imenu_items) / sizeof(struct imenu_T); 24 25 window_set_title(menu_w, "Game settings"); 26 while (display_imenu(menu_w, imenu_items, imenu_items_s)) { 27 int row = atoi(imenu_items[0].buffer); 28 int column = atoi(imenu_items[1].buffer); 29 30 game(row, column, index); 31 break; 32 } 33 34 for (int i = 0; i < imenu_items_s; i++) { 35 free(imenu_items[i].buffer); 36 imenu_items[i].buffer = NULL; 37 } 38 } 39 40 void mode_select(char *pass, int index) { 41 struct menu_T *mode_items; 42 43 MEM_CHECK(mode_items = malloc(evolution_size * sizeof(struct menu_T))); 44 for (int i = 0; i < evolution_size; i++) { 45 mode_items[i].name = evolution_names[i]; 46 mode_items[i].callback = settings; 47 } 48 display_menu(menu_w, "Game Mode", mode_items, evolution_size, 0); 49 50 free(mode_items); 51 } 52 53 void (*file_save_method)(char *, int); 54 55 void new_file(char *pass, int index) { 56 struct imenu_T new_file_items[] = { 57 {"Pick a name", 10, isalnum, NULL}, 58 }; 59 int new_file_items_s = sizeof(new_file_items) / sizeof(struct imenu_T); 60 61 window_set_title(menu_w, "New File"); 62 while (display_imenu(menu_w, new_file_items, new_file_items_s)) { 63 file_save_method(new_file_items[0].buffer, index); 64 break; 65 } 66 67 for (int i = 0; i < new_file_items_s; i++) { 68 free(new_file_items[i].buffer); 69 new_file_items[i].buffer = NULL; 70 } 71 } 72 73 struct menu_T *file_menu_list(char *ext, void (*callback)(char *, int), 74 int offset, int *size) { 75 struct menu_T *file_items; 76 char **buffer; 77 int n; 78 79 load_files(); 80 n = file_select_extension(ext, &buffer); 81 82 MEM_CHECK(file_items = malloc((n + offset) * sizeof(struct menu_T))); 83 for (int i = 0; i < n; i++) { 84 file_items[i + offset].name = buffer[i]; 85 file_items[i + offset].callback = callback; 86 } 87 free(buffer); 88 89 *size = n + offset; 90 return file_items; 91 } 92 93 void save_pattern(void) { 94 int n; 95 struct menu_T *file_items = file_menu_list("part", file_save_pattern, 1, &n); 96 file_items[0].name = "NEW"; 97 file_items[0].callback = new_file; 98 99 file_save_method = file_save_pattern; 100 display_menu(menu_w, "Save Pattern", file_items, n, 0); 101 free(file_items); 102 } 103 104 void load_pattern(void) { 105 int n; 106 struct menu_T *file_items = file_menu_list("part", file_load_pattern, 0, &n); 107 108 display_menu(menu_w, "Load Pattern", file_items, n, 0); 109 free(file_items); 110 } 111 112 void load(char *pass, int index) { 113 int n; 114 struct menu_T *file_items = file_menu_list("all", file_load, 0, &n); 115 116 display_menu(menu_w, "Load Game", file_items, n, 0); 117 free(file_items); 118 } 119 120 void save(void) { 121 int n; 122 struct menu_T *file_items = file_menu_list("all", file_save, 1, &n); 123 file_items[0].name = "NEW"; 124 file_items[0].callback = new_file; 125 126 file_save_method = file_save; 127 display_menu(menu_w, "Save Pattern", file_items, n, 0); 128 free(file_items); 129 } 130 131 void help(char *pass, int index) { 132 window_set_title(menu_w, "Help"); 133 display_patterns(menu_w); 134 } 135 136 void exitp(char *pass, int index) { exit(0); } 137 138 struct menu_T menu_items[] = { 139 {mode_select, "Start"}, 140 { load, "Load"}, 141 { help, "Help"}, 142 { exitp, "Exit"} 143 }; 144 145 int menu_items_s = sizeof(menu_items) / sizeof(struct menu_T); 146 147 int main(void) { 148 setlocale(LC_ALL, ""); 149 atexit(display_stop); 150 151 file_setup(); 152 display_start(); 153 154 menu_w = MAIN_w; 155 while (TRUE) { 156 display_menu(menu_w, "Main menu", menu_items, menu_items_s, 1); 157 } 158 159 return 0; 160 }