]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/firework.c
557a7bf231ece347aade3b97e5316a51e6f9d6a8
[ncurses.git] / test / firework.c
1 /*
2  * $Id: firework.c,v 1.15 1999/10/16 21:33:39 tom Exp $
3  */
4 #include <test.priv.h>
5
6 #include <signal.h>
7 #include <time.h>
8
9 static int my_bg = COLOR_BLACK;
10
11 static void
12 cleanup(void)
13 {
14     curs_set(1);
15     endwin();
16 }
17
18 static RETSIGTYPE
19 onsig(int n GCC_UNUSED)
20 {
21     cleanup();
22     exit(EXIT_FAILURE);
23 }
24
25 static void
26 showit(void)
27 {
28     int ch;
29     napms(120);
30     if ((ch = getch()) != ERR) {
31 #ifdef KEY_RESIZE
32         if (ch == KEY_RESIZE) {
33             erase();
34         } else
35 #endif
36         if (ch == 'q') {
37             cleanup();
38             exit(EXIT_SUCCESS);
39         } else if (ch == 's') {
40             nodelay(stdscr, FALSE);
41         } else if (ch == ' ') {
42             nodelay(stdscr, TRUE);
43         }
44     }
45 }
46
47 static
48 int
49 get_colour(chtype * bold)
50 {
51     int attr;
52     attr = (rand() % 16) + 1;
53
54     *bold = A_NORMAL;
55     if (attr > 8) {
56         *bold = A_BOLD;
57         attr &= 7;
58     }
59     return (attr);
60 }
61
62 static
63 void
64 explode(int row, int col)
65 {
66     chtype bold;
67     erase();
68     mvprintw(row, col, "-");
69     showit();
70
71     init_pair(1, get_colour(&bold), my_bg);
72     attrset(COLOR_PAIR(1) | bold);
73     mvprintw(row - 1, col - 1, " - ");
74     mvprintw(row + 0, col - 1, "-+-");
75     mvprintw(row + 1, col - 1, " - ");
76     showit();
77
78     init_pair(1, get_colour(&bold), my_bg);
79     attrset(COLOR_PAIR(1) | bold);
80     mvprintw(row - 2, col - 2, " --- ");
81     mvprintw(row - 1, col - 2, "-+++-");
82     mvprintw(row + 0, col - 2, "-+#+-");
83     mvprintw(row + 1, col - 2, "-+++-");
84     mvprintw(row + 2, col - 2, " --- ");
85     showit();
86
87     init_pair(1, get_colour(&bold), my_bg);
88     attrset(COLOR_PAIR(1) | bold);
89     mvprintw(row - 2, col - 2, " +++ ");
90     mvprintw(row - 1, col - 2, "++#++");
91     mvprintw(row + 0, col - 2, "+# #+");
92     mvprintw(row + 1, col - 2, "++#++");
93     mvprintw(row + 2, col - 2, " +++ ");
94     showit();
95
96     init_pair(1, get_colour(&bold), my_bg);
97     attrset(COLOR_PAIR(1) | bold);
98     mvprintw(row - 2, col - 2, "  #  ");
99     mvprintw(row - 1, col - 2, "## ##");
100     mvprintw(row + 0, col - 2, "#   #");
101     mvprintw(row + 1, col - 2, "## ##");
102     mvprintw(row + 2, col - 2, "  #  ");
103     showit();
104
105     init_pair(1, get_colour(&bold), my_bg);
106     attrset(COLOR_PAIR(1) | bold);
107     mvprintw(row - 2, col - 2, " # # ");
108     mvprintw(row - 1, col - 2, "#   #");
109     mvprintw(row + 0, col - 2, "     ");
110     mvprintw(row + 1, col - 2, "#   #");
111     mvprintw(row + 2, col - 2, " # # ");
112     showit();
113 }
114
115 int
116 main(
117     int argc GCC_UNUSED,
118     char *argv[]GCC_UNUSED)
119 {
120     int j;
121     int start, end, row, diff, flag = 0, direction;
122     unsigned seed;
123
124     for (j = SIGHUP; j <= SIGTERM; j++)
125         if (signal(j, SIG_IGN) != SIG_IGN)
126             signal(j, onsig);
127
128     initscr();
129     noecho();
130     cbreak();
131     keypad(stdscr, TRUE);
132     nodelay(stdscr, TRUE);
133
134     if (has_colors()) {
135         start_color();
136 #ifdef NCURSES_VERSION
137         if (use_default_colors() == OK)
138             my_bg = -1;
139 #endif
140     }
141     curs_set(0);
142
143     seed = time((time_t *) 0);
144     srand(seed);
145     for (;;) {
146         do {
147             start = rand() % (COLS - 3);
148             end = rand() % (COLS - 3);
149             start = (start < 2) ? 2 : start;
150             end = (end < 2) ? 2 : end;
151             direction = (start > end) ? -1 : 1;
152             diff = abs(start - end);
153         } while (diff < 2 || diff >= LINES - 2);
154         attrset(A_NORMAL);
155         for (row = 0; row < diff; row++) {
156             mvprintw(LINES - row, start + (row * direction),
157                 (direction < 0) ? "\\" : "/");
158             if (flag++) {
159                 showit();
160                 erase();
161                 flag = 0;
162             }
163         }
164         if (flag++) {
165             showit();
166             flag = 0;
167         }
168         seed = time((time_t *) 0);
169         srand(seed);
170         explode(LINES - row, start + (diff * direction));
171         erase();
172         showit();
173     }
174 }