summaryrefslogtreecommitdiffstats
path: root/hacks/anemone.c
blob: deedb270b659da0c929e426e94c9a6a0ebadc429 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* anemone, Copyright (c) 2001 Gabriel Finch
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 */

/*------------------------------------------------------------------------
  |
  |  FILE            anemone.c
  |  MODULE OF       xscreensaver
  |
  |  DESCRIPTION     Anemone.
  |
  |  WRITTEN BY      Gabriel Finch
  |                  
  |
  |
  |  MODIFICATIONS   june 2001 started
  |           
  +----------------------------------------------------------------------*/


#include <math.h>
#include "screenhack.h"


#ifdef HAVE_DOUBLE_BUFFER_EXTENSION
#include "xdbe.h"
#endif /* HAVE_DOUBLE_BUFFER_EXTENSION */


/*-----------------------------------------------------------------------+
  |  PRIVATE DATA                                                          |
  +-----------------------------------------------------------------------*/


#define TWO_PI     (2.0 * M_PI)
#define RND(x)     (random() % (x))
#define MAXPEND    2000
#define MAXPTS    200
#define TRUE 1
#define FALSE 0


typedef struct {
  double x,y,z;
  int sx,sy,sz;
} vPend;

typedef struct {
  long col;
  int numpt;
  int growth;
  unsigned short rate;
} appDef;

struct state {
  Display *dpy;
  Pixmap b, ba, bb;

#ifdef HAVE_DOUBLE_BUFFER_EXTENSION
  XdbeBackBuffer backb;
#endif /* HAVE_DOUBLE_BUFFER_EXTENSION */

  int arms;                       /* number of arms */
  int finpoints;                  /* final number of points in each array. */
  long delay;              /* usecs to wait between updates. */

  int scrWidth, scrHeight;
  GC gcDraw, gcClear;

  Bool dbuf;
  int width;

  vPend *vPendage;  /* 3D representation of appendages */
  appDef *appD;  /* defaults */
  vPend *vCurr, *vNext;
  appDef *aCurr;

  double turn, turndelta;

  int mx, my;            /* max screen coordinates. */
  int withdraw;

  XGCValues gcv;
  Colormap cmap;
  XColor *colors;
  int ncolors;
};



/*-----------------------------------------------------------------------+
  |  PUBLIC DATA                                                           |
  +-----------------------------------------------------------------------*/



/*-----------------------------------------------------------------------+
  |  PRIVATE FUNCTIONS                                                     |
  +-----------------------------------------------------------------------*/

static void *
xmalloc(size_t size)
{
  void *ret;

  if ((ret = malloc(size)) == NULL) {
    fprintf(stderr, "anemone: out of memory\n");
    exit(1);
  }
  return ret;
}


static void
initAppendages(struct state *st)
{
  int    i;
  /*int    marginx, marginy; */
    
  /*double scalex, scaley;*/

  double x,y,z,dist;

  st->mx = st->scrWidth - 1;
  st->my = st->scrHeight - 1;

  /* each appendage will have: colour,
     number of points, and a grow or shrink indicator */

  /* added: growth rate 1-10 (smaller==faster growth) */
  /* each appendage needs virtual coords (x,y,z) with y and z combining to
     give the screen y */

  st->vPendage = (vPend *) xmalloc((st->finpoints + 1) * sizeof(vPend) * st->arms);
  st->appD = (appDef *) xmalloc(sizeof(appDef) * st->arms);


  for (i = 0; i < st->arms; i++) {
    st->aCurr = st->appD + i;
    st->vCurr = st->vPendage + (st->finpoints + 1) * i;
    st->vNext = st->vCurr + 1;

    st->aCurr->col = st->colors[random() % st->ncolors].pixel;
    st->aCurr->numpt = 1;
    st->aCurr->growth = st->finpoints / 2 + RND(st->finpoints / 2);
    st->aCurr->rate = RND(11) * RND(11);

    do {
      x = (1 - RND(1001) / 500);
      y = (1 - RND(1001) / 500);
      z = (1 - RND(1001) / 500);
      dist = x * x + y * y + z * z;
    } while (dist >= 1.);

    st->vCurr->x = x * 200;
    st->vCurr->y = st->my / 2 + y * 200;
    st->vCurr->z = 0 + z * 200;

    /* start the arm going outwards */
    st->vCurr->sx = st->vCurr->x / 5;
    st->vCurr->sy = (st->vCurr->y - st->my / 2) / 5;
    st->vCurr->sz = (st->vCurr->z) / 5;

    
    st->vNext->x = st->vCurr->x + st->vCurr->sx;
    st->vNext->y = st->vCurr->y + st->vCurr->sy;
    st->vNext->z = st->vCurr->z + st->vCurr->sz;
  }
}

