began for maemo
[xscreensaver] / 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     dist = 1.;
156
157     do {
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.);
163
164     st->vCurr->x = x * 200;
165     st->vCurr->y = st->my / 2 + y * 200;
166     st->vCurr->z = 0 + z * 200;
167
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;
172
173     
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;
177   }
178 }
179
180 static void *
181 anemone_init (Display *disp, Window window)
182 {
183   struct state *st = (struct state *) calloc (1, sizeof(*st));
184   XWindowAttributes wa;
185
186   st->dpy = disp;
187   st->turn = 0.;
188   
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;
195
196   st->dbuf = TRUE;
197
198 # ifdef HAVE_COCOA      /* Don't second-guess Quartz's double-buffering */
199   st->dbuf = False;
200 # endif
201
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 */
206
207
208   XGetWindowAttributes(st->dpy, window, &wa);
209   st->scrWidth = wa.width;
210   st->scrHeight = wa.height;
211   st->cmap = wa.colormap;
212
213   st->ncolors = get_integer_resource (st->dpy, "colors", "Colors");
214   st->ncolors += 3;
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,
217                         True, 0, True);
218
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);
223
224   if (st->dbuf) {
225     if (!st->b)
226       {
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);
229         st->b = st->ba;
230       }
231   }
232   else
233     {   
234       st->b = window;
235     }
236
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);
239
240   XClearWindow(st->dpy, window);
241   XSetLineAttributes(st->dpy, st->gcDraw,  st->width, LineSolid, CapRound, JoinBevel);
242
243   initAppendages(st);
244
245   return st;
246 }
247
248
249 static void
250 createPoints(struct state *st)
251 {
252   int i;
253   int withdrawall = RND(st->withdraw);
254
255   for (i = 0; i< st->arms; i++) {
256     st->aCurr = st->appD + i;
257     if (!withdrawall) {
258       st->aCurr->growth = -st->finpoints;
259       st->turndelta = -st->turndelta;
260     }
261
262     else if (withdrawall<11) st->aCurr->growth = -st->aCurr->numpt;
263
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) {
269           /* add a piece */     
270           st->vNext = st->vCurr + 1;
271           st->aCurr->numpt++;
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;
279         }
280       }
281     }
282   }
283 }
284
285
286 static void
287 drawImage(struct state *st, Drawable curr_window, double sint, double cost)
288 {
289   int q,numpt,mx2 = st->mx / 2;
290   double cx,cy,cz,nx = 0,ny = 0,nz = 0;
291
292   if ((numpt = st->aCurr->numpt)==1) return;
293   XSetForeground(st->dpy, st->gcDraw, st->aCurr->col);
294     
295   st->vNext = st->vCurr + 1;
296
297   cx = st->vCurr->x;
298   cy = st->vCurr->y;
299   cz = st->vCurr->z;
300
301
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);
306
307     XDrawLine(st->dpy, curr_window, st->gcDraw,
308               mx2 + cx * cost - cz * sint, cy,
309               mx2 + nx * cost - nz * sint, ny);
310     st->vCurr++;
311     st->vNext++;
312
313     cx = nx;
314     cy = ny;
315     cz = nz;
316   }
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);
324
325 }
326
327 static void
328 animateAnemone(struct state *st, Drawable curr_window)
329 {
330   int i;
331   double sint = sin(st->turn),cost = cos(st->turn);
332
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;
340       }
341     }
342     drawImage(st, curr_window, sint, cost);
343     st->turn += st->turndelta;
344     st->aCurr++;
345   }
346   createPoints(st);
347
348   if (st->turn >= TWO_PI) st->turn -= TWO_PI;
349 }
350
351 static unsigned long
352 anemone_draw (Display *dpy, Window window, void *closure)
353 {
354   struct state *st = (struct state *) closure;
355
356     XFillRectangle (st->dpy, st->b, st->gcClear, 0, 0, st->scrWidth, st->scrHeight);
357
358     animateAnemone(st, st->b);
359
360 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
361     if (st->backb)
362       {
363         XdbeSwapInfo info[1];
364         info[0].swap_window = window;
365         info[0].swap_action = XdbeUndefined;
366         XdbeSwapBuffers (st->dpy, info, 1);
367       }
368     else
369 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
370       if (st->dbuf)
371         {
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);
375         }
376
377     return st->delay;
378 }
379
380
381 static void
382 anemone_reshape (Display *dpy, Window window, void *closure, 
383                  unsigned int w, unsigned int h)
384 {
385   /* need to re-make pixmaps too...
386   struct state *st = (struct state *) closure;
387   st->scrWidth = w;
388   st->scrHeight = h;
389   */
390 }
391
392 static Bool
393 anemone_event (Display *dpy, Window window, void *closure, XEvent *event)
394 {
395   return False;
396 }
397
398 static void
399 anemone_free (Display *dpy, Window window, void *closure)
400 {
401   struct state *st = (struct state *) closure;
402   if (st->vPendage) free (st->vPendage);
403   if (st->appD) free (st->appD);
404   free (st);
405 }
406
407
408
409 static const char *anemone_defaults [] = {
410   ".background: black",
411   "*arms: 128",
412   "*width: 2",
413   "*finpoints: 64",
414   "*delay: 40000",
415   "*withdraw: 1200",
416   "*turnspeed: 50",
417   "*colors: 20",
418 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
419   "*useDBE:             True",
420 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
421   0
422 };
423
424
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 },
433   { 0, 0, 0, 0 }
434 };
435
436
437 XSCREENSAVER_MODULE ("Anemone", anemone)