1 /* anemon, Copyright (c) 2001 Gabriel Finch
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 /*------------------------------------------------------------------------
15 | MODULE OF xscreensaver
17 | DESCRIPTION Anemone.
19 | WRITTEN BY Gabriel Finch
23 | MODIFICATIONS june 2001 started
25 +----------------------------------------------------------------------*/
29 #include "screenhack.h"
32 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
34 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
37 /*-----------------------------------------------------------------------+
39 +-----------------------------------------------------------------------*/
42 #define TWO_PI (2.0 * M_PI)
43 #define RND(x) (random() % (x))
66 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
68 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
70 int arms; /* number of arms */
71 int finpoints; /* final number of points in each array. */
72 long delay; /* usecs to wait between updates. */
74 int scrWidth, scrHeight;
80 vPend *vPendage; /* 3D representation of appendages */
81 appDef *appD; /* defaults */
85 double turn, turndelta;
87 int mx, my; /* max screen coordinates. */
98 /*-----------------------------------------------------------------------+
100 +-----------------------------------------------------------------------*/
104 /*-----------------------------------------------------------------------+
105 | PRIVATE FUNCTIONS |
106 +-----------------------------------------------------------------------*/
113 if ((ret = malloc(size)) == NULL) {
114 fprintf(stderr, "anemone: out of memory\n");
122 initAppendages(struct state *st)
125 /*int marginx, marginy; */
127 /*double scalex, scaley;*/
131 st->mx = st->scrWidth - 1;
132 st->my = st->scrHeight - 1;
134 /* each appendage will have: colour,
135 number of points, and a grow or shrink indicator */
137 /* added: growth rate 1-10 (smaller==faster growth) */
138 /* each appendage needs virtual coords (x,y,z) with y and z combining to
141 st->vPendage = (vPend *) xmalloc((st->finpoints + 1) * sizeof(vPend) * st->arms);
142 st->appD = (appDef *) xmalloc(sizeof(appDef) * st->arms);
145 for (i = 0; i < st->arms; i++) {
146 st->aCurr = st->appD + i;
147 st->vCurr = st->vPendage + (st->finpoints + 1) * i;
148 st->vNext = st->vCurr + 1;
150 st->aCurr->col = st->colors[random() % st->ncolors].pixel;
151 st->aCurr->numpt = 1;
152 st->aCurr->growth = st->finpoints / 2 + RND(st->finpoints / 2);
153 st->aCurr->rate = RND(11) * RND(11);
156 x = (1 - RND(1001) / 500);
157 y = (1 - RND(1001) / 500);
158 z = (1 - RND(1001) / 500);
159 dist = x * x + y * y + z * z;
160 } while (dist >= 1.);
162 st->vCurr->x = x * 200;
163 st->vCurr->y = st->my / 2 + y * 200;
164 st->vCurr->z = 0 + z * 200;
166 /* start the arm going outwards */
167 st->vCurr->sx = st->vCurr->x / 5;
168 st->vCurr->sy = (st->vCurr->y - st->my / 2) / 5;
169 st->vCurr->sz = (st->vCurr->z) / 5;
172 st->vNext->x = st->vCurr->x + st->vCurr->sx;
173 st->vNext->y = st->vCurr->y + st->vCurr->sy;
174 st->vNext->z = st->vCurr->z + st->vCurr->sz;
179 anemone_init (Display *disp, Window window)
181 struct state *st = (struct state *) calloc (1, sizeof(*st));
182 XWindowAttributes wa;
187 st->width = get_integer_resource(st->dpy, "width", "Integer");
188 st->arms = get_integer_resource(st->dpy, "arms", "Integer");
189 st->finpoints = get_integer_resource(st->dpy, "finpoints", "Integer");
190 st->delay = get_integer_resource(st->dpy, "delay", "Integer");
191 st->withdraw = get_integer_resource(st->dpy, "withdraw", "Integer");
192 st->turndelta = get_float_resource(st->dpy, "turnspeed", "float") / 100000;
196 # ifdef HAVE_COCOA /* Don't second-guess Quartz's double-buffering */
200 st->b = st->ba = st->bb = 0; /* double-buffer to reduce flicker */
201 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
202 st->b = st->backb = xdbe_get_backbuffer (st->dpy, window, XdbeUndefined);
203 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
206 XGetWindowAttributes(st->dpy, window, &wa);
207 st->scrWidth = wa.width;
208 st->scrHeight = wa.height;
209 st->cmap = wa.colormap;
211 st->ncolors = get_integer_resource (st->dpy, "colors", "Colors");
213 st->colors = (XColor *) malloc(sizeof(*st->colors) * (st->ncolors+1));
214 make_smooth_colormap (st->dpy, wa.visual, st->cmap, st->colors, &st->ncolors,
217 st->gcDraw = XCreateGC(st->dpy, window, 0, &st->gcv);
218 st->gcv.foreground = get_pixel_resource(st->dpy, st->cmap,
219 "background", "Background");
220 st->gcClear = XCreateGC(st->dpy, window, GCForeground, &st->gcv);
225 st->ba = XCreatePixmap (st->dpy, window, st->scrWidth, st->scrHeight, wa.depth);
226 st->bb = XCreatePixmap (st->dpy, window, st->scrWidth, st->scrHeight, wa.depth);
235 if (st->ba) XFillRectangle (st->dpy, st->ba, st->gcClear, 0, 0, st->scrWidth, st->scrHeight);
236 if (st->bb) XFillRectangle (st->dpy, st->bb, st->gcClear, 0, 0, st->scrWidth, st->scrHeight);
238 XClearWindow(st->dpy, window);
239 XSetLineAttributes(st->dpy, st->gcDraw, st->width, LineSolid, CapRound, JoinBevel);
248 createPoints(struct state *st)
251 int withdrawall = RND(st->withdraw);
253 for (i = 0; i< st->arms; i++) {
254 st->aCurr = st->appD + i;
256 st->aCurr->growth = -st->finpoints;
257 st->turndelta = -st->turndelta;
260 else if (withdrawall<11) st->aCurr->growth = -st->aCurr->numpt;
262 else if (RND(100)<st->aCurr->rate) {
263 if (st->aCurr->growth>0) {
264 if (!(--st->aCurr->growth)) st->aCurr->growth = -RND(st->finpoints) - 1;
265 st->vCurr = st->vPendage + (st->finpoints + 1) * i + st->aCurr->numpt - 1;
266 if (st->aCurr->numpt<st->finpoints - 1) {
268 st->vNext = st->vCurr + 1;
270 st->vNext->sx = st->vCurr->sx + RND(3) - 1;
271 st->vNext->sy = st->vCurr->sy + RND(3) - 1;
272 st->vNext->sz = st->vCurr->sz + RND(3) - 1;
273 st->vCurr = st->vNext + 1;
274 st->vCurr->x = st->vNext->x + st->vNext->sx;
275 st->vCurr->y = st->vNext->y + st->vNext->sy;
276 st->vCurr->z = st->vNext->z + st->vNext->sz;
285 drawImage(struct state *st, Drawable curr_window, double sint, double cost)
287 int q,numpt,mx2 = st->mx / 2;
288 double cx,cy,cz,nx = 0,ny = 0,nz = 0;
290 if ((numpt = st->aCurr->numpt)==1) return;
291 XSetForeground(st->dpy, st->gcDraw, st->aCurr->col);
293 st->vNext = st->vCurr + 1;
300 for (q = 0; q < numpt - 1; q++) {
301 nx = st->vNext->x + 2 - RND(5);
302 ny = st->vNext->y + 2 - RND(5);
303 nz = st->vNext->z + 2 - RND(5);
305 XDrawLine(st->dpy, curr_window, st->gcDraw,
306 mx2 + cx * cost - cz * sint, cy,
307 mx2 + nx * cost - nz * sint, ny);
315 XSetLineAttributes(st->dpy, st->gcDraw, st->width * 3,
316 LineSolid, CapRound, JoinBevel);
317 XDrawLine(st->dpy, curr_window, st->gcDraw,
318 st->mx / 2 + cx * cost - cz * sint, cy,
319 st->mx / 2 + nx * cost - nz * sint, ny);
320 XSetLineAttributes(st->dpy, st->gcDraw, st->width,
321 LineSolid, CapRound, JoinBevel);
326 animateAnemone(struct state *st, Drawable curr_window)
329 double sint = sin(st->turn),cost = cos(st->turn);
331 st->aCurr = st->appD;
332 for (i = 0; i< st->arms; i++) {
333 st->vCurr = st->vPendage + (st->finpoints + 1) * i;
334 if (RND(25)<st->aCurr->rate) {
335 if (st->aCurr->growth<0) {
336 st->aCurr->numpt -= st->aCurr->numpt>1;
337 if (!(++st->aCurr->growth)) st->aCurr->growth = RND(st->finpoints - st->aCurr->numpt) + 1;
340 drawImage(st, curr_window, sint, cost);
341 st->turn += st->turndelta;
346 if (st->turn >= TWO_PI) st->turn -= TWO_PI;
350 anemone_draw (Display *dpy, Window window, void *closure)
352 struct state *st = (struct state *) closure;
354 XFillRectangle (st->dpy, st->b, st->gcClear, 0, 0, st->scrWidth, st->scrHeight);
356 animateAnemone(st, st->b);
358 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
361 XdbeSwapInfo info[1];
362 info[0].swap_window = window;
363 info[0].swap_action = XdbeUndefined;
364 XdbeSwapBuffers (st->dpy, info, 1);
367 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
370 XCopyArea (st->dpy, st->b, window, st->gcClear, 0, 0,
371 st->scrWidth, st->scrHeight, 0, 0);
372 st->b = (st->b == st->ba ? st->bb : st->ba);
380 anemone_reshape (Display *dpy, Window window, void *closure,
381 unsigned int w, unsigned int h)
383 /* need to re-make pixmaps too...
384 struct state *st = (struct state *) closure;
391 anemone_event (Display *dpy, Window window, void *closure, XEvent *event)
397 anemone_free (Display *dpy, Window window, void *closure)
399 struct state *st = (struct state *) closure;
400 if (st->vPendage) free (st->vPendage);
401 if (st->appD) free (st->appD);
407 static const char *anemone_defaults [] = {
408 ".background: black",
416 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
418 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
423 static XrmOptionDescRec anemone_options [] = {
424 { "-arms", ".arms", XrmoptionSepArg, 0 },
425 { "-finpoints", ".finpoints", XrmoptionSepArg, 0 },
426 { "-delay", ".delay", XrmoptionSepArg, 0 },
427 { "-width", ".width", XrmoptionSepArg, 0 },
428 { "-withdraw", ".withdraw", XrmoptionSepArg, 0 },
429 { "-turnspeed", ".turnspeed", XrmoptionSepArg, 0 },
430 { "-colors", ".colors", XrmoptionSepArg, 0 },
435 XSCREENSAVER_MODULE ("Anemone", anemone)