1 /*
2 * newdemo.c - A demo program using PDCurses. The program
3 * illustrates the use of colors for text output.
4 *
5 * Hacks by [email protected] on 12/29/96
6 *
7 * $Id: newdemo.c,v 1.39 2008/07/13 16:08:17 wmcbrine Exp $
8 */
9
10 #include <stdio.h>
11 #include <signal.h>
12 #include <string.h>
13 #include <curses.h>
14 #include <stdlib.h>
15 #include <time.h>
16
17 int WaitForUser(void);
18 int SubWinTest(WINDOW *);
19 int BouncingBalls(WINDOW *);
20 void trap(int);
21
22 /* An ASCII map of Australia */
23
24 char *AusMap[17] =
25 {
26 " A ",
27 " AA AA ",
28 " N.T. AAAAA AAAA ",
29 " AAAAAAAAAAA AAAAAAAA ",
30 " AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
31 " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
32 " AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
33 " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
34 " AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
35 "W.A. AAAAAAAAA AAAAAA Vic.",
36 " AAA S.A. AA",
37 " A Tas.",
38 ""
39 };
40
41 /* Funny messages for the scroller */
42
43 char *messages[] =
44 {
45 "Hello from the Land Down Under",
46 "The Land of crocs, and a big Red Rock",
47 "Where the sunflower runs along the highways",
48 "The dusty red roads lead one to loneliness",
49 "Blue sky in the morning and",
50 "Freezing nights and twinkling stars",
51 NULL
52 };
53
WaitForUser(void)54 int WaitForUser(void)
55 {
56 chtype ch;
57
58 nodelay(stdscr, TRUE);
59 halfdelay(50);
60
61 ch = getch();
62
63 nodelay(stdscr, FALSE);
64 nocbreak(); /* Reset the halfdelay() value */
65 cbreak();
66
67 return (ch == '\033') ? ch : 0;
68 }
69
SubWinTest(WINDOW * win)70 int SubWinTest(WINDOW *win)
71 {
72 WINDOW *swin1, *swin2, *swin3;
73 int w, h, sw, sh, bx, by;
74
75 wattrset(win, 0);
76 getmaxyx(win, h, w);
77 getbegyx(win, by, bx);
78
79 sw = w / 3;
80 sh = h / 3;
81
82 if ((swin1 = derwin(win, sh, sw, 3, 5)) == NULL)
83 return 1;
84 if ((swin2 = subwin(win, sh, sw, by + 4, bx + 8)) == NULL)
85 return 1;
86 if ((swin3 = subwin(win, sh, sw, by + 5, bx + 11)) == NULL)
87 return 1;
88
89 init_pair(8, COLOR_RED, COLOR_BLUE);
90 wbkgd(swin1, COLOR_PAIR(8));
91 werase(swin1);
92 mvwaddstr(swin1, 0, 3, "Sub-window 1");
93 wrefresh(swin1);
94
95 init_pair(9, COLOR_CYAN, COLOR_MAGENTA);
96 wbkgd(swin2, COLOR_PAIR(9));
97 werase(swin2);
98 mvwaddstr(swin2, 0, 3, "Sub-window 2");
99 wrefresh(swin2);
100
101 init_pair(10, COLOR_YELLOW, COLOR_GREEN);
102 wbkgd(swin3, COLOR_PAIR(10));
103 werase(swin3);
104 mvwaddstr(swin3, 0, 3, "Sub-window 3");
105 wrefresh(swin3);
106
107 delwin(swin1);
108 delwin(swin2);
109 delwin(swin3);
110 WaitForUser();
111
112 return 0;
113 }
114
BouncingBalls(WINDOW * win)115 int BouncingBalls(WINDOW *win)
116 {
117 chtype c1, c2, c3, ball1, ball2, ball3;
118 int w, h, x1, y1, xd1, yd1, x2, y2, xd2, yd2, x3, y3, xd3, yd3, c;
119
120 curs_set(0);
121
122 wbkgd(win, COLOR_PAIR(1));
123 wrefresh(win);
124 wattrset(win, 0);
125
126 init_pair(11, COLOR_RED, COLOR_GREEN);
127 init_pair(12, COLOR_BLUE, COLOR_RED);
128 init_pair(13, COLOR_YELLOW, COLOR_WHITE);
129
130 ball1 = 'O' | COLOR_PAIR(11);
131 ball2 = '*' | COLOR_PAIR(12);
132 ball3 = '@' | COLOR_PAIR(13);
133
134 getmaxyx(win, h, w);
135
136 x1 = 2 + rand() % (w - 4);
137 y1 = 2 + rand() % (h - 4);
138 x2 = 2 + rand() % (w - 4);
139 y2 = 2 + rand() % (h - 4);
140 x3 = 2 + rand() % (w - 4);
141 y3 = 2 + rand() % (h - 4);
142
143 xd1 = 1;
144 yd1 = 1;
145 xd2 = 1;
146 yd2 = -1;
147 xd3 = -1;
148 yd3 = 1;
149
150 nodelay(stdscr, TRUE);
151
152 while ((c = getch()) == ERR)
153 {
154 x1 += xd1;
155 if (x1 <= 1 || x1 >= w - 2)
156 xd1 *= -1;
157
158 y1 += yd1;
159 if (y1 <= 1 || y1 >= h - 2)
160 yd1 *= -1;
161
162 x2 += xd2;
163 if (x2 <= 1 || x2 >= w - 2)
164 xd2 *= -1;
165
166 y2 += yd2;
167 if (y2 <= 1 || y2 >= h - 2)
168 yd2 *= -1;
169
170 x3 += xd3;
171 if (x3 <= 1 || x3 >= w - 2)
172 xd3 *= -1;
173
174 y3 += yd3;
175 if (y3 <= 1 || y3 >= h - 2)
176 yd3 *= -1;
177
178 c1 = mvwinch(win, y1, x1);
179 c2 = mvwinch(win, y2, x2);
180 c3 = mvwinch(win, y3, x3);
181
182 mvwaddch(win, y1, x1, ball1);
183 mvwaddch(win, y2, x2, ball2);
184 mvwaddch(win, y3, x3, ball3);
185
186 wmove(win, 0, 0);
187 wrefresh(win);
188
189 mvwaddch(win, y1, x1, c1);
190 mvwaddch(win, y2, x2, c2);
191 mvwaddch(win, y3, x3, c3);
192
193 napms(150);
194 }
195
196 nodelay(stdscr, FALSE);
197 ungetch(c);
198 return 0;
199 }
200
201 /* Trap interrupt */
202
trap(int sig)203 void trap(int sig)
204 {
205 if (sig == SIGINT)
206 {
207 endwin();
208
209 exit(0);
210 }
211 }
212
main(int argc,char ** argv)213 int main(int argc, char **argv)
214 {
215 WINDOW *win;
216 chtype save[80], ch;
217 int width, height, w, x, y, i, j, seed;
218
219 #ifdef XCURSES
220 Xinitscr(argc, argv);
221 #else
222 initscr();
223 #endif
224 seed = time((time_t *)0);
225 srand(seed);
226
227 start_color();
228 # if defined(NCURSES_VERSION) || (defined(PDC_BUILD) && PDC_BUILD > 3000)
229 use_default_colors();
230 # endif
231 cbreak();
232 noecho();
233
234 curs_set(0);
235
236 #if !defined(__TURBOC__) && !defined(OS2)
237 signal(SIGINT, trap);
238 #endif
239 noecho();
240
241 /* refresh stdscr so that reading from it will not cause it to
242 overwrite the other windows that are being created */
243
244 refresh();
245
246 /* Create a drawing window */
247
248 width = 48;
249 height = 15;
250
251 win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
252
253 if (win == NULL)
254 {
255 endwin();
256
257 return 1;
258 }
259
260 for (;;)
261 {
262 init_pair(1, COLOR_WHITE, COLOR_BLUE);
263 wbkgd(win, COLOR_PAIR(1));
264 werase(win);
265
266 init_pair(2, COLOR_RED, COLOR_RED);
267 wattrset(win, COLOR_PAIR(2));
268 box(win, ' ', ' ');
269 wrefresh(win);
270
271 wattrset(win, 0);
272
273 /* Do random output of a character */
274
275 ch = 'a';
276
277 nodelay(stdscr, TRUE);
278
279 for (i = 0; i < 5000; ++i)
280 {
281 x = rand() % (width - 2) + 1;
282 y = rand() % (height - 2) + 1;
283
284 mvwaddch(win, y, x, ch);
285 wrefresh(win);
286
287 if (getch() != ERR)
288 break;
289
290 if (i == 2000)
291 {
292 ch = 'b';
293 init_pair(3, COLOR_CYAN, COLOR_YELLOW);
294 wattrset(win, COLOR_PAIR(3));
295 }
296 }
297
298 nodelay(stdscr, FALSE);
299
300 SubWinTest(win);
301
302 /* Erase and draw green window */
303
304 init_pair(4, COLOR_YELLOW, COLOR_GREEN);
305 wbkgd(win, COLOR_PAIR(4));
306 wattrset(win, A_BOLD);
307 werase(win);
308 wrefresh(win);
309
310 /* Draw RED bounding box */
311
312 wattrset(win, COLOR_PAIR(2));
313 box(win, ' ', ' ');
314 wrefresh(win);
315
316 /* Display Australia map */
317
318 wattrset(win, A_BOLD);
319 i = 0;
320
321 while (*AusMap[i])
322 {
323 mvwaddstr(win, i + 1, 8, AusMap[i]);
324 wrefresh(win);
325 napms(100);
326 ++i;
327 }
328
329 init_pair(5, COLOR_BLUE, COLOR_WHITE);
330 wattrset(win, COLOR_PAIR(5) | A_BLINK);
331 mvwaddstr(win, height - 2, 3,
332 " PDCurses 3.4 - DOS, OS/2, Win32, X11, SDL");
333 wrefresh(win);
334
335 /* Draw running messages */
336
337 init_pair(6, COLOR_BLACK, COLOR_WHITE);
338 wattrset(win, COLOR_PAIR(6));
339 w = width - 2;
340 nodelay(win, TRUE);
341
342 /* jbuhler's re-hacked scrolling messages */
343
344 for (j = 0; messages[j] != NULL; j++)
345 {
346 char *message = messages[j];
347 int msg_len = strlen(message);
348 int scroll_len = w + 2 * msg_len;
349 char *scrollbuf = malloc(scroll_len);
350 char *visbuf = scrollbuf + msg_len;
351 int stop = 0;
352 int i;
353
354 for (i = w + msg_len; i > 0; i--)
355 {
356 memset(visbuf, ' ', w);
357 strncpy(scrollbuf + i, message, msg_len);
358 mvwaddnstr(win, height / 2, 1, visbuf, w);
359 wrefresh(win);
360
361 if (wgetch(win) != ERR)
362 {
363 flushinp();
364 stop = 1;
365 break;
366 }
367
368 napms(100);
369 }
370
371 free(scrollbuf);
372
373 if (stop)
374 break;
375 }
376
377 j = 0;
378
379 /* Draw running 'A's across in RED */
380
381 init_pair(7, COLOR_RED, COLOR_GREEN);
382 wattron(win, COLOR_PAIR(7));
383
384 for (i = 2; i < width - 4; ++i)
385 {
386 ch = mvwinch(win, 5, i);
387 save[j++] = ch;
388 ch = ch & 0x7f;
389 mvwaddch(win, 5, i, ch);
390 }
391
392 wrefresh(win);
393
394 /* Put a message up; wait for a key */
395
396 i = height - 2;
397 wattrset(win, COLOR_PAIR(5));
398 mvwaddstr(win, i, 3,
399 " Type a key to continue or ESC to quit ");
400 wrefresh(win);
401
402 if (WaitForUser() == '\033')
403 break;
404
405 /* Restore the old line */
406
407 wattrset(win, 0);
408
409 for (i = 2, j = 0; i < width - 4; ++i)
410 mvwaddch(win, 5, i, save[j++]);
411
412 wrefresh(win);
413
414 BouncingBalls(win);
415
416 /* BouncingBalls() leaves a keystroke in the queue */
417
418 if (WaitForUser() == '\033')
419 break;
420 }
421
422 endwin();
423
424 return 0;
425 }
426