]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/rain.c
ncurses 5.6 - patch 20070407
[ncurses.git] / test / rain.c
1 /****************************************************************************
2  * Copyright (c) 1998-2002,2006 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: rain.c,v 1.22 2006/05/20 15:34:27 tom Exp $
30  */
31 #include <test.priv.h>
32
33 /* rain 11/3/1980 EPS/CITHEP */
34
35 static float ranf(void);
36 static void onsig(int sig);
37
38 static int
39 next_j(int j)
40 {
41     if (j == 0)
42         j = 4;
43     else
44         --j;
45     if (has_colors()) {
46         int z = (int) (3 * ranf());
47         chtype color = COLOR_PAIR(z);
48         if (z)
49             color |= A_BOLD;
50         attrset(color);
51     }
52     return j;
53 }
54
55 int
56 main(
57         int argc GCC_UNUSED,
58         char *argv[]GCC_UNUSED)
59 {
60     int x, y, j;
61     static int xpos[5], ypos[5];
62     float r;
63     float c;
64
65     setlocale(LC_ALL, "");
66
67     CATCHALL(onsig);
68
69     initscr();
70     if (has_colors()) {
71         int bg = COLOR_BLACK;
72         start_color();
73 #if HAVE_USE_DEFAULT_COLORS
74         if (use_default_colors() == OK)
75             bg = -1;
76 #endif
77         init_pair(1, COLOR_BLUE, bg);
78         init_pair(2, COLOR_CYAN, bg);
79     }
80     nl();
81     noecho();
82     curs_set(0);
83     timeout(0);
84
85     r = (float) (LINES - 4);
86     c = (float) (COLS - 4);
87     for (j = 5; --j >= 0;) {
88         xpos[j] = (int) (c * ranf()) + 2;
89         ypos[j] = (int) (r * ranf()) + 2;
90     }
91
92     for (j = 0;;) {
93         x = (int) (c * ranf()) + 2;
94         y = (int) (r * ranf()) + 2;
95
96         mvaddch(y, x, '.');
97
98         mvaddch(ypos[j], xpos[j], 'o');
99
100         j = next_j(j);
101         mvaddch(ypos[j], xpos[j], 'O');
102
103         j = next_j(j);
104         mvaddch(ypos[j] - 1, xpos[j], '-');
105         mvaddstr(ypos[j], xpos[j] - 1, "|.|");
106         mvaddch(ypos[j] + 1, xpos[j], '-');
107
108         j = next_j(j);
109         mvaddch(ypos[j] - 2, xpos[j], '-');
110         mvaddstr(ypos[j] - 1, xpos[j] - 1, "/ \\");
111         mvaddstr(ypos[j], xpos[j] - 2, "| O |");
112         mvaddstr(ypos[j] + 1, xpos[j] - 1, "\\ /");
113         mvaddch(ypos[j] + 2, xpos[j], '-');
114
115         j = next_j(j);
116         mvaddch(ypos[j] - 2, xpos[j], ' ');
117         mvaddstr(ypos[j] - 1, xpos[j] - 1, "   ");
118         mvaddstr(ypos[j], xpos[j] - 2, "     ");
119         mvaddstr(ypos[j] + 1, xpos[j] - 1, "   ");
120         mvaddch(ypos[j] + 2, xpos[j], ' ');
121
122         xpos[j] = x;
123         ypos[j] = y;
124
125         switch (getch()) {
126         case ('q'):
127         case ('Q'):
128             curs_set(1);
129             endwin();
130             ExitProgram(EXIT_SUCCESS);
131         case 's':
132             nodelay(stdscr, FALSE);
133             break;
134         case ' ':
135             nodelay(stdscr, TRUE);
136             break;
137 #ifdef KEY_RESIZE
138         case (KEY_RESIZE):
139             r = (float) (LINES - 4);
140             c = (float) (COLS - 4);
141             break;
142 #endif
143         }
144         napms(50);
145     }
146 }
147
148 static void
149 onsig(int n GCC_UNUSED)
150 {
151     curs_set(1);
152     endwin();
153     ExitProgram(EXIT_FAILURE);
154 }
155
156 static float
157 ranf(void)
158 {
159     long r = (rand() & 077777);
160     return ((float) r / 32768.);
161 }