Continuato il refactor, spostate funzioni gui

This commit is contained in:
Carlo 2025-11-10 09:40:02 +01:00
parent e0c5a2537b
commit a374c0edee
6 changed files with 149 additions and 123 deletions

View file

@ -0,0 +1,3 @@
Per compilare:
cd space-shooter/
gcc -o /build/shooter src/main.c src/gui.c -lncurses

Binary file not shown.

0
data/savelog Normal file
View file

24
include/gui.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef GUI_H
#define GUI_H
#include <ncurses.h>
#include <stdio.h>
#include <string.h>
// Aspect constatnts
#define TEXTLINES 4
#define BORDERLINES 2
#define ENEMY_PAIR 1
#define BONUS_PAIR 2
#define TITLE_PAIR 3
#define GMOVR_PAIR 4
#define WINSC_PAIR 5
void init_colors();
void print_menu(WINDOW* win);
void print_gameover(WINDOW* win);
void print_win_screen(WINDOW* win);
#endif

120
src/gui.c Normal file
View file

@ -0,0 +1,120 @@
#include "../include/gui.h"
/************** FUNZIONI GUI ***************/
char* title[] =
{
"###########################################################################################################",
"# o8888o o8888o o8888o o8888o o8888o o8888o o8 80 o8888o o8888o o8888o o8888o o8888o #",
"# 888o 0oooo0 88 888 88 88oo oo 888o 888o88 88 888 88 888 88 88oo 88oo88 #",
"# o888 88 888888 88 88 o888 88 88 88 888 88 888 88 88 88 88 #",
"# 088880 88 88 88 888880 088888 088880 08 80 088880 088880 00 088888 88 88o #",
"###########################################################################################################"
};
char menu_desc[] = "BENVENUTO. Premi qualsiasi tasto per giocare...";
char* gameover[] =
{
"o8888o o8888o o8oo8o o8888o o8888o 88 8o o8888o o8888o",
"888 oo 88 888 88\\/88 88oo oo 88 888 88 88 88oo 88oo88",
"888 8 888888 88 88 88 88 888 88 o8 88 88 88 ",
"O88880 88 88 88 88 888880 088880 888O 888880 88 88o"
};
char gameover_desc[] = "HAI PERSO. Premi q per uscire...";
char* win_screen[] =
{
"o8 o8 o8888o o8 8o o8 8o o8888o o8 8o",
"88 o8 88 888 88 88 oo 88 88 88 888o88",
"888 88 888 88 88 88/\\88 88 88 888",
"888 088880 088880 8888 088880 88 88"
};
char win_desc[] = "HAI VINTO. Premi q per uscire...";
void init_colors() {
start_color();
init_pair(ENEMY_PAIR, COLOR_RED, COLOR_BLACK);
init_pair(TITLE_PAIR, COLOR_MAGENTA, COLOR_BLACK);
init_pair(GMOVR_PAIR, COLOR_RED, COLOR_BLACK);
init_pair(BONUS_PAIR, COLOR_GREEN, COLOR_BLACK);
init_pair(WINSC_PAIR, COLOR_YELLOW, COLOR_BLACK);
}
void print_menu(WINDOW* win) {
// Ottieni parametri della finestra
int height;
int width;
getmaxyx(win, height, width);
// Scritta colorata
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));
// Scritta che blinka
wattron(win, A_BLINK);
mvwprintw(win, (TEXTLINES+BORDERLINES)+1, (width-(strlen(menu_desc)))/2, "%s", menu_desc);
wattroff(win, A_BLINK);
// stampa a video
refresh();
return;
}
void print_gameover(WINDOW* win) {
// Parametri della finestra e pulisci tutto
int height;
int width;
int ch;
wclear(win);
getmaxyx(win, height, width);
// Stampa della scritta colorata
wattron(win, COLOR_PAIR(GMOVR_PAIR));
for(int i = 0; i < TEXTLINES; i++) {
mvwprintw(win, i+1, (width-(strlen(gameover[0])))/2, "%s", gameover[i]);
}
wattroff(win, COLOR_PAIR(GMOVR_PAIR));
// Stampa della scritta che blinka
wattron(win, A_BLINK);
mvwprintw(win, TEXTLINES+1, (width-(strlen(gameover_desc)))/2, "%s", gameover_desc);
wattroff(win, A_BLINK);
// Stampa a video e attendi input
refresh();
while((ch = wgetch(win)) != 'q');
return;
}
void print_win_screen(WINDOW* win) {
// Parametri della finestra e pulisci tutto
int height;
int width;
int ch;
wclear(win);
getmaxyx(win, height, width);
// Stampa della scritta colorata
wattron(win, COLOR_PAIR(WINSC_PAIR));
for(int i = 0; i < TEXTLINES; i++) {
mvwprintw(win, i+1, (width-(strlen(win_screen[0])))/2, "%s", win_screen[i]);
}
wattroff(win, COLOR_PAIR(WINSC_PAIR));
// Stampa della scritta che blinka
wattron(win, A_BLINK);
mvwprintw(win, TEXTLINES+1, (width-(strlen(win_desc)))/2, "%s", win_desc);
wattroff(win, A_BLINK);
// Stampa a video e attendi input
refresh();
while((ch = wgetch(win)) != 'q');
return;
}

View file

