1 /* xscreensaver, Copyright (c) 1992, 1995, 1996, 1997, 1998, 2004, 2006
2 * Jamie Zawinski <jwz@jwz.org>
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
14 * Animation of moving through a wormhole. Based on my own code written
16 * author: Jon Rafkind <jon@rafkind.com>
21 #include "screenhack.h"
24 #define debug printf("File:%s Line:%d\n", __FILE__, __LINE__ );
31 int center_x, center_y;
34 typedef struct STARLINE{
39 typedef struct RGBHANDLE{
41 unsigned short rwant, gwant, bwant;
45 typedef struct COLORCHANGER{
51 /* RGBHandle handle_begin, handle_end; */
54 typedef struct WORMHOLE{
58 double virtualx, virtualy;
66 color_changer changer;
68 int num_stars; /* top of array we are using */
77 int SCREEN_X, SCREEN_Y;
88 /*inline*/ static int rnd( int q )
95 static int gang( int x1, int y1, int x2, int y2 )
104 } else if ( y1 == y2 ) {
112 tang = (int)(0.5+atan2( -(y2-y1), x2 - x1 ) * 180.0 / M_PI );
121 static void blend_palette( XColor * pal, int max, XColor * sc, XColor * ec )
127 int sc_g = sc->green;
131 int ec_g = ec->green;
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 ); */
149 static void initHandle( RGBHandle * handle )
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 );
162 static void initXColor( XColor * color )
164 color->red = rnd( 50000 ) + 10000;
165 color->blue = rnd( 50000 ) + 10000;
166 color->green = rnd( 50000 ) + 10000;
169 static void initColorChanger( struct state *st, color_changer * ch )
173 XColor old_color, new_color;
175 ch->shade_max = 2048;
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 );
183 initXColor( &old_color );
184 initXColor( &new_color );
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;
190 initXColor( &new_color );
193 for ( q = 0; q < ch->shade_max; q++ )
194 XAllocColor( st->dpy, st->cmap, &( ch->shade[q] ) );
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 );
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] ) );
211 static void changeColor( unsigned short * col, unsigned short * change, int min, int max )
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 ) )
219 else *change = rnd(max-min) + min;
225 static void moveRGBHandle( RGBHandle * handle, int min, int max )
228 unsigned short * want[ 3 ];
231 want[0] = &(handle->rwant);
232 want[1] = &(handle->gwant);
233 want[2] = &(handle->bwant);
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 );
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;
245 for ( q = 0; q < 3; q++ )
246 cy = cy && *(want[q]) == 0;
247 if ( cy ) *(want[rnd(3)]) = rnd(max-min)+min;
249 if ( rnd( 30 ) == rnd( 30 ) )
250 *(want[rnd(3)]) = rnd(max-min)+min;
255 static void moveColorChanger( color_changer * ch )
260 if ( ch->min < ch->min_want ){
264 if ( ch->min > ch->min_want ) {
268 if ( ch->min == ch->min_want )
269 ch->min_want = rnd( ch->shade_max - ch->shade_use );
272 for ( q = 0; q < ch->max; q++ )
273 XFreeColors( st->dpy, *cmap, &( ch->shade[q].pixel ), 1, 0 );
275 moveRGBHandle( &( ch->handle_begin ), 5000, 65500 );
276 moveRGBHandle( &( ch->handle_end ), 5000, 65500 );
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] ) );
286 static void destroyColorChanger( color_changer * ch )
289 for ( q = 0; q < ch->max; q++ )
290 XFreeColors( st->dpy, *cmap, &( ch->shade[q].pixel ), 1, 0 );
295 static void resizeWormhole( struct state *st, wormhole * worm )
298 XWindowAttributes attr;
300 XGetWindowAttributes( st->dpy, st->window, &attr );
302 st->cmap = attr.colormap;
304 st->SCREEN_X = attr.width;
305 st->SCREEN_Y = attr.height;
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 );
314 static void initWormhole( struct state *st, wormhole * worm, Display * display, Window win )
318 XWindowAttributes attr;
320 XGetWindowAttributes( st->dpy, st->window, &attr );
322 st->cmap = attr.colormap;
324 st->SCREEN_X = attr.width;
325 st->SCREEN_Y = attr.height;
327 # ifdef HAVE_COCOA /* Don't second-guess Quartz's double-buffering */
328 worm->work = st->window;
330 worm->work = XCreatePixmap( st->dpy, st->window, st->SCREEN_X, st->SCREEN_Y, attr.depth );
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; */
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;
351 worm->black.green = 0;
352 worm->black.blue = 0;
353 XAllocColor( st->dpy, st->cmap, &worm->black );
354 initColorChanger( st, &(worm->changer) );
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;
364 static void destroyWormhole( wormhole * worm )
366 destroyColorChanger( &(worm->changer), st->dpy, cmap );
367 if (work->work != st->window)
368 XFreePixmap( st->dpy, worm->work );
373 static double Cos( int a )
375 return cos( a * 180.0 / M_PI );
378 static double Sine( int a )
380 return sin( a * 180.0 / M_PI );
385 static void calcStar( star * st )
387 if ( st->center_x == 0 || st->center_y == 0 ){
392 st->calc_x = (st->x << 10) / st->center_x;
393 st->calc_y = (st->y << 10) / st->center_y;
395 st->calc_x = (st->x << 10 ) / st->Z + st->center_x;
396 st->calc_y = (st->y << 10 ) / st->Z + st->center_y;
400 static void initStar( star * st, int Z, int ang, wormhole * worm )
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;
412 static void addStar( wormhole * worm )
420 star_new = (starline *)malloc( sizeof( starline ) );
422 initStar( &star_new->begin, worm->max_Z, ang, worm );
423 initStar( &star_new->end, worm->max_Z+rnd(6)+4, ang, worm );
425 for ( q = 0; q < worm->num_stars; q++ ){
426 if ( worm->stars[q] == NULL ){
427 worm->stars[q] = star_new;
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++ )
438 for ( q = 0; q < old_stars; q++ )
439 if ( worm->stars[q] != NULL ) xstars[q] = worm->stars[q];
441 worm->stars = xstars;
443 worm->stars[ old_stars ] = star_new;
447 static int moveStar( struct state *st, starline * stl )
450 stl->begin.Z -= st->z_speed;
451 stl->end.Z -= st->z_speed;
453 calcStar( &stl->begin );
454 calcStar( &stl->end );
456 return ( stl->begin.Z <= 0 || stl->end.Z <= 0 );
460 static int dist( int x1, int y1, int x2, int y2 )
465 return (int)sqrt( xs*xs + ys*ys );
468 static void moveWormhole( struct state *st, wormhole * worm )
473 /* int x1, y1, x2, y2; */
476 dx = Cos( worm->ang ) * worm->speed;
477 dy = Sine( worm->ang ) * worm->speed;
479 worm->virtualx += dx;
480 worm->virtualy += dy;
481 worm->actualx = (int)worm->virtualx;
482 worm->actualy = (int)worm->virtualy;
486 if ( worm->spiral % 5 == 0 )
487 worm->ang = (worm->ang + 1 ) % 360;
489 if ( worm->spiral <= 0 ) find = 1;
493 if ( dist( worm->actualx, worm->actualy, worm->want_x, worm->want_y ) < 20 )
496 if ( rnd( 20 ) == rnd( 20 ) )
499 if ( worm->actualx < min_dist ){
500 worm->actualx = min_dist;
501 worm->virtualx = worm->actualx;
504 if ( worm->actualy < min_dist ){
505 worm->actualy = min_dist;
506 worm->virtualy = worm->actualy;
509 if ( worm->actualx > st->SCREEN_X - min_dist ){
510 worm->actualx = st->SCREEN_X - min_dist;
511 worm->virtualx = worm->actualx;
514 if ( worm->actualy > st->SCREEN_Y - min_dist ){
515 worm->actualy = st->SCREEN_Y - min_dist;
516 worm->virtualy = worm->actualy;
520 if ( rnd( 500 ) == rnd( 500 ) ) worm->spiral = rnd( 30 ) + 50;
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 );
530 /* worm->ang = ( worm->ang + 360 + rnd( 30 ) - 15 ) % 360; */
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 );
539 if ( rnd( 25 ) == rnd( 25 ) ){
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);
549 if ( worm->actualx < min_dist || worm->actualx > SCREEN_X - min_dist || worm->actualy < min_dist || worm->actualy > SCREEN_Y - min_dist ){
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 ); * /
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;
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;
581 moveColorChanger( &worm->changer );
583 if ( worm->diameter < worm->diameter_change )
585 if ( worm->diameter > worm->diameter_change )
587 if ( rnd( 30 ) == rnd( 30 ) )
588 worm->diameter_change = rnd( 35 ) + 5;
590 for ( q = 0; q < worm->addStar; q++ )
595 static XColor * getColorShade( color_changer * ch )
597 return ch->shade + ch->min;
600 static void drawWormhole( struct state *st, wormhole * worm )
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 );
613 for ( i = 0; i < worm->num_stars; i++ )
614 if ( worm->stars[i] != NULL ){
616 current = worm->stars[i];
617 z = current->begin.Z;
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 ];
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 );
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 );
634 static void eraseWormhole( Display * display, Window * st->window, wormhole * worm ){
638 for ( i = 0; i < worm->num_stars; i++ )
639 if ( worm->stars[i] != NULL ){
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 );
651 wormhole_init (Display *dpy, Window window)
653 struct state *st = (struct state *) calloc (1, sizeof(*st));
655 XWindowAttributes attr;
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" );
663 initWormhole( st, &st->worm, st->dpy, st->window );
665 st->gc = XCreateGC( st->dpy, st->window, 0, &gcv );
666 XGetWindowAttributes( st->dpy, st->window, &attr );
667 st->cmap = attr.colormap;
673 wormhole_draw (Display *dpy, Window window, void *closure)
675 struct state *st = (struct state *) closure;
677 moveWormhole( st, &st->worm );
678 drawWormhole( st, &st->worm );
683 wormhole_reshape (Display *dpy, Window window, void *closure,
684 unsigned int w, unsigned int h)
686 struct state *st = (struct state *) closure;
687 resizeWormhole( st, &st->worm );
691 wormhole_event (Display *dpy, Window window, void *closure, XEvent *event)
697 wormhole_free (Display *dpy, Window window, void *closure)
699 struct state *st = (struct state *) closure;
706 static const char *wormhole_defaults [] = {
707 ".background: Black",
708 ".foreground: #E9967A",
715 static XrmOptionDescRec wormhole_options [] = {
716 { "-delay", ".delay", XrmoptionSepArg, 0 },
717 { "-zspeed", ".zspeed", XrmoptionSepArg, 0 },
718 { "-stars", ".stars", XrmoptionSepArg, 0 },
722 XSCREENSAVER_MODULE ("Wormhole", wormhole)