Commit a1ba7d3e authored by Chris's avatar Chris
Browse files

added "action" to menu_item struct

parent f6cdb877
Loading
Loading
Loading
Loading
+21 −22
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ int main(void) {
	clear_screen();

	printf("yaht.c\n");
	printf("\n\n\n"); // new menu would erase game name otherwise
	printf("\n\n"); // new menu would erase game name otherwise

	// set terminal to raw mode for registering keypresses without need to press Enter
	struct termios old_terminal_configuration, new_terminal_configuration;
@@ -31,15 +31,17 @@ int main(void) {

	typedef struct {
		char *title;

		char *action;
	} menu_item;

	const unsigned int num_menu_items = 2; // lowercase because this will be calculated in more dynamic code
	menu_item items[num_menu_items];
	items[0].title = "New game";
	items[1].title = "Exit";
	int choice = -1;
	unsigned int hovered_choice = 0;
	menu_item items[] = {
		{.title = "New game", .action = "newgame"},
		{.title = "Exit", .action = "exit"},
	};
	const unsigned int num_menu_items = sizeof(items) / sizeof(menu_item);

	char *selected_action = "";
	unsigned int hovered_selection_index = 0;
	// menu logic! TODO make this cross platform (play nice with Windows...)
	do {
		// return cursor to menu position
@@ -50,7 +52,7 @@ int main(void) {

		// print menu items, showing selection
		for (int i = 0; i < num_menu_items; i++) {
			if (i == hovered_choice) {
			if (i == hovered_selection_index) {
				printf("> ");
			}
			printf("%s\n", items[i].title);
@@ -77,37 +79,34 @@ int main(void) {
		case 'J':
		case 's':
		case 'S':
			if (hovered_choice < num_menu_items - 1) {
				hovered_choice++;
			if (hovered_selection_index < num_menu_items - 1) {
				hovered_selection_index++;
			}
			break;
		case 'k':
		case 'K':
		case 'w':
		case 'W':
			if (hovered_choice > 0) {
				hovered_choice--;
			if (hovered_selection_index > 0) {
				hovered_selection_index--;
			}
			break;
		case '\n':
			choice = (int)hovered_choice;
			const unsigned int selected_index = hovered_selection_index;
			selected_action = items[selected_index].action;
			break;
		default:
			break;
		}
	} while (choice == -1);
	} while (selected_action == "");

	// un-hold all dice
	clear_held_values(&ctx);

	// do_demo_dice_rolls(&ctx, next_roll); // uncomment if you want to see a little demo

	switch (choice) {
		case 0: // new game
	if (selected_action == "newgame") {
		clear_held_values(&ctx); // part of demo/dice drawing
		do_demo_dice_rolls(&ctx, next_roll);
			break;
	default:
		break;
	}

	fclose(ctx.random_device_fd);