@ -6,6 +6,7 @@
#include <time.h> #include <time.h>
#include <curses.h> #include <curses.h>
#include <menu.h> #include <menu.h>
#include "../include/gui.h"
/************* STATIC DEFINITIONS ************/ /************* STATIC DEFINITIONS ************/
// Game constants // Game constants
@ -20,15 +21,6 @@
#define HEIGHT 30 #define HEIGHT 30
#define MIN_COLS 110 #define MIN_COLS 110
// Aspect constatnts
#define TEXTLINES 4
#define BORDERLINES 2
#define ENEMY_PAIR 1
#define BONUS_PAIR 2
#define TITLE_PAIR 3
#define GMOVR_PAIR 4
#define WINSC_PAIR 5
/************* GAME STRUCTURES **************/ /************* GAME STRUCTURES **************/
struct speed { struct speed {
@ -50,114 +42,6 @@ struct status {
int remaining_bullets; int remaining_bullets;
}; };
/************** FUNZIONI GUI ***************/
char* title[] =
{
"###########################################################################################################",
"# o8888o o8888o o8888o o8888o o8888o o8888o o8 80 o8888o o8888o o8888o o8888o o8888o #",
"# 888o 0oooo0 88 888 88 88oo oo 888o 888o88 88 888 88 888 88 88oo 88oo88 #",
"# o888 88 888888 88 88 o888 88 88 88 888 88 888 88 88 88 88 #",
"# 088880 88 88 88 888880 088888 088880 08 80 088880 088880 00 088888 88 88o #",
"###########################################################################################################"
};
char menu_desc[] = "BENVENUTO. Premi qualsiasi tasto per giocare...";
char* gameover[] =
{
"o8888o o8888o o8oo8o o8888o o8888o 88 8o o8888o o8888o",
"888 oo 88 888 88\\/88 88oo oo 88 888 88 88 88oo 88oo88",
"888 8 888888 88 88 88 88 888 88 o8 88 88 88 ",
"O88880 88 88 88 88 888880 088880 888O 888880 88 88o"
};
char gameover_desc[] = "HAI PERSO. Premi q per uscire...";
char* win_screen[] =
{
"o8 o8 o8888o o8 8o o8 8o o8888o o8 8o",
"88 o8 88 888 88 88 oo 88 88 88 888o88",
"888 88 888 88 88 88/\\88 88 88 888",
"888 088880 088880 8888 088880 88 88"
};
char win_desc[] = "HAI VINTO. Premi q per uscire...";
void print_menu(WINDOW* win) {
// Ottieni parametri della finestra
int height;
int width;
getmaxyx(win, height, width);
// Scritta colorata
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));
// Scritta che blinka
wattron(win, A_BLINK);
mvwprintw(win, (TEXTLINES+BORDERLINES)+1, (width-(strlen(menu_desc)))/2, "%s", menu_desc);
wattroff(win, A_BLINK);
// stampa a video
refresh();
return;
}
void print_gameover(WINDOW* win) {
// Parametri della finestra e pulisci tutto
int height;
int width;
int ch;
wclear(win);
getmaxyx(win, height, width);
// Stampa della scritta colorata
wattron(win, COLOR_PAIR(GMOVR_PAIR));
for(int i = 0; i < TEXTLINES; i++) {
mvwprintw(win, i+1, (width-(strlen(gameover[0])))/2, "%s", gameover[i]);
}
wattroff(win, COLOR_PAIR(GMOVR_PAIR));
// Stampa della scritta che blinka
wattron(win, A_BLINK);
mvwprintw(win, TEXTLINES+1, (width-(strlen(gameover_desc)))/2, "%s", gameover_desc);
wattroff(win, A_BLINK);
// Stampa a video e attendi input
refresh();
while((ch = wgetch(win)) != 'q');
return;
}
void print_win_screen(WINDOW* win) {
// Parametri della finestra e pulisci tutto
int height;
int width;
int ch;
wclear(win);
getmaxyx(win, height, width);
// Stampa della scritta colorata
wattron(win, COLOR_PAIR(WINSC_PAIR));
for(int i = 0; i < TEXTLINES; i++) {
mvwprintw(win, i+1, (width-(strlen(win_screen[0])))/2, "%s", win_screen[i]);
}
wattroff(win, COLOR_PAIR(WINSC_PAIR));
// Stampa della scritta che blinka
wattron(win, A_BLINK);
mvwprintw(win, TEXTLINES+1, (width-(strlen(win_desc)))/2, "%s", win_desc);
wattroff(win, A_BLINK);
// Stampa a video e attendi input
refresh();
while((ch = wgetch(win)) != 'q');
return;
}
void print_status_bar(WINDOW* main_win, int y, int x, struct status game_status) { void print_status_bar(WINDOW* main_win, int y, int x, struct status game_status) {
int status_x = x + 1; int status_x = x + 1;
@ -215,12 +99,7 @@ int main() {
srand(time(NULL)); srand(time(NULL));
// Colori // Colori
start_color(); init_colors();
init_pair(ENEMY_PAIR, COLOR_RED, COLOR_BLACK);
init_pair(TITLE_PAIR, COLOR_MAGENTA, COLOR_BLACK);
init_pair(GMOVR_PAIR, COLOR_RED, COLOR_BLACK);
init_pair(BONUS_PAIR, COLOR_GREEN, COLOR_BLACK);
init_pair(WINSC_PAIR, COLOR_YELLOW, COLOR_BLACK);
// Dimensioni finestra di gioco e controlli // Dimensioni finestra di gioco e controlli
// calcolo delle misure della finestra // calcolo delle misure della finestra