Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libcurses / PSD.doc / twinkle2.c
blob5d339fb5ea0afbf520645d81d1cc9fd01a3285a9
1 /* $NetBSD: twinkle2.c,v 1.5 2003/08/07 16:44:29 agc Exp $ */
3 /*
4 *
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#)twinkle2.c 8.1 (Berkeley) 6/8/93
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <curses.h>
38 #include <signal.h>
40 #define NCOLS 80
41 #define NLINES 24
42 #define MAXPATTERNS 4
44 typedef struct {
45 int y, x;
46 } LOCS;
48 static LOCS Layout[NCOLS * NLINES]; /* current board layout */
50 static int Pattern, /* current pattern number */
51 Numstars; /* number of stars in pattern */
53 static void puton(char);
54 static void die(int);
55 static void makeboard(void);
56 static int ison(int, int);
58 static int AM;
59 static char *VS;
60 static char *TI;
61 static char *CL;
63 int
64 main(void)
66 char *sp;
67 char buf[1024];
68 char *ptr = buf;
70 srand(getpid()); /* initialize random sequence */
72 if (isatty(0)) {
73 initscr();
74 gettmode();
75 if ((sp = getenv("TERM")) != NULL)
76 setterm(sp);
77 signal(SIGINT, die);
79 else {
80 printf("Need a terminal on fd=%d\n", 0);
81 exit(1);
84 tgetent(buf, sp);
86 AM = tgetflag("am");
87 TI = tgetstr("ti", &ptr);
88 if (TI == NULL) {
89 printf("terminal does not have the ti capability\n");
90 exit(1);
92 VS = tgetstr("vs", &ptr);
93 if (VS == NULL) {
94 printf("terminal does not have the vs capability\n");
95 exit(1);
97 CL = tgetstr("cl", &ptr);
98 if (CL == NULL) {
99 printf("terminal does not have the cl capability\n");
100 exit(1);
102 puts(TI);
103 puts(VS);
105 noecho();
106 nonl();
107 tputs(CL, NLINES, putchar);
108 for (;;) {
109 makeboard(); /* make the board setup */
110 puton('*'); /* put on '*'s */
111 puton(' '); /* cover up with ' 's */
116 * On program exit, move the cursor to the lower left corner by
117 * direct addressing, since current location is not guaranteed.
118 * We lie and say we used to be at the upper right corner to guarantee
119 * absolute addressing.
121 static void
122 die(int n)
124 signal(SIGINT, SIG_IGN);
125 mvcur(0, COLS - 1, LINES - 1, 0);
126 endwin();
127 exit(n);
130 static void
131 puton(char ch)
133 LOCS *lp;
134 int r;
135 LOCS *end;
136 LOCS temp;
137 static int lasty, lastx;
139 end = &Layout[Numstars];
140 for (lp = Layout; lp < end; lp++) {
141 r = rand() % Numstars;
142 temp = *lp;
143 *lp = Layout[r];
144 Layout[r] = temp;
147 for (lp = Layout; lp < end; lp++)
148 /* prevent scrolling */
149 if (!AM || (lp->y < NLINES - 1 || lp->x < NCOLS - 1)) {
150 mvcur(lasty, lastx, lp->y, lp->x);
151 putchar(ch);
152 lasty = lp->y;
153 if ((lastx = lp->x + 1) >= NCOLS)
154 if (AM) {
155 lastx = 0;
156 lasty++;
158 else
159 lastx = NCOLS - 1;
164 * Make the current board setup. It picks a random pattern and
165 * calls ison() to determine if the character is on that pattern
166 * or not.
168 static void
169 makeboard(void)
171 int y, x;
172 LOCS *lp;
174 Pattern = rand() % MAXPATTERNS;
175 lp = Layout;
176 for (y = 0; y < NLINES; y++)
177 for (x = 0; x < NCOLS; x++)
178 if (ison(y, x)) {
179 lp->y = y;
180 lp->x = x;
181 lp++;
183 Numstars = lp - Layout;
187 * Return TRUE if (y, x) is on the current pattern.
189 static int
190 ison(int y, int x)
192 switch (Pattern) {
193 case 0: /* alternating lines */
194 return !(y & 01);
195 case 1: /* box */
196 if (x >= LINES && y >= NCOLS)
197 return FALSE;
198 if (y < 3 || y >= NLINES - 3)
199 return TRUE;
200 return (x < 3 || x >= NCOLS - 3);
201 case 2: /* holy pattern! */
202 return ((x + y) & 01);
203 case 3: /* bar across center */
204 return (y >= 9 && y <= 15);
206 /* NOTREACHED */