http://ftp.x.org/contrib/applications/xscreensaver-2.23.tar.gz
[xscreensaver] / hacks / coral.c
1 /* coral, by "Frederick G.M. Roeber" <roeber@netscape.com>, 15-jul-97.
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 #include "screenhack.h"
13 #include "colors.h"
14 #include "erase.h"
15
16 static GC draw_gc, erase_gc;
17 static unsigned int default_fg_pixel;
18 #define NCOLORSMAX 200
19 static XColor colors[NCOLORSMAX];
20 static int ncolors = 0;
21 static int colorindex = 0;
22 static int colorsloth;
23
24 static XPoint *walkers;
25 static int nwalkers;
26 static int width, widthb;
27 static int height;
28
29 static unsigned int *board;
30 #define getdot(x,y) (board[(y*widthb)+(x>>5)] &  (1<<(x & 31)))
31 #define setdot(x,y) (board[(y*widthb)+(x>>5)] |= (1<<(x & 31)))
32
33
34 static void
35 init_coral(Display *dpy, Window window)
36 {
37     XGCValues gcv;
38     Colormap cmap;
39     XWindowAttributes xgwa;
40     Bool writeable = False;
41     int seeds;
42     int density;
43     int i;
44
45     XClearWindow(dpy, window);
46     XGetWindowAttributes(dpy, window, &xgwa);
47     width = xgwa.width;
48     widthb = ((xgwa.width + 31) >> 5);
49     height = xgwa.height;
50     board = (unsigned int *)calloc(widthb * xgwa.height, sizeof(unsigned int));
51     if(!board) exit(1);
52     cmap = xgwa.colormap;
53     if( ncolors ) {
54         free_colors(dpy, cmap, colors, ncolors);
55         ncolors = 0;
56     }
57     gcv.foreground = default_fg_pixel = get_pixel_resource("foreground", "Foreground", dpy, cmap);
58     draw_gc = XCreateGC(dpy, window, GCForeground, &gcv);
59     gcv.foreground = get_pixel_resource ("background", "Background",dpy, cmap);
60     erase_gc = XCreateGC (dpy, window, GCForeground, &gcv);
61     ncolors = NCOLORSMAX;
62     make_uniform_colormap(dpy, xgwa.visual, cmap, colors, &ncolors, True, &writeable, False);
63     if (ncolors <= 0) {
64       ncolors = 2;
65       colors[0].red = colors[0].green = colors[0].blue = 0;
66       colors[1].red = colors[1].green = colors[1].blue = 0xFFFF;
67       XAllocColor(dpy, cmap, &colors[0]);
68       XAllocColor(dpy, cmap, &colors[1]);
69    }
70     colorindex = random()%ncolors;
71     
72     density = get_integer_resource("density", "Integer");
73     if( density < 1 ) density = 1;
74     if( density > 100 ) density = 90; /* more like mold than coral */
75     nwalkers = (width*height*density)/100;
76     walkers = (XPoint *)calloc(nwalkers, sizeof(XPoint));
77     if( (XPoint *)0 == walkers ) exit(1);
78
79     seeds = get_integer_resource("seeds", "Integer");
80     if( seeds < 1 ) seeds = 1;
81     if( seeds > 1000 ) seeds = 1000;
82
83     colorsloth = nwalkers*2/ncolors;
84     XSetForeground(dpy, draw_gc, colors[colorindex].pixel);
85
86     for( i = 0; i < seeds; i++ ) {
87         int x, y;
88         do {
89             x = random() % width;
90             y = random() % height;
91         } while( getdot(x, y) );
92
93         setdot((x-1), (y-1)); setdot(x, (y-1)); setdot((x+1), (y-1));
94         setdot((x-1),  y   ); setdot(x,  y   ); setdot((x+1),  y   );
95         setdot((x-1), (y+1)); setdot(x, (y+1)); setdot((x+1), (y+1));
96         XDrawPoint(dpy, window, draw_gc, x, y);
97     }
98
99     for( i = 0; i < nwalkers; i++ ) {
100         walkers[i].x = (random() % (width-2)) + 1;
101         walkers[i].y = (random() % (height-2)) + 1;
102     }
103 }
104
105
106 /* returns 2 bits of randomness (conserving calls to random()).
107    This speeds things up a little, but not a lot (5-10% or so.)
108  */
109 static int 
110 rand_2(void)
111 {
112   static int i = 0;
113   static int r = 0;
114   if (i != 0) {
115     i--;
116   } else {
117     i = 15;
118     r = random();
119   }
120
121   {
122     register int j = (r & 3);
123     r = r >> 2;
124     return j;
125   }
126 }
127
128
129 static void
130 coral(Display *dpy, Window window)
131 {
132     int delay2 = get_integer_resource ("delay2", "Integer");
133
134     int max_points = 200;
135     int npoints = 0;
136     XPoint *pointbuf = (XPoint *) calloc(sizeof(XPoint), max_points+2);
137     if (!pointbuf) exit(-1);
138
139     while( 1 ) {
140         int i;
141
142         for( i = 0; i < nwalkers; i++ ) {
143             int x = walkers[i].x;
144             int y = walkers[i].y;
145
146             if( getdot(x, y) ) {
147
148                 Bool flush = False;
149                 Bool color = False;
150
151                 /* XDrawPoint(dpy, window, draw_gc, x, y); */
152                 pointbuf[npoints].x = x;
153                 pointbuf[npoints].y = y;
154                 npoints++;
155
156                 /* Mark the surrounding area as "sticky" */
157                 setdot((x-1), (y-1)); setdot(x, (y-1)); setdot((x+1), (y-1));
158                 setdot((x-1),  y   );                   setdot((x+1),  y   );
159                 setdot((x-1), (y+1)); setdot(x, (y+1)); setdot((x+1), (y+1));
160                 nwalkers--;
161                 walkers[i].x = walkers[nwalkers].x;
162                 walkers[i].y = walkers[nwalkers].y;
163                 if( 0 == (nwalkers%colorsloth) ) {
164                   color = True;
165                 }
166                   
167                 if (flush || color || 0 == nwalkers || npoints >= max_points) {
168                   XDrawPoints(dpy, window, draw_gc, pointbuf, npoints,
169                               CoordModeOrigin);
170                   npoints = 0;
171                   XSync(dpy, True);
172                 }
173
174                 if (color) {
175                     colorindex++;
176                     if( colorindex == ncolors )
177                         colorindex = 0;
178                     XSetForeground(dpy, draw_gc, colors[colorindex].pixel);
179                 }
180
181                 if( 0 == nwalkers ) {
182                     XSync(dpy, True);
183                     free(pointbuf);
184                     return;
185                 }
186             } else {
187                 /* move it a notch */
188                 do {
189                     switch(rand_2()) {
190                     case 0:
191                         if( 1 == x ) continue;
192                         walkers[i].x--;
193                         break;
194                     case 1:
195                         if( width-2 == x ) continue;
196                         walkers[i].x++;
197                         break;
198                     case 2:
199                         if( 1 == y ) continue;
200                         walkers[i].y--;
201                         break;
202                     default: /* case 3: */
203                         if( height-2 == y ) continue;
204                         walkers[i].y++;
205                         break;
206                     /* default:
207                       abort(); */
208                     }
209                 } while(0);
210             }
211         }
212
213         if (delay2 > 0) {
214           if (npoints > 0) {
215             XDrawPoints(dpy, window, draw_gc, pointbuf, npoints,
216                         CoordModeOrigin);
217             npoints = 0;
218             XSync(dpy, True);
219           }
220           usleep(delay2);
221         }
222     }
223 }
224
225 char *progclass = "Coral";
226
227 char *defaults[] = {
228   ".background: black",
229   ".foreground: white",
230   "*density:    25",
231   "*seeds:      20", /* too many for 640x480, too few for 1280x1024 */
232   "*delay:      5",
233   "*delay2:     1000",
234   0
235 };
236
237 XrmOptionDescRec options[] = {
238     { "-density", ".density", XrmoptionSepArg, 0 },
239     { "-seeds", ".seeds", XrmoptionSepArg, 0 },
240     { "-delay", ".delay", XrmoptionSepArg, 0 },
241     { "-delay2", ".delay2", XrmoptionSepArg, 0 },
242     { 0, 0, 0, 0 }
243 };
244
245 void
246 screenhack(dpy, window)
247 Display *dpy;
248 Window window;
249 {
250     int delay = get_integer_resource ("delay", "Integer");
251     while( 1 ) {
252         init_coral(dpy, window);
253         coral(dpy, window);
254         if( delay ) sleep(delay);
255         erase_full_window(dpy, window);
256     }
257 }