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