]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ncurses/test/rain.c
This commit was generated by cvs2svn to compensate for changes in r98937,
[FreeBSD/FreeBSD.git] / contrib / ncurses / test / rain.c
1 /*
2  * $Id: rain.c,v 1.19 2002/04/06 20:45:22 tom Exp $
3  */
4 #include <test.priv.h>
5
6 /* rain 11/3/1980 EPS/CITHEP */
7
8 static float ranf(void);
9 static void onsig(int sig);
10
11 static int
12 next_j(int j)
13 {
14     if (j == 0)
15         j = 4;
16     else
17         --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)
40             signal(j, onsig);
41
42     initscr();
43     if (has_colors()) {
44         int bg = COLOR_BLACK;
45         start_color();
46 #if HAVE_USE_DEFAULT_COLORS
47         if (use_default_colors() == OK)
48             bg = -1;
49 #endif
50         init_pair(1, COLOR_BLUE, bg);
51         init_pair(2, COLOR_CYAN, bg);
52     }
53     nl();
54     noecho();
55     curs_set(0);
56     timeout(0);
57
58     r = (float) (LINES - 4);
59     c = (float) (COLS - 4);
60     for (j = 5; --j >= 0;) {
61         xpos[j] = (int) (c * ranf()) + 2;
62         ypos[j] = (int) (r * ranf()) + 2;
63     }
64
65     for (j = 0;;) {
66         x = (int) (c * ranf()) + 2;
67         y = (int) (r * ranf()) + 2;
68
69         mvaddch(y, x, '.');
70
71         mvaddch(ypos[j], xpos[j], 'o');
72
73         j = next_j(j);
74         mvaddch(ypos[j], xpos[j], 'O');
75
76         j = next_j(j);
77         mvaddch(ypos[j] - 1, xpos[j], '-');
78         mvaddstr(ypos[j], xpos[j] - 1, "|.|");
79         mvaddch(ypos[j] + 1, xpos[j], '-');
80
81         j = next_j(j);
82         mvaddch(ypos[j] - 2, xpos[j], '-');
83         mvaddstr(ypos[j] - 1, xpos[j] - 1, "/ \\");
84         mvaddstr(ypos[j], xpos[j] - 2, "| O |");
85         mvaddstr(ypos[j] + 1, xpos[j] - 1, "\\ /");
86         mvaddch(ypos[j] + 2, xpos[j], '-');
87
88         j = next_j(j);
89         mvaddch(ypos[j] - 2, xpos[j], ' ');
90         mvaddstr(ypos[j] - 1, xpos[j] - 1, "   ");
91         mvaddstr(ypos[j], xpos[j] - 2, "     ");
92         mvaddstr(ypos[j] + 1, xpos[j] - 1, "   ");
93         mvaddch(ypos[j] + 2, xpos[j], ' ');
94
95         xpos[j] = x;
96         ypos[j] = y;
97
98         switch (getch()) {
99         case ('q'):
100         case ('Q'):
101             curs_set(1);
102             endwin();
103             ExitProgram(EXIT_SUCCESS);
104         case 's':
105             nodelay(stdscr, FALSE);
106             break;
107         case ' ':
108             nodelay(stdscr, TRUE);
109             break;
110 #ifdef KEY_RESIZE
111         case (KEY_RESIZE):
112             r = (float) (LINES - 4);
113             c = (float) (COLS - 4);
114             break;
115 #endif
116         }
117         napms(50);
118     }
119 }
120
121 static void
122 onsig(int n GCC_UNUSED)
123 {
124     curs_set(1);
125     endwin();
126     ExitProgram(EXIT_FAILURE);
127 }
128
129 static float
130 ranf(void)
131 {
132     long r = (rand() & 077777);
133     return ((float) r / 32768.);
134 }