]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/newdemo.c
ncurses 5.1
[ncurses.git] / test / newdemo.c
1 /*
2  *  newdemo.c   -       A demo program using PDCurses. The program illustrate
3  *                      the use of colours for text output.
4  *
5  * $Id: newdemo.c,v 1.17 1997/09/20 15:11:26 tom Exp $
6  */
7
8 #include <test.priv.h>
9
10 #include <signal.h>
11 #include <time.h>
12 #include <string.h>
13
14 static int SubWinTest(WINDOW *win);
15 static int WaitForUser(WINDOW *win);
16 static int BouncingBalls(WINDOW *win);
17 static RETSIGTYPE trap(int);
18
19 #define delay_output(x) napms(x)
20
21 /*
22  *  The Australian map
23  */
24 const char *AusMap[16] =
25 {
26     "           A           A ",
27     "    N.T. AAAAA       AAAA ",
28     "     AAAAAAAAAAA  AAAAAAAA ",
29     "   AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
30     "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
31     "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
32     " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
33     "   AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
34     "W.A. AAAAAAAAA      AAAAAA Vic.",
35     "       AAA   S.A.     AA",
36     "                       A  Tas.",
37     ""
38 };
39
40 /*
41  *  Funny messages
42  */
43 #define NMESSAGES   6
44
45 NCURSES_CONST char *messages[] =
46 {
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",
53     ""
54 };
55
56 /*
57  *  Main driver
58  */
59 int
60 main(
61         int argc GCC_UNUSED,
62         char *argv[] GCC_UNUSED)
63 {
64 WINDOW  *win;
65 int     w, x, y, i, j, k;
66 char    buffer[200];
67 const char *message;
68 int     width, height;
69 chtype  save[80];
70 chtype  c;
71
72     initscr();
73     start_color();
74     cbreak();
75     signal(SIGINT, trap);
76     width  = 48;
77     height = 14;                /* Create a drawing window */
78     win = newwin(height, width, (LINES-height)/2, (COLS-width)/2);
79     if(win == NULL)
80     {   endwin();
81         return 1;
82     }
83
84     while(1)
85     {   init_pair(1,COLOR_WHITE,COLOR_BLUE);
86         wattrset(win, COLOR_PAIR(1));
87         werase(win);
88
89         init_pair(2,COLOR_RED,COLOR_RED);
90         wattrset(win, COLOR_PAIR(2));
91         box(win, ACS_VLINE, ACS_HLINE);
92         wrefresh(win);
93                                 /* Do ramdom output of a character */
94         wattrset(win, COLOR_PAIR(1));
95         c = 'a';
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);
100             wrefresh(win);
101             nodelay(win,TRUE);
102             if (wgetch(win) != ERR)
103                 break;
104             if(i == 2000)
105             {   c = 'b';
106                 init_pair(3,COLOR_CYAN,COLOR_YELLOW);
107                 wattron(win, COLOR_PAIR(3));
108             }
109         }
110
111         SubWinTest(win);
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);
116         werase(win);
117         wrefresh(win);
118                                 /* Draw RED bounding box */
119         wattrset(win, COLOR_PAIR(2));
120         box(win, ' ', ' ');
121         wrefresh(win);
122                                 /* Display Australia map */
123         wattrset(win, COLOR_PAIR(4) | A_BOLD);
124         i = 0;
125         while(*AusMap[i])
126         {   mvwaddstr(win, i+1, 8, AusMap[i]);
127             wrefresh(win);
128             delay_output(50);
129             ++i;
130         }
131
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");
135         wrefresh(win);
136
137                                 /* Draw running messages */
138         init_pair(6,COLOR_YELLOW,COLOR_WHITE);
139         wattrset(win, COLOR_PAIR(6));
140         message = messages[j = 0];
141         i = 1;
142         w = width-2;
143         strcpy(buffer, message);
144         while(j < NMESSAGES) {
145             while ((int)strlen(buffer) < w) {
146                 strcat(buffer, " ... ");
147                 strcat(buffer, messages[++j % NMESSAGES]);
148             }
149
150             if (i < w)
151                 mvwaddnstr(win, height/2, w - i, buffer, i);
152             else
153                 mvwaddnstr(win, height/2, 1, buffer, w);
154
155             wrefresh(win);
156             nodelay(win,TRUE);
157             if (wgetch(win) != ERR)
158             {   flushinp();
159                 break;
160             }
161             if (i++ >= w) {
162                 for (k = 0; (buffer[k] = buffer[k+1]) != '\0'; k++)
163                     ;
164             }
165             delay_output(100);
166         }
167
168         j = 0;
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)
173         {
174             k = mvwinch(win, 4, i);
175             if (k == ERR)
176                 break;
177             save[j++] = c = k;
178             c &= A_CHARTEXT;
179             mvwaddch(win, 4, i, c);
180         }
181         wrefresh(win);
182
183                                 /* Put a message up wait for a key */
184         i = height-2;
185         wattrset(win, COLOR_PAIR(5));
186         mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
187         wrefresh(win);
188
189         if(WaitForUser(win) == 1)
190             break;
191
192         j = 0;                  /* Restore the old line */
193         for(i=2; i < width - 4; ++i)
194             mvwaddch(win, 4, i, save[j++]);
195         wrefresh(win);
196
197         BouncingBalls(win);
198                                 /* Put a message up wait for a key */
199         i = height-2;
200         wattrset(win, COLOR_PAIR(5));
201         mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
202         wrefresh(win);
203         if(WaitForUser(win) == 1)
204             break;
205     }
206     endwin();
207     return 0;
208 }
209
210 /*
211  * Test sub windows
212  */
213 static int
214 SubWinTest(WINDOW *win)
215 {
216 int     w, h, sw, sh, bx, by;
217 WINDOW  *swin1, *swin2, *swin3;
218
219     getmaxyx(win, h,  w);
220     getbegyx(win, by, bx);
221     sw = w / 3;
222     sh = h / 3;
223     if((swin1 = subwin(win, sh, sw, by+3, bx+5)) == NULL)
224         return  1;
225     if((swin2 = subwin(win, sh, sw, by+4, bx+8)) == NULL)
226         return  1;
227     if((swin3 = subwin(win, sh, sw, by+5, bx+11)) == NULL)
228         return  1;
229
230     init_pair(8,COLOR_RED,COLOR_BLUE);
231     wattrset(swin1, COLOR_PAIR(8));
232     werase(swin1);
233     mvwaddstr(swin1, 0, 3, "Sub-window 1");
234     wrefresh(swin1);
235
236     init_pair(8,COLOR_CYAN,COLOR_MAGENTA);
237     wattrset(swin2, COLOR_PAIR(8));
238     werase(swin2);
239     mvwaddstr(swin2, 0, 3, "Sub-window 2");
240     wrefresh(swin2);
241
242     init_pair(8,COLOR_YELLOW,COLOR_GREEN);
243     wattrset(swin3, COLOR_PAIR(8));
244     werase(swin3);
245     mvwaddstr(swin3, 0, 3, "Sub-window 3");
246     wrefresh(swin3);
247
248     delwin(swin1);
249     delwin(swin2);
250     delwin(swin3);
251     WaitForUser(win);
252     return  0;
253 }
254
255 /*
256  *  Bouncing balls
257  */
258 static int
259 BouncingBalls(WINDOW *win)
260 {
261 int     w, h;
262 int     x1, y1, xd1, yd1;
263 int     x2, y2, xd2, yd2;
264 int     x3, y3, xd3, yd3;
265
266     getmaxyx(win, h, w);
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);
273     xd1  = 1; yd1 = 1;
274     xd2  = 1; yd2 = 0;
275     xd3  = 0; yd3 = 1;
276     nodelay(win,TRUE);
277     while(wgetch(win) == ERR)
278     {   x1 = xd1 > 0 ? ++x1 : --x1;
279         if(x1 <= 1 || x1 >= w - 2)
280             xd1 = xd1 ? 0 : 1;
281         y1 = yd1 > 0 ? ++y1 : --y1;
282         if(y1 <= 1 || y1 >= h - 2)
283             yd1 = yd1 ? 0 : 1;
284
285         x2 = xd2 > 0 ? ++x2 : --x2;
286         if(x2 <= 1 || x2 >= w - 2)
287             xd2 = xd2 ? 0 : 1;
288         y2 = yd2 > 0 ? ++y2 : --y2;
289         if(y2 <= 1 || y2 >= h - 2)
290             yd2 = yd2 ? 0 : 1;
291
292         x3 = xd3 > 0 ? ++x3 : --x3;
293         if(x3 <= 1 || x3 >= w - 2)
294             xd3 = xd3 ? 0 : 1;
295         y3 = yd3 > 0 ? ++y3 : --y3;
296         if(y3 <= 1 || y3 >= h - 2)
297             yd3 = yd3 ? 0 : 1;
298
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, '@');
308         wmove(win, 0, 0);
309         wrefresh(win);
310         delay_output(100);
311     }
312     return 0;
313 }
314
315 /*
316  *  Wait for user
317  */
318 static int WaitForUser(WINDOW *win)
319 {
320  time_t  t;
321  chtype key;
322
323  nodelay(win,TRUE);
324  t = time((time_t *)0);
325  while(1)
326    {
327     if ((int)(key = wgetch(win)) != ERR)
328       {
329        if (key  == 'q' || key == 'Q')
330           return  1;
331        else
332           return  0;
333       }
334     if (time((time_t *)0) - t > 5)
335        return  0;
336    }
337 }
338
339 /*
340  *  Trap interrupt
341  */
342 static RETSIGTYPE trap(int sig GCC_UNUSED)
343 {
344     endwin();
345     exit(EXIT_FAILURE);
346 }
347
348 /*  End of DEMO.C */