aboutsummaryrefslogtreecommitdiff
path: root/contrib/ncurses/test/rain.c
blob: a7771b09d3e346831bb9b46a542b667d6164d695 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * $Id: rain.c,v 1.15 2000/09/02 18:41:22 tom Exp $
 */
#include <test.priv.h>

#include <term.h>	/* for tparm() */

#include <signal.h>

/* rain 11/3/1980 EPS/CITHEP */

static float ranf(void);
static void onsig(int sig);

static int next_j(int j)
{
    if (j==0) j=4; else --j;
    if (has_colors()) {
	int z = (int)(3*ranf());
	chtype color = COLOR_PAIR(z);
	if (z)
	    color |= A_BOLD;
	attrset(color);
    }
    return j;
}

int
main(
	int argc GCC_UNUSED,
	char *argv[] GCC_UNUSED)
{
int x, y, j;
static int xpos[5], ypos[5];
float r;
float c;

    for (j=SIGHUP;j<=SIGTERM;j++)
	if (signal(j,SIG_IGN)!=SIG_IGN) signal(j,onsig);

    initscr();
    if (has_colors()) {
	int bg = COLOR_BLACK;
	start_color();
#if HAVE_USE_DEFAULT_COLORS
	if (use_default_colors() == OK)
		bg = -1;
#endif
	init_pair(1, COLOR_BLUE, bg);
	init_pair(2, COLOR_CYAN, bg);
    }
    nl();
    noecho();
    curs_set(0);
    timeout(0);

    r = (float)(LINES - 4);
    c = (float)(COLS - 4);
    for (j=5;--j>=0;) {
	xpos[j]=(int)(c* ranf())+2;
	ypos[j]=(int)(r* ranf())+2;
    }

    for (j=0;;) {
	x=(int)(c*ranf())+2;
	y=(int)(r*ranf())+2;

	mvaddch(y,x, '.');

	mvaddch(ypos[j], xpos[j], 'o');

	j = next_j(j);
	mvaddch(ypos[j], xpos[j], 'O');

	j = next_j(j);
	mvaddch( ypos[j]-1, xpos[j],    '-');
	mvaddstr(ypos[j],   xpos[j]-1, "|.|");
	mvaddch( ypos[j]+1, xpos[j],    '-');

	j = next_j(j);
	mvaddch( ypos[j]-2, xpos[j],     '-');
	mvaddstr(ypos[j]-1, xpos[j]-1,  "/ \\");
	mvaddstr(ypos[j],   xpos[j]-2,  "| O |");
	mvaddstr(ypos[j]+1, xpos[j]-1,  "\\ /");
	mvaddch( ypos[j]+2, xpos[j],     '-');

	j = next_j(j);
	mvaddch( ypos[j]-2, xpos[j],     ' ');
	mvaddstr(ypos[j]-1, xpos[j]-1,  "   ");
	mvaddstr(ypos[j],   xpos[j]-2, "     ");
	mvaddstr(ypos[j]+1, xpos[j]-1,  "   ");
	mvaddch( ypos[j]+2, xpos[j],     ' ');

	xpos[j] = x; ypos[j] = y;

	switch(getch())
	{
	case('q'):
	case('Q'):
	    curs_set(1);
	    endwin();
	    return(EXIT_SUCCESS);
	case 's':
	    nodelay(stdscr, FALSE);
	    break;
	case ' ':
	    nodelay(stdscr, TRUE);
	    break;
#ifdef KEY_RESIZE
	case(KEY_RESIZE):
	    r = (float)(LINES - 4);
	    c = (float)(COLS - 4);
	    break;
#endif
	}
	napms(50);
    }
}

static void
onsig(int n GCC_UNUSED)
{
    curs_set(1);
    endwin();
    exit(EXIT_FAILURE);
}

static float
ranf(void)
{
    long r = (rand() & 077777);
    return ((float) r / 32768.);
}