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.24 2002/06/29 23:32:18 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;
226 setlocale(LC_ALL, "");
233 signal(SIGINT, trap);
235 height = 14; /* Create a drawing window */
236 win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
239 ExitProgram(EXIT_FAILURE);
243 set_colors(win, 1, COLOR_WHITE, COLOR_BLUE);
246 set_colors(win, 2, COLOR_RED, COLOR_RED);
247 box(win, ACS_VLINE, ACS_HLINE);
249 /* Do ramdom output of a character */
250 use_colors(win, 1, A_NORMAL);
252 for (i = 0; i < 5000; ++i) {
253 x = rand() % (width - 2) + 1;
254 y = rand() % (height - 2) + 1;
255 mvwaddch(win, y, x, c);
258 if (wgetch(win) != ERR)
262 set_colors(win, 3, COLOR_CYAN, COLOR_YELLOW);
267 /* Erase and draw green window */
268 set_colors(win, 4, COLOR_YELLOW, COLOR_GREEN);
269 wbkgd(win, use_colors(win, 4, A_BOLD));
272 /* Draw RED bounding box */
273 use_colors(win, 2, A_NORMAL);
276 /* Display Australia map */
277 use_colors(win, 4, A_BOLD);
280 mvwaddstr(win, i + 1, 8, AusMap[i]);
286 set_colors(win, 5, COLOR_BLUE, COLOR_WHITE);
287 use_colors(win, 5, A_BLINK);
288 mvwaddstr(win, height - 2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
291 /* Draw running messages */
292 set_colors(win, 6, COLOR_YELLOW, COLOR_WHITE);
293 message = messages[j = 0];
296 strcpy(buffer, message);
297 while (j < NMESSAGES) {
298 while ((int) strlen(buffer) < w) {
299 strcat(buffer, " ... ");
300 strcat(buffer, messages[++j % NMESSAGES]);
304 mvwaddnstr(win, height / 2, w - i, buffer, i);
306 mvwaddnstr(win, height / 2, 1, buffer, w);
310 if (wgetch(win) != ERR) {
315 for (k = 0; (buffer[k] = buffer[k + 1]) != '\0'; k++) ;
321 /* Draw running As across in RED */
322 set_colors(win, 7, COLOR_RED, COLOR_GREEN);
323 for (i = 2; i < width - 4; ++i) {
324 k = mvwinch(win, 4, i);
329 mvwaddch(win, 4, i, c);
333 /* Put a message up wait for a key */
335 use_colors(win, 5, A_NORMAL);
336 mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
339 if (WaitForUser(win) == 1)
342 j = 0; /* Restore the old line */
343 for (i = 2; i < width - 4; ++i)
344 mvwaddch(win, 4, i, save[j++]);
348 /* Put a message up wait for a key */
350 use_colors(win, 5, A_NORMAL);
351 mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
353 if (WaitForUser(win) == 1)
357 ExitProgram(EXIT_SUCCESS);