]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/firework.c
ncurses 6.0 - patch 20171230
[ncurses.git] / test / firework.c
1 /****************************************************************************
2  * Copyright (c) 1998-2014,2017 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: firework.c,v 1.35 2017/09/30 15:42:24 tom Exp $
30  */
31 #include <test.priv.h>
32
33 #include <time.h>
34
35 static short my_bg = COLOR_BLACK;
36
37 static void
38 cleanup(void)
39 {
40     exit_curses();
41 }
42
43 static void
44 onsig(int n GCC_UNUSED)
45 {
46     cleanup();
47     ExitProgram(EXIT_FAILURE);
48 }
49
50 static void
51 showit(void)
52 {
53     int ch;
54     napms(120);
55     if ((ch = getch()) != ERR) {
56 #ifdef KEY_RESIZE
57         if (ch == KEY_RESIZE) {
58             erase();
59         } else
60 #endif
61         if (ch == 'q') {
62             cleanup();
63             ExitProgram(EXIT_SUCCESS);
64         } else if (ch == 's') {
65             nodelay(stdscr, FALSE);
66         } else if (ch == ' ') {
67             nodelay(stdscr, TRUE);
68         }
69     }
70 }
71
72 static short
73 get_colour(chtype *bold)
74 {
75     int attr;
76     attr = (rand() % 16) + 1;
77
78     *bold = A_NORMAL;
79     if (attr > 8) {
80         *bold = A_BOLD;
81         attr &= 7;
82     }
83     return (short) (attr);
84 }
85
86 static
87 void
88 explode(int row, int col)
89 {
90     chtype bold;
91     erase();
92     MvPrintw(row, col, "-");
93     showit();
94
95     init_pair(1, get_colour(&bold), my_bg);
96     (void) attrset(AttrArg(COLOR_PAIR(1), bold));
97     MvPrintw(row - 1, col - 1, " - ");
98     MvPrintw(row + 0, col - 1, "-+-");
99     MvPrintw(row + 1, col - 1, " - ");
100     showit();
101
102     init_pair(1, get_colour(&bold), my_bg);
103     (void) attrset(AttrArg(COLOR_PAIR(1), bold));
104     MvPrintw(row - 2, col - 2, " --- ");
105     MvPrintw(row - 1, col - 2, "-+++-");
106     MvPrintw(row + 0, col - 2, "-+#+-");
107     MvPrintw(row + 1, col - 2, "-+++-");
108     MvPrintw(row + 2, col - 2, " --- ");
109     showit();
110
111     init_pair(1, get_colour(&bold), my_bg);
112     (void) attrset(AttrArg(COLOR_PAIR(1), bold));
113     MvPrintw(row - 2, col - 2, " +++ ");
114     MvPrintw(row - 1, col - 2, "++#++");
115     MvPrintw(row + 0, col - 2, "+# #+");
116     MvPrintw(row + 1, col - 2, "++#++");
117     MvPrintw(row + 2, col - 2, " +++ ");
118     showit();
119
120     init_pair(1, get_colour(&bold), my_bg);
121     (void) attrset(AttrArg(COLOR_PAIR(1), bold));
122     MvPrintw(row - 2, col - 2, "  #  ");
123     MvPrintw(row - 1, col - 2, "## ##");
124     MvPrintw(row + 0, col - 2, "#   #");
125     MvPrintw(row + 1, col - 2, "## ##");
126     MvPrintw(row + 2, col - 2, "  #  ");
127     showit();
128
129     init_pair(1, get_colour(&bold), my_bg);
130     (void) attrset(AttrArg(COLOR_PAIR(1), bold));
131     MvPrintw(row - 2, col - 2, " # # ");
132     MvPrintw(row - 1, col - 2, "#   #");
133     MvPrintw(row + 0, col - 2, "     ");
134     MvPrintw(row + 1, col - 2, "#   #");
135     MvPrintw(row + 2, col - 2, " # # ");
136     showit();
137 }
138
139 static void
140 usage(void)
141 {
142     static const char *msg[] =
143     {
144         "Usage: firework [options]"
145         ,""
146         ,"Options:"
147 #if HAVE_USE_DEFAULT_COLORS
148         ," -d       invoke use_default_colors, repeat to use in init_pair"
149 #endif
150     };
151     size_t n;
152
153     for (n = 0; n < SIZEOF(msg); n++)
154         fprintf(stderr, "%s\n", msg[n]);
155
156     ExitProgram(EXIT_FAILURE);
157 }
158
159 int
160 main(int argc, char *argv[])
161 {
162     int ch;
163     int start, end;
164     int row, diff;
165     int flag = 0;
166     int direction;
167     unsigned seed;
168 #if HAVE_USE_DEFAULT_COLORS
169     bool d_option = FALSE;
170 #endif
171
172     while ((ch = getopt(argc, argv, "d")) != -1) {
173         switch (ch) {
174 #if HAVE_USE_DEFAULT_COLORS
175         case 'd':
176             d_option = TRUE;
177             break;
178 #endif
179         default:
180             usage();
181         }
182     }
183     if (optind < argc)
184         usage();
185
186     InitAndCatch(initscr(), onsig);
187     noecho();
188     cbreak();
189     keypad(stdscr, TRUE);
190     nodelay(stdscr, TRUE);
191
192     if (has_colors()) {
193         start_color();
194 #if HAVE_USE_DEFAULT_COLORS
195         if (d_option && (use_default_colors() == OK))
196             my_bg = -1;
197 #endif
198     }
199     curs_set(0);
200
201     seed = (unsigned) time((time_t *) 0);
202     srand(seed);
203     for (;;) {
204         do {
205             start = rand() % (COLS - 3);
206             end = rand() % (COLS - 3);
207             start = (start < 2) ? 2 : start;
208             end = (end < 2) ? 2 : end;
209             direction = (start > end) ? -1 : 1;
210             diff = abs(start - end);
211         } while (diff < 2 || diff >= LINES - 2);
212         (void) attrset(AttrArg(0, A_NORMAL));
213         for (row = 1; row < diff; row++) {
214             MvPrintw(LINES - row, start + (row * direction),
215                      (direction < 0) ? "\\" : "/");
216             if (flag++) {
217                 showit();
218                 erase();
219                 flag = 0;
220             }
221         }
222         if (flag++) {
223             showit();
224             flag = 0;
225         }
226         seed = (unsigned) time((time_t *) 0);
227         srand(seed);
228         explode(LINES - row, start + (diff * direction));
229         erase();
230         showit();
231     }
232 }