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.14 1997/04/06 01:43:32 tom Exp $
14 static int SubWinTest(WINDOW *win);
15 static int WaitForUser(WINDOW *win);
16 static int BouncingBalls(WINDOW *win);
17 static RETSIGTYPE trap(int);
19 #define delay_output(x) napms(x)
24 const char *AusMap[16] =
28 " AAAAAAAAAAA AAAAAAAA ",
29 " AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
30 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
31 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
32 " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
33 " AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
34 "W.A. AAAAAAAAA AAAAAA Vic.",
45 NCURSES_CONST char *messages[] =
47 "Hello from the Land Down Under",
48 "The Land of crocs. and a big Red Rock",
49 "Where the sunflower runs along the highways",
50 "the dusty red roads lead one to loneliness",
51 "Blue sky in the morning and",
52 "freezing nights and twinkling stars",
62 char *argv[] GCC_UNUSED)
65 int w, x, y, i, j, k, len;
77 height = 14; /* Create a drawing window */
78 win = newwin(height, width, (LINES-height)/2, (COLS-width)/2);
85 { init_pair(1,COLOR_WHITE,COLOR_BLUE);
86 wattrset(win, COLOR_PAIR(1));
89 init_pair(2,COLOR_RED,COLOR_RED);
90 wattrset(win, COLOR_PAIR(2));
91 box(win, ACS_VLINE, ACS_HLINE);
93 /* Do ramdom output of a character */
94 wattrset(win, COLOR_PAIR(1));
96 for(i=0; i < 5000; ++i)
97 { x = rand() % (width-2) + 1;
98 y = rand() % (height-2) + 1;
99 mvwaddch(win, y, x, c);
102 if (wgetch(win) != ERR)
106 init_pair(3,COLOR_CYAN,COLOR_YELLOW);
107 wattron(win, COLOR_PAIR(3));
112 /* Erase and draw green window */
113 init_pair(4,COLOR_YELLOW,COLOR_GREEN);
114 wbkgd(win, COLOR_PAIR(4) | A_BOLD);
115 wattrset(win, COLOR_PAIR(4) | A_BOLD);
118 /* Draw RED bounding box */
119 wattrset(win, COLOR_PAIR(2));
122 /* Display Australia map */
123 wattrset(win, COLOR_PAIR(4) | A_BOLD);
126 { mvwaddstr(win, i+1, 8, AusMap[i]);
132 init_pair(5,COLOR_BLUE,COLOR_WHITE);
133 wattrset(win, COLOR_PAIR(5) | A_BLINK);
134 mvwaddstr(win, height-2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
137 /* Draw running messages */
138 init_pair(6,COLOR_YELLOW,COLOR_WHITE);
139 wattrset(win, COLOR_PAIR(6));
140 message = messages[0];
141 len = strlen(message);
146 { strncpy(buffer, message, (size_t)(w - i));
148 mvwaddstr(win, height/2, i, buffer);
150 { memset(buffer, ' ', (size_t)i);
151 strcpy(buffer, message + (w - i));
152 buffer[strlen(buffer)] = ' ';
154 mvwaddstr(win, height/2, 2, buffer);
158 if (wgetch(win) != ERR)
162 mvwaddch(win, height/2, i, ' ');
165 { message = messages[++j%NMESSAGES];
166 memset(buffer, ' ', (size_t)(w-2));
168 mvwaddstr(win, height/2, 2, buffer);
175 /* Draw running As across in RED */
176 init_pair(7,COLOR_RED,COLOR_GREEN);
177 wattron(win, COLOR_PAIR(7));
178 for(i=2; i < width - 4; ++i)
180 k = mvwinch(win, 4, i);
185 mvwaddch(win, 4, i, c);
189 /* Put a message up wait for a key */
191 wattrset(win, COLOR_PAIR(5));
192 mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
195 if(WaitForUser(win) == 1)
198 j = 0; /* Restore the old line */
199 for(i=2; i < width - 4; ++i)
200 mvwaddch(win, 4, i, save[j++]);
204 /* Put a message up wait for a key */
206 wattrset(win, COLOR_PAIR(5));
207 mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
209 if(WaitForUser(win) == 1)
220 SubWinTest(WINDOW *win)
222 int w, h, sw, sh, bx, by;
223 WINDOW *swin1, *swin2, *swin3;
231 if((swin1 = subwin(win, sh, sw, by+3, bx+5)) == NULL)
233 if((swin2 = subwin(win, sh, sw, by+4, bx+8)) == NULL)
235 if((swin3 = subwin(win, sh, sw, by+5, bx+11)) == NULL)
238 init_pair(8,COLOR_RED,COLOR_BLUE);
239 wattrset(swin1, COLOR_PAIR(8));
241 mvwaddstr(swin1, 0, 3, "Sub-window 1");
244 init_pair(8,COLOR_CYAN,COLOR_MAGENTA);
245 wattrset(swin2, COLOR_PAIR(8));
247 mvwaddstr(swin2, 0, 3, "Sub-window 2");
250 init_pair(8,COLOR_YELLOW,COLOR_GREEN);
251 wattrset(swin3, COLOR_PAIR(8));
253 mvwaddstr(swin3, 0, 3, "Sub-window 3");
267 BouncingBalls(WINDOW *win)
270 int x1, y1, xd1, yd1;
271 int x2, y2, xd2, yd2;
272 int x3, y3, xd3, yd3;
276 x1 = 2 + rand() % (w - 4);
277 y1 = 2 + rand() % (h - 4);
278 x2 = 2 + rand() % (w - 4);
279 y2 = 2 + rand() % (h - 4);
280 x3 = 2 + rand() % (w - 4);
281 y3 = 2 + rand() % (h - 4);
286 while(wgetch(win) == ERR)
287 { x1 = xd1 > 0 ? ++x1 : --x1;
288 if(x1 <= 1 || x1 >= w - 2)
290 y1 = yd1 > 0 ? ++y1 : --y1;
291 if(y1 <= 1 || y1 >= h - 2)
294 x2 = xd2 > 0 ? ++x2 : --x2;
295 if(x2 <= 1 || x2 >= w - 2)
297 y2 = yd2 > 0 ? ++y2 : --y2;
298 if(y2 <= 1 || y2 >= h - 2)
301 x3 = xd3 > 0 ? ++x3 : --x3;
302 if(x3 <= 1 || x3 >= w - 2)
304 y3 = yd3 > 0 ? ++y3 : --y3;
305 if(y3 <= 1 || y3 >= h - 2)
308 init_pair(8,COLOR_RED,COLOR_BLUE);
309 wattrset(win, COLOR_PAIR(8));
310 mvwaddch(win, y1, x1, 'O');
311 init_pair(8,COLOR_BLUE,COLOR_RED);
312 wattrset(win, COLOR_PAIR(8));
313 mvwaddch(win, y2, x2, '*');
314 init_pair(8,COLOR_YELLOW,COLOR_WHITE);
315 wattrset(win, COLOR_PAIR(8));
316 mvwaddch(win, y3, x3, '@');
327 static int WaitForUser(WINDOW *win)
333 t = time((time_t *)0);
336 if ((int)(key = wgetch(win)) != ERR)
338 if (key == 'q' || key == 'Q')
343 if (time((time_t *)0) - t > 5)
351 static RETSIGTYPE trap(int sig GCC_UNUSED)