]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/newdemo.c
ncurses 4.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.14 1997/04/06 01:43:32 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, len;
66 char    buffer[80];
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[0];
141         len = strlen(message);
142         j = 0;
143         i = 2;
144         w = width-2;
145         while(j < NMESSAGES)
146         {   strncpy(buffer, message, (size_t)(w - i));
147             buffer[w-i] = 0;
148             mvwaddstr(win, height/2, i, buffer);
149             if(w - i < len)
150             {   memset(buffer, ' ', (size_t)i);
151                 strcpy(buffer, message + (w - i));
152                 buffer[strlen(buffer)]   = ' ';
153                 buffer[i-2] = '\0';
154                 mvwaddstr(win, height/2, 2, buffer);
155             }
156             wrefresh(win);
157             nodelay(win,TRUE);
158             if (wgetch(win) != ERR)
159             {   flushinp();
160                 break;
161             }
162             mvwaddch(win, height/2, i, ' ');
163             i = ++i % w;
164             if(i < 2)
165             {   message = messages[++j%NMESSAGES];
166                 memset(buffer, ' ', (size_t)(w-2));
167                 buffer[w-2] = 0;
168                 mvwaddstr(win, height/2, 2, buffer);
169                 i = 2;
170             }
171             delay_output(100);
172         }
173
174         j = 0;
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)
179         {
180             k = mvwinch(win, 4, i);
181             if (k == ERR)
182                 break;
183             save[j++] = c = k;
184             c &= A_CHARTEXT;
185             mvwaddch(win, 4, i, c);
186         }
187         wrefresh(win);
188
189                                 /* Put a message up wait for a key */
190         i = height-2;
191         wattrset(win, COLOR_PAIR(5));
192         mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
193         wrefresh(win);
194
195         if(WaitForUser(win) == 1)
196             break;
197
198         j = 0;                  /* Restore the old line */
199         for(i=2; i < width - 4; ++i)
200             mvwaddch(win, 4, i, save[j++]);
201         wrefresh(win);
202
203         BouncingBalls(win);
204                                 /* Put a message up wait for a key */
205         i = height-2;
206         wattrset(win, COLOR_PAIR(5));
207         mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
208         wrefresh(win);
209         if(WaitForUser(win) == 1)
210             break;
211     }
212     endwin();
213     return 0;
214 }
215
216 /*
217  * Test sub windows
218  */
219 static int
220 SubWinTest(WINDOW *win)
221 {
222 int     w, h, sw, sh, bx, by;
223 WINDOW  *swin1, *swin2, *swin3;
224
225     w  = win->_maxx;
226     h  = win->_maxy;
227     bx = win->_begx;
228     by = win->_begy;
229     sw = w / 3;
230     sh = h / 3;
231     if((swin1 = subwin(win, sh, sw, by+3, bx+5)) == NULL)
232         return  1;
233     if((swin2 = subwin(win, sh, sw, by+4, bx+8)) == NULL)
234         return  1;
235     if((swin3 = subwin(win, sh, sw, by+5, bx+11)) == NULL)
236         return  1;
237
238     init_pair(8,COLOR_RED,COLOR_BLUE);
239     wattrset(swin1, COLOR_PAIR(8));
240     werase(swin1);
241     mvwaddstr(swin1, 0, 3, "Sub-window 1");
242     wrefresh(swin1);
243
244     init_pair(8,COLOR_CYAN,COLOR_MAGENTA);
245     wattrset(swin2, COLOR_PAIR(8));
246     werase(swin2);
247     mvwaddstr(swin2, 0, 3, "Sub-window 2");
248     wrefresh(swin2);
249
250     init_pair(8,COLOR_YELLOW,COLOR_GREEN);
251     wattrset(swin3, COLOR_PAIR(8));
252     werase(swin3);
253     mvwaddstr(swin3, 0, 3, "Sub-window 3");
254     wrefresh(swin3);
255
256     delwin(swin1);
257     delwin(swin2);
258     delwin(swin3);
259     WaitForUser(win);
260     return  0;
261 }
262
263 /*
264  *  Bouncing balls
265  */
266 static int
267 BouncingBalls(WINDOW *win)
268 {
269 int     w, h;
270 int     x1, y1, xd1, yd1;
271 int     x2, y2, xd2, yd2;
272 int     x3, y3, xd3, yd3;
273
274     w    = win->_maxx;
275     h    = win->_maxy;
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);
282     xd1  = 1; yd1 = 1;
283     xd2  = 1; yd2 = 0;
284     xd3  = 0; yd3 = 1;
285     nodelay(win,TRUE);
286     while(wgetch(win) == ERR)
287     {   x1 = xd1 > 0 ? ++x1 : --x1;
288         if(x1 <= 1 || x1 >= w - 2)
289             xd1 = xd1 ? 0 : 1;
290         y1 = yd1 > 0 ? ++y1 : --y1;
291         if(y1 <= 1 || y1 >= h - 2)
292             yd1 = yd1 ? 0 : 1;
293
294         x2 = xd2 > 0 ? ++x2 : --x2;
295         if(x2 <= 1 || x2 >= w - 2)
296             xd2 = xd2 ? 0 : 1;
297         y2 = yd2 > 0 ? ++y2 : --y2;
298         if(y2 <= 1 || y2 >= h - 2)
299             yd2 = yd2 ? 0 : 1;
300
301         x3 = xd3 > 0 ? ++x3 : --x3;
302         if(x3 <= 1 || x3 >= w - 2)
303             xd3 = xd3 ? 0 : 1;
304         y3 = yd3 > 0 ? ++y3 : --y3;
305         if(y3 <= 1 || y3 >= h - 2)
306             yd3 = yd3 ? 0 : 1;
307
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, '@');
317         wmove(win, 0, 0);
318         wrefresh(win);
319         delay_output(100);
320     }
321     return 0;
322 }
323
324 /*
325  *  Wait for user
326  */
327 static int WaitForUser(WINDOW *win)
328 {
329  time_t  t;
330  chtype key;
331
332  nodelay(win,TRUE);
333  t = time((time_t *)0);
334  while(1)
335    {
336     if ((int)(key = wgetch(win)) != ERR)
337       {
338        if (key  == 'q' || key == 'Q')
339           return  1;
340        else
341           return  0;
342       }
343     if (time((time_t *)0) - t > 5)
344        return  0;
345    }
346 }
347
348 /*
349  *  Trap interrupt
350  */
351 static RETSIGTYPE trap(int sig GCC_UNUSED)
352 {
353     endwin();
354     exit(EXIT_FAILURE);
355 }
356
357 /*  End of DEMO.C */