Skip to content
Snippets Groups Projects
Commit 638a62ea authored by tanguy.cavagna's avatar tanguy.cavagna :desktop:
Browse files

Added menu entry selection

parent 07f90cc2
Branches
No related tags found
No related merge requests found
...@@ -58,7 +58,7 @@ BreakConstructorInitializersBeforeComma: false ...@@ -58,7 +58,7 @@ BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true BreakStringLiterals: true
ColumnLimit: 80 ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:' CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false ConstructorInitializerAllOnOneLineOrOnePerLine: false
......
#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
#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
#include "cli/cli.h"
#include "ui/ui.h" #include "ui/ui.h"
#include "wordle/wordle.h" #include "wordle/wordle.h"
#include <locale.h>
#include <ncurses.h> #include <ncurses.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
int main(void) { int main(void) {
setlocale(LC_ALL, "");
init_term(); init_term();
int menu_choice = show_menu();
clear();
if (menu_choice == MENU_QUIT) {
cleanup_term();
return EXIT_SUCCESS;
}
clear();
set_gamemode(menu_choice);
getch(); getch();
cleanup_term(); cleanup_term();
......
#include "ui.h" #include "ui.h"
#define MENU_CHOICE_COUNT 4
#define TITLE_Y_OFFSET 5
//==========================
// PRIVATE
//==========================
WINDOW *ui_window = NULL; 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() { void set_colors() {
init_pair(GREEN_PAIR, COLOR_GREEN, COLOR_BLACK); init_pair(GREEN_PAIR, COLOR_GREEN, COLOR_BLACK);
init_pair(ORANGE_PAIR, COLOR_YELLOW, 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() { bool init_term() {
ui_window = initscr(); ui_window = initscr();
noecho(); noecho();
curs_set(0);
keypad(stdscr, true);
if (ui_window == NULL) if (ui_window == NULL)
return false; return false;
...@@ -26,7 +102,28 @@ bool init_term() { ...@@ -26,7 +102,28 @@ bool init_term() {
} }
void cleanup_term() { void cleanup_term() {
curs_set(1);
delwin(ui_window); delwin(ui_window);
endwin(); endwin();
refresh(); 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
...@@ -3,9 +3,24 @@ ...@@ -3,9 +3,24 @@
#include <ncurses.h> #include <ncurses.h>
#include <stdbool.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 GREEN_PAIR 1
#define ORANGE_PAIR 2 #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 * @brief Initialize the terminal
...@@ -21,4 +36,11 @@ bool init_term(); ...@@ -21,4 +36,11 @@ bool init_term();
*/ */
void cleanup_term(); void cleanup_term();
/**
* @brief Show the game menu
*
* @return int Choice id
*/
int show_menu();
#endif #endif
\ No newline at end of file
...@@ -3,7 +3,9 @@ ...@@ -3,7 +3,9 @@
//========================== //==========================
// PRIVATE // PRIVATE
//========================== //==========================
int gamemode = -1;
//========================== //==========================
// PUBLIC // PUBLIC
//========================== //==========================
\ No newline at end of file void set_gamemode(int menu_gamemode) { gamemode = menu_gamemode; }
\ No newline at end of file
#ifndef WORDLE_H_ #ifndef WORDLE_H_
#define 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 #endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment