summaryrefslogtreecommitdiff
path: root/test/newdemo.c
blob: 75626a9051e174043b6891d64fa5a47b65bf25df (plain)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 *  newdemo.c	-	A demo program using PDCurses. The program illustrate
 *  	 		the use of colours for text output.
 *
 * $Id: newdemo.c,v 1.35 2010/12/12 00:19:15 tom Exp $
 */

#include <test.priv.h>

#include <time.h>

/*
 *  The Australian map
 */
static CONST_MENUS char *AusMap[16] =
{
    "           A           A ",
    "    N.T. AAAAA       AAAA ",
    "     AAAAAAAAAAA  AAAAAAAA ",
    "   AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
    "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
    "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
    " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
    "   AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
    "W.A. AAAAAAAAA      AAAAAA Vic.",
    "       AAA   S.A.     AA",
    "                       A  Tas.",
    ""
};

/*
 *  Funny messages
 */
#define NMESSAGES   6

static const char *messages[] =
{
    "Hello from the Land Down Under",
    "The Land of crocs. and a big Red Rock",
    "Where the sunflower runs along the highways",
    "the dusty red roads lead one to loneliness",
    "Blue sky in the morning and",
    "freezing nights and twinkling stars",
    ""
};

/*
 *  Trap interrupt
 */
static RETSIGTYPE
trap(int sig GCC_UNUSED)
{
    endwin();
    ExitProgram(EXIT_FAILURE);
}

/*
 *  Wait for user
 */
static int
WaitForUser(WINDOW *win)
{
    time_t t;
    chtype key;

    nodelay(win, TRUE);
    t = time((time_t *) 0);
    while (1) {
	if ((int) (key = (chtype) wgetch(win)) != ERR) {
	    if (key == 'q' || key == 'Q')
		return 1;
	    else
		return 0;
	}
	if (time((time_t *) 0) - t > 5)
	    return 0;
    }
}

static void
set_colors(WINDOW *win, int pair, int foreground, int background)
{
    if (has_colors()) {
	if (pair > COLOR_PAIRS)
	    pair = COLOR_PAIRS;
	init_pair((short) pair, (short) foreground, (short) background);
	(void) wattrset(win, (attr_t) COLOR_PAIR(pair));
    }
}

static chtype
use_colors(WINDOW *win, int pair, chtype attrs)
{
    if (has_colors()) {
	if (pair > COLOR_PAIRS)
	    pair = COLOR_PAIRS;
	attrs |= (chtype) COLOR_PAIR(pair);
    }
    (void) wattrset(win, attrs);
    return attrs;
}

/*
 * Test sub windows
 */
static int
SubWinTest(WINDOW *win)
{
    int w, h, sw, sh, bx, by;
    WINDOW *swin1, *swin2, *swin3;

    getmaxyx(win, h, w);
    getbegyx(win, by, bx);
    sw = w / 3;
    sh = h / 3;
    if ((swin1 = subwin(win, sh, sw, by + 3, bx + 5)) == NULL)
	return 1;
    if ((swin2 = subwin(win, sh, sw, by + 4, bx + 8)) == NULL)
	return 1;
    if ((swin3 = subwin(win, sh, sw, by + 5, bx + 11)) == NULL)
	return 1;

    set_colors(swin1, 8, COLOR_RED, COLOR_BLUE);
    werase(swin1);
    MvWAddStr(swin1, 0, 3, "Sub-window 1");
    wrefresh(swin1);

    set_colors(swin2, 9, COLOR_CYAN, COLOR_MAGENTA);
    werase(swin2);
    MvWAddStr(swin2, 0, 3, "Sub-window 2");
    wrefresh(swin2);

    set_colors(swin3, 10, COLOR_YELLOW, COLOR_GREEN);
    werase(swin3);
    MvWAddStr(swin3, 0, 3, "Sub-window 3");
    wrefresh(swin3);

    delwin(swin1);
    delwin(swin2);
    delwin(swin3);
    WaitForUser(win);
    return 0;
}

static int
bounce(int n, int *dir, int len)
{
    if (*dir > 0)
	++n;
    else
	--n;
    if (n <= 1 || n >= len - 2)
	*dir = *dir ? 0 : 1;
    return n;
}

/*
 *  Bouncing balls
 */
static int
BouncingBalls(WINDOW *win)
{
    int w, h;
    int x1, y1, xd1, yd1;
    int x2, y2, xd2, yd2;
    int x3, y3, xd3, yd3;

    getmaxyx(win, h, w);

    x1 = 2 + rand() % (w - 4);
    y1 = 2 + rand() % (h - 4);
    x2 = 2 + rand() % (w - 4);
    y2 = 2 + rand() % (h - 4);
    x3 = 2 + rand() % (w - 4);
    y3 = 2 + rand() % (h - 4);

    xd1 = 1;
    yd1 = 1;
    xd2 = 1;
    yd2 = 0;
    xd3 = 0;
    yd3 = 1;

    nodelay(win, TRUE);

    while (wgetch(win) == ERR) {
	x1 = bounce(x1, &xd1, w);
	y1 = bounce(y1, &yd1, h);
	x2 = bounce(x2, &xd2, w);
	y2 = bounce(y2, &yd2, h);
	x3 = bounce(x3, &xd3, w);
	y3 = bounce(y3, &yd3, h);

	set_colors(win, 11, COLOR_RED, COLOR_BLUE);
	MvWAddCh(win, y1, x1, 'O');

	set_colors(win, 12, COLOR_BLUE, COLOR_RED);
	MvWAddCh(win, y2, x2, '*');

	set_colors(win, 13, COLOR_YELLOW, COLOR_WHITE);
	MvWAddCh(win, y3, x3, '@');

	wmove(win, 0, 0);
	wrefresh(win);
	delay_output(100);
    }
    return 0;
}

