f69e0967f1354a29cb592eae9b2f16c4651f7e11
[xscreensaver] / hacks / anemone.c
1 /* anemon, Copyright (c) 2001 Gabriel Finch
2  *
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
9  * implied warranty.
10  */
11
12 /*------------------------------------------------------------------------
13   |
14   |  FILE            anemone.c
15   |  MODULE OF       xscreensaver
16   |
17   |  DESCRIPTION     Anemone.
18   |
19   |  WRITTEN BY      Gabriel Finch
20   |                  
21   |
22   |
23   |  MODIFICATIONS   june 2001 started
24   |           
25   +----------------------------------------------------------------------*/
26
27
28 #include <math.h>
29 #include "screenhack.h"
30
31
32 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
33 #include "xdbe.h"
34 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
35
36
37 /*-----------------------------------------------------------------------+
38   |  PRIVATE DATA                                                          |
39   +-----------------------------------------------------------------------*/
40
41
42 #define TWO_PI     (2.0 * M_PI)
43 #define RND(x)     (random() % (x))
44 #define MAXPEND    2000
45 #define MAXPTS    200
46 #define TRUE 1
47 #define FALSE 0
48
49
50 typedef struct {
51   double x,y,z;
52   int sx,sy,sz;
53 } vPend;
54
55 typedef struct {
56   long col;
57   int numpt;
58   int growth;
59   unsigned short rate;
60 } appDef;
61
62 struct state {
63   Display *dpy;
64   Pixmap b, ba, bb;
65
66 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
67   XdbeBackBuffer backb;
68 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
69
70   int arms;                       /* number of arms */
71   int finpoints;                  /* final number of points in each array. */
72   long delay;              /* usecs to wait between updates. */
73
74   int scrWidth, scrHeight;
75   GC gcDraw, gcClear;
76
77   Bool dbuf;
78   int width;
79
80   vPend *vPendage;  /* 3D representation of appendages */
81   appDef *appD;  /* defaults */
82   vPend *vCurr, *vNext;
83   appDef *aCurr;
84
85   double turn, turndelta;
86
87   int mx, my;            /* max screen coordinates. */
88   int withdraw;
89
90   XGCValues gcv;
91   Colormap cmap;
92   XColor *colors;
93   int ncolors;
94 };
95
96
97
98 /*-----------------------------------------------------------------------+
99   |  PUBLIC DATA                                                           |
100   +-----------------------------------------------------------------------*/
101
102
103
104 /*-----------------------------------------------------------------------+
105   |  PRIVATE FUNCTIONS                                                     |
106   +-----------------------------------------------------------------------*/
107
108 static void *
109 xmalloc(size_t size)
110 {
111   void *ret;
112
113   if ((ret = malloc(size)) == NULL) {
114     fprintf(stderr, "anemone: out of memory\n");
115     exit(1);
116   }
117   return ret;
118 }
119
120
121 static void
122 initAppendages(struct state *st)
123 {
124   int    i;
125   /*int    marginx, marginy; */
126     
127   /*double scalex, scaley;*/
128
129   double x,y,z,dist;
130
131   st->mx = st->scrWidth - 1;
132   st->my = st->scrHeight - 1;
133
134   /* each appendage will have: colour,
135      number of points, and a grow or shrink indicator */
136
137   /* added: growth rate 1-10 (smaller==faster growth) */
138   /* each appendage needs virtual coords (x,y,z) with y and z combining to
139      give the screen y */
140
141   st->vPendage = (vPend *) xmalloc((st->finpoints + 1) * sizeof(vPend) * st->arms);
142   st->appD = (appDef *) xmalloc(sizeof(appDef) * st->arms);
143
144
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;
149
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);
154
155     do {
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.);
161
162     st->vCurr->x = x * 200;
163     st->vCurr->y = st->my / 2 + y * 200;
164     st->vCurr->z = 0 + z * 200;
165
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;
170
171     
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;
175   }
176 }
177
178 static void *
179 anemone_init (Display *disp, Window window)
180 {
181   struct state *st = (struct state *) calloc (1, sizeof(*st));
182   XWindowAttributes wa;
183
184   st->dpy = disp;
185   st->turn = 0.;
186   
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;
193
194   st->dbuf = TRUE;
195
196 # ifdef HAVE_COCOA      /* Don't second-guess Quartz's double-buffering */
197   st->dbuf = False;
198 # endif
199
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 */
204
205
206   XGetWindowAttributes(st->dpy, window, &wa);
207   st->scrWidth = wa.width;
208   st->scrHeight = wa.height;
209   st->cmap = wa.colormap;
210
211   st->ncolors = get_integer_resource (st->dpy, "colors", "Colors");
212   st->ncolors += 3;
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,
215                         True, 0, True);
216
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);
221
222   if (st->dbuf) {
223     if (!st->b)
224       {
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);
227         st->b = st->ba;
228       }
229   }
230   else
231     {   
232       st->b = window;
233     }
234
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);
237
238   XClearWindow(st->dpy, window);
239   XSetLineAttributes(st->dpy, st->gcDraw,  st->width, LineSolid, CapRound, JoinBevel);
240
241   initAppendages(st);
242
243   return st;
244 }
245
246
247 static void
248 createPoints(struct state *st)
249 {
250   int i;
251   int withdrawall = RND(st->withdraw);
252
253   for (i = 0; i< st->arms; i++) {
254     st->aCurr = st->appD + i;
255     if (!withdrawall) {
256       st->aCurr->growth = -st->finpoints;
257       st->turndelta = -st->turndelta;
258     }
259
260     else if (withdrawall<11) st->aCurr->growth = -st->aCurr->numpt;
261
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) {
267           /* add a piece */     
268           st->vNext = st->vCurr + 1;
269           st->aCurr->numpt++;
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;
277         }
278       }
279     }
280   }
281 }
282
283
284 static void
285 drawImage(struct state *st, Drawable curr_window, double sint, double cost)
286 {
287   int q,numpt,mx2 = st->mx / 2;
288   double cx,cy,cz,nx = 0,ny = 0,nz = 0;
289
290   if ((numpt = st->aCurr->numpt)==1) return;
291   XSetForeground(st->dpy, st->gcDraw, st->aCurr->col);
292     
293   st->vNext = st->vCurr + 1;
294
295   cx = st->vCurr->x;
296   cy = st->vCurr->y;
297   cz = st->vCurr->z;
298
299
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);
304
305     XDrawLine(st->dpy, curr_window, st->gcDraw,
306               mx2 + cx * cost - cz * sint, cy,
307               mx2 + nx * cost - nz * sint, ny);
308     st->vCurr++;
309     st->vNext++;
310
311     cx = nx;
312     cy = ny;
313     cz = nz;
314   }
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);
322
323 }
324
325 static void
326 animateAnemone(struct state *st, Drawable curr_window)
327 {
328   int i;
329   double sint = sin(st->turn),cost = cos(st->turn);
330
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;
338       }
339     }
340     drawImage(st, curr_window, sint, cost);
341     st->turn += st->turndelta;
342     st->aCurr++;
343   }
344   createPoints(st);
345
346   if (st->turn >= TWO_PI) st->turn -= TWO_PI;
347 }
348
349 static unsigned long
350 anemone_draw (Display *dpy, Window window, void *closure)
351 {
352   struct state *st = (struct state *) closure;
353
354     XFillRectangle (st->dpy, st->b, st->gcClear, 0, 0, st->scrWidth, st->scrHeight);
355
356     animateAnemone(st, st->b);
357
358 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
359     if (st->backb)
360       {
361         XdbeSwapInfo info[1];
362         info[0].swap_window = window;
363         info[0].swap_action = XdbeUndefined;
364         XdbeSwapBuffers (st->dpy, info, 1);
365       }
366     else
367 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
368       if (st->dbuf)
369         {
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);
373         }
374
375     return st->delay;
376 }
377
378
379 static void
380 anemone_reshape (Display *dpy, Window window, void *closure, 
381                  unsigned int w, unsigned int h)
382 {
383   struct state *st = (struct state *) closure;
384   st->scrWidth = w;
385   st->scrHeight = h;
386 #if 0
387   if (st->dbuf) {
388     XWindowAttributes wa;
389     XGetWindowAttributes(dpy, window, &wa);
390     if (st->ba) XFreePixmap (dpy, st->ba);
391     if (st->bb) XFreePixmap (dpy, st->bb);
392     st->ba = XCreatePixmap (dpy, window, st->scrWidth, st->scrHeight, wa.depth);
393     st->bb = XCreatePixmap (dpy, window, st->scrWidth, st->scrHeight, wa.depth);
394     st->b = st->ba;
395   }
396 #endif
397 }
398
399 static Bool
400 anemone_event (Display *dpy, Window window, void *closure, XEvent *event)
401 {
402   return False;
403 }
404
405 static void
406 anemone_free (Display *dpy, Window window, void *closure)
407 {
408   struct state *st = (struct state *) closure;
409   if (st->vPendage) free (st->vPendage);
410   if (st->appD) free (st->appD);
411   free (st);
412 }
413
414
415
416 static const char *anemone_defaults [] = {
417   ".background: black",
418   "*arms: 128",
419   "*width: 2",
420   "*finpoints: 64",
421   "*delay: 40000",
422   "*withdraw: 1200",
423   "*turnspeed: 50",
424   "*colors: 20",
425 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
426   "*useDBE:             True",
427 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
428   0
429 };
430
431
432 static XrmOptionDescRec anemone_options [] = {
433   { "-arms",        ".arms",        XrmoptionSepArg, 0 },
434   { "-finpoints",   ".finpoints",   XrmoptionSepArg, 0 },
435   { "-delay",       ".delay",       XrmoptionSepArg, 0 },
436   { "-width",       ".width",       XrmoptionSepArg, 0 },
437   { "-withdraw",    ".withdraw",    XrmoptionSepArg, 0 },
438   { "-turnspeed",   ".turnspeed",   XrmoptionSepArg, 0 },
439   { "-colors",      ".colors",      XrmoptionSepArg, 0 },
440   { 0, 0, 0, 0 }
441 };
442
443
444 XSCREENSAVER_MODULE ("Anemone", anemone)