static void *
anemone_init (Display *disp, Window window)
{
  struct state *st = (struct state *) calloc (1, sizeof(*st));
  XWindowAttributes wa;

  st->dpy = disp;
  st->turn = 0.;
  
  st->width = get_integer_resource(st->dpy, "width", "Integer");
  st->arms = get_integer_resource(st->dpy, "arms", "Integer");
  st->finpoints = get_integer_resource(st->dpy, "finpoints", "Integer");
  st->delay = get_integer_resource(st->dpy, "delay", "Integer");
  st->withdraw = get_integer_resource(st->dpy, "withdraw", "Integer");
  st->turndelta = get_float_resource(st->dpy, "turnspeed", "float") / 100000;

  st->dbuf = TRUE;

# ifdef HAVE_JWXYZ	/* Don't second-guess Quartz's double-buffering */
  st->dbuf = False;
# endif

  st->b = st->ba = st->bb = 0;	/* double-buffer to reduce flicker */
#ifdef HAVE_DOUBLE_BUFFER_EXTENSION
  st->b = st->backb = xdbe_get_backbuffer (st->dpy, window, XdbeUndefined);
#endif /* HAVE_DOUBLE_BUFFER_EXTENSION */


  XGetWindowAttributes(st->dpy, window, &wa);
  st->scrWidth = wa.width;
  st->scrHeight = wa.height;
  st->cmap = wa.colormap;

  st->ncolors = get_integer_resource (st->dpy, "colors", "Colors");
  st->ncolors += 3;
  st->colors = (XColor *) malloc(sizeof(*st->colors) * (st->ncolors+1));
  make_smooth_colormap (wa.screen, wa.visual, st->cmap,
                        st->colors, &st->ncolors,
                        True, 0, True);

  st->gcDraw = XCreateGC(st->dpy, window, 0, &st->gcv);
  st->gcv.foreground = get_pixel_resource(st->dpy, st->cmap,
                                          "background", "Background");
  st->gcClear = XCreateGC(st->dpy, window, GCForeground, &st->gcv);

  if (st->dbuf) {
    if (!st->b)
      {
	st->ba = XCreatePixmap (st->dpy, window, st->scrWidth, st->scrHeight, wa.depth);
	st->bb = XCreatePixmap (st->dpy, window, st->scrWidth, st->scrHeight, wa.depth);
	st->b = st->ba;
      }
  }
  else
    {	
      st->b = window;
    }

  if (st->ba) XFillRectangle (st->dpy, st->ba, st->gcClear, 0, 0, st->scrWidth, st->scrHeight);
  if (st->bb) XFillRectangle (st->dpy, st->bb, st->gcClear, 0, 0, st->scrWidth, st->scrHeight);

  XClearWindow(st->dpy, window);
  XSetLineAttributes(st->dpy, st->gcDraw,  st->width, LineSolid, CapRound, JoinBevel);

  initAppendages(st);

  return st;
}


static void
createPoints(struct state *st)
{
  int i;
  int withdrawall = RND(st->withdraw);

  for (i = 0; i< st->arms; i++) {
    st->aCurr = st->appD + i;
    if (!withdrawall) {
      st->aCurr->growth = -st->finpoints;
      st->turndelta = -st->turndelta;
    }

    else if (withdrawall<11) st->aCurr->growth = -st->aCurr->numpt;

    else if (RND(100)<st->aCurr->rate) {
      if (st->aCurr->growth>0) {
	if (!(--st->aCurr->growth)) st->aCurr->growth = -RND(st->finpoints) - 1;
	st->vCurr = st->vPendage + (st->finpoints + 1) * i + st->aCurr->numpt - 1;
	if (st->aCurr->numpt<st->finpoints - 1) {
	  /* add a piece */	
	  st->vNext = st->vCurr + 1;
	  st->aCurr->numpt++;
	  st->vNext->sx = st->vCurr->sx + RND(3) - 1;
	  st->vNext->sy = st->vCurr->sy + RND(3) - 1;
	  st->vNext->sz = st->vCurr->sz + RND(3) - 1;
	  st->vCurr = st->vNext + 1;
	  st->vCurr->x = st->vNext->x + st->vNext->sx;
	  st->vCurr->y = st->vNext->y + st->vNext->sy;
	  st->vCurr->z = st->vNext->z + st->vNext->sz;
	}
      }
    }
  }
}


