]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/rain.c
4132a43826fe5948435c91a1ee08e1653b2c499d
[ncurses.git] / test / rain.c
1 /*
2  * $Id: rain.c,v 1.11 1997/09/18 18:36:46 tom Exp $
3  */
4 #include <test.priv.h>
5
6 #include <term.h>       /* for tparm() */
7
8 #include <signal.h>
9
10 /* rain 11/3/1980 EPS/CITHEP */
11
12 static float ranf(void);
13 static void onsig(int sig);
14
15 static int next_j(int j)
16 {
17     if (j==0) j=4; else --j;
18     if (has_colors()) {
19         int z = (int)(3*ranf());
20         chtype color = COLOR_PAIR(z);
21         if (z)
22             color |= A_BOLD;
23         attrset(color);
24     }
25     return j;
26 }
27
28 int
29 main(
30         int argc GCC_UNUSED,
31         char *argv[] GCC_UNUSED)
32 {
33 int x, y, j;
34 static int xpos[5], ypos[5];
35 float r;
36 float c;
37
38     for (j=SIGHUP;j<=SIGTERM;j++)
39         if (signal(j,SIG_IGN)!=SIG_IGN) signal(j,onsig);
40
41     initscr();
42     if (has_colors()) {
43         int bg = COLOR_BLACK;
44         start_color();
45 #ifdef NCURSES_VERSION
46         if (use_default_colors() == OK)
47                 bg = -1;
48 #endif
49         init_pair(1, COLOR_BLUE, bg);
50         init_pair(2, COLOR_CYAN, bg);
51     }
52     nl();
53     noecho();
54     curs_set(0);
55     timeout(0);
56
57     r = (float)(LINES - 4);
58     c = (float)(COLS - 4);
59     for (j=5;--j>=0;) {
60         xpos[j]=(int)(c* ranf())+2;
61         ypos[j]=(int)(r* ranf())+2;
62     }
63
64     for (j=0;;) {
65         x=(int)(c*ranf())+2;
66         y=(int)(r*ranf())+2;
67
68         mvaddch(y,x, '.');
69
70         mvaddch(ypos[j], xpos[j], 'o');
71
72         j = next_j(j);
73         mvaddch(ypos[j], xpos[j], 'O');
74
75         j = next_j(j);
76         mvaddch( ypos[j]-1, xpos[j],    '-');
77         mvaddstr(ypos[j],   xpos[j]-1, "|.|");
78         mvaddch( ypos[j]+1, xpos[j],    '-');
79
80         j = next_j(j);
81         mvaddch( ypos[j]-2, xpos[j],     '-');
82         mvaddstr(ypos[j]-1, xpos[j]-1,  "/ \\");
83         mvaddstr(ypos[j],   xpos[j]-2,  "| O |");
84         mvaddstr(ypos[j]+1, xpos[j]-1,  "\\ /");
85         mvaddch( ypos[j]+2, xpos[j],     '-');
86
87         j = next_j(j);
88         mvaddch( ypos[j]-2, xpos[j],     ' ');
89         mvaddstr(ypos[j]-1, xpos[j]-1,  "   ");
90         mvaddstr(ypos[j],   xpos[j]-2, "     ");
91         mvaddstr(ypos[j]+1, xpos[j]-1,  "   ");
92         mvaddch( ypos[j]+2, xpos[j],     ' ');
93
94         xpos[j] = x; ypos[j] = y;
95
96         switch(getch())
97         {
98         case('q'):
99         case('Q'):
100             curs_set(1);
101             endwin();
102             return(EXIT_SUCCESS);
103 #ifdef KEY_RESIZE
104         case(KEY_RESIZE):
105             r = (float)(LINES - 4);
106             c = (float)(COLS - 4);
107             break;
108 #endif
109         }
110         napms(50);
111     }
112 }
113
114 static void
115 onsig(int n GCC_UNUSED)
116 {
117     curs_set(1);
118     endwin();
119     exit(EXIT_FAILURE);
120 }
121
122 static float
123 ranf(void)
124 {
125     float rv;
126     long r = rand();
127
128     r &= 077777;
129     rv =((float)r/32767.);
130     return rv;
131 }