From 638a62ea66b64f9214d6309f288f237c52b6fa3d Mon Sep 17 00:00:00 2001 From: "tanguy.cavagna" <tanguy.cavagna@etu.hesge.ch> Date: Tue, 10 May 2022 19:15:30 +0200 Subject: [PATCH] Added menu entry selection --- .clang-format | 2 +- cli/cli.c | 14 ------- cli/cli.h | 16 -------- main.c | 14 ++++++- ui/ui.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ ui/ui.h | 22 +++++++++++ wordle/wordle.c | 4 +- wordle/wordle.h | 10 +++++ 8 files changed, 146 insertions(+), 33 deletions(-) delete mode 100644 cli/cli.c delete mode 100644 cli/cli.h diff --git a/.clang-format b/.clang-format index 84c1ea5..c897662 100644 --- a/.clang-format +++ b/.clang-format @@ -58,7 +58,7 @@ BreakConstructorInitializersBeforeComma: false BreakConstructorInitializers: BeforeColon BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true -ColumnLimit: 80 +ColumnLimit: 100 CommentPragmas: '^ IWYU pragma:' CompactNamespaces: false ConstructorInitializerAllOnOneLineOrOnePerLine: false diff --git a/cli/cli.c b/cli/cli.c deleted file mode 100644 index c1f183e..0000000 --- a/cli/cli.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "cli.h" -#include <ncurses.h> - -//========================== -// PRIVATE -//========================== - -//========================== -// PUBLIC -//========================== -void print_center(char *text) { - int padlen = (TERMINAL_WIDTH - strlen(text)) / 2; - printw("%*s%s%*s\n", padlen, "", text, padlen, ""); -} \ No newline at end of file diff --git a/cli/cli.h b/cli/cli.h deleted file mode 100644 index 7fd541f..0000000 --- a/cli/cli.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef CLI_H_ -#define CLI_H_ - -#include <stdio.h> -#include <string.h> - -#define TERMINAL_WIDTH 120 - -/** - * @brief Print center text - * - * @param text - */ -void print_center(char *text); - -#endif \ No newline at end of file diff --git a/main.c b/main.c index 8b3d140..081068e 100644 --- a/main.c +++ b/main.c @@ -1,14 +1,26 @@ -#include "cli/cli.h" #include "ui/ui.h" #include "wordle/wordle.h" +#include <locale.h> #include <ncurses.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { + setlocale(LC_ALL, ""); init_term(); + int menu_choice = show_menu(); + clear(); + + if (menu_choice == MENU_QUIT) { + cleanup_term(); + return EXIT_SUCCESS; + } + + clear(); + set_gamemode(menu_choice); + getch(); cleanup_term(); diff --git a/ui/ui.c b/ui/ui.c index 5220ed4..034c6e5 100644 --- a/ui/ui.c +++ b/ui/ui.c @@ -1,15 +1,91 @@ #include "ui.h" +#define MENU_CHOICE_COUNT 4 +#define TITLE_Y_OFFSET 5 + +//========================== +// PRIVATE +//========================== WINDOW *ui_window = NULL; +char menu_choices[MENU_CHOICE_COUNT][10] = {"Solo", "1 Vs 1", "Computer", "Quit"}; + +char title[8][64] = {"$$\\ $$\\ $$$$$$\\ $$$$$$$\\ $$$$$$$\\ $$\\ $$$$$$$$\\ ", + "$$ | $\\ $$ |$$ __$$\\ $$ __$$\\ $$ __$$\\ $$ | $$ _____|", + "$$ |$$$\\ $$ |$$ / $$ |$$ | $$ |$$ | $$ |$$ | $$ | ", + "$$ $$ $$\\$$ |$$ | $$ |$$$$$$$ |$$ | $$ |$$ | $$$$$\\ ", + "$$$$ _$$$$ |$$ | $$ |$$ __$$< $$ | $$ |$$ | $$ __| ", + "$$$ / \\$$$ |$$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ | ", + "$$ / \\$$ | $$$$$$ |$$ | $$ |$$$$$$$ |$$$$$$$$\\ $$$$$$$$\\ ", + "\\__/ \\__| \\______/ \\__| \\__|\\_______/ \\________|\\________|"}; + +/** + * @brief Print a text at the center of the given line + * + * @param foo + * @param line + */ +void printw_center(char *foo, int line) { mvaddstr(line, TERM_MID_COLS - (strlen(foo) / 2), foo); } +/** + * @brief Set the color scheme of the programm + * + */ void set_colors() { init_pair(GREEN_PAIR, COLOR_GREEN, COLOR_BLACK); init_pair(ORANGE_PAIR, COLOR_YELLOW, COLOR_BLACK); + init_pair(WHITE_PAIR, COLOR_WHITE, COLOR_BLACK); } +/** + * @brief Get the selected menu choice + * + * @return int + */ +int select_menu_choice() { + int current_choice = 0; + int selection_ctrl = 0; + + do { + // Keyboard controls + switch (selection_ctrl) { + case KEY_DOWN: + current_choice += 1; + current_choice %= MENU_CHOICE_COUNT; + break; + + case KEY_UP: + current_choice -= 1; + current_choice = current_choice < 0 ? MENU_CHOICE_COUNT - 1 : current_choice; + break; + } + + // Print of the menu choices + for (int i = 0; i < MENU_CHOICE_COUNT; i++) { + int menu_selector_x = TERM_MID_COLS - (strlen(menu_choices[i]) / 2) - 2; + + if (i == current_choice) { + attron(A_BOLD); + mvaddch(23 + i, menu_selector_x, '>'); // Menu selector ON + printw_center(menu_choices[i], 23 + i); + attroff(A_BOLD); + } else { + mvaddch(23 + i, menu_selector_x, ' '); // Menu selector OFF + printw_center(menu_choices[i], 23 + i); + } + } + } while ((selection_ctrl = getch()) != '\n'); + + return current_choice; +} + +//========================== +// PUBLIC +//========================== bool init_term() { ui_window = initscr(); noecho(); + curs_set(0); + keypad(stdscr, true); if (ui_window == NULL) return false; @@ -26,7 +102,28 @@ bool init_term() { } void cleanup_term() { + curs_set(1); delwin(ui_window); endwin(); refresh(); +} + +int show_menu() { + // Title print + for (int i = 0; i < 8; i++) + printw_center(title[i], i + TITLE_Y_OFFSET); + + // Menu print + attron(COLOR_PAIR(GREEN_PAIR)); + attron(A_BOLD); + printw_center("MENU", 20); + attroff(A_BOLD); + + // Menu choices handler + attron(COLOR_PAIR(WHITE_PAIR)); + int menu_choice = select_menu_choice(); + + refresh(); + + return menu_choice; } \ No newline at end of file diff --git a/ui/ui.h b/ui/ui.h index db98b61..13eb9eb 100644 --- a/ui/ui.h +++ b/ui/ui.h @@ -3,9 +3,24 @@ #include <ncurses.h> #include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +// Placements +#define TERM_MID_LINES (LINES / 2) +#define TERM_MID_COLS (COLS / 2) + +// Color pairs #define GREEN_PAIR 1 #define ORANGE_PAIR 2 +#define WHITE_PAIR 3 + +// Menu choices +#define MENU_SOLO 0 +#define MENU_1_V_1 1 +#define MENU_COMPUTER 2 +#define MENU_QUIT 3 /** * @brief Initialize the terminal @@ -21,4 +36,11 @@ bool init_term(); */ void cleanup_term(); +/** + * @brief Show the game menu + * + * @return int Choice id + */ +int show_menu(); + #endif \ No newline at end of file diff --git a/wordle/wordle.c b/wordle/wordle.c index c15575c..e477c02 100644 --- a/wordle/wordle.c +++ b/wordle/wordle.c @@ -3,7 +3,9 @@ //========================== // PRIVATE //========================== +int gamemode = -1; //========================== // PUBLIC -//========================== \ No newline at end of file +//========================== +void set_gamemode(int menu_gamemode) { gamemode = menu_gamemode; } \ No newline at end of file diff --git a/wordle/wordle.h b/wordle/wordle.h index 2d57091..b0ac85f 100644 --- a/wordle/wordle.h +++ b/wordle/wordle.h @@ -1,6 +1,16 @@ #ifndef WORDLE_H_ #define WORDLE_H_ +// Gamemodes +#define GAMEMODE_SOLO 0 +#define GAMEMODE_1_V_1 1 +#define GAMEMODE_COMPUTER 2 +/** + * @brief Set the gamemode + * + * @param menu_gamemode + */ +void set_gamemode(int menu_gamemode); #endif \ No newline at end of file -- GitLab