Added menu
This commit is contained in:
parent
a988017110
commit
5de7f55889
10 changed files with 78 additions and 19 deletions
4
Makefile
4
Makefile
|
|
@ -1,10 +1,10 @@
|
||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -Wall -Wextra -Iinclude -std=c11
|
CFLAGS = -Wall -Wextra -Iinclude -std=c11
|
||||||
LDFLAGS = -lncurses
|
LDFLAGS = -lncurses -lmenu
|
||||||
|
|
||||||
SRC = $(wildcard src/*.c)
|
SRC = $(wildcard src/*.c)
|
||||||
OBJ = $(patsubst src/%.c, build/%.o, $(SRC))
|
OBJ = $(patsubst src/%.c, build/%.o, $(SRC))
|
||||||
TARGET = build/space_shooter
|
TARGET = build/shooter
|
||||||
|
|
||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
|
|
||||||
|
|
|
||||||
BIN
build/game.o
BIN
build/game.o
Binary file not shown.
BIN
build/gui.o
BIN
build/gui.o
Binary file not shown.
BIN
build/main.o
BIN
build/main.o
Binary file not shown.
BIN
build/shooter
BIN
build/shooter
Binary file not shown.
Binary file not shown.
|
|
@ -2,6 +2,10 @@
|
||||||
#define GUI_H
|
#define GUI_H
|
||||||
|
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
#include <menu.h>
|
||||||
|
#include <curses.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include "objects.h"
|
#include "objects.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
|
@ -14,9 +18,12 @@ enum {
|
||||||
WINSC_PAIR
|
WINSC_PAIR
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||||
|
//extern char* choices[];
|
||||||
|
|
||||||
/* initialization and screens */
|
/* initialization and screens */
|
||||||
void init_colors(void);
|
void init_colors(void);
|
||||||
void print_menu(WINDOW* win);
|
int print_menu(WINDOW* win);
|
||||||
void print_gameover(WINDOW* win);
|
void print_gameover(WINDOW* win);
|
||||||
void print_win_screen(WINDOW* win);
|
void print_win_screen(WINDOW* win);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,13 +49,6 @@ int game_run(void) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* show menu */
|
|
||||||
print_menu(stdscr);
|
|
||||||
getch();
|
|
||||||
werase(stdscr);
|
|
||||||
mvprintw(1, 1, "p per pausa, q per uscire");
|
|
||||||
refresh();
|
|
||||||
|
|
||||||
/* create windows */
|
/* create windows */
|
||||||
game_window = newwin(height, width, win_starty, win_startx);
|
game_window = newwin(height, width, win_starty, win_startx);
|
||||||
nodelay(game_window, TRUE);
|
nodelay(game_window, TRUE);
|
||||||
|
|
|
||||||
62
src/gui.c
62
src/gui.c
|
|
@ -1,8 +1,14 @@
|
||||||
#include "../include/gui.h"
|
#include "../include/gui.h"
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
/************** FUNZIONI GUI ***************/
|
/************** FUNZIONI GUI ***************/
|
||||||
|
|
||||||
|
static char *choices[] = {
|
||||||
|
"Start Game",
|
||||||
|
"Exit",
|
||||||
|
NULL,
|
||||||
|
};
|
||||||
|
|
||||||
char* title[] =
|
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[] =
|
char* gameover[] =
|
||||||
|
|
@ -36,6 +42,7 @@ char* win_screen[] =
|
||||||
char win_desc[] = "HAI VINTO. Premi q per uscire...";
|
char win_desc[] = "HAI VINTO. Premi q per uscire...";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void init_colors() {
|
void init_colors() {
|
||||||
start_color();
|
start_color();
|
||||||
init_pair(ENEMY_PAIR, COLOR_RED, COLOR_BLACK);
|
init_pair(ENEMY_PAIR, COLOR_RED, COLOR_BLACK);
|
||||||
|
|
@ -45,23 +52,62 @@ void init_colors() {
|
||||||
init_pair(WINSC_PAIR, COLOR_YELLOW, COLOR_BLACK);
|
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 height;
|
||||||
int width;
|
int width;
|
||||||
|
int ch;
|
||||||
getmaxyx(win, height, width);
|
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));
|
wattron(win, COLOR_PAIR(TITLE_PAIR));
|
||||||
for(int i = 0; i < (TEXTLINES+BORDERLINES); i++) {
|
for(int i = 0; i < (TEXTLINES+BORDERLINES); i++) {
|
||||||
mvwprintw(win, i+1, (width-(strlen(title[0])))/2, "%s", title[i]);
|
mvwprintw(win, i+1, (width-(strlen(title[0])))/2, "%s", title[i]);
|
||||||
}
|
}
|
||||||
wattroff(win, COLOR_PAIR(TITLE_PAIR));
|
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);
|
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) {
|
void print_gameover(WINDOW* win) {
|
||||||
|
|
|
||||||
15
src/main.c
15
src/main.c
|
|
@ -14,7 +14,20 @@ int main(void) {
|
||||||
|
|
||||||
init_colors();
|
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 */
|
/* cleanup terminal */
|
||||||
curs_set(1);
|
curs_set(1);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue