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