-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.c
260 lines (173 loc) · 3.78 KB
/
display.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <curses.h>
#include <panel.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "display.h"
#include "map.h"
WINDOW *win_bg, *win_msg, *win_main, *win_stat, *win_menu;
PANEL *pnl_bg, *pnl_msg, *pnl_main, *pnl_stat, *pnl_menu;
int colours[NUM_COLOURS];
void display_init()
{
initscr();
/* TODO: check the screen is big enough */
atexit(display_end);
noecho(); nonl(); cbreak();
curs_set(1);
nodelay(stdscr, FALSE);
keypad(stdscr, TRUE);
if(has_colors()) {
start_color();
init_pair(1, COLOR_BLACK, COLOR_WHITE);
colours[COL_BLACK] = COLOR_PAIR(1);
init_pair(2, COLOR_RED, COLOR_BLACK);
colours[COL_RED] = COLOR_PAIR(2);
colours[COL_ORANGE] = COLOR_PAIR(2) | A_BOLD;
init_pair(3, COLOR_GREEN, COLOR_BLACK);
colours[COL_GREEN] = COLOR_PAIR(3);
colours[COL_BGREEN] = COLOR_PAIR(30) | A_BOLD;
init_pair(4, COLOR_YELLOW, COLOR_BLACK);
colours[COL_BROWN] = COLOR_PAIR(4);
colours[COL_YELLOW] = COLOR_PAIR(4) | A_BOLD;
init_pair(5, COLOR_BLUE, COLOR_BLACK);
colours[COL_BLUE] = COLOR_PAIR(5) | A_BOLD;
init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
colours[COL_MAGENTA] = COLOR_PAIR(6);
colours[COL_PINK] = COLOR_PAIR(6) | A_BOLD;
init_pair(7, COLOR_CYAN, COLOR_BLACK);
colours[COL_CYAN] = COLOR_PAIR(7);
colours[COL_GREY] = COLOR_PAIR(0);
colours[COL_WHITE] = COLOR_PAIR(0) | A_BOLD;
} else {
unsigned int i;
for(i = 0; i < NUM_COLOURS; i++) {
switch(i) {
case COL_BLACK:
colours[i] = A_REVERSE;
break;
case COL_WHITE:
colours[i] = A_BOLD;
break;
default:
colours[i] = A_NORMAL;
}
}
}
/* Create windows and panels */
win_bg = newwin(LINES, COLS, 0, 0);
pnl_bg = new_panel(win_bg);
win_msg = newwin(1, COLS, 0, 0);
pnl_msg = new_panel(win_msg);
win_main = newwin(MAP_Y, MAP_X, 1, (COLS - MAP_X) >> 1);
pnl_main = new_panel(win_main);
win_stat = newwin(2, COLS, LINES - 3, 0);
pnl_stat = new_panel(win_stat);
win_menu = newwin(LINES - 4, COLS, 0, 0);
pnl_menu = new_panel(win_menu);
menu_hide();
}
void display_end()
{
endwin();
}
void display_refresh()
{
update_panels();
doupdate();
}
int display_getch()
{
return getch();
}
void msg_printf(const char *fmt, ...)
{
va_list ap;
char str[1024];
int x, y;
va_start(ap, fmt);
vsnprintf(str, 1024, fmt, ap);
va_end(ap);
getyx(win_msg, y, x);
if(x && (x + strlen(str)) + 10 >= COLS) {
mvwaddstr(win_msg, 0, COLS - 10, " --More--");
display_refresh();
while(getch() != ' ');
werase(win_msg);
wmove(win_msg, 0, 0);
}
waddstr(win_msg, str);
waddstr(win_msg, " ");
top_panel(pnl_msg);
display_refresh();
}
char *msg_getstr()
{
char str[1024];
echo();
wgetnstr(win_msg, str, 1024);
noecho();
return strdup(str);
}
void msg_clear()
{
werase(win_msg);
wmove(win_msg, 0, 0);
}
void main_plot(unsigned int x, unsigned int y, unsigned int col, char ch)
{
wattrset(win_main, colours[col]);
mvwaddch(win_main, y, x, ch);
wmove(win_main, y, x);
top_panel(pnl_main);
}
void main_move(unsigned int x, unsigned int y)
{
wmove(win_main, y, x);
top_panel(pnl_main);
}
void main_clear()
{
werase(win_main);
}
char main_get_character(unsigned int x, unsigned int y)
{
return winch(win_main);
}
void stat_printf(const char *fmt, ...)
{
va_list ap;
char str[1024];
va_start(ap, fmt);
vsnprintf(str, 1024, fmt, ap);
va_end(ap);
werase(win_stat);
wmove(win_stat, 0, 0);
waddstr(win_stat, str);
display_refresh();
}
void menu_printf(const char *fmt, ...)
{
va_list ap;
char str[1024];
va_start(ap, fmt);
vsnprintf(str, 1024, fmt, ap);
va_end(ap);
waddstr(win_menu, str);
}
void menu_clear()
{
werase(win_menu);
wmove(win_menu, 0, 0);
}
void menu_show()
{
top_panel(pnl_menu);
display_refresh();
}
void menu_hide()
{
bottom_panel(pnl_menu);
display_refresh();
}