blob: 54e4036cf6a29f960c0b43a6567536138dcb73dc [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2019-2020,2022 Thomas E. Dickey *
3 * Copyright 1998-2014,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
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: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
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. *
23 * *
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 *
27 * authorization. *
28 ****************************************************************************/
29/*
micky3879b9f5e72025-07-08 18:04:53 -040030 * $Id: firework.c,v 1.40 2022/12/04 00:40:11 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010031 */
32#include <test.priv.h>
33
34#include <time.h>
35
36static short my_bg = COLOR_BLACK;
37
38static void
39cleanup(void)
40{
micky3879b9f5e72025-07-08 18:04:53 -040041 stop_curses();
Steve Kondikae271bc2015-11-15 02:50:53 +010042}
43
44static void
45onsig(int n GCC_UNUSED)
46{
47 cleanup();
48 ExitProgram(EXIT_FAILURE);
49}
50
51static void
52showit(void)
53{
54 int ch;
55 napms(120);
56 if ((ch = getch()) != ERR) {
57#ifdef KEY_RESIZE
58 if (ch == KEY_RESIZE) {
59 erase();
60 } else
61#endif
62 if (ch == 'q') {
63 cleanup();
64 ExitProgram(EXIT_SUCCESS);
65 } else if (ch == 's') {
66 nodelay(stdscr, FALSE);
67 } else if (ch == ' ') {
68 nodelay(stdscr, TRUE);
69 }
70 }
71}
72
73static short
74get_colour(chtype *bold)
75{
76 int attr;
77 attr = (rand() % 16) + 1;
78
79 *bold = A_NORMAL;
80 if (attr > 8) {
81 *bold = A_BOLD;
82 attr &= 7;
83 }
84 return (short) (attr);
85}
86
87static
88void
89explode(int row, int col)
90{
91 chtype bold;
92 erase();
93 MvPrintw(row, col, "-");
94 showit();
95
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, " - ");
101 showit();
102
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, " --- ");
110 showit();
111
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, " +++ ");
119 showit();
120
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, " # ");
128 showit();
129
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, " # # ");
137 showit();
138}
139
micky3879b9f5e72025-07-08 18:04:53 -0400140static void
141usage(int ok)
Steve Kondikae271bc2015-11-15 02:50:53 +0100142{
micky3879b9f5e72025-07-08 18:04:53 -0400143 static const char *msg[] =
144 {
145 "Usage: firework [options]"
146 ,""
147 ,USAGE_COMMON
148 ,"Options:"
149#if HAVE_USE_DEFAULT_COLORS
150 ," -d invoke use_default_colors, repeat to use in init_pair"
151#endif
152 };
153 size_t n;
154
155 for (n = 0; n < SIZEOF(msg); n++)
156 fprintf(stderr, "%s\n", msg[n]);
157
158 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
159}
160/* *INDENT-OFF* */
161VERSION_COMMON()
162/* *INDENT-ON* */
163
164int
165main(int argc, char *argv[])
166{
167 int ch;
168 int start, end;
169 int row, diff;
170 int flag = 0;
171 int direction;
Steve Kondikae271bc2015-11-15 02:50:53 +0100172 unsigned seed;
micky3879b9f5e72025-07-08 18:04:53 -0400173#if HAVE_USE_DEFAULT_COLORS
174 bool d_option = FALSE;
175#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100176
micky3879b9f5e72025-07-08 18:04:53 -0400177 while ((ch = getopt(argc, argv, OPTS_COMMON "d")) != -1) {
178 switch (ch) {
179#if HAVE_USE_DEFAULT_COLORS
180 case 'd':
181 d_option = TRUE;
182 break;
183#endif
184 case OPTS_VERSION:
185 show_version(argv);
186 ExitProgram(EXIT_SUCCESS);
187 default:
188 usage(ch == OPTS_USAGE);
189 /* NOTREACHED */
190 }
191 }
192 if (optind < argc)
193 usage(FALSE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100194
micky3879b9f5e72025-07-08 18:04:53 -0400195 InitAndCatch(initscr(), onsig);
Steve Kondikae271bc2015-11-15 02:50:53 +0100196 noecho();
197 cbreak();
198 keypad(stdscr, TRUE);
199 nodelay(stdscr, TRUE);
200
201 if (has_colors()) {
202 start_color();
203#if HAVE_USE_DEFAULT_COLORS
micky3879b9f5e72025-07-08 18:04:53 -0400204 if (d_option && (use_default_colors() == OK))
Steve Kondikae271bc2015-11-15 02:50:53 +0100205 my_bg = -1;
206#endif
207 }
208 curs_set(0);
209
210 seed = (unsigned) time((time_t *) 0);
211 srand(seed);
212 for (;;) {
213 do {
214 start = rand() % (COLS - 3);
215 end = rand() % (COLS - 3);
216 start = (start < 2) ? 2 : start;
217 end = (end < 2) ? 2 : end;
218 direction = (start > end) ? -1 : 1;
219 diff = abs(start - end);
220 } while (diff < 2 || diff >= LINES - 2);
221 (void) attrset(AttrArg(0, A_NORMAL));
micky3879b9f5e72025-07-08 18:04:53 -0400222 for (row = 1; row < diff; row++) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100223 MvPrintw(LINES - row, start + (row * direction),
224 (direction < 0) ? "\\" : "/");
225 if (flag++) {
226 showit();
227 erase();
228 flag = 0;
229 }
230 }
231 if (flag++) {
232 showit();
233 flag = 0;
234 }
235 seed = (unsigned) time((time_t *) 0);
236 srand(seed);
237 explode(LINES - row, start + (diff * direction));
238 erase();
239 showit();
240 }
241}