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.17 1997/09/20 15:11:26 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)
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[j = 0];
143 strcpy(buffer, message);
144 while(j < NMESSAGES) {
145 while ((int)strlen(buffer) < w) {
146 strcat(buffer, " ... ");
147 strcat(buffer, messages[++j % NMESSAGES]);
151 mvwaddnstr(win, height/2, w - i, buffer, i);
153 mvwaddnstr(win, height/2, 1, buffer, w);
157 if (wgetch(win) != ERR)
162 for (k = 0; (buffer[k] = buffer[k+1]) != '\0'; k++)
169 /* Draw running As across in RED */
170 init_pair(7,COLOR_RED,COLOR_GREEN);
171 wattron(win, COLOR_PAIR(7));
172 for(i=2; i < width - 4; ++i)
174 k = mvwinch(win, 4, i);
179 mvwaddch(win, 4, i, c);
183 /* Put a message up wait for a key */
185 wattrset(win, COLOR_PAIR(5));
186 mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
189 if(WaitForUser(win) == 1)
192 j = 0; /* Restore the old line */
193 for(i=2; i < width - 4; ++i)
194 mvwaddch(win, 4, i, save[j++]);
198 /* Put a message up wait for a key */
200 wattrset(win, COLOR_PAIR(5));
201 mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
203 if(WaitForUser(win) == 1)
214 SubWinTest(WINDOW *win)
216 int w, h, sw, sh, bx, by;
217 WINDOW *swin1, *swin2, *swin3;
220 getbegyx(win, by, bx);
223 if((swin1 = subwin(win, sh, sw, by+3, bx+5)) == NULL)
225 if((swin2 = subwin(win, sh, sw, by+4, bx+8)) == NULL)
227 if((swin3 = subwin(win, sh, sw, by+5, bx+11)) == NULL)
230 init_pair(8,COLOR_RED,COLOR_BLUE);
231 wattrset(swin1, COLOR_PAIR(8));
233 mvwaddstr(swin1, 0, 3, "Sub-window 1");
236 init_pair(8,COLOR_CYAN,COLOR_MAGENTA);
237 wattrset(swin2, COLOR_PAIR(8));
239 mvwaddstr(swin2, 0, 3, "Sub-window 2");
242 init_pair(8,COLOR_YELLOW,COLOR_GREEN);
243 wattrset(swin3, COLOR_PAIR(8));
245 mvwaddstr(swin3, 0, 3, "Sub-window 3");
259 BouncingBalls(WINDOW *win)
262 int x1, y1, xd1, yd1;
263 int x2, y2, xd2, yd2;
264 int x3, y3, xd3, yd3;
267 x1 = 2 + rand() % (w - 4);
268 y1 = 2 + rand() % (h - 4);
269 x2 = 2 + rand() % (w - 4);
270 y2 = 2 + rand() % (h - 4);
271 x3 = 2 + rand() % (w - 4);
272 y3 = 2 + rand() % (h - 4);
277 while(wgetch(win) == ERR)
278 { x1 = xd1 > 0 ? ++x1 : --x1;
279 if(x1 <= 1 || x1 >= w - 2)
281 y1 = yd1 > 0 ? ++y1 : --y1;
282 if(y1 <= 1 || y1 >= h - 2)
285 x2 = xd2 > 0 ? ++x2 : --x2;
286 if(x2 <= 1 || x2 >= w - 2)
288 y2 = yd2 > 0 ? ++y2 : --y2;
289 if(y2 <= 1 || y2 >= h - 2)
292 x3 = xd3 > 0 ? ++x3 : --x3;
293 if(x3 <= 1 || x3 >= w - 2)
295 y3 = yd3 > 0 ? ++y3 : --y3;
296 if(y3 <= 1 || y3 >= h - 2)
299 init_pair(8,COLOR_RED,COLOR_BLUE);
300 wattrset(win, COLOR_PAIR(8));
301 mvwaddch(win, y1, x1, 'O');
302 init_pair(8,COLOR_BLUE,COLOR_RED);
303 wattrset(win, COLOR_PAIR(8));
304 mvwaddch(win, y2, x2, '*');
305 init_pair(8,COLOR_YELLOW,COLOR_WHITE);
306 wattrset(win, COLOR_PAIR(8));
307 mvwaddch(win, y3, x3, '@');
318 static int WaitForUser(WINDOW *win)
324 t = time((time_t *)0);
327 if ((int)(key = wgetch(win)) != ERR)
329 if (key == 'q' || key == 'Q')
334 if (time((time_t *)0) - t > 5)
342 static RETSIGTYPE trap(int sig GCC_UNUSED)