2 * newdemo.c - A demo program using PDCurses. The program illustrate
3 * the use of colours for text output.
5 * $Id: newdemo.c,v 1.23 2002/03/23 22:17:24 tom Exp $
10 #include <test.priv.h>
12 #define delay_output(x) napms(x)
17 const char *AusMap[16] =
21 " AAAAAAAAAAA AAAAAAAA ",
22 " AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
23 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
24 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
25 " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
26 " AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
27 "W.A. AAAAAAAAA AAAAAA Vic.",
38 NCURSES_CONST char *messages[] =
40 "Hello from the Land Down Under",
41 "The Land of crocs. and a big Red Rock",
42 "Where the sunflower runs along the highways",
43 "the dusty red roads lead one to loneliness",
44 "Blue sky in the morning and",
45 "freezing nights and twinkling stars",
53 trap(int sig GCC_UNUSED)
56 ExitProgram(EXIT_FAILURE);
63 WaitForUser(WINDOW *win)
69 t = time((time_t *) 0);
71 if ((int) (key = wgetch(win)) != ERR) {
72 if (key == 'q' || key == 'Q')
77 if (time((time_t *) 0) - t > 5)
83 set_colors(WINDOW *win, int pair, int foreground, int background)
86 if (pair > COLOR_PAIRS)
88 init_pair(pair, foreground, background);
89 wattrset(win, COLOR_PAIR(pair));
94 use_colors(WINDOW *win, int pair, int attrs)
97 if (pair > COLOR_PAIRS)
99 attrs |= COLOR_PAIR(pair);
101 wattrset(win, attrs);
109 SubWinTest(WINDOW *win)
111 int w, h, sw, sh, bx, by;
112 WINDOW *swin1, *swin2, *swin3;
115 getbegyx(win, by, bx);
118 if ((swin1 = subwin(win, sh, sw, by + 3, bx + 5)) == NULL)
120 if ((swin2 = subwin(win, sh, sw, by + 4, bx + 8)) == NULL)
122 if ((swin3 = subwin(win, sh, sw, by + 5, bx + 11)) == NULL)
125 set_colors(swin1, 8, COLOR_RED, COLOR_BLUE);
127 mvwaddstr(swin1, 0, 3, "Sub-window 1");
130 set_colors(swin2, 9, COLOR_CYAN, COLOR_MAGENTA);
132 mvwaddstr(swin2, 0, 3, "Sub-window 2");
135 set_colors(swin3, 10, COLOR_YELLOW, COLOR_GREEN);
137 mvwaddstr(swin3, 0, 3, "Sub-window 3");
148 bounce(int n, int *dir, int len)
154 if (n <= 1 || n >= len - 2)
163 BouncingBalls(WINDOW *win)
166 int x1, y1, xd1, yd1;
167 int x2, y2, xd2, yd2;
168 int x3, y3, xd3, yd3;
172 x1 = 2 + rand() % (w - 4);
173 y1 = 2 + rand() % (h - 4);
174 x2 = 2 + rand() % (w - 4);
175 y2 = 2 + rand() % (h - 4);
176 x3 = 2 + rand() % (w - 4);
177 y3 = 2 + rand() % (h - 4);
188 while (wgetch(win) == ERR) {
189 x1 = bounce(x1, &xd1, w);
190 y1 = bounce(y1, &yd1, h);
191 x2 = bounce(x2, &xd2, w);
192 y2 = bounce(y2, &yd2, h);
193 x3 = bounce(x3, &xd3, w);
194 y3 = bounce(y3, &yd3, h);
196 set_colors(win, 11, COLOR_RED, COLOR_BLUE);
197 mvwaddch(win, y1, x1, 'O');
199 set_colors(win, 12, COLOR_BLUE, COLOR_RED);
200 mvwaddch(win, y2, x2, '*');
202 set_colors(win, 13, COLOR_YELLOW, COLOR_WHITE);
203 mvwaddch(win, y3, x3, '@');
216 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
219 int w, x, y, i, j, k;
231 signal(SIGINT, trap);
233 height = 14; /* Create a drawing window */
234 win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
237 ExitProgram(EXIT_FAILURE);
241 set_colors(win, 1, COLOR_WHITE, COLOR_BLUE);
244 set_colors(win, 2, COLOR_RED, COLOR_RED);
245 box(win, ACS_VLINE, ACS_HLINE);
247 /* Do ramdom output of a character */
248 use_colors(win, 1, A_NORMAL);
250 for (i = 0; i < 5000; ++i) {
251 x = rand() % (width - 2) + 1;
252 y = rand() % (height - 2) + 1;
253 mvwaddch(win, y, x, c);
256 if (wgetch(win) != ERR)
260 set_colors(win, 3, COLOR_CYAN, COLOR_YELLOW);
265 /* Erase and draw green window */
266 set_colors(win, 4, COLOR_YELLOW, COLOR_GREEN);
267 wbkgd(win, use_colors(win, 4, A_BOLD));
270 /* Draw RED bounding box */
271 use_colors(win, 2, A_NORMAL);
274 /* Display Australia map */
275 use_colors(win, 4, A_BOLD);
278 mvwaddstr(win, i + 1, 8, AusMap[i]);
284 set_colors(win, 5, COLOR_BLUE, COLOR_WHITE);
285 use_colors(win, 5, A_BLINK);
286 mvwaddstr(win, height - 2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
289 /* Draw running messages */
290 set_colors(win, 6, COLOR_YELLOW, COLOR_WHITE);
291 message = messages[j = 0];
294 strcpy(buffer, message);
295 while (j < NMESSAGES) {
296 while ((int) strlen(buffer) < w) {
297 strcat(buffer, " ... ");
298 strcat(buffer, messages[++j % NMESSAGES]);
302 mvwaddnstr(win, height / 2, w - i, buffer, i);
304 mvwaddnstr(win, height / 2, 1, buffer, w);
308 if (wgetch(win) != ERR) {
313 for (k = 0; (buffer[k] = buffer[k + 1]) != '\0'; k++) ;
319 /* Draw running As across in RED */
320 set_colors(win, 7, COLOR_RED, COLOR_GREEN);
321 for (i = 2; i < width - 4; ++i) {
322 k = mvwinch(win, 4, i);
327 mvwaddch(win, 4, i, c);
331 /* Put a message up wait for a key */
333 use_colors(win, 5, A_NORMAL);
334 mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
337 if (WaitForUser(win) == 1)
340 j = 0; /* Restore the old line */
341 for (i = 2; i < width - 4; ++i)
342 mvwaddch(win, 4, i, save[j++]);
346 /* Put a message up wait for a key */
348 use_colors(win, 5, A_NORMAL);
349 mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
351 if (WaitForUser(win) == 1)
355 ExitProgram(EXIT_SUCCESS);