1 /****************************************************************************
2 * Copyright 2019,2020 Thomas E. Dickey *
3 * Copyright 1998-2014,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: firework.c,v 1.37 2020/02/02 23:34:34 tom Exp $
32 #include <test.priv.h>
36 static short my_bg = COLOR_BLACK;
45 onsig(int n GCC_UNUSED)
48 ExitProgram(EXIT_FAILURE);
56 if ((ch = getch()) != ERR) {
58 if (ch == KEY_RESIZE) {
64 ExitProgram(EXIT_SUCCESS);
65 } else if (ch == 's') {
66 nodelay(stdscr, FALSE);
67 } else if (ch == ' ') {
68 nodelay(stdscr, TRUE);
74 get_colour(chtype *bold)
77 attr = (rand() % 16) + 1;
84 return (short) (attr);
89 explode(int row, int col)
93 MvPrintw(row, col, "-");
96 init_pair(1, get_colour(&bold), my_bg);
97 (void) attrset(AttrArg(COLOR_PAIR(1), bold));
98 MvPrintw(row - 1, col - 1, " - ");
99 MvPrintw(row + 0, col - 1, "-+-");
100 MvPrintw(row + 1, col - 1, " - ");
103 init_pair(1, get_colour(&bold), my_bg);
104 (void) attrset(AttrArg(COLOR_PAIR(1), bold));
105 MvPrintw(row - 2, col - 2, " --- ");
106 MvPrintw(row - 1, col - 2, "-+++-");
107 MvPrintw(row + 0, col - 2, "-+#+-");
108 MvPrintw(row + 1, col - 2, "-+++-");
109 MvPrintw(row + 2, col - 2, " --- ");
112 init_pair(1, get_colour(&bold), my_bg);
113 (void) attrset(AttrArg(COLOR_PAIR(1), bold));
114 MvPrintw(row - 2, col - 2, " +++ ");
115 MvPrintw(row - 1, col - 2, "++#++");
116 MvPrintw(row + 0, col - 2, "+# #+");
117 MvPrintw(row + 1, col - 2, "++#++");
118 MvPrintw(row + 2, col - 2, " +++ ");
121 init_pair(1, get_colour(&bold), my_bg);
122 (void) attrset(AttrArg(COLOR_PAIR(1), bold));
123 MvPrintw(row - 2, col - 2, " # ");
124 MvPrintw(row - 1, col - 2, "## ##");
125 MvPrintw(row + 0, col - 2, "# #");
126 MvPrintw(row + 1, col - 2, "## ##");
127 MvPrintw(row + 2, col - 2, " # ");
130 init_pair(1, get_colour(&bold), my_bg);
131 (void) attrset(AttrArg(COLOR_PAIR(1), bold));
132 MvPrintw(row - 2, col - 2, " # # ");
133 MvPrintw(row - 1, col - 2, "# #");
134 MvPrintw(row + 0, col - 2, " ");
135 MvPrintw(row + 1, col - 2, "# #");
136 MvPrintw(row + 2, col - 2, " # # ");
143 static const char *msg[] =
145 "Usage: firework [options]"
148 #if HAVE_USE_DEFAULT_COLORS
149 ," -d invoke use_default_colors, repeat to use in init_pair"
154 for (n = 0; n < SIZEOF(msg); n++)
155 fprintf(stderr, "%s\n", msg[n]);
157 ExitProgram(EXIT_FAILURE);
161 main(int argc, char *argv[])
169 #if HAVE_USE_DEFAULT_COLORS
170 bool d_option = FALSE;
173 while ((ch = getopt(argc, argv, "d")) != -1) {
175 #if HAVE_USE_DEFAULT_COLORS
187 InitAndCatch(initscr(), onsig);
190 keypad(stdscr, TRUE);
191 nodelay(stdscr, TRUE);
195 #if HAVE_USE_DEFAULT_COLORS
196 if (d_option && (use_default_colors() == OK))
202 seed = (unsigned) time((time_t *) 0);
206 start = rand() % (COLS - 3);
207 end = rand() % (COLS - 3);
208 start = (start < 2) ? 2 : start;
209 end = (end < 2) ? 2 : end;
210 direction = (start > end) ? -1 : 1;
211 diff = abs(start - end);
212 } while (diff < 2 || diff >= LINES - 2);
213 (void) attrset(AttrArg(0, A_NORMAL));
214 for (row = 1; row < diff; row++) {
215 MvPrintw(LINES - row, start + (row * direction),
216 (direction < 0) ? "\\" : "/");
227 seed = (unsigned) time((time_t *) 0);
229 explode(LINES - row, start + (diff * direction));