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.29 2006/12/03 00:15:28 tom Exp $
15 static const char *AusMap[16] =
19 " AAAAAAAAAAA AAAAAAAA ",
20 " AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
21 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
22 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
23 " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
24 " AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
25 "W.A. AAAAAAAAA AAAAAA Vic.",
36 static NCURSES_CONST char *messages[] =
38 "Hello from the Land Down Under",
39 "The Land of crocs. and a big Red Rock",
40 "Where the sunflower runs along the highways",
41 "the dusty red roads lead one to loneliness",
42 "Blue sky in the morning and",
43 "freezing nights and twinkling stars",
51 trap(int sig GCC_UNUSED)
54 ExitProgram(EXIT_FAILURE);
61 WaitForUser(WINDOW *win)
67 t = time((time_t *) 0);
69 if ((int) (key = wgetch(win)) != ERR) {
70 if (key == 'q' || key == 'Q')
75 if (time((time_t *) 0) - t > 5)
81 set_colors(WINDOW *win, int pair, int foreground, int background)
84 if (pair > COLOR_PAIRS)
86 init_pair(pair, foreground, background);
87 wattrset(win, COLOR_PAIR(pair));
92 use_colors(WINDOW *win, int pair, chtype attrs)
95 if (pair > COLOR_PAIRS)
97 attrs |= COLOR_PAIR(pair);
107 SubWinTest(WINDOW *win)
109 int w, h, sw, sh, bx, by;
110 WINDOW *swin1, *swin2, *swin3;
113 getbegyx(win, by, bx);
116 if ((swin1 = subwin(win, sh, sw, by + 3, bx + 5)) == NULL)
118 if ((swin2 = subwin(win, sh, sw, by + 4, bx + 8)) == NULL)
120 if ((swin3 = subwin(win, sh, sw, by + 5, bx + 11)) == NULL)
123 set_colors(swin1, 8, COLOR_RED, COLOR_BLUE);
125 mvwaddstr(swin1, 0, 3, "Sub-window 1");
128 set_colors(swin2, 9, COLOR_CYAN, COLOR_MAGENTA);
130 mvwaddstr(swin2, 0, 3, "Sub-window 2");
133 set_colors(swin3, 10, COLOR_YELLOW, COLOR_GREEN);
135 mvwaddstr(swin3, 0, 3, "Sub-window 3");
146 bounce(int n, int *dir, int len)
152 if (n <= 1 || n >= len - 2)
161 BouncingBalls(WINDOW *win)
164 int x1, y1, xd1, yd1;
165 int x2, y2, xd2, yd2;
166 int x3, y3, xd3, yd3;
170 x1 = 2 + rand() % (w - 4);
171 y1 = 2 + rand() % (h - 4);
172 x2 = 2 + rand() % (w - 4);
173 y2 = 2 + rand() % (h - 4);
174 x3 = 2 + rand() % (w - 4);
175 y3 = 2 + rand() % (h - 4);
186 while (wgetch(win) == ERR) {
187 x1 = bounce(x1, &xd1, w);
188 y1 = bounce(y1, &yd1, h);
189 x2 = bounce(x2, &xd2, w);
190 y2 = bounce(y2, &yd2, h);
191 x3 = bounce(x3, &xd3, w);
192 y3 = bounce(y3, &yd3, h);
194 set_colors(win, 11, COLOR_RED, COLOR_BLUE);
195 mvwaddch(win, y1, x1, 'O');
197 set_colors(win, 12, COLOR_BLUE, COLOR_RED);
198 mvwaddch(win, y2, x2, '*');
200 set_colors(win, 13, COLOR_YELLOW, COLOR_WHITE);
201 mvwaddch(win, y3, x3, '@');
214 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
217 int w, x, y, i, j, k;
224 setlocale(LC_ALL, "");
234 height = 14; /* Create a drawing window */
235 win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
238 ExitProgram(EXIT_FAILURE);
242 set_colors(win, 1, COLOR_WHITE, COLOR_BLUE);
245 set_colors(win, 2, COLOR_RED, COLOR_RED);
246 box(win, ACS_VLINE, ACS_HLINE);
248 /* Do ramdom output of a character */
249 use_colors(win, 1, A_NORMAL);
251 for (i = 0; i < 5000; ++i) {
252 x = rand() % (width - 2) + 1;
253 y = rand() % (height - 2) + 1;
254 mvwaddch(win, y, x, c);
257 if (wgetch(win) != ERR)
261 set_colors(win, 3, COLOR_CYAN, COLOR_YELLOW);
266 /* Erase and draw green window */
267 set_colors(win, 4, COLOR_YELLOW, COLOR_GREEN);
268 wbkgd(win, use_colors(win, 4, A_BOLD));
271 /* Draw RED bounding box */
272 use_colors(win, 2, A_NORMAL);
275 /* Display Australia map */
276 use_colors(win, 4, A_BOLD);
279 mvwaddstr(win, i + 1, 8, AusMap[i]);
285 set_colors(win, 5, COLOR_BLUE, COLOR_WHITE);
286 use_colors(win, 5, A_BLINK);
287 mvwaddstr(win, height - 2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
290 /* Draw running messages */
291 set_colors(win, 6, COLOR_YELLOW, COLOR_WHITE);
292 message = messages[j = 0];
295 strcpy(buffer, message);
296 while (j < NMESSAGES) {
297 while ((int) strlen(buffer) < w) {
298 strcat(buffer, " ... ");
299 strcat(buffer, messages[++j % NMESSAGES]);
303 mvwaddnstr(win, height / 2, w - i, buffer, i);
305 mvwaddnstr(win, height / 2, 1, buffer, w);
309 if (wgetch(win) != ERR) {
314 for (k = 0; (buffer[k] = buffer[k + 1]) != '\0'; k++) ;
320 /* Draw running As across in RED */
321 set_colors(win, 7, COLOR_RED, COLOR_GREEN);
322 for (i = 2; i < width - 4; ++i) {
323 k = mvwinch(win, 4, i);
328 mvwaddch(win, 4, i, c);
332 /* Put a message up wait for a key */
334 use_colors(win, 5, A_NORMAL);
335 mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
338 if (WaitForUser(win) == 1)
341 j = 0; /* Restore the old line */
342 for (i = 2; i < width - 4; ++i)
343 mvwaddch(win, 4, i, save[j++]);
347 /* Put a message up wait for a key */
349 use_colors(win, 5, A_NORMAL);
350 mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
352 if (WaitForUser(win) == 1)
356 ExitProgram(EXIT_SUCCESS);