/*
 *  Main driver
 */
int
main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
{
    WINDOW *win;
    int w, x, y, i, j, k;
    char buffer[200];
    const char *message;
    int width, height;
    chtype save[80];
    chtype c;

    setlocale(LC_ALL, "");

    CATCHALL(trap);

    initscr();
    if (has_colors())
	start_color();
    cbreak();
    curs_set(0);
    width = 48;
    height = 14;		/* Create a drawing window */
    win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
    if (win == NULL) {
	endwin();
	ExitProgram(EXIT_FAILURE);
    }

    while (1) {
	set_colors(win, 1, COLOR_WHITE, COLOR_BLUE);
	werase(win);

	set_colors(win, 2, COLOR_RED, COLOR_RED);
	box(win, ACS_VLINE, ACS_HLINE);
	wrefresh(win);
	/* Do ramdom output of a character */
	use_colors(win, 1, A_NORMAL);
	c = 'a';
	for (i = 0; i < 5000; ++i) {
	    x = rand() % (width - 2) + 1;
	    y = rand() % (height - 2) + 1;
	    MvWAddCh(win, y, x, c);
	    wrefresh(win);
	    nodelay(win, TRUE);
	    if (wgetch(win) != ERR)
		break;
	    if (i == 2000) {
		c = 'b';
		set_colors(win, 3, COLOR_CYAN, COLOR_YELLOW);
	    }
	}

	SubWinTest(win);
	/* Erase and draw green window */
	set_colors(win, 4, COLOR_YELLOW, COLOR_GREEN);
	wbkgd(win, use_colors(win, 4, A_BOLD));
	werase(win);
	wrefresh(win);
	/* Draw RED bounding box */
	use_colors(win, 2, A_NORMAL);
	box(win, ' ', ' ');
	wrefresh(win);
	/* Display Australia map */
	use_colors(win, 4, A_BOLD);
	i = 0;
	while (*AusMap[i]) {
	    MvWAddStr(win, i + 1, 8, AusMap[i]);
	    wrefresh(win);
	    delay_output(50);
	    ++i;
	}

	set_colors(win, 5, COLOR_BLUE, COLOR_WHITE);
	use_colors(win, 5, A_BLINK);
	MvWAddStr(win, height - 2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
	wrefresh(win);

	/* Draw running messages */
	set_colors(win, 6, COLOR_YELLOW, COLOR_WHITE);
	message = messages[j = 0];
	i = 1;
	w = width - 2;
	strcpy(buffer, message);
	while (j < NMESSAGES) {
	    while ((int) strlen(buffer) < w) {
		strcat(buffer, " ... ");
		strcat(buffer, messages[++j % NMESSAGES]);
	    }

	    if (i < w)
		(void) mvwaddnstr(win, height / 2, w - i, buffer, i);
	    else
		(void) mvwaddnstr(win, height / 2, 1, buffer, w);

	    wrefresh(win);
	    nodelay(win, TRUE);
	    if (wgetch(win) != ERR) {
		flushinp();
		break;
	    }
	    if (i++ >= w) {
		for (k = 0; (buffer[k] = buffer[k + 1]) != '\0'; k++) ;
	    }
	    delay_output(100);
	}

	j = 0;
	/*  Draw running As across in RED */
	set_colors(win, 7, COLOR_RED, COLOR_GREEN);
	memset(save, ' ', sizeof(save));
	for (i = 2; i < width - 4; ++i) {
	    k = (int) mvwinch(win, 4, i);
	    if (k == ERR)
		break;
	    save[j++] = c = (chtype) k;
	    c &= A_CHARTEXT;
	    MvWAddCh(win, 4, i, c);
	}
	wrefresh(win);

	/* Put a message up wait for a key */
	i = height - 2;
	use_colors(win, 5, A_NORMAL);
	MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit ");
	wrefresh(win);

	if (WaitForUser(win) == 1)
	    break;

	j = 0;			/* Restore the old line */
	for (i = 2; i < width - 4; ++i)
	    MvWAddCh(win, 4, i, save[j++]);
	wrefresh(win);

	BouncingBalls(win);
	/* Put a message up wait for a key */
	i = height - 2;
	use_colors(win, 5, A_NORMAL);
	MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit ");
	wrefresh(win);
	if (WaitForUser(win) == 1)
	    break;
    }
    endwin();
    ExitProgram(EXIT_SUCCESS);
}