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);
158 x = (1 - RND(1001) / 500);
159 y = (1 - RND(1001) / 500);
160 z = (1 - RND(1001) / 500);
161 dist = x * x + y * y + z * z;
162 } while (dist >= 1.);
164 st->vCurr->x = x * 200;
165 st->vCurr->y = st->my / 2 + y * 200;
166 st->vCurr->z = 0 + z * 200;
168 /* start the arm going outwards */
169 st->vCurr->sx = st->vCurr->x / 5;
170 st->vCurr->sy = (st->vCurr->y - st->my / 2) / 5;
171 st->vCurr->sz = (st->vCurr->z) / 5;
174 st->vNext->x = st->vCurr->x + st->vCurr->sx;
175 st->vNext->y = st->vCurr->y + st->vCurr->sy;
176 st->vNext->z = st->vCurr->z + st->vCurr->sz;
181 anemone_init (Display *disp, Window window)
183 struct state *st = (struct state *) calloc (1, sizeof(*st));
184 XWindowAttributes wa;
189 st->width = get_integer_resource(st->dpy, "width", "Integer");
190 st->arms = get_integer_resource(st->dpy, "arms", "Integer");
191 st->finpoints = get_integer_resource(st->dpy, "finpoints", "Integer");
192 st->delay = get_integer_resource(st->dpy, "delay", "Integer");
193 st->withdraw = get_integer_resource(st->dpy, "withdraw", "Integer");
194 st->turndelta = get_float_resource(st->dpy, "turnspeed", "float") / 100000;
198 # ifdef HAVE_COCOA /* Don't second-guess Quartz's double-buffering */
202 st->b = st->ba = st->bb = 0; /* double-buffer to reduce flicker */
203 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
204 st->b = st->backb = xdbe_get_backbuffer (st->dpy, window, XdbeUndefined);
205 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
208 XGetWindowAttributes(st->dpy, window, &wa);
209 st->scrWidth = wa.width;
210 st->scrHeight = wa.height;
211 st->cmap = wa.colormap;
213 st->ncolors = get_integer_resource (st->dpy, "colors", "Colors");
215 st->colors = (XColor *) malloc(sizeof(*st->colors) * (st->ncolors+1));
216 make_smooth_colormap (st->dpy, wa.visual, st->cmap, st->colors, &st->ncolors,
219 st->gcDraw = XCreateGC(st->dpy, window, 0, &st->gcv);
220 st->gcv.foreground = get_pixel_resource(st->dpy, st->cmap,
221 "background", "Background");
222 st->gcClear = XCreateGC(st->dpy, window, GCForeground, &st->gcv);
227 st->ba = XCreatePixmap (st->dpy, window, st->scrWidth, st->scrHeight, wa.depth);
228 st->bb = XCreatePixmap (st->dpy, window, st->scrWidth, st->scrHeight, wa.depth);
237 if (st->ba) XFillRectangle (st->dpy, st->ba, st->gcClear, 0, 0, st->scrWidth, st->scrHeight);
238 if (st->bb) XFillRectangle (st->dpy, st->bb, st->gcClear, 0, 0, st->scrWidth, st->scrHeight);
240 XClearWindow(st->dpy, window);
241 XSetLineAttributes(st->dpy, st->gcDraw, st->width, LineSolid, CapRound, JoinBevel);
250 createPoints(struct state *st)
253 int withdrawall = RND(st->withdraw);
255 for (i = 0; i< st->arms; i++) {
256 st->aCurr = st->appD + i;
258 st->aCurr->growth = -st->finpoints;
259 st->turndelta = -st->turndelta;
262 else if (withdrawall<11) st->aCurr->growth = -st->aCurr->numpt;
264 else if (RND(100)<st->aCurr->rate) {
265 if (st->aCurr->growth>0) {
266 if (!(--st->aCurr->growth)) st->aCurr->growth = -RND(st->finpoints) - 1;
267 st->vCurr = st->vPendage + (st->finpoints + 1) * i + st->aCurr->numpt - 1;
268 if (st->aCurr->numpt<st->finpoints - 1) {
270 st->vNext = st->vCurr + 1;
272 st->vNext->sx = st->vCurr->sx + RND(3) - 1;
273 st->vNext->sy = st->vCurr->sy + RND(3) - 1;
274 st->vNext->sz = st->vCurr->sz + RND(3) - 1;
275 st->vCurr = st->vNext + 1;
276 st->vCurr->x = st->vNext->x + st->vNext->sx;
277 st->vCurr->y = st->vNext->y + st->vNext->sy;
278 st->vCurr->z = st->vNext->z + st->vNext->sz;
287 drawImage(struct state *st, Drawable curr_window, double sint, double cost)
289 int q,numpt,mx2 = st->mx / 2;
290 double cx,cy,cz,nx = 0,ny = 0,nz = 0;
292 if ((numpt = st->aCurr->numpt)==1) return;
293 XSetForeground(st->dpy, st->gcDraw, st->aCurr->col);
295 st->vNext = st->vCurr + 1;
302 for (q = 0; q < numpt - 1; q++) {
303 nx = st->vNext->x + 2 - RND(5);
304 ny = st->vNext->y + 2 - RND(5);
305 nz = st->vNext->z + 2 - RND(5);
307 XDrawLine(st->dpy, curr_window, st->gcDraw,
308 mx2 + cx * cost - cz * sint, cy,
309 mx2 + nx * cost - nz * sint, ny);
317 XSetLineAttributes(st->dpy, st->gcDraw, st->width * 3,
318 LineSolid, CapRound, JoinBevel);
319 XDrawLine(st->dpy, curr_window, st->gcDraw,
320 st->mx / 2 + cx * cost - cz * sint, cy,
321 st->mx / 2 + nx * cost - nz * sint, ny);
322 XSetLineAttributes(st->dpy, st->gcDraw, st->width,
323 LineSolid, CapRound, JoinBevel);
328 animateAnemone(struct state *st, Drawable curr_window)
331 double sint = sin(st->turn),cost = cos(st->turn);
333 st->aCurr = st->appD;
334 for (i = 0; i< st->arms; i++) {
335 st->vCurr = st->vPendage + (st->finpoints + 1) * i;
336 if (RND(25)<st->aCurr->rate) {
337 if (st->aCurr->growth<0) {
338 st->aCurr->numpt -= st->aCurr->numpt>1;
339 if (!(++st->aCurr->growth)) st->aCurr->growth = RND(st->finpoints - st->aCurr->numpt) + 1;
342 drawImage(st, curr_window, sint, cost);
343 st->turn += st->turndelta;
348 if (st->turn >= TWO_PI) st->turn -= TWO_PI;
352 anemone_draw (Display *dpy, Window window, void *closure)
354 struct state *st = (struct state *) closure;
356 XFillRectangle (st->dpy, st->b, st->gcClear, 0, 0, st->scrWidth, st->scrHeight);
358 animateAnemone(st, st->b);
360 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
363 XdbeSwapInfo info[1];
364 info[0].swap_window = window;
365 info[0].swap_action = XdbeUndefined;
366 XdbeSwapBuffers (st->dpy, info, 1);
369 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
372 XCopyArea (st->dpy, st->b, window, st->gcClear, 0, 0,
373 st->scrWidth, st->scrHeight, 0, 0);
374 st->b = (st->b == st->ba ? st->bb : st->ba);
382 anemone_reshape (Display *dpy, Window window, void *closure,
383 unsigned int w, unsigned int h)
385 /* need to re-make pixmaps too...
386 struct state *st = (struct state *) closure;
393 anemone_event (Display *dpy, Window window, void *closure, XEvent *event)
399 anemone_free (Display *dpy, Window window, void *closure)
401 struct state *st = (struct state *) closure;
402 if (st->vPendage) free (st->vPendage);
403 if (st->appD) free (st->appD);
409 static const char *anemone_defaults [] = {
410 ".background: black",
418 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
420 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
425 static XrmOptionDescRec anemone_options [] = {
426 { "-arms", ".arms", XrmoptionSepArg, 0 },
427 { "-finpoints", ".finpoints", XrmoptionSepArg, 0 },
428 { "-delay", ".delay", XrmoptionSepArg, 0 },
429 { "-width", ".width", XrmoptionSepArg, 0 },
430 { "-withdraw", ".withdraw", XrmoptionSepArg, 0 },
431 { "-turnspeed", ".turnspeed", XrmoptionSepArg, 0 },
432 { "-colors", ".colors", XrmoptionSepArg, 0 },
437 XSCREENSAVER_MODULE ("Anemone", anemone)