]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/rain.c
ncurses 5.2
[ncurses.git] / test / rain.c
1 /*
2  * $Id: rain.c,v 1.15 2000/09/02 18:41:22 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 #if HAVE_USE_DEFAULT_COLORS
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         case 's':
104             nodelay(stdscr, FALSE);
105             break;
106         case ' ':
107             nodelay(stdscr, TRUE);
108             break;
109 #ifdef KEY_RESIZE
110         case(KEY_RESIZE):
111             r = (float)(LINES - 4);
112             c = (float)(COLS - 4);
113             break;
114 #endif
115         }
116         napms(50);
117     }
118 }
119
120 static void
121 onsig(int n GCC_UNUSED)
122 {
123     curs_set(1);
124     endwin();
125     exit(EXIT_FAILURE);
126 }
127
128 static float
129 ranf(void)
130 {
131     long r = (rand() & 077777);
132     return ((float) r / 32768.);
133 }