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 unsigned int default_fg_pixel;
17 #define NCOLORSMAX 200
18 static XColor colors[NCOLORSMAX];
19 static int ncolors = 0;
20 static int colorindex = 0;
21 static int colorsloth;
23 static XPoint *walkers;
25 static int width, widthb;
28 static unsigned int *board;
29 #define getdot(x,y) (board[(y*widthb)+(x>>5)] & (1<<(x & 31)))
30 #define setdot(x,y) (board[(y*widthb)+(x>>5)] |= (1<<(x & 31)))
34 init_coral(Display *dpy, Window window)
38 XWindowAttributes xgwa;
39 Bool writeable = False;
44 XClearWindow(dpy, window);
45 XGetWindowAttributes(dpy, window, &xgwa);
47 widthb = ((xgwa.width + 31) >> 5);
49 board = (unsigned int *)calloc(widthb * xgwa.height, sizeof(unsigned int));
53 free_colors(dpy, cmap, colors, ncolors);
56 gcv.foreground = default_fg_pixel = get_pixel_resource("foreground", "Foreground", dpy, cmap);
57 draw_gc = XCreateGC(dpy, window, GCForeground, &gcv);
59 make_uniform_colormap(dpy, xgwa.visual, cmap, colors, &ncolors, True, &writeable, False);
60 colorindex = random()%ncolors;
62 density = get_integer_resource("density", "Integer");
63 if( density < 1 ) density = 1;
64 if( density > 100 ) density = 90; /* more like mold than coral */
65 nwalkers = (width*height*density)/100;
66 walkers = (XPoint *)calloc(nwalkers, sizeof(XPoint));
67 if( (XPoint *)0 == walkers ) exit(1);
69 seeds = get_integer_resource("seeds", "Integer");
70 if( seeds < 1 ) seeds = 1;
71 if( seeds > 1000 ) seeds = 1000;
73 colorsloth = nwalkers*2/ncolors;
74 XSetForeground(dpy, draw_gc, colors[colorindex].pixel);
76 for( i = 0; i < seeds; i++ ) {
80 y = random() % height;
81 } while( getdot(x, y) );
83 setdot((x-1), (y-1)); setdot(x, (y-1)); setdot((x+1), (y-1));
84 setdot((x-1), y ); setdot(x, y ); setdot((x+1), y );
85 setdot((x-1), (y+1)); setdot(x, (y+1)); setdot((x+1), (y+1));
86 XDrawPoint(dpy, window, draw_gc, x, y);
89 for( i = 0; i < nwalkers; i++ ) {
90 walkers[i].x = (random() % (width-2)) + 1;
91 walkers[i].y = (random() % (height-2)) + 1;
96 /* returns 2 bits of randomness (conserving calls to random()).
97 This speeds things up a little, but not a lot (5-10% or so.)
112 register int j = (r & 3);
120 coral(Display *dpy, Window window)
122 int delay2 = get_integer_resource ("delay2", "Integer");
124 int max_points = 200;
126 XPoint *pointbuf = (XPoint *) calloc(sizeof(XPoint), max_points+2);
127 if (!pointbuf) exit(-1);
132 for( i = 0; i < nwalkers; i++ ) {
133 int x = walkers[i].x;
134 int y = walkers[i].y;
141 /* XDrawPoint(dpy, window, draw_gc, x, y); */
142 pointbuf[npoints].x = x;
143 pointbuf[npoints].y = y;
146 /* Mark the surrounding area as "sticky" */
147 setdot((x-1), (y-1)); setdot(x, (y-1)); setdot((x+1), (y-1));
148 setdot((x-1), y ); setdot((x+1), y );
149 setdot((x-1), (y+1)); setdot(x, (y+1)); setdot((x+1), (y+1));
151 walkers[i].x = walkers[nwalkers].x;
152 walkers[i].y = walkers[nwalkers].y;
153 if( 0 == (nwalkers%colorsloth) ) {
157 if (flush || color || 0 == nwalkers || npoints >= max_points) {
158 XDrawPoints(dpy, window, draw_gc, pointbuf, npoints,
166 if( colorindex == ncolors )
168 XSetForeground(dpy, draw_gc, colors[colorindex].pixel);
171 if( 0 == nwalkers ) {
177 /* move it a notch */
181 if( 1 == x ) continue;
185 if( width-2 == x ) continue;
189 if( 1 == y ) continue;
192 default: /* case 3: */
193 if( height-2 == y ) continue;
205 XDrawPoints(dpy, window, draw_gc, pointbuf, npoints,
215 char *progclass = "Coral";
218 "Coral.background: black",
219 "Coral.foreground: white",
221 "*seeds: 20", /* too many for 640x480, too few for 1280x1024 */
227 XrmOptionDescRec options[] = {
228 { "-density", ".density", XrmoptionSepArg, 0 },
229 { "-seeds", ".seeds", XrmoptionSepArg, 0 },
230 { "-delay", ".delay", XrmoptionSepArg, 0 },
231 { "-delay2", ".delay2", XrmoptionSepArg, 0 },
236 screenhack(dpy, window)
240 int delay = get_integer_resource ("delay", "Integer");
243 init_coral(dpy, window);
245 if( delay ) sleep(delay);