-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
85 lines (73 loc) · 2.24 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Oświadczam, że niniejsza praca stanowiąca podstawę do uznania osiągnięcia efektów
* uczenia się z przedmiotu SOP1 została wykonana przeze mnie samodzielnie.
* Piotr Rogulski
* 305867
*/
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include "util_funs.h"
#define ERROR(source) (perror(source),\
fprintf(stderr, "%s:%d\n", __FILE__, __LINE__),\
exit(EXIT_FAILURE))
#define TRY(expr) if (expr) ERROR(#expr)
int main(int argc, char **argv) {
srand(time(NULL));
parse_args(argc, argv);
// Initialize curses
initscr();
start_color();
echo();
keypad(stdscr, TRUE);
init_pair(1, COLOR_BLACK, COLOR_RED);
init_pair(2, COLOR_BLACK, COLOR_GREEN);
int maxY, maxX;
getmaxyx(stdscr, maxY, maxX);
WINDOW *main_win = newwin(maxY - 3, maxX, 0, 0);
WINDOW *cmd_win = newwin(3, maxX, maxY - 3, 0);
wborder(main_win, 0, 0, 0, 0, 0, 0, 0, 0);
wborder(cmd_win, 0, 0, 0, 0, 0, 0, 0, 0);
wmove(cmd_win, 1, 2);
wprintw(cmd_win, "Command: ");
wrefresh(main_win);
wrefresh(cmd_win);
// Set up signals
sigset_t new_mask;
sigemptyset(&new_mask);
sigaddset(&new_mask, SIGALRM);
sigaddset(&new_mask, SIGUSR1);
TRY(pthread_sigmask(SIG_BLOCK, &new_mask, NULL));
// Main program loop
char buf[maxX];
pthread_mutex_t game_mutex = PTHREAD_MUTEX_INITIALIZER;
gameState_t game;
game.game_mutex = &game_mutex;
game.win = main_win;
game.swap_seed = rand();
while (1) {
char* is_game_mode = getenv("IS_GAME_MODE");
if (is_game_mode == NULL || atoi(is_game_mode) == 0)
mvwprintw(cmd_win, 0, 2, " Mode: Menu ");
else
mvwprintw(cmd_win, 0, 2, " Mode: Game ");
wrefresh(cmd_win);
wmove(cmd_win, 1, 11);
whline(cmd_win, ' ', maxX - 12);
TRY(memset(buf, 0, maxX) == NULL);
wgetstr(cmd_win, buf);
int ret = exec_command(buf, main_win, &game);
if (ret == INVALID_CMD)
continue;
if (ret == EXIT_CMD)
break;
wrefresh(cmd_win);
}
// curses cleanup
delwin(main_win);
delwin(cmd_win);
endwin();
return EXIT_SUCCESS;
}