static void
drawImage(struct state *st, Drawable curr_window, double sint, double cost)
{
  int q,numpt,mx2 = st->mx / 2;
  double cx,cy,cz,nx = 0,ny = 0,nz = 0;

  if ((numpt = st->aCurr->numpt)==1) return;
  XSetForeground(st->dpy, st->gcDraw, st->aCurr->col);
    
  st->vNext = st->vCurr + 1;

  cx = st->vCurr->x;
  cy = st->vCurr->y;
  cz = st->vCurr->z;


  for (q = 0; q < numpt - 1; q++) {
    nx = st->vNext->x + 2 - RND(5);
    ny = st->vNext->y + 2 - RND(5);
    nz = st->vNext->z + 2 - RND(5);

    XDrawLine(st->dpy, curr_window, st->gcDraw,
              mx2 + cx * cost - cz * sint, cy,
              mx2 + nx * cost - nz * sint, ny);
    st->vCurr++;
    st->vNext++;

    cx = nx;
    cy = ny;
    cz = nz;
  }
  XSetLineAttributes(st->dpy, st->gcDraw, st->width * 3,
                     LineSolid, CapRound, JoinBevel);
  XDrawLine(st->dpy, curr_window, st->gcDraw,
            st->mx / 2 + cx * cost - cz * sint, cy,
            st->mx / 2 + nx * cost - nz * sint, ny);
  XSetLineAttributes(st->dpy, st->gcDraw, st->width,
                     LineSolid, CapRound, JoinBevel);

}

static void
animateAnemone(struct state *st, Drawable curr_window)
{
  int i;
  double sint = sin(st->turn),cost = cos(st->turn);

  st->aCurr = st->appD;
  for (i = 0; i< st->arms; i++) {
    st->vCurr = st->vPendage + (st->finpoints + 1) * i;
    if (RND(25)<st->aCurr->rate) {
      if (st->aCurr->growth<0) {
	st->aCurr->numpt -= st->aCurr->numpt>1;
	if (!(++st->aCurr->growth)) st->aCurr->growth = RND(st->finpoints - st->aCurr->numpt) + 1;
      }
    }
    drawImage(st, curr_window, sint, cost);
    st->turn += st->turndelta;
    st->aCurr++;
  }
  createPoints(st);

  if (st->turn >= TWO_PI) st->turn -= TWO_PI;
}

static unsigned long
anemone_draw (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;

    XFillRectangle (st->dpy, st->b, st->gcClear, 0, 0, st->scrWidth, st->scrHeight);

    animateAnemone(st, st->b);

#ifdef HAVE_DOUBLE_BUFFER_EXTENSION
    if (st->backb)
      {
	XdbeSwapInfo info[1];
	info[0].swap_window = window;
	info[0].swap_action = XdbeUndefined;
	XdbeSwapBuffers (st->dpy, info, 1);
      }
    else
#endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
      if (st->dbuf)
	{
	  XCopyArea (st->dpy, st->b, window, st->gcClear, 0, 0,
		     st->scrWidth, st->scrHeight, 0, 0);
	  st->b = (st->b == st->ba ? st->bb : st->ba);
	}

    return st->delay;
}


static void
anemone_reshape (Display *dpy, Window window, void *closure, 
                 unsigned int w, unsigned int h)
{
  struct state *st = (struct state *) closure;
  st->scrWidth = w;
  st->scrHeight = h;
#if 0
  if (st->dbuf) {
    XWindowAttributes wa;
    XGetWindowAttributes(dpy, window, &wa);
    if (st->ba) XFreePixmap (dpy, st->ba);
    if (st->bb) XFreePixmap (dpy, st->bb);
    st->ba = XCreatePixmap (dpy, window, st->scrWidth, st->scrHeight, wa.depth);
    st->bb = XCreatePixmap (dpy, window, st->scrWidth, st->scrHeight, wa.depth);
    st->b = st->ba;
  }
#endif
}

static Bool
anemone_event (Display *dpy, Window window, void *closure, XEvent *event)
{
  return False;
}

static void
anemone_free (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  if (st->vPendage) free (st->vPendage);
  if (st->appD) free (st->appD);
  free (st);
}



static const char *anemone_defaults [] = {
  ".background: black",
  "*arms: 128",
  "*width: 2",
  "*finpoints: 64",
  "*delay: 40000",
  "*withdraw: 1200",
  "*turnspeed: 50",
  "*colors: 20",
#ifdef HAVE_DOUBLE_BUFFER_EXTENSION
  "*useDBE:		True",
#endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
#ifdef HAVE_MOBILE
  "*ignoreRotation: True",
#endif
  0
};


static XrmOptionDescRec anemone_options [] = {
  { "-arms",        ".arms",        XrmoptionSepArg, 0 },
  { "-finpoints",   ".finpoints",   XrmoptionSepArg, 0 },
  { "-delay",       ".delay",       XrmoptionSepArg, 0 },
  { "-width",       ".width",       XrmoptionSepArg, 0 },
  { "-withdraw",    ".withdraw",    XrmoptionSepArg, 0 },
  { "-turnspeed",   ".turnspeed",   XrmoptionSepArg, 0 },
  { "-colors",      ".colors",      XrmoptionSepArg, 0 },
  { 0, 0, 0, 0 }
};


XSCREENSAVER_MODULE ("Anemone", anemone)