diff --git a/Makefile b/Makefile index 5bef432..ec9dc70 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ CC = gcc CFLAGS = -Wall -Wextra -Iinclude -std=c11 -LDFLAGS = -lncurses +LDFLAGS = -lncurses -lmenu SRC = $(wildcard src/*.c) OBJ = $(patsubst src/%.c, build/%.o, $(SRC)) -TARGET = build/space_shooter +TARGET = build/shooter all: $(TARGET) diff --git a/build/game.o b/build/game.o index 8ee3cbf..c6e2a79 100644 Binary files a/build/game.o and b/build/game.o differ diff --git a/build/gui.o b/build/gui.o index 1c5a1ef..82e6037 100644 Binary files a/build/gui.o and b/build/gui.o differ diff --git a/build/main.o b/build/main.o index 7d1b69d..9b66896 100644 Binary files a/build/main.o and b/build/main.o differ diff --git a/build/shooter b/build/shooter index 159b823..d10c07d 100755 Binary files a/build/shooter and b/build/shooter differ diff --git a/build/space_shooter b/build/space_shooter index 1d636f9..da27dae 100755 Binary files a/build/space_shooter and b/build/space_shooter differ diff --git a/include/gui.h b/include/gui.h index 34426ed..f98e139 100644 --- a/include/gui.h +++ b/include/gui.h @@ -2,6 +2,10 @@ #define GUI_H #include +#include +#include +#include +#include #include "objects.h" #include "utils.h" @@ -14,9 +18,12 @@ enum { WINSC_PAIR }; +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +//extern char* choices[]; + /* initialization and screens */ void init_colors(void); -void print_menu(WINDOW* win); +int print_menu(WINDOW* win); void print_gameover(WINDOW* win); void print_win_screen(WINDOW* win); diff --git a/src/game.c b/src/game.c index d351a2e..6a6187e 100644 --- a/src/game.c +++ b/src/game.c @@ -49,13 +49,6 @@ int game_run(void) { return 0; } - /* show menu */ - print_menu(stdscr); - getch(); - werase(stdscr); - mvprintw(1, 1, "p per pausa, q per uscire"); - refresh(); - /* create windows */ game_window = newwin(height, width, win_starty, win_startx); nodelay(game_window, TRUE); diff --git a/src/gui.c b/src/gui.c index 009b69d..5cd3814 100644 --- a/src/gui.c +++ b/src/gui.c @@ -1,8 +1,14 @@ #include "../include/gui.h" -#include + /************** FUNZIONI GUI ***************/ +static char *choices[] = { + "Start Game", + "Exit", + NULL, +}; + char* title[] = { "###########################################################################################################", @@ -13,7 +19,7 @@ char* title[] = "###########################################################################################################" }; -char menu_desc[] = "BENVENUTO. Premi qualsiasi tasto per giocare..."; +char menu_desc[] = "BENVENUTO. Premi p per giocare..."; char* gameover[] = @@ -36,6 +42,7 @@ char* win_screen[] = char win_desc[] = "HAI VINTO. Premi q per uscire..."; + void init_colors() { start_color(); init_pair(ENEMY_PAIR, COLOR_RED, COLOR_BLACK); @@ -45,23 +52,62 @@ void init_colors() { init_pair(WINSC_PAIR, COLOR_YELLOW, COLOR_BLACK); } -void print_menu(WINDOW* win) { +// FUNCTION THAT PRINTS THE MENU +int print_menu(WINDOW* win) { int height; int width; + int ch; getmaxyx(win, height, width); + ITEM **items; + MENU *menu; + int n_choices; + int index = -1; + + n_choices = ARRAY_SIZE(choices); + items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); + for(int i = 0; i < n_choices; ++i) { + items[i] = new_item(choices[i], ""); + } + items[n_choices] = NULL; + menu = new_menu(items); + + // Create a menu window + WINDOW *menu_win = newwin(10, 40, (LINES-10)/2, (COLS-40)/2); + keypad(menu_win, TRUE); + set_menu_win(menu, menu_win); + set_menu_sub(menu, derwin(menu_win, 6, 38, 3, 1)); + set_menu_mark(menu, " > "); + box(menu_win, 0, 0); + post_menu(menu); + wattron(win, COLOR_PAIR(TITLE_PAIR)); for(int i = 0; i < (TEXTLINES+BORDERLINES); i++) { mvwprintw(win, i+1, (width-(strlen(title[0])))/2, "%s", title[i]); } wattroff(win, COLOR_PAIR(TITLE_PAIR)); - wattron(win, A_BLINK); - mvwprintw(win, (TEXTLINES+BORDERLINES)+1, (width-(strlen(menu_desc)))/2, "%s", menu_desc); - wattroff(win, A_BLINK); - wrefresh(win); - return; + wrefresh(menu_win); + + while((ch = wgetch(win)) != '\n') { + switch(ch) { + case KEY_DOWN: menu_driver(menu, REQ_DOWN_ITEM); break; + case KEY_UP: menu_driver(menu, REQ_UP_ITEM); break; + } + wrefresh(menu_win); + } + + ITEM *cur = current_item(menu); + index = item_index(cur); + + unpost_menu(menu); + free_menu(menu); + for(int i = 0; i < n_choices; i++) free_item(items[i]); + free(items); + delwin(menu_win); + + return index; } void print_gameover(WINDOW* win) { diff --git a/src/main.c b/src/main.c index c87703e..5caff27 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,20 @@ int main(void) { init_colors(); - game_run(); + /* show menu */ + int choice = print_menu(stdscr); + + switch(choice) { + case 0: { + werase(stdscr); + mvprintw(1, 1, "p per pausa, q per uscire"); + refresh(); + game_run(); + break; + } + case 1: break; + } + /* cleanup terminal */ curs_set(1);