]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/firework.c
ncurses 5.9 - patch 20140209
[ncurses.git] / test / firework.c
1 /****************************************************************************
2  * Copyright (c) 1998-2010,2013 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.29 2013/04/27 19:46:53 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     curs_set(1);
41     endwin();
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((chtype) 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((chtype) 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((chtype) 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((chtype) 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((chtype) 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 int
141 main(
142         int argc GCC_UNUSED,
143         char *argv[]GCC_UNUSED)
144 {
145     int start, end, row, diff, flag = 0, direction;
146     unsigned seed;
147
148     CATCHALL(onsig);
149
150     initscr();
151     noecho();
152     cbreak();
153     keypad(stdscr, TRUE);
154     nodelay(stdscr, TRUE);
155
156     if (has_colors()) {
157         start_color();
158 #if HAVE_USE_DEFAULT_COLORS
159         if (use_default_colors() == OK)
160             my_bg = -1;
161 #endif
162     }
163     curs_set(0);
164
165     seed = (unsigned) time((time_t *) 0);
166     srand(seed);
167     for (;;) {
168         do {
169             start = rand() % (COLS - 3);
170             end = rand() % (COLS - 3);
171             start = (start < 2) ? 2 : start;
172             end = (end < 2) ? 2 : end;
173             direction = (start > end) ? -1 : 1;
174             diff = abs(start - end);
175         } while (diff < 2 || diff >= LINES - 2);
176         (void) attrset(A_NORMAL);
177         for (row = 0; row < diff; row++) {
178             MvPrintw(LINES - row, start + (row * direction),
179                      (direction < 0) ? "\\" : "/");
180             if (flag++) {
181                 showit();
182                 erase();
183                 flag = 0;
184             }
185         }
186         if (flag++) {
187             showit();
188             flag = 0;
189         }
190         seed = (unsigned) time((time_t *) 0);
191         srand(seed);
192         explode(LINES - row, start + (diff * direction));
193         erase();
194         showit();
195     }
196 }