]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/firework.c
ncurses 6.2 - patch 20210904
[ncurses.git] / test / firework.c
1 /****************************************************************************
2  * Copyright 2019,2020 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.37 2020/02/02 23:34:34 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(void)
142 {
143     static const char *msg[] =
144     {
145         "Usage: firework [options]"
146         ,""
147         ,"Options:"
148 #if HAVE_USE_DEFAULT_COLORS
149         ," -d       invoke use_default_colors, repeat to use in init_pair"
150 #endif
151     };
152     size_t n;
153
154     for (n = 0; n < SIZEOF(msg); n++)
155         fprintf(stderr, "%s\n", msg[n]);
156
157     ExitProgram(EXIT_FAILURE);
158 }
159
160 int
161 main(int argc, char *argv[])
162 {
163     int ch;
164     int start, end;
165     int row, diff;
166     int flag = 0;
167     int direction;
168     unsigned seed;
169 #if HAVE_USE_DEFAULT_COLORS
170     bool d_option = FALSE;
171 #endif
172
173     while ((ch = getopt(argc, argv, "d")) != -1) {
174         switch (ch) {
175 #if HAVE_USE_DEFAULT_COLORS
176         case 'd':
177             d_option = TRUE;
178             break;
179 #endif
180         default:
181             usage();
182         }
183     }
184     if (optind < argc)
185         usage();
186
187     InitAndCatch(initscr(), onsig);
188     noecho();
189     cbreak();
190     keypad(stdscr, TRUE);
191     nodelay(stdscr, TRUE);
192
193     if (has_colors()) {
194         start_color();
195 #if HAVE_USE_DEFAULT_COLORS
196         if (d_option && (use_default_colors() == OK))
197             my_bg = -1;
198 #endif
199     }
200     curs_set(0);
201
202     seed = (unsigned) time((time_t *) 0);
203     srand(seed);
204     for (;;) {
205         do {
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) ? "\\" : "/");
217             if (flag++) {
218                 showit();
219                 erase();
220                 flag = 0;
221             }
222         }
223         if (flag++) {
224             showit();
225             flag = 0;
226         }
227         seed = (unsigned) time((time_t *) 0);
228         srand(seed);
229         explode(LINES - row, start + (diff * direction));
230         erase();
231         showit();
232     }
233 }