source: trunk/ncurses/test/firework.c@ 3003

Last change on this file since 3003 was 2621, checked in by bird, 19 years ago

GNU ncurses 5.5

File size: 3.5 KB
Line 
1/*
2 * $Id: firework.c,v 1.21 2005/05/28 21:39:04 tom Exp $
3 */
4#include <test.priv.h>
5
6#include <time.h>
7
8static int my_bg = COLOR_BLACK;
9
10static void
11cleanup(void)
12{
13 curs_set(1);
14 endwin();
15}
16
17static RETSIGTYPE
18onsig(int n GCC_UNUSED)
19{
20 cleanup();
21 ExitProgram(EXIT_FAILURE);
22}
23
24static void
25showit(void)
26{
27 int ch;
28 napms(120);
29 if ((ch = getch()) != ERR) {
30#ifdef KEY_RESIZE
31 if (ch == KEY_RESIZE) {
32 erase();
33 } else
34#endif
35 if (ch == 'q') {
36 cleanup();
37 ExitProgram(EXIT_SUCCESS);
38 } else if (ch == 's') {
39 nodelay(stdscr, FALSE);
40 } else if (ch == ' ') {
41 nodelay(stdscr, TRUE);
42 }
43 }
44}
45
46static
47int
48get_colour(chtype * bold)
49{
50 int attr;
51 attr = (rand() % 16) + 1;
52
53 *bold = A_NORMAL;
54 if (attr > 8) {
55 *bold = A_BOLD;
56 attr &= 7;
57 }
58 return (attr);
59}
60
61static
62void
63explode(int row, int col)
64{
65 chtype bold;
66 erase();
67 mvprintw(row, col, "-");
68 showit();
69
70 init_pair(1, get_colour(&bold), my_bg);
71 attrset(COLOR_PAIR(1) | bold);
72 mvprintw(row - 1, col - 1, " - ");
73 mvprintw(row + 0, col - 1, "-+-");
74 mvprintw(row + 1, col - 1, " - ");
75 showit();
76
77 init_pair(1, get_colour(&bold), my_bg);
78 attrset(COLOR_PAIR(1) | bold);
79 mvprintw(row - 2, col - 2, " --- ");
80 mvprintw(row - 1, col - 2, "-+++-");
81 mvprintw(row + 0, col - 2, "-+#+-");
82 mvprintw(row + 1, col - 2, "-+++-");
83 mvprintw(row + 2, col - 2, " --- ");
84 showit();
85
86 init_pair(1, get_colour(&bold), my_bg);
87 attrset(COLOR_PAIR(1) | bold);
88 mvprintw(row - 2, col - 2, " +++ ");
89 mvprintw(row - 1, col - 2, "++#++");
90 mvprintw(row + 0, col - 2, "+# #+");
91 mvprintw(row + 1, col - 2, "++#++");
92 mvprintw(row + 2, col - 2, " +++ ");
93 showit();
94
95 init_pair(1, get_colour(&bold), my_bg);
96 attrset(COLOR_PAIR(1) | bold);
97 mvprintw(row - 2, col - 2, " # ");
98 mvprintw(row - 1, col - 2, "## ##");
99 mvprintw(row + 0, col - 2, "# #");
100 mvprintw(row + 1, col - 2, "## ##");
101 mvprintw(row + 2, col - 2, " # ");
102 showit();
103
104 init_pair(1, get_colour(&bold), my_bg);
105 attrset(COLOR_PAIR(1) | bold);
106 mvprintw(row - 2, col - 2, " # # ");
107 mvprintw(row - 1, col - 2, "# #");
108 mvprintw(row + 0, col - 2, " ");
109 mvprintw(row + 1, col - 2, "# #");
110 mvprintw(row + 2, col - 2, " # # ");
111 showit();
112}
113
114int
115main(
116 int argc GCC_UNUSED,
117 char *argv[]GCC_UNUSED)
118{
119 int j;
120 int start, end, row, diff, flag = 0, direction;
121 unsigned seed;
122
123 for (j = SIGHUP; j <= SIGTERM; j++)
124 if (signal(j, SIG_IGN) != SIG_IGN)
125 signal(j, onsig);
126
127 initscr();
128 noecho();
129 cbreak();
130 keypad(stdscr, TRUE);
131 nodelay(stdscr, TRUE);
132
133 if (has_colors()) {
134 start_color();
135#if HAVE_USE_DEFAULT_COLORS
136 if (use_default_colors() == OK)
137 my_bg = -1;
138#endif
139 }
140 curs_set(0);
141
142 seed = time((time_t *) 0);
143 srand(seed);
144 for (;;) {
145 do {
146 start = rand() % (COLS - 3);
147 end = rand() % (COLS - 3);
148 start = (start < 2) ? 2 : start;
149 end = (end < 2) ? 2 : end;
150 direction = (start > end) ? -1 : 1;
151 diff = abs(start - end);
152 } while (diff < 2 || diff >= LINES - 2);
153 attrset(A_NORMAL);
154 for (row = 0; row < diff; row++) {
155 mvprintw(LINES - row, start + (row * direction),
156 (direction < 0) ? "\\" : "/");
157 if (flag++) {
158 showit();
159 erase();
160 flag = 0;
161 }
162 }
163 if (flag++) {
164 showit();
165 flag = 0;
166 }
167 seed = time((time_t *) 0);
168 srand(seed);
169 explode(LINES - row, start + (diff * direction));
170 erase();
171 showit();
172 }
173}
Note: See TracBrowser for help on using the repository browser.