1 /* xscreensaver, Copyright (c) 1992-2011 Jamie Zawinski <jwz@jwz.org>
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
13 * Animation of moving through a wormhole. Based on my own code written
15 * author: Jon Rafkind <jon@rafkind.com>
20 #include "screenhack.h"
23 #define debug printf("File:%s Line:%d\n", __FILE__, __LINE__ );
30 int center_x, center_y;
33 typedef struct STARLINE{
38 typedef struct RGBHANDLE{
40 unsigned short rwant, gwant, bwant;
44 typedef struct COLORCHANGER{
50 /* RGBHandle handle_begin, handle_end; */
53 typedef struct WORMHOLE{
57 double virtualx, virtualy;
65 color_changer changer;
67 int num_stars; /* top of array we are using */
76 int SCREEN_X, SCREEN_Y;
87 /*inline*/ static int rnd( int q )
94 static int gang( int x1, int y1, int x2, int y2 )
103 } else if ( y1 == y2 ) {
111 tang = (int)(0.5+atan2( -(y2-y1), x2 - x1 ) * 180.0 / M_PI );
120 static void blend_palette( XColor * pal, int max, XColor * sc, XColor * ec )
126 int sc_g = sc->green;
130 int ec_g = ec->green;
133 for ( q = 0; q < max; q++ ) {
134 float j = (float)( q ) / (float)( max );
135 int f_r = (int)( 0.5 + (float)( sc_r ) + (float)( ec_r-sc_r ) * j );
136 int f_g = (int)( 0.5 + (float)( sc_g ) + (float)( ec_g-sc_g ) * j );
137 int f_b = (int)( 0.5 + (float)( sc_b ) + (float)( ec_b-sc_b ) * j );
138 /* pal[q] = makecol( f_r, f_g, f_b ); */
148 static void initHandle( RGBHandle * handle )
151 handle->mine.red = rnd( 65536 );
152 handle->mine.green = rnd( 65536 );
153 handle->mine.blue = rnd( 65536 );
154 handle->rwant = rnd( 65536 );
155 handle->gwant = rnd( 65536 );
156 handle->bwant = rnd( 65536 );
161 static void initXColor( XColor * color )
163 color->red = rnd( 50000 ) + 10000;
164 color->blue = rnd( 50000 ) + 10000;
165 color->green = rnd( 50000 ) + 10000;
168 static void initColorChanger( struct state *st, color_changer * ch )
172 XColor old_color, new_color;
174 ch->shade_max = 2048;
177 ch->max = ch->shade_use;
178 ch->min_want = rnd( ch->shade_max - ch->shade_use );
179 ch->shade = (XColor *)malloc( sizeof(XColor) * ch->shade_max );
180 memset( ch->shade, 0, sizeof(XColor) * ch->shade_max );
182 initXColor( &old_color );
183 initXColor( &new_color );
185 for ( q = 0; q < ch->shade_max; q += ch->shade_use ){
186 blend_palette( ch->shade + q, ch->shade_use, &old_color, &new_color );
187 old_color = new_color;
189 initXColor( &new_color );
192 for ( q = 0; q < ch->shade_max; q++ )
193 XAllocColor( st->dpy, st->cmap, &( ch->shade[q] ) );
196 initHandle( &(ch->handle_begin) );
197 initHandle( &(ch->handle_end) );
198 ch->shade = (XColor *)malloc( sizeof(XColor) * MAX_COLORS );
199 ch->max = MAX_COLORS;
200 memset( ch->shade, 0, sizeof(XColor) * ch->max );
202 blend_palette( ch->shade, ch->max, &(ch->handle_begin.mine), &(ch->handle_end.mine) );
203 for ( q = 0; q < ch->max; q++ )
204 XAllocColor( st->dpy, *cmap, &( ch->shade[q] ) );
210 static void changeColor( unsigned short * col, unsigned short * change, int min, int max )
212 int RGB_GO_BLACK = 30;
213 if ( *col < *change ) *col++;
214 if ( *col > *change ) *col--;
215 if ( *col == *change ){
216 if ( rnd( RGB_GO_BLACK ) == rnd( RGB_GO_BLACK ) )
218 else *change = rnd(max-min) + min;
224 static void moveRGBHandle( RGBHandle * handle, int min, int max )
227 unsigned short * want[ 3 ];
230 want[0] = &(handle->rwant);
231 want[1] = &(handle->gwant);
232 want[2] = &(handle->bwant);
234 for ( q = 0; q < 10; q++ ){
235 changeColor( &( handle->mine.red ), &handle->rwant, min, max );
236 changeColor( &( handle->mine.green ), &handle->gwant, min, max );
237 changeColor( &( handle->mine.blue ), &handle->bwant, min, max );
240 for ( q = 0; q < 3; q++ )
241 cy = cy || (*(want[q]) >= min && *(want[q]) <= max);
242 if ( !cy ) *(want[rnd(3)]) = rnd(max-min)+min;
244 for ( q = 0; q < 3; q++ )
245 cy = cy && *(want[q]) == 0;
246 if ( cy ) *(want[rnd(3)]) = rnd(max-min)+min;
248 if ( rnd( 30 ) == rnd( 30 ) )
249 *(want[rnd(3)]) = rnd(max-min)+min;
254 static void moveColorChanger( color_changer * ch )
259 if ( ch->min < ch->min_want ){
263 if ( ch->min > ch->min_want ) {
267 if ( ch->min == ch->min_want )
268 ch->min_want = rnd( ch->shade_max - ch->shade_use );
271 for ( q = 0; q < ch->max; q++ )
272 XFreeColors( st->dpy, *cmap, &( ch->shade[q].pixel ), 1, 0 );
274 moveRGBHandle( &( ch->handle_begin ), 5000, 65500 );
275 moveRGBHandle( &( ch->handle_end ), 5000, 65500 );
277 blend_palette( ch->shade, ch->max, &(ch->handle_begin.mine), &(ch->handle_end.mine) );
278 for ( q = 0; q < ch->max; q++ )
279 XAllocColor( st->dpy, *cmap, &( ch->shade[q] ) );
285 static void destroyColorChanger( color_changer * ch )
288 for ( q = 0; q < ch->max; q++ )
289 XFreeColors( st->dpy, *cmap, &( ch->shade[q].pixel ), 1, 0 );
294 static void resizeWormhole( struct state *st, wormhole * worm )
297 XWindowAttributes attr;
299 XGetWindowAttributes( st->dpy, st->window, &attr );
301 st->cmap = attr.colormap;
303 st->SCREEN_X = attr.width;
304 st->SCREEN_Y = attr.height;
306 # ifndef HAVE_COCOA /* Don't second-guess Quartz's double-buffering */
307 XFreePixmap( st->dpy, worm->work );
308 worm->work = XCreatePixmap( st->dpy, st->window, st->SCREEN_X, st->SCREEN_Y, attr.depth );
313 static void initWormhole( struct state *st, wormhole * worm, Display * display, Window win )
317 XWindowAttributes attr;
319 XGetWindowAttributes( st->dpy, st->window, &attr );
321 st->cmap = attr.colormap;
323 st->SCREEN_X = attr.width;
324 st->SCREEN_Y = attr.height;
326 # ifdef HAVE_COCOA /* Don't second-guess Quartz's double-buffering */
327 worm->work = st->window;
329 worm->work = XCreatePixmap( st->dpy, st->window, st->SCREEN_X, st->SCREEN_Y, attr.depth );
332 worm->diameter = rnd( 10 ) + 15;
333 worm->diameter_change = rnd( 10 ) + 15;
334 /* worm->actualx = rnd( attr.width );
335 worm->actualy = rnd( attr.height ); */
336 worm->actualx = attr.width / 2;
337 worm->actualy = attr.height / 2;
338 worm->virtualx = worm->actualx;
339 worm->virtualy = worm->actualy;
340 worm->speed = (float)st->SCREEN_X / 180.0;
341 /* z_speed = SCREEN_X / 120; */
343 worm->addStar = st->make_stars;
344 worm->want_x = rnd( attr.width - 50 ) + 25;
345 worm->want_y = rnd( attr.height - 50 ) + 25;
346 worm->want_ang = gang( worm->actualx, worm->actualy, worm->want_x, worm->want_y );
347 worm->ang = worm->want_ang;
350 worm->black.green = 0;
351 worm->black.blue = 0;
352 XAllocColor( st->dpy, st->cmap, &worm->black );
353 initColorChanger( st, &(worm->changer) );
355 worm->num_stars = 64;
356 worm->stars = (starline **)malloc( sizeof(starline *) * worm->num_stars );
357 for ( i = 0; i < worm->num_stars; i++ )
358 worm->stars[i] = NULL;
363 static void destroyWormhole( wormhole * worm )
365 destroyColorChanger( &(worm->changer), st->dpy, cmap );
366 if (work->work != st->window)
367 XFreePixmap( st->dpy, worm->work );
372 static double Cos( int a )
374 return cos( a * 180.0 / M_PI );
377 static double Sine( int a )
379 return sin( a * 180.0 / M_PI );
384 static void calcStar( star * st )
386 if ( st->center_x == 0 || st->center_y == 0 ){
391 st->calc_x = (st->x << 10) / st->center_x;
392 st->calc_y = (st->y << 10) / st->center_y;
394 st->calc_x = (st->x << 10 ) / st->Z + st->center_x;
395 st->calc_y = (st->y << 10 ) / st->Z + st->center_y;
399 static void initStar( star * st, int Z, int ang, wormhole * worm )
402 st->x = Cos( ang ) * worm->diameter;
403 st->y = Sine( ang ) * worm->diameter;
404 st->center_x = worm->actualx;
405 st->center_y = worm->actualy;
411 static void addStar( wormhole * worm )
419 star_new = (starline *)malloc( sizeof( starline ) );
421 initStar( &star_new->begin, worm->max_Z, ang, worm );
422 initStar( &star_new->end, worm->max_Z+rnd(6)+4, ang, worm );
424 for ( q = 0; q < worm->num_stars; q++ ){
425 if ( worm->stars[q] == NULL ){
426 worm->stars[q] = star_new;
431 old_stars = worm->num_stars;
432 worm->num_stars = worm->num_stars << 1;
433 xstars = (starline **)malloc( sizeof(starline *) * worm->num_stars );
434 for ( q = 0; q < worm->num_stars; q++ )
437 for ( q = 0; q < old_stars; q++ )
438 if ( worm->stars[q] != NULL ) xstars[q] = worm->stars[q];
440 worm->stars = xstars;
442 worm->stars[ old_stars ] = star_new;
446 static int moveStar( struct state *st, starline * stl )
449 stl->begin.Z -= st->z_speed;
450 stl->end.Z -= st->z_speed;
452 calcStar( &stl->begin );
453 calcStar( &stl->end );
455 return ( stl->begin.Z <= 0 || stl->end.Z <= 0 );
459 static int dist( int x1, int y1, int x2, int y2 )
464 return (int)sqrt( xs*xs + ys*ys );
467 static void moveWormhole( struct state *st, wormhole * worm )
472 /* int x1, y1, x2, y2; */
475 dx = Cos( worm->ang ) * worm->speed;
476 dy = Sine( worm->ang ) * worm->speed;
478 worm->virtualx += dx;
479 worm->virtualy += dy;
480 worm->actualx = (int)worm->virtualx;
481 worm->actualy = (int)worm->virtualy;
485 if ( worm->spiral % 5 == 0 )
486 worm->ang = (worm->ang + 1 ) % 360;
488 if ( worm->spiral <= 0 ) find = 1;
492 if ( dist( worm->actualx, worm->actualy, worm->want_x, worm->want_y ) < 20 )
495 if ( rnd( 20 ) == rnd( 20 ) )
498 if ( worm->actualx < min_dist ){
499 worm->actualx = min_dist;
500 worm->virtualx = worm->actualx;
503 if ( worm->actualy < min_dist ){
504 worm->actualy = min_dist;
505 worm->virtualy = worm->actualy;
508 if ( worm->actualx > st->SCREEN_X - min_dist ){
509 worm->actualx = st->SCREEN_X - min_dist;
510 worm->virtualx = worm->actualx;
513 if ( worm->actualy > st->SCREEN_Y - min_dist ){
514 worm->actualy = st->SCREEN_Y - min_dist;
515 worm->virtualy = worm->actualy;
519 if ( rnd( 500 ) == rnd( 500 ) ) worm->spiral = rnd( 30 ) + 50;
523 worm->want_x = rnd( st->SCREEN_X - min_dist * 2 ) + min_dist;
524 worm->want_y = rnd( st->SCREEN_Y - min_dist * 2 ) + min_dist;
525 worm->ang = gang( worm->actualx, worm->actualy, worm->want_x, worm->want_y );
529 /* worm->ang = ( worm->ang + 360 + rnd( 30 ) - 15 ) % 360; */
532 if ( worm->ang < worm->want_ang ) worm->ang++;
533 if ( worm->ang > worm->want_ang ) worm->ang--;
534 if ( worm->ang == worm->want_ang && rnd( 3 ) == rnd( 3 ) ) worm->want_ang = rnd( 360 );
538 if ( rnd( 25 ) == rnd( 25 ) ){
541 x2 = SCREEN_X / 2 + rnd( 20 ) - 10;
542 y2 = SCREEN_Y / 2 + rnd( 20 ) - 10;
543 worm->want_ang = gang(x1,y1,x2,y2);
548 if ( worm->actualx < min_dist || worm->actualx > SCREEN_X - min_dist || worm->actualy < min_dist || worm->actualy > SCREEN_Y - min_dist ){
551 x2 = SCREEN_X / 2 + rnd( 20 ) - 10;
552 y2 = SCREEN_Y / 2 + rnd( 20 ) - 10;
553 / * worm->ang = gang( worm->actualx, worm->actualy, SCREEN_X/2+rnd(20)-10, SCREEN_Y/2+rnd(20)-10 ); * /
554 worm->ang = gang( x1, y1, x2, y2 );
555 worm->want_ang = worm->ang;
556 / * printf("Angle = %d\n", worm->ang ); * /
558 if ( worm->actualx < min_dist )
559 worm->actualx = min_dist;
560 if ( worm->actualx > SCREEN_X - min_dist )
561 worm->actualx = SCREEN_X - min_dist;
562 if ( worm->actualy < min_dist )
563 worm->actualy = min_dist;
564 if ( worm->actualy > SCREEN_Y - min_dist )
565 worm->actualy = SCREEN_Y - min_dist;
566 worm->virtualx = worm->actualx;
567 worm->virtualy = worm->actualy;
571 for ( q = 0; q < worm->num_stars; q++ ){
572 if ( worm->stars[q] != NULL ){
573 if ( moveStar( st, worm->stars[q] ) ){
574 free( worm->stars[q] );
575 worm->stars[q] = NULL;
580 moveColorChanger( &worm->changer );
582 if ( worm->diameter < worm->diameter_change )
584 if ( worm->diameter > worm->diameter_change )
586 if ( rnd( 30 ) == rnd( 30 ) )
587 worm->diameter_change = rnd( 35 ) + 5;
589 for ( q = 0; q < worm->addStar; q++ )
594 static XColor * getColorShade( color_changer * ch )
596 return ch->shade + ch->min;
599 static void drawWormhole( struct state *st, wormhole * worm )
609 XSetForeground( st->dpy, st->gc, worm->black.pixel );
610 XFillRectangle( st->dpy, worm->work, st->gc, 0, 0, st->SCREEN_X, st->SCREEN_Y );
612 for ( i = 0; i < worm->num_stars; i++ )
613 if ( worm->stars[i] != NULL ){
615 current = worm->stars[i];
616 z = current->begin.Z;
618 color = z * worm->changer.shade_use / worm->max_Z;
619 shade = getColorShade( &worm->changer );
620 /* xcol = &worm->changer.shade[ color ]; */
621 xcol = &shade[ color ];
623 XSetForeground( st->dpy, st->gc, xcol->pixel );
624 /* XDrawLine( st->dpy, st->window, *gc, current->begin.calc_x, current->begin.calc_y, current->end.calc_x, current->end.calc_y ); */
625 XDrawLine( st->dpy, worm->work, st->gc, current->begin.calc_x, current->begin.calc_y, current->end.calc_x, current->end.calc_y );
628 if (worm->work != st->window)
629 XCopyArea( st->dpy, worm->work, st->window, st->gc, 0, 0, st->SCREEN_X, st->SCREEN_Y, 0, 0 );
633 static void eraseWormhole( Display * display, Window * st->window, wormhole * worm ){
637 for ( i = 0; i < worm->num_stars; i++ )
638 if ( worm->stars[i] != NULL ){
640 current = worm->stars[i];
641 XSetForeground( st->dpy, *gc, xcol->pixel );
642 XDrawLine( st->dpy, st->window, *gc, current->begin.calc_x, current->begin.calc_y, current->end.calc_x, current->end.calc_y );
650 wormhole_init (Display *dpy, Window window)
652 struct state *st = (struct state *) calloc (1, sizeof(*st));
654 XWindowAttributes attr;
658 st->delay = get_integer_resource(st->dpy, "delay", "Integer" );
659 st->make_stars = get_integer_resource(st->dpy, "stars", "Integer" );
660 st->z_speed = get_integer_resource(st->dpy, "zspeed", "Integer" );
662 initWormhole( st, &st->worm, st->dpy, st->window );
664 st->gc = XCreateGC( st->dpy, st->window, 0, &gcv );
665 XGetWindowAttributes( st->dpy, st->window, &attr );
666 st->cmap = attr.colormap;
672 wormhole_draw (Display *dpy, Window window, void *closure)
674 struct state *st = (struct state *) closure;
676 moveWormhole( st, &st->worm );
677 drawWormhole( st, &st->worm );
682 wormhole_reshape (Display *dpy, Window window, void *closure,
683 unsigned int w, unsigned int h)
685 struct state *st = (struct state *) closure;
686 resizeWormhole( st, &st->worm );
690 wormhole_event (Display *dpy, Window window, void *closure, XEvent *event)
696 wormhole_free (Display *dpy, Window window, void *closure)
698 struct state *st = (struct state *) closure;
705 static const char *wormhole_defaults [] = {
706 ".background: Black",
707 ".foreground: #E9967A",
714 static XrmOptionDescRec wormhole_options [] = {
715 { "-delay", ".delay", XrmoptionSepArg, 0 },
716 { "-zspeed", ".zspeed", XrmoptionSepArg, 0 },
717 { "-stars", ".stars", XrmoptionSepArg, 0 },
721 XSCREENSAVER_MODULE ("Wormhole", wormhole)