diff --git a/.clang-format b/.clang-format index 84c1ea5bdbc24d714cbc7666bba7fc498ace5070..c897662cfaa543a38998a83da4a4ebf1ddd124f7 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 c1f183e238666057d6db031d255e295383e3d9ac..0000000000000000000000000000000000000000 --- 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 7fd541f3789e8808a132cfc527c0a836d3f727c7..0000000000000000000000000000000000000000 --- 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 8b3d1407f3761bd43507f294d0aa5cbe2ef3e61b..081068ebbd7b0cf0c6c6ee21b365f2aaf1e51276 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 5220ed4484112799d2614b4fd0a7fef8dad13f8b..034c6e5a11342048150d5dee8a30e71ffe840a28 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 db98b61b269def7c4f9759220b91fb022f893b33..13eb9eb19dcf7921ace7a43b1a08bd684fa6da20 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 c15575cdcdb4f21ad116607a6b8eca31f035ed5a..e477c02de39c5184a74eaa5b236be8480b3a0cb6 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 2d57091375bd633e4a2b4bff484490cd75365bd3..b0ac85f2ee9279b65d2a44737da7bc95058e4721 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