http://www.jwz.org/xscreensaver/xscreensaver-5.12.tar.gz
[xscreensaver] / hacks / wormhole.c
1 /* xscreensaver, Copyright (c) 1992, 1995, 1996, 1997, 1998, 2004, 2006
2  *  Jamie Zawinski <jwz@jwz.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  */
12
13 /* wormhole:
14  * Animation of moving through a wormhole. Based on my own code written
15  * a few years ago.
16  * author: Jon Rafkind <jon@rafkind.com>
17  * date: 1/19/04
18  */
19
20 #include <math.h>
21 #include "screenhack.h"
22
23 #ifndef debug
24 #define debug printf("File:%s Line:%d\n", __FILE__, __LINE__ );
25 #endif
26
27 typedef struct STAR{
28         int x, y;
29         int calc_x, calc_y;
30         int Z;
31         int center_x, center_y;
32 } star;
33
34 typedef struct STARLINE{
35         star begin, end;
36 } starline;
37
38 /*
39 typedef struct RGBHANDLE{
40         XColor mine;
41         unsigned short rwant, gwant, bwant;
42 } RGBHandle;
43 */
44
45 typedef struct COLORCHANGER{
46         XColor * shade;
47         int min, max; 
48         int shade_use;
49         int shade_max;
50         int min_want;
51         /* RGBHandle handle_begin, handle_end; */
52 } color_changer;
53
54 typedef struct WORMHOLE{
55         int diameter;
56         int diameter_change;
57         int actualx, actualy;
58         double virtualx, virtualy;
59         double speed;
60         int ang;
61         int want_ang;
62         int want_x, want_y;
63         int max_Z;
64         int addStar;
65         int spiral;
66         color_changer changer;
67         starline ** stars;
68         int num_stars; /* top of array we are using */
69         XColor black;
70         Pixmap work;
71 } wormhole;
72
73 struct state {
74   Display *dpy;
75   Window window;
76
77   int SCREEN_X, SCREEN_Y;
78   int z_speed;
79   int make_stars;
80
81   int delay;
82   wormhole worm;
83   GC gc;
84   Colormap cmap;
85 };
86
87
88 /*inline*/ static int rnd( int q )
89 {
90
91         return random() % q;
92
93 }
94
95 static int gang( int x1, int y1, int x2, int y2 )
96 {
97
98         int tang = 0;
99         if ( x1 == x2 ) {
100                 if ( y1 < y2 )
101                         tang = 90;
102                 else
103                         tang = 270;
104         } else if ( y1 == y2 ) {
105
106                 if ( x1 < x2 )
107                         tang = 0;
108                 else
109                         tang = 180;
110
111         } else {
112           tang = (int)(0.5+atan2( -(y2-y1), x2 - x1 ) * 180.0 / M_PI );
113         }
114
115         while ( tang < 0 )
116                 tang += 360;
117         return tang % 360;
118
119 }
120
121 static void blend_palette( XColor * pal, int max, XColor * sc, XColor * ec ) 
122 {
123
124         int q;
125
126         int sc_r = sc->red;
127         int sc_g = sc->green;
128         int sc_b = sc->blue;
129
130         int ec_r = ec->red;
131         int ec_g = ec->green;
132         int ec_b = ec->blue;
133
134         for ( q = 0; q < max; q++ ) {
135                 float j = (float)( q ) / (float)( max );
136                 int f_r = (int)( 0.5 + (float)( sc_r ) + (float)( ec_r-sc_r ) * j );
137                 int f_g = (int)( 0.5 + (float)( sc_g ) + (float)( ec_g-sc_g ) * j );
138                 int f_b = (int)( 0.5 + (float)( sc_b ) + (float)( ec_b-sc_b ) * j );
139                 /* pal[q] = makecol( f_r, f_g, f_b ); */
140                 pal[q].red = f_r;
141                 pal[q].blue = f_b;
142                 pal[q].green = f_g; 
143         }
144
145 }
146
147
148 /*
149 static void initHandle( RGBHandle * handle )
150 {
151
152         handle->mine.red = rnd( 65536 );
153         handle->mine.green = rnd( 65536 );
154         handle->mine.blue = rnd( 65536 );
155         handle->rwant = rnd( 65536 );
156         handle->gwant = rnd( 65536 );
157         handle->bwant = rnd( 65536 );
158
159 }
160 */
161
162 static void initXColor( XColor * color )
163 {
164         color->red = rnd( 50000 ) + 10000;
165         color->blue = rnd( 50000 ) + 10000;
166         color->green = rnd( 50000 ) + 10000;
167 }
168
169 static void initColorChanger( struct state *st, color_changer * ch )
170 {
171
172         int q;
173         XColor old_color, new_color;
174
175         ch->shade_max = 2048;
176         ch->shade_use = 128;
177         ch->min = 0;
178         ch->max = ch->shade_use;
179         ch->min_want = rnd( ch->shade_max - ch->shade_use );
180         ch->shade = (XColor *)malloc( sizeof(XColor) * ch->shade_max );
181         memset( ch->shade, 0, sizeof(XColor) * ch->shade_max );
182
183         initXColor( &old_color );
184         initXColor( &new_color );
185         
186         for ( q = 0; q < ch->shade_max; q += ch->shade_use ){
187                 blend_palette( ch->shade + q, ch->shade_use, &old_color, &new_color );
188                 old_color = new_color;
189
190                 initXColor( &new_color );
191         }
192
193         for ( q = 0; q < ch->shade_max; q++ )
194                 XAllocColor( st->dpy, st->cmap, &( ch->shade[q] ) );
195
196         /*
197         initHandle( &(ch->handle_begin) );
198         initHandle( &(ch->handle_end) );
199         ch->shade = (XColor *)malloc( sizeof(XColor) * MAX_COLORS );
200         ch->max = MAX_COLORS;
201         memset( ch->shade, 0, sizeof(XColor) * ch->max );
202
203         blend_palette( ch->shade, ch->max, &(ch->handle_begin.mine), &(ch->handle_end.mine) );
204         for ( q = 0; q < ch->max; q++ )
205                 XAllocColor( st->dpy, *cmap, &( ch->shade[q] ) );
206         */
207
208 }
209
210 /*
211 static void changeColor( unsigned short * col, unsigned short * change, int min, int max )
212 {
213         int RGB_GO_BLACK = 30;
214         if ( *col < *change ) *col++;
215         if ( *col > *change ) *col--;
216         if ( *col == *change ){
217                 if ( rnd( RGB_GO_BLACK ) == rnd( RGB_GO_BLACK ) )
218                         *change = 0;
219                 else    *change = rnd(max-min) + min;
220         }
221 }
222 */
223
224 /*
225 static void moveRGBHandle( RGBHandle * handle, int min, int max )
226 {
227
228         unsigned short * want[ 3 ];
229         int q;
230         int cy = 0;
231         want[0] = &(handle->rwant);
232         want[1] = &(handle->gwant);
233         want[2] = &(handle->bwant);
234
235         for ( q = 0; q < 10; q++ ){
236                 changeColor( &( handle->mine.red ), &handle->rwant, min, max );
237                 changeColor( &( handle->mine.green ), &handle->gwant, min, max );
238                 changeColor( &( handle->mine.blue ), &handle->bwant, min, max );
239         }
240         
241         for ( q = 0; q < 3; q++ )
242                 cy = cy || (*(want[q]) >= min && *(want[q]) <= max);
243         if ( !cy ) *(want[rnd(3)]) = rnd(max-min)+min;
244         cy = 1;
245         for ( q = 0; q < 3; q++ )
246                 cy = cy && *(want[q]) == 0;
247         if ( cy ) *(want[rnd(3)]) = rnd(max-min)+min;
248
249         if ( rnd( 30 ) == rnd( 30 ) )
250                 *(want[rnd(3)]) = rnd(max-min)+min;
251
252 }
253 */
254
255 static void moveColorChanger( color_changer * ch )
256 {
257
258         /* int q; */
259
260         if ( ch->min < ch->min_want ){
261                 ch->min++;
262                 ch->max++;
263         }
264         if ( ch->min > ch->min_want ) {
265                 ch->min--;
266                 ch->max--;
267         }
268         if ( ch->min == ch->min_want )
269                 ch->min_want = rnd( ch->shade_max - ch->shade_use );
270
271         /*
272         for ( q = 0; q < ch->max; q++ )
273                 XFreeColors( st->dpy, *cmap, &( ch->shade[q].pixel ), 1, 0 );
274
275         moveRGBHandle( &( ch->handle_begin ), 5000, 65500 );
276         moveRGBHandle( &( ch->handle_end ), 5000, 65500 );
277         
278         blend_palette( ch->shade, ch->max, &(ch->handle_begin.mine), &(ch->handle_end.mine) );
279         for ( q = 0; q < ch->max; q++ )
280                 XAllocColor( st->dpy, *cmap, &( ch->shade[q] ) );
281         */
282
283 }
284
285 #if 0
286 static void destroyColorChanger( color_changer * ch )
287 {
288         int q;
289         for ( q = 0; q < ch->max; q++ )
290                 XFreeColors( st->dpy, *cmap, &( ch->shade[q].pixel ), 1, 0 );
291         free( ch->shade );
292 }
293 #endif
294
295 static void resizeWormhole( struct state *st, wormhole * worm )
296 {
297         
298         XWindowAttributes attr;
299
300         XGetWindowAttributes( st->dpy, st->window, &attr );
301
302         st->cmap = attr.colormap;
303         
304         st->SCREEN_X = attr.width;
305         st->SCREEN_Y = attr.height;
306
307 # ifndef HAVE_COCOA     /* Don't second-guess Quartz's double-buffering */
308         XFreePixmap( st->dpy, worm->work );
309         worm->work = XCreatePixmap( st->dpy, st->window, st->SCREEN_X, st->SCREEN_Y, attr.depth );
310 # endif
311
312 }
313
314 static void initWormhole( struct state *st, wormhole * worm, Display * display, Window win )
315 {
316         
317         int i;
318         XWindowAttributes attr;
319
320         XGetWindowAttributes( st->dpy, st->window, &attr );
321
322         st->cmap = attr.colormap;
323         
324         st->SCREEN_X = attr.width;
325         st->SCREEN_Y = attr.height;
326
327 # ifdef HAVE_COCOA      /* Don't second-guess Quartz's double-buffering */
328         worm->work = st->window;
329 # else
330         worm->work = XCreatePixmap( st->dpy, st->window, st->SCREEN_X, st->SCREEN_Y, attr.depth );
331 # endif
332
333         worm->diameter = rnd( 10 ) + 15;
334         worm->diameter_change = rnd( 10 ) + 15;
335         /* worm->actualx = rnd( attr.width );
336         worm->actualy = rnd( attr.height ); */
337         worm->actualx = attr.width / 2;
338         worm->actualy = attr.height / 2;
339         worm->virtualx = worm->actualx;
340         worm->virtualy = worm->actualy;
341         worm->speed = (float)st->SCREEN_X / 180.0;
342         /* z_speed = SCREEN_X / 120; */
343         worm->spiral = 0;
344         worm->addStar = st->make_stars;
345         worm->want_x = rnd( attr.width - 50 ) + 25;
346         worm->want_y = rnd( attr.height - 50 ) + 25;
347         worm->want_ang = gang( worm->actualx, worm->actualy, worm->want_x, worm->want_y );
348         worm->ang = worm->want_ang;
349         worm->max_Z = 600;
350         worm->black.red = 0;
351         worm->black.green = 0;
352         worm->black.blue = 0;
353         XAllocColor( st->dpy, st->cmap, &worm->black );
354         initColorChanger( st, &(worm->changer) );
355
356         worm->num_stars = 64;
357         worm->stars = (starline **)malloc( sizeof(starline *) * worm->num_stars );
358         for ( i = 0; i < worm->num_stars; i++ )
359                 worm->stars[i] = NULL;
360
361 }
362
363 #if 0
364 static void destroyWormhole( wormhole * worm )
365 {
366         destroyColorChanger( &(worm->changer), st->dpy, cmap );
367         if (work->work != st->window)
368           XFreePixmap( st->dpy, worm->work );
369         free( worm->stars );
370 }
371 #endif
372
373 static double Cos( int a )
374 {
375         return cos( a * 180.0 / M_PI );
376 }
377
378 static double Sine( int a )
379 {
380         return sin( a * 180.0 / M_PI );
381 }
382
383
384
385 static void calcStar( star * st )
386 {
387         if ( st->center_x == 0 || st->center_y == 0 ){
388                 st->Z = 0;
389                 return;
390         }
391         if ( st->Z <= 0 ){
392                 st->calc_x = (st->x << 10) / st->center_x;
393                 st->calc_y = (st->y << 10) / st->center_y;
394         } else {
395                 st->calc_x = (st->x << 10 ) / st->Z + st->center_x;
396                 st->calc_y = (st->y << 10 ) / st->Z + st->center_y;
397         }
398 }
399
400 static void initStar( star * st, int Z, int ang, wormhole * worm )
401 {
402
403         st->x = Cos( ang ) * worm->diameter;
404         st->y = Sine( ang ) * worm->diameter;
405         st->center_x = worm->actualx;
406         st->center_y = worm->actualy;
407         st->Z = Z;
408         calcStar( st );
409
410 }
411
412 static void addStar( wormhole * worm )
413 {
414
415         starline * star_new;
416         starline ** xstars;
417         int old_stars;
418         int q;
419         int ang;
420         star_new = (starline *)malloc( sizeof( starline ) );
421         ang = rnd( 360 );
422         initStar( &star_new->begin, worm->max_Z, ang, worm );
423         initStar( &star_new->end, worm->max_Z+rnd(6)+4, ang, worm );
424
425         for ( q = 0; q < worm->num_stars; q++ ){
426                 if ( worm->stars[q] == NULL ){
427                         worm->stars[q] = star_new;
428                         return;
429                 }
430         }
431
432         old_stars = worm->num_stars;
433         worm->num_stars = worm->num_stars << 1;
434         xstars = (starline **)malloc( sizeof(starline *) * worm->num_stars );
435         for ( q = 0; q < worm->num_stars; q++ )
436                 xstars[q] = NULL;
437
438         for ( q = 0; q < old_stars; q++ )
439                 if ( worm->stars[q] != NULL ) xstars[q] = worm->stars[q];
440         free( worm->stars );
441         worm->stars = xstars;
442
443         worm->stars[ old_stars ] = star_new;
444
445 }
446
447 static int moveStar( struct state *st, starline * stl )
448 {
449
450         stl->begin.Z -= st->z_speed;    
451         stl->end.Z -= st->z_speed;
452
453         calcStar( &stl->begin );
454         calcStar( &stl->end );
455
456         return ( stl->begin.Z <= 0 || stl->end.Z <= 0 );
457
458
459
460 static int dist( int x1, int y1, int x2, int y2 )
461 {
462         int xs, ys;
463         xs = x1-x2;
464         ys = y1-y2;
465         return (int)sqrt( xs*xs + ys*ys );
466 }
467
468 static void moveWormhole( struct state *st, wormhole * worm )
469 {
470
471         int q;
472         double dx, dy;
473         /* int x1, y1, x2, y2; */
474         int min_dist = 100;
475         int find = 0;
476         dx = Cos( worm->ang ) * worm->speed;
477         dy = Sine( worm->ang ) * worm->speed;
478
479         worm->virtualx += dx;
480         worm->virtualy += dy;
481         worm->actualx = (int)worm->virtualx;
482         worm->actualy = (int)worm->virtualy;
483
484         if ( worm->spiral ){
485
486                 if ( worm->spiral % 5 == 0 )
487                         worm->ang = (worm->ang + 1 ) % 360;
488                 worm->spiral--;
489                 if ( worm->spiral <= 0 ) find = 1;
490
491         } else {
492
493                 if ( dist( worm->actualx, worm->actualy, worm->want_x, worm->want_y ) < 20 )
494                         find = 1;
495
496                 if ( rnd( 20 ) == rnd( 20 ) )
497                         find = 1;
498
499                 if ( worm->actualx < min_dist ){
500                         worm->actualx = min_dist;
501                         worm->virtualx = worm->actualx;
502                         find = 1;
503                 }
504                 if ( worm->actualy < min_dist ){
505                         worm->actualy = min_dist;
506                         worm->virtualy = worm->actualy;
507                         find = 1;
508                 }
509                 if ( worm->actualx > st->SCREEN_X - min_dist ){
510                         worm->actualx = st->SCREEN_X - min_dist;
511                         worm->virtualx = worm->actualx;
512                         find = 1;
513                 }
514                 if ( worm->actualy > st->SCREEN_Y - min_dist ){
515                         worm->actualy = st->SCREEN_Y - min_dist;
516                         worm->virtualy = worm->actualy;
517                         find = 1;
518                 }
519         
520                 if ( rnd( 500 ) == rnd( 500 ) ) worm->spiral = rnd( 30 ) + 50;
521         }
522
523         if ( find ){
524                 worm->want_x = rnd( st->SCREEN_X - min_dist * 2 ) + min_dist;
525                 worm->want_y = rnd( st->SCREEN_Y - min_dist * 2 ) + min_dist;
526                 worm->ang = gang( worm->actualx, worm->actualy, worm->want_x, worm->want_y );
527         }
528
529
530         /* worm->ang = ( worm->ang + 360 + rnd( 30 ) - 15 ) % 360; */
531
532         /*
533         if ( worm->ang < worm->want_ang ) worm->ang++;
534         if ( worm->ang > worm->want_ang ) worm->ang--;
535         if ( worm->ang == worm->want_ang && rnd( 3 ) == rnd( 3 ) ) worm->want_ang = rnd( 360 );
536         */
537
538         /*
539         if ( rnd( 25 ) == rnd( 25 ) ){
540                 x1 = worm->actualx;
541                 y1 = worm->actualy;
542                 x2 = SCREEN_X / 2 + rnd( 20 ) - 10;
543                 y2 = SCREEN_Y / 2 + rnd( 20 ) - 10;
544                 worm->want_ang = gang(x1,y1,x2,y2);
545         }
546         */
547
548         /*
549         if ( worm->actualx < min_dist || worm->actualx > SCREEN_X - min_dist || worm->actualy < min_dist || worm->actualy > SCREEN_Y - min_dist ){
550                 x1 = worm->actualx;
551                 y1 = worm->actualy;
552                 x2 = SCREEN_X / 2 + rnd( 20 ) - 10;
553                 y2 = SCREEN_Y / 2 + rnd( 20 ) - 10;
554                 / * worm->ang = gang( worm->actualx, worm->actualy, SCREEN_X/2+rnd(20)-10, SCREEN_Y/2+rnd(20)-10 ); * /
555                 worm->ang = gang( x1, y1, x2, y2 );
556                 worm->want_ang = worm->ang;
557                 / * printf("Angle = %d\n", worm->ang ); * /
558
559                 if ( worm->actualx < min_dist )
560                         worm->actualx = min_dist;
561                 if ( worm->actualx > SCREEN_X - min_dist )
562                         worm->actualx = SCREEN_X - min_dist;
563                 if ( worm->actualy < min_dist )
564                         worm->actualy = min_dist;
565                 if ( worm->actualy > SCREEN_Y - min_dist )
566                         worm->actualy = SCREEN_Y - min_dist;
567                 worm->virtualx = worm->actualx;
568                 worm->virtualy = worm->actualy;
569         }
570         */
571
572         for ( q = 0; q < worm->num_stars; q++ ){
573                 if ( worm->stars[q] != NULL ){
574                         if ( moveStar( st, worm->stars[q] ) ){
575                                 free( worm->stars[q] );
576                                 worm->stars[q] = NULL;
577                         }
578                 }
579         }
580
581         moveColorChanger( &worm->changer );
582
583         if ( worm->diameter < worm->diameter_change )
584                 worm->diameter++;
585         if ( worm->diameter > worm->diameter_change )
586                 worm->diameter--;
587         if ( rnd( 30 ) == rnd( 30 ) )
588                 worm->diameter_change = rnd( 35 ) + 5;
589
590         for ( q = 0; q < worm->addStar; q++ )
591                 addStar( worm );
592
593 }
594
595 static XColor * getColorShade( color_changer * ch )
596 {
597         return ch->shade + ch->min;
598 }
599
600 static void drawWormhole( struct state *st, wormhole * worm )
601 {
602
603         int i;
604         int color;
605         int z;
606         starline * current;
607         XColor * xcol;
608         XColor * shade;
609
610         XSetForeground( st->dpy, st->gc, worm->black.pixel );
611         XFillRectangle( st->dpy, worm->work, st->gc, 0, 0, st->SCREEN_X, st->SCREEN_Y );
612
613         for ( i = 0; i < worm->num_stars; i++ )
614                 if ( worm->stars[i] != NULL ){
615         
616                         current = worm->stars[i];
617                         z = current->begin.Z;
618
619                         color = z * worm->changer.shade_use / worm->max_Z;
620                         shade = getColorShade( &worm->changer );
621                         /* xcol = &worm->changer.shade[ color ]; */
622                         xcol = &shade[ color ];
623
624                         XSetForeground( st->dpy, st->gc, xcol->pixel );
625                         /* XDrawLine( st->dpy, st->window, *gc, current->begin.calc_x, current->begin.calc_y, current->end.calc_x, current->end.calc_y ); */
626                         XDrawLine( st->dpy, worm->work, st->gc, current->begin.calc_x, current->begin.calc_y, current->end.calc_x, current->end.calc_y );
627
628                 }
629         if (worm->work != st->window)
630           XCopyArea( st->dpy, worm->work, st->window, st->gc, 0, 0, st->SCREEN_X, st->SCREEN_Y, 0, 0 );
631 }
632
633 /*
634 static void eraseWormhole( Display * display, Window * st->window, wormhole * worm ){
635         starline * current;
636         int i;
637         XColor * xcol;
638         for ( i = 0; i < worm->num_stars; i++ )
639                 if ( worm->stars[i] != NULL ){
640                         xcol = &worm->black;
641                         current = worm->stars[i];
642                         XSetForeground( st->dpy, *gc, xcol->pixel );
643                         XDrawLine( st->dpy, st->window, *gc, current->begin.calc_x, current->begin.calc_y, current->end.calc_x, current->end.calc_y );
644                 }
645 }
646 */
647
648
649
650 static void *
651 wormhole_init (Display *dpy, Window window)
652 {
653         struct state *st = (struct state *) calloc (1, sizeof(*st));
654         XGCValues gcv;
655         XWindowAttributes attr;
656
657         st->dpy = dpy;
658         st->window = window;
659         st->delay = get_integer_resource(st->dpy,  "delay", "Integer" );
660         st->make_stars = get_integer_resource(st->dpy,  "stars", "Integer" );
661         st->z_speed = get_integer_resource(st->dpy,  "zspeed", "Integer" );
662
663         initWormhole( st, &st->worm, st->dpy, st->window );
664
665         st->gc = XCreateGC( st->dpy, st->window, 0, &gcv );
666         XGetWindowAttributes( st->dpy, st->window, &attr );
667         st->cmap = attr.colormap;
668
669         return st;
670 }
671
672 static unsigned long
673 wormhole_draw (Display *dpy, Window window, void *closure)
674 {
675   struct state *st = (struct state *) closure;
676
677   moveWormhole( st, &st->worm );
678   drawWormhole( st, &st->worm );
679   return st->delay;
680 }
681
682 static void
683 wormhole_reshape (Display *dpy, Window window, void *closure, 
684                  unsigned int w, unsigned int h)
685 {
686   struct state *st = (struct state *) closure;
687   resizeWormhole( st, &st->worm );
688 }
689
690 static Bool
691 wormhole_event (Display *dpy, Window window, void *closure, XEvent *event)
692 {
693   return False;
694 }
695
696 static void
697 wormhole_free (Display *dpy, Window window, void *closure)
698 {
699   struct state *st = (struct state *) closure;
700   free (st);
701 }
702
703
704
705
706 static const char *wormhole_defaults [] = {
707   ".background: Black",
708   ".foreground: #E9967A",
709   "*delay:      10000",
710   "*zspeed:     10",
711   "*stars:      20",
712   0
713 };
714
715 static XrmOptionDescRec wormhole_options [] = {
716   { "-delay",           ".delay",       XrmoptionSepArg, 0 },
717   { "-zspeed",          ".zspeed",      XrmoptionSepArg, 0 },
718   { "-stars",           ".stars",       XrmoptionSepArg, 0 },
719   { 0, 0, 0, 0 }
720 };
721
722 XSCREENSAVER_MODULE ("Wormhole", wormhole)