1 /* coral, by "Frederick G.M. Roeber" <roeber@netscape.com>, 15-jul-97.
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
12 #include "screenhack.h"
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;
24 static XPoint *walkers;
26 static int width, widthb;
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)))
35 init_coral(Display *dpy, Window window)
39 XWindowAttributes xgwa;
40 Bool writeable = False;
45 XClearWindow(dpy, window);
46 XGetWindowAttributes(dpy, window, &xgwa);
48 widthb = ((xgwa.width + 31) >> 5);
50 board = (unsigned int *)calloc(widthb * xgwa.height, sizeof(unsigned int));
54 free_colors(dpy, cmap, colors, ncolors);
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);
62 make_uniform_colormap(dpy, xgwa.visual, cmap, colors, &ncolors, True, &writeable, False);
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]);
70 colorindex = random()%ncolors;
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);
79 seeds = get_integer_resource("seeds", "Integer");
80 if( seeds < 1 ) seeds = 1;
81 if( seeds > 1000 ) seeds = 1000;
83 colorsloth = nwalkers*2/ncolors;
84 XSetForeground(dpy, draw_gc, colors[colorindex].pixel);
86 for( i = 0; i < seeds; i++ ) {
90 y = random() % height;
91 } while( getdot(x, y) );
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);
99 for( i = 0; i < nwalkers; i++ ) {
100 walkers[i].x = (random() % (width-2)) + 1;
101 walkers[i].y = (random() % (height-2)) + 1;
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.)
122 register int j = (r & 3);
130 coral(Display *dpy, Window window)
132 int delay2 = get_integer_resource ("delay2", "Integer");
134 int max_points = 200;
136 XPoint *pointbuf = (XPoint *) calloc(sizeof(XPoint), max_points+2);
137 if (!pointbuf) exit(-1);
142 for( i = 0; i < nwalkers; i++ ) {
143 int x = walkers[i].x;
144 int y = walkers[i].y;
151 /* XDrawPoint(dpy, window, draw_gc, x, y); */
152 pointbuf[npoints].x = x;
153 pointbuf[npoints].y = y;
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));
161 walkers[i].x = walkers[nwalkers].x;
162 walkers[i].y = walkers[nwalkers].y;
163 if( 0 == (nwalkers%colorsloth) ) {
167 if (flush || color || 0 == nwalkers || npoints >= max_points) {
168 XDrawPoints(dpy, window, draw_gc, pointbuf, npoints,
176 if( colorindex == ncolors )
178 XSetForeground(dpy, draw_gc, colors[colorindex].pixel);
181 if( 0 == nwalkers ) {
187 /* move it a notch */
191 if( 1 == x ) continue;
195 if( width-2 == x ) continue;
199 if( 1 == y ) continue;
202 default: /* case 3: */
203 if( height-2 == y ) continue;
215 XDrawPoints(dpy, window, draw_gc, pointbuf, npoints,
225 char *progclass = "Coral";
228 "Coral.background: black",
229 "Coral.foreground: white",
231 "*seeds: 20", /* too many for 640x480, too few for 1280x1024 */
237 XrmOptionDescRec options[] = {
238 { "-density", ".density", XrmoptionSepArg, 0 },
239 { "-seeds", ".seeds", XrmoptionSepArg, 0 },
240 { "-delay", ".delay", XrmoptionSepArg, 0 },
241 { "-delay2", ".delay2", XrmoptionSepArg, 0 },
246 screenhack(dpy, window)
250 int delay = get_integer_resource ("delay", "Integer");
252 init_coral(dpy, window);
254 if( delay ) sleep(delay);
255 erase_full_window(dpy, window);