2 * newdemo.c - A demo program using PDCurses. The program
3 * illustrates the use of colors for text output.
5 * Hacks by jbuhler@cs.washington.edu on 12/29/96
7 * $Id: newdemo.c,v 1.39 2008/07/13 16:08:17 wmcbrine Exp $
17 int WaitForUser(void);
18 int SubWinTest(WINDOW
*);
19 int BouncingBalls(WINDOW
*);
22 /* An ASCII map of Australia */
29 " AAAAAAAAAAA AAAAAAAA ",
30 " AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
31 " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
32 " AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
33 " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
34 " AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
35 "W.A. AAAAAAAAA AAAAAA Vic.",
41 /* Funny messages for the scroller */
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",
58 nodelay(stdscr
, TRUE
);
63 nodelay(stdscr
, FALSE
);
64 nocbreak(); /* Reset the halfdelay() value */
67 return (ch
== '\033') ? ch
: 0;
70 int SubWinTest(WINDOW
*win
)
72 WINDOW
*swin1
, *swin2
, *swin3
;
73 int w
, h
, sw
, sh
, bx
, by
;
77 getbegyx(win
, by
, bx
);
82 if ((swin1
= derwin(win
, sh
, sw
, 3, 5)) == NULL
)
84 if ((swin2
= subwin(win
, sh
, sw
, by
+ 4, bx
+ 8)) == NULL
)
86 if ((swin3
= subwin(win
, sh
, sw
, by
+ 5, bx
+ 11)) == NULL
)
89 init_pair(8, COLOR_RED
, COLOR_BLUE
);
90 wbkgd(swin1
, COLOR_PAIR(8));
92 mvwaddstr(swin1
, 0, 3, "Sub-window 1");
95 init_pair(9, COLOR_CYAN
, COLOR_MAGENTA
);
96 wbkgd(swin2
, COLOR_PAIR(9));
98 mvwaddstr(swin2
, 0, 3, "Sub-window 2");
101 init_pair(10, COLOR_YELLOW
, COLOR_GREEN
);
102 wbkgd(swin3
, COLOR_PAIR(10));
104 mvwaddstr(swin3
, 0, 3, "Sub-window 3");
115 int BouncingBalls(WINDOW
*win
)
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
;
122 wbkgd(win
, COLOR_PAIR(1));
126 init_pair(11, COLOR_RED
, COLOR_GREEN
);
127 init_pair(12, COLOR_BLUE
, COLOR_RED
);
128 init_pair(13, COLOR_YELLOW
, COLOR_WHITE
);
130 ball1
= 'O' | COLOR_PAIR(11);
131 ball2
= '*' | COLOR_PAIR(12);
132 ball3
= '@' | COLOR_PAIR(13);
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);
150 nodelay(stdscr
, TRUE
);
152 while ((c
= getch()) == ERR
)
155 if (x1
<= 1 || x1
>= w
- 2)
159 if (y1
<= 1 || y1
>= h
- 2)
163 if (x2
<= 1 || x2
>= w
- 2)
167 if (y2
<= 1 || y2
>= h
- 2)
171 if (x3
<= 1 || x3
>= w
- 2)
175 if (y3
<= 1 || y3
>= h
- 2)
178 c1
= mvwinch(win
, y1
, x1
);
179 c2
= mvwinch(win
, y2
, x2
);
180 c3
= mvwinch(win
, y3
, x3
);
182 mvwaddch(win
, y1
, x1
, ball1
);
183 mvwaddch(win
, y2
, x2
, ball2
);
184 mvwaddch(win
, y3
, x3
, ball3
);
189 mvwaddch(win
, y1
, x1
, c1
);
190 mvwaddch(win
, y2
, x2
, c2
);
191 mvwaddch(win
, y3
, x3
, c3
);
196 nodelay(stdscr
, FALSE
);
213 int main(int argc
, char **argv
)
217 int width
, height
, w
, x
, y
, i
, j
, seed
;
220 Xinitscr(argc
, argv
);
224 seed
= time((time_t *)0);
228 # if defined(NCURSES_VERSION) || (defined(PDC_BUILD) && PDC_BUILD > 3000)
229 use_default_colors();
236 #if !defined(__TURBOC__) && !defined(OS2)
237 signal(SIGINT
, trap
);
241 /* refresh stdscr so that reading from it will not cause it to
242 overwrite the other windows that are being created */
246 /* Create a drawing window */
251 win
= newwin(height
, width
, (LINES
- height
) / 2, (COLS
- width
) / 2);
262 init_pair(1, COLOR_WHITE
, COLOR_BLUE
);
263 wbkgd(win
, COLOR_PAIR(1));
266 init_pair(2, COLOR_RED
, COLOR_RED
);
267 wattrset(win
, COLOR_PAIR(2));
273 /* Do random output of a character */
277 nodelay(stdscr
, TRUE
);
279 for (i
= 0; i
< 5000; ++i
)
281 x
= rand() % (width
- 2) + 1;
282 y
= rand() % (height
- 2) + 1;
284 mvwaddch(win
, y
, x
, ch
);
293 init_pair(3, COLOR_CYAN
, COLOR_YELLOW
);
294 wattrset(win
, COLOR_PAIR(3));
298 nodelay(stdscr
, FALSE
);
302 /* Erase and draw green window */
304 init_pair(4, COLOR_YELLOW
, COLOR_GREEN
);
305 wbkgd(win
, COLOR_PAIR(4));
306 wattrset(win
, A_BOLD
);
310 /* Draw RED bounding box */
312 wattrset(win
, COLOR_PAIR(2));
316 /* Display Australia map */
318 wattrset(win
, A_BOLD
);
323 mvwaddstr(win
, i
+ 1, 8, AusMap
[i
]);
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");
335 /* Draw running messages */
337 init_pair(6, COLOR_BLACK
, COLOR_WHITE
);
338 wattrset(win
, COLOR_PAIR(6));
342 /* jbuhler's re-hacked scrolling messages */
344 for (j
= 0; messages
[j
] != NULL
; j
++)
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
;
354 for (i
= w
+ msg_len
; i
> 0; i
--)
356 memset(visbuf
, ' ', w
);
357 strncpy(scrollbuf
+ i
, message
, msg_len
);
358 mvwaddnstr(win
, height
/ 2, 1, visbuf
, w
);
361 if (wgetch(win
) != ERR
)
379 /* Draw running 'A's across in RED */
381 init_pair(7, COLOR_RED
, COLOR_GREEN
);
382 wattron(win
, COLOR_PAIR(7));
384 for (i
= 2; i
< width
- 4; ++i
)
386 ch
= mvwinch(win
, 5, i
);
389 mvwaddch(win
, 5, i
, ch
);
394 /* Put a message up; wait for a key */
397 wattrset(win
, COLOR_PAIR(5));
399 " Type a key to continue or ESC to quit ");
402 if (WaitForUser() == '\033')
405 /* Restore the old line */
409 for (i
= 2, j
= 0; i
< width
- 4; ++i
)
410 mvwaddch(win
, 5, i
, save
[j
++]);
416 /* BouncingBalls() leaves a keystroke in the queue */
418 if (WaitForUser() == '\033')