]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/firework.c
ncurses 6.4 - patch 20240414
[ncurses.git] / test / firework.c
1 /****************************************************************************
2  * Copyright 2019-2020,2022 Thomas E. Dickey                                *
3  * Copyright 1998-2014,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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 /*
30  * $Id: firework.c,v 1.40 2022/12/04 00:40:11 tom Exp $
31  */
32 #include <test.priv.h>
33
34 #include <time.h>
35
36 static short my_bg = COLOR_BLACK;
37
38 static void
39 cleanup(void)
40 {
41     stop_curses();
42 }
43
44 static void
45 onsig(int n GCC_UNUSED)
46 {
47     cleanup();
48     ExitProgram(EXIT_FAILURE);
49 }
50
51 static void
52 showit(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
73 static short
74 get_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
87 static
88 void
89 explode(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
140 static void
141 usage(int ok)
142 {
143     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* */
161 VERSION_COMMON()
162 /* *INDENT-ON* */
163
164 int
165 main(int argc, char *argv[])
166 {
167     int ch;
168     int start, end;
169     int row, diff;
170     int flag = 0;
171     int direction;
172     unsigned seed;
173 #if HAVE_USE_DEFAULT_COLORS
174     bool d_option = FALSE;
175 #endif
176
177     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);
194
195     InitAndCatch(initscr(), onsig);
196     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
204         if (d_option && (use_default_colors() == OK))
205             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));
222         for (row = 1; row < diff; row++) {
223             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 }