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