1 /* squiral, by "Jeff Epler" <jepler@inetnebr.com>, 18-mar-1999.
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"
17 #define R(x) (abs(random())%x)
18 #define PROB(x) (abs(random())<(x*(double)RAND_MAX))
20 #define NCOLORSMAX 255
21 static int width, height, count, cycle;
22 static double frac, disorder, handedness;
24 static GC draw_gc, erase_gc;
25 static XColor colors[NCOLORSMAX];
28 /* 0- 3 left-winding */
29 /* 4- 7 right-winding */
31 #define CLEAR1(x,y) (!fill[((y)%height)*width+(x)%width])
32 #define MOVE1(x,y) (fill[((y)%height)*width+(x)%width]=1, XDrawPoint(dpy, window, draw_gc, (x)%width,(y)%height), cov++)
40 #define CLEARDXY(x,y,dx,dy) CLEAR1(x+dx, y+dy) && CLEAR1(x+dx+dx, y+dy+dy)
41 #define MOVEDXY(x,y,dx,dy) MOVE1 (x+dx, y+dy), MOVE1 (x+dx+dx, y+dy+dy)
43 #define CLEAR(d) CLEARDXY(w->h,w->v, dirh[d],dirv[d])
44 #define MOVE(d) (XSetForeground(dpy, draw_gc, colors[w->c].pixel), \
45 MOVEDXY(w->h,w->v, dirh[d],dirv[d]), \
46 w->h=w->h+dirh[d]*2, \
47 w->v=w->v+dirv[d]*2, dir=d)
49 #define RANDOM (void) (w->h = R(width), w->v = R(height), w->c = R(ncolors), \
50 type=R(2), dir=R(4), (cycle && (w->cc=R(3)+ncolors)))
60 #define SUCC(x) ((x+1)%4)
61 #define PRED(x) ((x+3)%4)
64 #define REV ((dir+2)%4)
66 #define TRY(x) if (CLEAR(x)) { MOVE(x); break; }
69 do_worm(Display *dpy, Window window, struct worm *w)
74 w->c = (w->c+w->cc) % ncolors;
76 if (PROB(disorder)) type=PROB(handedness);
97 init_squiral(Display *dpy, Window window)
101 XWindowAttributes xgwa;
102 Bool writeable = False;
105 XClearWindow(dpy, window);
106 XGetWindowAttributes(dpy, window, &xgwa);
108 height = xgwa.height;
110 cmap = xgwa.colormap;
111 gcv.foreground = get_pixel_resource("foreground",
112 "Foreground", dpy, cmap);
113 draw_gc = XCreateGC(dpy, window, GCForeground, &gcv);
114 gcv.foreground = get_pixel_resource ("background", "Background",dpy, cmap);
115 erase_gc = XCreateGC (dpy, window, GCForeground, &gcv);
116 cmap = xgwa.colormap;
118 free_colors(dpy, cmap, colors, ncolors);
123 colors[0].pixel=get_pixel_resource("foreground","Foreground", dpy, cmap);
125 ncolors = get_integer_resource("ncolors", "Integer");
126 if (ncolors < 0 || ncolors > NCOLORSMAX)
127 ncolors = NCOLORSMAX;
128 make_uniform_colormap(dpy, xgwa.visual, cmap, colors, &ncolors, True,
132 colors[0].pixel=get_pixel_resource("foreground","Foreground",dpy, cmap);
135 count= get_integer_resource("count", "Integer");
136 frac = get_integer_resource("fill", "Integer")*0.01;
137 cycle= get_boolean_resource("cycle", "Cycle");
138 disorder=get_float_resource("disorder", "Float");
139 handedness=get_float_resource("handedness", "Float");
141 if(frac<0.01) frac=0.01;
142 if(frac>0.99) frac=0.99;
143 if(count==0) count=width/32;
145 if(count>1000) count=1000;
147 if(worms) free(worms);
150 worms=calloc(count, sizeof(struct worm));
151 fill=calloc(width*height, sizeof(int));
153 dirh[0]=0; dirh[1]=1; dirh[2]=0; dirh[3]=width-1;
154 dirv[0]=height-1; dirv[1]=0; dirv[2]=1; dirv[3]=0;
155 for(i=0;i<count;i++) {
157 worms[i].v=R(height);
158 worms[i].s=R(4)+4*PROB(handedness);
159 worms[i].c=R(ncolors);
160 if(cycle) { worms[i].cc=R(3)+ncolors; }
166 screenhack(Display *dpy, Window window)
169 int delay= get_integer_resource("delay", "Integer");
170 init_squiral(dpy, window);
171 cov=0; inclear=height;
174 XDrawLine(dpy, window, erase_gc, 0, inclear, width-1, inclear);
175 memset(&fill[inclear*width], 0, sizeof(int)*width);
176 XDrawLine(dpy, window, erase_gc, 0, height-inclear-1, width-1,
178 memset(&fill[(height-inclear-1)*width], 0, sizeof(int)*width);
180 XDrawLine(dpy, window, erase_gc, 0, inclear, width-1, inclear);
181 memset(&fill[inclear*width], 0, sizeof(int)*width);
182 XDrawLine(dpy, window, erase_gc, 0, height-inclear-1, width-1,
184 memset(&fill[(height-inclear-1)*width], 0, sizeof(int)*width);
186 if(inclear>height/2) inclear=height;
188 else if(cov>(frac*width*height)) {
192 for(i=0;i<count;i++) do_worm(dpy, window, &worms[i]);
193 screenhack_handle_events(dpy);
198 char *progclass="Squiral";
201 ".background: black",
202 ".foreground: white",
213 XrmOptionDescRec options[] = {
214 {"-fill", ".fill", XrmoptionSepArg, 0},
215 {"-count", ".count", XrmoptionSepArg, 0},
216 {"-delay", ".delay", XrmoptionSepArg, 0},
217 {"-disorder", ".disorder", XrmoptionSepArg, 0},
218 {"-handedness", ".handedness", XrmoptionSepArg, 0},
219 {"-ncolors", ".ncolors", XrmoptionSepArg, 0},
220 {"-cycle", ".cycle", XrmoptionNoArg, "True"},
221 {"-no-cycle", ".cycle", XrmoptionNoArg, "False"},