-
Notifications
You must be signed in to change notification settings - Fork 0
/
ncurse.c
144 lines (123 loc) · 2.77 KB
/
ncurse.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "ncurse.h"
#include "snake.h"
int window_rows, window_cols;
/* libncurse initialization function */
void ncurses_init(void)
{
/* ncurse initialize */
initscr();
getmaxyx(stdscr, window_rows, window_cols);
/* disabling line buffer */
cbreak();
/* disable character echoing */
noecho();
/* enable function keys */
keypad(stdscr, TRUE);
/* make cursor invisible */
curs_set(0);
/* set random seed */
srandom(time(NULL));
}
/* libncurse destroy function */
void ncurses_destroy()
{
/* ncurse cleanup */
endwin();
}
static WINDOW *logwin;
/* Create the log window */
static char *logbuf[LOGBUF_NUM];
static char *emptyline;
void logwin_init()
{
int i;
logwin = newwin(LOGWIN_YLEN, /* height */
window_cols - 6, /* width */
window_rows - LOGWIN_YLEN, /* start y */
3 /* start x */
);
/* Decorate the logwin */
box(logwin, 0, 0);
/* Print a LOG on boundary */
mvwprintw(logwin, 0, 2, " LOG ");
/* Remember to refresh the window */
wrefresh(logwin);
/* Initialize logbuf */
for (i = 0; i < LOGBUF_NUM; i++) {
logbuf[i] = (char*)malloc(window_cols - 8);
logbuf[i][0] = 0;
}
emptyline = malloc(window_cols);
memset(emptyline, ' ', window_cols - 9);
emptyline[window_cols - 9] = 0;
}
void logwin_destroy()
{
int i;
/* Free logbuf */
for (i = 0; i < LOGBUF_NUM; i++)
free(logbuf[i]);
free(emptyline);
delwin(logwin);
}
void runlog(char *str)
{
static int index = 0;
int i;
strncpy(logbuf[index], str, window_cols - 10);
logbuf[index][window_cols - 9] = 0;
for (i = 0; i < LOGBUF_NUM; i++) {
mvwprintw(logwin, i+1, 1, emptyline);
mvwprintw(logwin, i+1, 1, logbuf[(index+i) % LOGBUF_NUM]);
wrefresh(logwin);
}
index = (index + LOGBUF_NUM - 1) % LOGBUF_NUM;
}
static WINDOW *gmwin;
void gamewin_init()
{
gmwin = newwin(window_rows - LOGWIN_YLEN - 2, /* height */
window_cols - 6, /* width */
1, /* start y */
3 /* start x */
);
/* Decorate the gmwin */
box(gmwin, 0, 0);
/* Print a logo on boundary */
mvwprintw(gmwin, 0, 2, " Snake ");
mvwprintw(gmwin, window_rows - LOGWIN_YLEN - 3 , 2, " Level: 0 ");
/* Remember to refresh the window */
wrefresh(gmwin);
}
void gamewin_destroy()
{
delwin(gmwin);
}
void setlevel_gamewin(int i)
{
mvwprintw(gmwin, window_rows - LOGWIN_YLEN - 3 , 2, " Level: %d ", i);
wrefresh(gmwin);
}
void draw_snake_head()
{
struct snake_body *head = snake.tail;
while (head->front)
head = head->front;
mvwaddch(gmwin, head->y, head->x, snake.c);
wrefresh(gmwin);
}
void draw_snake_tail()
{
mvwaddch(gmwin, snake.tail->y, snake.tail->x, snake.c);
wrefresh(gmwin);
}
void clear_snake_tail()
{
mvwaddch(gmwin, snake.tail->y, snake.tail->x, ' ');
wrefresh(gmwin);
}
void draw_food()
{
mvwaddch(gmwin, food.y, food.x, food.c);
wrefresh(gmwin);
}