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 #define NCOLORSMAX 200
23 unsigned int default_fg_pixel;
24 XColor colors[NCOLORSMAX];
45 #define getdot(x,y) (st->board[(y*st->widthb)+(x>>5)] & (1<<(x & 31)))
46 #define setdot(x,y) (st->board[(y*st->widthb)+(x>>5)] |= (1<<(x & 31)))
50 init_coral(struct state *st)
54 XWindowAttributes xgwa;
55 Bool writeable = False;
60 XClearWindow(st->dpy, st->window);
61 XGetWindowAttributes(st->dpy, st->window, &xgwa);
62 st->width = xgwa.width;
63 st->widthb = ((xgwa.width + 31) >> 5);
64 st->height = xgwa.height;
65 if (st->board) free(st->board);
66 st->board = (unsigned int *)calloc(st->widthb * xgwa.height, sizeof(unsigned int));
67 if(!st->board) exit(1);
70 free_colors(st->dpy, cmap, st->colors, st->ncolors);
73 gcv.foreground = st->default_fg_pixel = get_pixel_resource(st->dpy, cmap, "foreground", "Foreground");
74 st->draw_gc = XCreateGC(st->dpy, st->window, GCForeground, &gcv);
75 gcv.foreground = get_pixel_resource (st->dpy, cmap, "background", "Background");
76 st->erase_gc = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
77 st->ncolors = NCOLORSMAX;
78 make_uniform_colormap(st->dpy, xgwa.visual, cmap, st->colors, &st->ncolors, True, &writeable, False);
79 if (st->ncolors <= 0) {
81 st->colors[0].red = st->colors[0].green = st->colors[0].blue = 0;
82 st->colors[1].red = st->colors[1].green = st->colors[1].blue = 0xFFFF;
83 XAllocColor(st->dpy, cmap, &st->colors[0]);
84 XAllocColor(st->dpy, cmap, &st->colors[1]);
86 st->colorindex = random()%st->ncolors;
88 density = get_integer_resource(st->dpy, "density", "Integer");
89 if( density < 1 ) density = 1;
90 if( density > 100 ) density = 90; /* more like mold than coral */
91 st->nwalkers = (st->width*st->height*density)/100;
92 if (st->walkers) free(st->walkers);
93 st->walkers = (XPoint *)calloc(st->nwalkers, sizeof(XPoint));
94 if( (XPoint *)0 == st->walkers ) exit(1);
96 seeds = get_integer_resource(st->dpy, "seeds", "Integer");
97 if( seeds < 1 ) seeds = 1;
98 if( seeds > 1000 ) seeds = 1000;
100 st->colorsloth = st->nwalkers*2/st->ncolors;
101 XSetForeground(st->dpy, st->draw_gc, st->colors[st->colorindex].pixel);
103 if ((st->width <= 2) || (st->height <= 2)) return;
105 for( i = 0; i < seeds; i++ ) {
109 x = 1 + random() % (st->width - 2);
110 y = 1 + random() % (st->height - 2);
111 } while( getdot(x, y) && max_repeat--);
113 setdot((x-1), (y-1)); setdot(x, (y-1)); setdot((x+1), (y-1));
114 setdot((x-1), y ); setdot(x, y ); setdot((x+1), y );
115 setdot((x-1), (y+1)); setdot(x, (y+1)); setdot((x+1), (y+1));
116 XDrawPoint(st->dpy, st->window, st->draw_gc, x, y);
119 for( i = 0; i < st->nwalkers; i++ ) {
120 st->walkers[i].x = (random() % (st->width-2)) + 1;
121 st->walkers[i].y = (random() % (st->height-2)) + 1;
126 /* returns 2 bits of randomness (conserving calls to random()).
127 This speeds things up a little, but not a lot (5-10% or so.)
142 register int j = (r & 3);
150 coral(struct state *st)
154 for( i = 0; i < st->nwalkers; i++ ) {
155 int x = st->walkers[i].x;
156 int y = st->walkers[i].y;
163 /* XDrawPoint(dpy, window, draw_gc, x, y); */
164 st->pointbuf[st->npoints].x = x;
165 st->pointbuf[st->npoints].y = y;
168 /* Mark the surrounding area as "sticky" */
169 setdot((x-1), (y-1)); setdot(x, (y-1)); setdot((x+1), (y-1));
170 setdot((x-1), y ); setdot((x+1), y );
171 setdot((x-1), (y+1)); setdot(x, (y+1)); setdot((x+1), (y+1));
173 st->walkers[i].x = st->walkers[st->nwalkers].x;
174 st->walkers[i].y = st->walkers[st->nwalkers].y;
176 ((st->colorsloth ? st->nwalkers%st->colorsloth : 0)) ) {
180 if (flush || color || 0 == st->nwalkers || st->npoints >= st->max_points) {
181 XDrawPoints(st->dpy, st->window, st->draw_gc, st->pointbuf, st->npoints,
188 if( st->colorindex == st->ncolors )
190 XSetForeground(st->dpy, st->draw_gc, st->colors[st->colorindex].pixel);
193 /* move it a notch */
197 if( 1 == x ) continue;
201 if( st->width-2 == x ) continue;
205 if( 1 == y ) continue;
208 default: /* case 3: */
209 if( st->height-2 == y ) continue;
219 return (0 == st->nwalkers);
223 coral_init (Display *dpy, Window window)
225 struct state *st = (struct state *) calloc (1, sizeof(*st));
228 st->max_points = 200;
229 st->pointbuf = (XPoint *) calloc(sizeof(XPoint), st->max_points+2);
230 if (!st->pointbuf) exit(-1);
232 st->delay = get_integer_resource (st->dpy, "delay", "Integer");
233 st->delay2 = get_integer_resource (st->dpy, "delay2", "Integer");
240 coral_draw (Display *dpy, Window window, void *closure)
242 struct state *st = (struct state *) closure;
244 if (st->eraser || st->done)
247 st->eraser = erase_window (st->dpy, st->window, st->eraser);
253 st->reset = st->done = coral(st);
256 ? (st->delay * 1000000)
261 coral_reshape (Display *dpy, Window window, void *closure,
262 unsigned int w, unsigned int h)
267 coral_event (Display *dpy, Window window, void *closure, XEvent *event)
273 coral_free (Display *dpy, Window window, void *closure)
275 struct state *st = (struct state *) closure;
277 if (st->walkers) free (st->walkers);
278 if (st->board) free (st->board);
282 static const char *coral_defaults[] = {
283 ".background: black",
284 ".foreground: white",
287 "*seeds: 20", /* too many for 640x480, too few for 1280x1024 */
293 static XrmOptionDescRec coral_options[] = {
294 { "-density", ".density", XrmoptionSepArg, 0 },
295 { "-seeds", ".seeds", XrmoptionSepArg, 0 },
296 { "-delay", ".delay", XrmoptionSepArg, 0 },
297 { "-delay2", ".delay2", XrmoptionSepArg, 0 },
301 XSCREENSAVER_MODULE ("Coral", coral)