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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
|
/*
* InterAggregate (dagraz@gmail.com)
* Based on code from complexification.net Intersection Aggregate
* http://www.complexification.net/gallery/machines/interAggregate/index.php
*
* Intersection Aggregate code:
* j.tarbell May, 2004
* Albuquerque, New Mexico
* complexification.net
*
* Also based on substrate, a port of j.tarbell's Substrate Art done
* by dragorn@kismetwireless.net
*
*
* Interesting command line options, all of which serve to
* concentrate the painting in some way:
*
* -percent-orbits 100 -base-orbits 50 -base-on-center -growth-delay 0
*
* Paint should be concentrated in the center of the canvas, orbiting
* about it. -percent-orbits 100 implies -base-on-center, so that's
* not really needed.
*
*
* -percent-orbits 99 -base-orbits 50 -growth-delay 0
*
* Like the above example, but the 'center' will rove about the screen.
*
* -percent-orbits 98 -base-orbits 50 -growth-delay 0
*
* Like the above example, but there will be two roving centers.
*
*
* TODO:
* -fix alpha blending / byte ordering
*
* CHANGES
*
*
* Directly based the hacks of:
*
* xscreensaver, Copyright (c) 1997, 1998, 2002 Jamie Zawinski <jwz@jwz.org>
*
* 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.
*/
#include <math.h>
#include "screenhack.h"
/* this program goes faster if some functions are inline. The following is
* borrowed from ifs.c */
#if !defined( __GNUC__ ) && !defined(__cplusplus) && !defined(c_plusplus)
#undef inline
#define inline /* */
#endif
#ifndef MIN
#define MIN(x,y) ((x < y) ? x : y)
#endif
#ifndef MAX
#define MAX(x,y) ((x < y) ? y : x)
#endif
static const char *interaggregate_defaults[] =
{
".background: white",
".foreground: black",
"*fpsSolid: true",
"*maxCycles: 100000",
#ifdef TIME_ME
"*growthDelay: 0",
#else
"*growthDelay: 18000",
#endif
"*numCircles: 100",
"*percentOrbits: 0",
"*baseOrbits: 75",
"*baseOnCenter: False",
"*drawCenters: False",
#ifdef HAVE_MOBILE
"*ignoreRotation: True",
#endif
0
};
static XrmOptionDescRec interaggregate_options[] =
{
{"-background", ".background", XrmoptionSepArg, 0},
{"-foreground", ".foreground", XrmoptionSepArg, 0},
{"-max-cycles", ".maxCycles", XrmoptionSepArg, 0},
{"-growth-delay", ".growthDelay", XrmoptionSepArg, 0},
{"-num-circles", ".numCircles", XrmoptionSepArg, 0},
{"-percent-orbits", ".percentOrbits", XrmoptionSepArg, 0},
{"-base-orbits", ".baseOrbits", XrmoptionSepArg, 0},
{"-base-on-center", ".baseOnCenter", XrmoptionNoArg, "true"},
{"-draw-centers", ".drawCenters", XrmoptionNoArg, "true"},
{0, 0, 0, 0}
};
/* Raw colormap extracted from pollockEFF.gif */
#if 0
char *rgb_colormap[] =
{
"#FFFFFF", /* white */
"#000000", /* black */
"#000000", /* more black */
/* "#736451", */
"#4e3e2e", /* olive */
/* "#666666", */
"#694d35", /* camel */
"#b9a88c", /* tan */
0
};
#endif
static const char *rgb_colormap[] =
{
"#FFFFFF", /* white */
"#000000", /* more black */
"#000000", /* more black */
"#4e3e2e", /* olive */
"#694d35", /* camel */
"#b0a085", /* tan */
"#e6d3ae",
0
};
/* black white brown olive grey camel */
typedef enum { LINEAR, ORBIT } PathType;
typedef struct
{
unsigned long color;
double gain;
double p;
} SandPainter;
typedef struct _circle
{
double radius;
double x;
double y;
PathType path_type;
/* for a linear path */
double dx;
double dy;
/* for orbital path */
double theta;
double r;
double dtheta;
struct _circle* center;
int num_painters;
SandPainter* painters;
} Circle;
struct field
{
int height;
int width;
int num_circles;
Circle* circles;
int percent_orbits;
int base_orbits;
Bool base_on_center;
/* used for orbits circling the center of the screen */
Circle center_of_universe;
/* Raw map of pixels we need to keep for alpha blending */
unsigned long int *off_img;
/* color parms */
int numcolors;
unsigned long *parsedcolors;
unsigned long fgcolor;
unsigned long bgcolor;
int visdepth;
unsigned int cycles;
double max_gain;
/* for debugging */
Bool draw_centers;
/* for profiling whatnot */
int possible_intersections;
int intersection_count;
};
static struct field *
init_field(void)
{
struct field *f = (struct field*) malloc(sizeof(struct field));
if ( f == NULL )
{
fprintf(stderr, "%s: Failed to allocate field!\n", progname);
exit(1);
}
f->height = 0;
f->width = 0;
f->num_circles = 0;
f->circles = NULL;
f->percent_orbits = 0;
f->base_orbits = 0;
f->base_on_center = False;
f->off_img = NULL;
f->numcolors = 0;
f->parsedcolors = NULL;
f->fgcolor = 0;
f->bgcolor = 0;
f->visdepth = 0;
f->cycles = 0;
f->max_gain = 0.22;
f->draw_centers = False;
f->possible_intersections = 0;
f->intersection_count = 0;
return f;
}
/* Quick references to pixels in the offscreen map and in the crack grid */
#define ref_pixel(f, x, y) ((f)->off_img[(y) * (f)->width + (x)])
#define in_bounds(f, x, y) ((x >= 0) && (x < f->width) && (y >= 0) && (y < f->height))
/* Consider rewriting with XQueryColor, or ImageByteOrder */
static inline void point2rgb(int depth, unsigned long c, int *r, int *g, int *b)
{
switch(depth)
{
case 32:
case 24:
#ifdef HAVE_JWXYZ
/* This program idiotically does not go through a color map, so
we have to hardcode in knowledge of how jwxyz.a packs pixels!
Fix it to go through st->colors[st->ncolors] instead!
*/
*r = (c & 0x00ff0000) >> 16;
*g = (c & 0x0000ffff) >> 8;
*b = (c & 0x000000ff);
#else
*b = c & 0xff;
*g = (c & 0xff00) >> 8;
*r = (c & 0xff0000) >> 16;
#endif
break;
case 16:
*b = (int) (c & 0x1f) << 3;
*g = (int) ((c >> 5) & 0x3f) << 2;
*r = (int) ((c >> 11) & 0x1f) << 3;
break;
case 15:
*b = (int) (c & 0x1f) << 3;
*g = (int) ((c >> 5) & 0x1f) << 3;
*r = (int) ((c >> 10) & 0x1f) << 3;
break;
}
}
static inline unsigned long rgb2point(int depth, int r, int g, int b)
{
unsigned long ret = 0;
switch(depth)
{
case 32:
case 24:
#ifdef HAVE_JWXYZ
/* This program idiotically does not go through a color map, so
we have to hardcode in knowledge of how jwxyz.a packs pixels!
Fix it to go through st->colors[st->ncolors] instead!
*/
ret = 0xFF000000 | (r << 16) | (g << 8) | b;
#else
ret |= (r << 16) | (g << 8) | b;
#endif
break;
case 16:
ret = ((r>>3) << 11) | ((g>>2)<<5) | (b>>3);
break;
case 15:
ret = ((r>>3) << 10) | ((g>>3)<<5) | (b>>3);
break;
}
return ret;
}
/* alpha blended point drawing -- this is Not Right and will likely fail on
* non-intel platforms as it is now, needs fixing */
static inline unsigned long trans_point(int x1, int y1, unsigned long myc, double a,
struct field *f)
{
if (a >= 1.0)
{
ref_pixel(f, x1, y1) = myc;
return myc;
}
else
{
int or=0, og=0, ob=0;
int r=0, g=0, b=0;
int nr, ng, nb;
unsigned long c;
c = ref_pixel(f, x1, y1);
point2rgb(f->visdepth, c, &or, &og, &ob);
point2rgb(f->visdepth, myc, &r, &g, &b);
nr = or + (r - or) * a;
ng = og + (g - og) * a;
nb = ob + (b - ob) * a;
c = rgb2point(f->visdepth, nr, ng, nb);
ref_pixel(f, x1, y1) = c;
return c;
}
}
static inline void drawPoint(int x, int y, unsigned long color, double intensity,
Display *dpy, Window window, GC fgc, struct field *f)
{
unsigned long c;
while ( x >= f->width ) x -= f->width;
while ( x < 0 ) x += f->width;
while ( y >= f->height ) y -= f->height;
while ( y < 0 ) y += f->height;
/* if ( in_bounds(f, x, y) ) ... */
c = trans_point(x, y, color, intensity, f);
XSetForeground(dpy, fgc, c);
XDrawPoint(dpy, window, fgc, x, y);
}
static inline void paint(SandPainter* painter, double ax, double ay, double bx, double by,
Display *dpy, Window window, GC fgc,
struct field *f)
{
/* the sand painter */
double inc, sandp;
int i;
/* XXX try adding tpoint here, like orig */
/* jitter the painter's values */
painter->gain += frand(0.05) - 0.025;
if ( painter->gain > f->max_gain )
painter->gain = -f->max_gain;
else if ( painter->gain < -f->max_gain )
painter->gain = f->max_gain;
painter->p += frand(0.1) - 0.05;
if ( 0 < painter->p )
painter->p = 0;
else if ( painter->p > 1.0 )
painter->p = 1.0;
/* replace 0.1 with 1 / f->grains */
inc = painter->gain * 0.1;
sandp = 0;
for(i = 0; i <= 10; ++i)
{
int drawx, drawy;
double sp, sm;
double intensity = 0.1 - 0.009 * i;
sp = sin(painter->p + sandp);
drawx = ax + (bx - ax) * sp;
drawy = ay + (by - ay) * sp;
drawPoint(drawx, drawy, painter->color,
intensity,
dpy, window, fgc, f);
sm = sin(painter->p - sandp);
drawx = ax + (bx - ax) * sm;
drawy = ay + (by - ay) * sm;
drawPoint(drawx, drawy, painter->color,
intensity,
dpy, window, fgc, f);
sandp += inc;
}
}
static void build_colors(struct field *f, Display *dpy, XWindowAttributes *xgwa)
{
XColor tmpcolor;
int i;
/* Count the colors in our map and assign them in a horrifically inefficient
* manner but it only happens once */
for( f->numcolors = 0;
rgb_colormap[f->numcolors] != NULL;
++f->numcolors )
{
;
}
if (f->numcolors < 1) f->numcolors = 1;
f->parsedcolors = (unsigned long *) calloc(f->numcolors,
sizeof(unsigned long));
if ( f->parsedcolors == NULL )
{
fprintf(stderr, "%s: Failed to allocate parsedcolors\n",
progname);
exit(1);
}
for(i = 0; i < f->numcolors; ++i)
{
if (!XParseColor(dpy, xgwa->colormap,
rgb_colormap[i], &tmpcolor))
{
fprintf(stderr, "%s: couldn't parse color %s\n", progname,
rgb_colormap[i]);
exit(1);
}
if (!XAllocColor(dpy, xgwa->colormap, &tmpcolor))
{
fprintf(stderr, "%s: couldn't allocate color %s\n", progname,
rgb_colormap[i]);
exit(1);
}
f->parsedcolors[i] = tmpcolor.pixel;
}
}
/* used when the window is resized */
static void build_img(struct field *f)
{
if (f->off_img) {
free(f->off_img);
f->off_img = NULL;
}
f->off_img = (unsigned long *) calloc(f->width * f->height,
sizeof(unsigned long));
if ( f->off_img == NULL )
{
fprintf(stderr, "%s: Failed to allocate off_img\n",
progname);
exit(1);
}
memset(f->off_img, f->bgcolor,
sizeof(unsigned long) * f->width * f->height);
}
static void free_circles(struct field *f)
{
int i;
if ( f->circles != NULL )
{
for(i = 0; i < f->num_circles; ++i)
{
free (f->circles[i].painters);
}
free (f->circles);
f->circles = NULL;
}
}
static void build_field(Display *dpy, Window window, XWindowAttributes xgwa, GC fgc,
struct field *f)
{
int i;
int num_orbits;
int base_orbits;
int orbit_start;
build_img(f);
f->center_of_universe.x = f->width / 2.0;
f->center_of_universe.y = f->height / 2.0;
f->center_of_universe.r = MAX(f->width, f->height) / 2.0;
num_orbits = (f->percent_orbits * f->num_circles) / 100;
orbit_start = f->num_circles - num_orbits;
base_orbits = orbit_start + (num_orbits * f->base_orbits) / 100;
free_circles(f);
f->circles = (Circle*) calloc(f->num_circles, sizeof(Circle));
if ( f->circles == NULL )
{
fprintf(stderr, "%s: Failed to allocate off_img\n",
progname);
exit(1);
}
for(i = 0; i < f->num_circles; ++i)
{
int j;
Circle *circle = f->circles + i;
/* make this a pref */
if ( i >= orbit_start )
circle->path_type = ORBIT;
else
circle->path_type = LINEAR;
if ( circle->path_type == LINEAR )
{
circle->x = frand(f->width);
circle->y = frand(f->height);
circle->dx = frand(0.5) - 0.25;
circle->dy = frand(0.5) - 0.25;
/* circle->dy = 0; */
/* circle->r = f->height * (0.05 + frand(0.1)); */
circle->radius = 5 + frand(55);
/* in case we want orbits based on lines */
circle->r = MIN(f->width, f->height) / 2.0;
circle->center = NULL;
}
else /* == ORBIT */
{
if (i < base_orbits )
{
if ( f->base_on_center )
circle->center = &f->center_of_universe;
else
{
circle->center = f->circles +
((int)frand(orbit_start - 0.1));
}
circle->r = 1 + frand(MIN(f->width, f->height) / 2.0);
/* circle->radius = 5 + frand(55); */
}
else
{
/* give a preference for the earlier circles */
double p = frand(0.9);
circle->center = f->circles + (int) (p*i);
circle->r = 1 + 0.5 * circle->center->r + 0.5 * frand(circle->center->r);
/* circle->r = 1 + frand(circle->center->r / 2); */
/* circle->radius = MAX(5, frand(circle->r)); */
/* circle->radius = 5 + frand(55); */
}
circle->radius = 5 + frand(MIN(55, circle->r));
circle->dtheta = (frand(0.5) - 0.25) / circle->r;
circle->theta = frand(2 * M_PI);
circle->x = circle->r * cos(circle->theta) + circle->center->x;
circle->y = circle->r * sin(circle->theta) + circle->center->y;
}
/* make this a command line option */
circle->num_painters = 3;
circle->painters = (SandPainter*) calloc(circle->num_painters,
sizeof(SandPainter));
if ( circle->painters == NULL )
{
fprintf(stderr, "%s: failed to allocate painters", progname);
exit(1);
}
for(j = 0; j < circle->num_painters; ++j)
{
SandPainter *painter = circle->painters + j;
painter->gain = frand(0.09) + 0.01;
painter->p = frand(1.0);
painter->color =
f->parsedcolors[(int)(frand(0.999) * f->numcolors)];
}
}
}
static void moveCircles(struct field *f)
{
int i;
for(i = 0; i < f->num_circles; ++i)
{
Circle *circle = f->circles + i;
if ( circle->path_type == LINEAR )
{
circle->x += circle->dx;
circle->y += circle->dy;
#if 0
if ( circle->x < -circle->radius )
circle->x = f->width + circle->radius;
else if ( circle->x >= f->width + circle->radius )
circle->x = -circle->radius;
if ( circle->y < -circle->radius )
circle->y = f->height + circle->radius;
else if ( circle->y >= f->height + circle->radius )
circle->y = -circle->radius;
#else
if ( circle->x < 0 ) circle->x += f->width;
else if ( circle->x >= f->width ) circle->x -= f->width;
if ( circle->y < 0 ) circle->y += f->height;
else if ( circle->y >= f->height ) circle->y -= f->height;
#endif
}
else /* (circle->path_type == ORBIT) */
{
circle->theta += circle->dtheta;
if ( circle->theta < 0 ) circle->theta += 2 * M_PI;
else if ( circle->theta > 2 * M_PI ) circle->theta -= 2 * M_PI;
circle->x = circle->r * cos(circle->theta) + circle->center->x;
circle->y = circle->r * sin(circle->theta) + circle->center->y;
#if 0
if ( circle->x < -circle->radius )
circle->x += f->width + 2 * circle->radius;
else if ( circle->x >= f->width + circle->radius )
circle->x -= f->width + 2 * circle->radius;
if ( circle->y < -circle->radius )
circle->y += f->height + 2 * circle->radius;
else if ( circle->y >= f->height + circle->radius )
circle->y -= f->height + 2 * circle->radius;
#else
if ( circle->x < 0 ) circle->x += f->width;
else if ( circle->x >= f->width ) circle->x -= f->width;
if ( circle->y < 0 ) circle->y += f->height;
else if ( circle->y >= f->height ) circle->y -= f->height;
#endif
}
}
}
static void drawIntersections(Display *dpy, Window window, GC fgc, struct field *f)
{
int i,j;
/* One might be tempted to think 'hey, this is a crude algorithm
* that is going to check each of the n (n-1) / 2 possible
* intersections! Why not try bsp trees, quad trees, etc, etc,
* etc'
*
* In practice the time spent drawing the intersection of two
* circles dwarfs the time takes to check for intersection.
* Profiling on a 640x480 screen with 100 circles shows possible
* speed gains to be only a couple of percent.
*
* But hey, if you're bored, go have fun. Let me know how it
* turns out.
*/
for(i = 0; i < f->num_circles; ++i)
{
Circle *c1 = f->circles + i;
if ( !f->draw_centers )
{
/* the default branch */
for(j = i + 1; j < f->num_circles; ++j)
{
double d, dsqr, dx, dy;
Circle *c2 = f->circles + j;
#ifdef TIME_ME
++f->possible_intersections;
#endif
dx = c2->x - c1->x;
dy = c2->y - c1->y;
dsqr = dx * dx + dy * dy;
d = sqrt(dsqr);
if ( (fabs(dx) < (c1->radius + c2->radius)) &&
(fabs(dy) < (c1->radius + c2->radius)) &&
( d < (c1->radius + c2->radius) ) &&
( d > fabs(c1->radius - c2->radius) ) )
{
double d1, d2, r1sqr;
double bx, by;
double midpx, midpy;
double int1x, int1y;
double int2x, int2y;
int s;
/* woo-hoo. the circles are neither outside nor
* inside each other. they intersect.
*
* Now, compute the coordinates of the points of
* intersection
*/
#ifdef TIME_ME
++f->intersection_count;
#endif
/* unit vector in direction of c1 to c2 */
bx = dx / d;
by = dy / d;
r1sqr = c1->radius * c1->radius;
/* distance from c1's center midpoint of intersection
* points */
d1 = 0.5 * (r1sqr - c2->radius * c2->radius + dsqr) / d;
midpx = c1->x + d1 * bx;
midpy = c1->y + d1 * by;
/* distance from midpoint to points of intersection */
d2 = sqrt(r1sqr - d1 * d1);
int1x = midpx + d2 * by;
int1y = midpy - d2 * bx;
int2x = midpx - d2 * by;
int2y = midpy + d2 * bx;
for(s = 0; s < c1->num_painters; ++s)
{
paint(c1->painters + s, int1x, int1y, int2x, int2y,
dpy, window, fgc, f);
}
}
}
}
else /* f->draw_centers */
{
XDrawPoint(dpy, window, fgc, c1->x, c1->y);
}
}
}
struct state {
Display *dpy;
Window window;
unsigned int max_cycles;
int growth_delay;
GC fgc;
XGCValues gcv;
XWindowAttributes xgwa;
struct field *f;
};
static void *
interaggregate_init (Display *dpy, Window window)
{
struct state *st = (struct state *) calloc (1, sizeof(*st));
#ifdef TIME_ME
int frames;
struct timeval tm1, tm2;
double tdiff;
#endif
st->dpy = dpy;
st->window = window;
st->f = init_field();
st->growth_delay = (get_integer_resource(st->dpy, "growthDelay", "Integer"));
st->max_cycles = (get_integer_resource(st->dpy, "maxCycles", "Integer"));
st->f->num_circles = (get_integer_resource(st->dpy, "numCircles", "Integer"));
st->f->percent_orbits = (get_integer_resource(st->dpy, "percentOrbits", "Integer"));
st->f->base_orbits = (get_integer_resource(st->dpy, "baseOrbits", "Integer"));
st->f->base_on_center = (get_boolean_resource(st->dpy, "baseOnCenter", "Boolean"));
st->f->draw_centers = (get_boolean_resource(st->dpy, "drawCenters", "Boolean"));
if (st->f->num_circles <= 1)
{
fprintf(stderr, "%s: Minimum number of circles is 2\n",
progname);
exit (1);
}
if ( (st->f->percent_orbits < 0) || (st->f->percent_orbits > 100) )
{
fprintf(stderr, "%s: percent-oribts must be between 0 and 100\n",
progname);
exit (1);
}
if ( (st->f->base_orbits < 0) || (st->f->base_orbits > 100) )
{
fprintf(stderr, "%s: base-oribts must be between 0 and 100\n",
progname);
exit (1);
}
if ( st->f->percent_orbits == 100 )
st->f->base_on_center = True;
XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
build_colors(st->f, st->dpy, &st->xgwa);
st->gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap,
"foreground", "Foreground");
st->gcv.background = get_pixel_resource(st->dpy, st->xgwa.colormap,
"background", "Background");
st->fgc = XCreateGC(st->dpy, st->window, GCForeground, &st->gcv);
st->f->height = st->xgwa.height;
st->f->width = st->xgwa.width;
st->f->visdepth = st->xgwa.depth;
st->f->fgcolor = st->gcv.foreground;
st->f->bgcolor = st->gcv.background;
/* Initialize stuff */
build_field(st->dpy, st->window, st->xgwa, st->fgc, st->f);
#ifdef TIME_ME
gettimeofday(&tm1, NULL);
frames = 0;
#endif
return st;
}
static unsigned long
interaggregate_draw (Display *dpy, Window window, void *closure)
{
struct state *st = (struct state *) closure;
if ((st->f->cycles % 10) == 0)
{
/* Restart if the window size changes */
XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
if (st->f->height != st->xgwa.height || st->f->width != st->xgwa.width)
{
st->f->height = st->xgwa.height;
st->f->width = st->xgwa.width;
st->f->visdepth = st->xgwa.depth;
build_field(st->dpy, st->window, st->xgwa, st->fgc, st->f);
XSetForeground(st->dpy, st->fgc, st->gcv.background);
XFillRectangle(st->dpy, st->window, st->fgc, 0, 0, st->xgwa.width, st->xgwa.height);
XSetForeground(st->dpy, st->fgc, st->gcv.foreground);
}
}
moveCircles(st->f);
drawIntersections(st->dpy, st->window, st->fgc, st->f);
st->f->cycles++;
if (st->f->cycles >= st->max_cycles && st->max_cycles != 0)
{
build_field(st->dpy, st->window, st->xgwa, st->fgc, st->f);
XSetForeground(st->dpy, st->fgc, st->gcv.background);
XFillRectangle(st->dpy, st->window, st->fgc, 0, 0, st->xgwa.width, st->xgwa.height);
XSetForeground(st->dpy, st->fgc, st->gcv.foreground);
}
#ifdef TIME_ME
frames++;
gettimeofday(&tm2, NULL);
tdiff = (tm2.tv_sec - tm1.tv_sec)
+ (tm2.tv_usec - tm1.tv_usec) * 0.00001;
if ( tdiff > 1 )
{
fprintf(stderr, "fps: %d %f %f\n",
frames, tdiff, frames / tdiff );
fprintf(stderr, "intersections: %d %d %f\n",
f->intersection_count, f->possible_intersections,
((double)f->intersection_count) /
f->possible_intersections);
fprintf(stderr, "fpi: %f\n",
((double)frames) / f->intersection_count );
frames = 0;
tm1.tv_sec = tm2.tv_sec;
tm1.tv_usec = tm2.tv_usec;
f->intersection_count = f->possible_intersections = 0;
}
#endif
return st->growth_delay;
}
static void
interaggregate_reshape (Display *dpy, Window window, void *closure,
unsigned int w, unsigned int h)
{
}
static Bool
interaggregate_event (Display *dpy, Window window, void *closure, XEvent *event)
{
struct state *st = (struct state *) closure;
if (screenhack_event_helper (dpy, window, event))
{
st->f->height--; /* act like a resize */
return True;
}
return False;
}
static void
interaggregate_free (Display *dpy, Window window, void *closure)
{
struct state *st = (struct state *) closure;
if (st->f) {
free_circles (st->f);
if (st->f->off_img) free (st->f->off_img);
if (st->f->parsedcolors) free (st->f->parsedcolors);
free (st->f);
}
if (st->fgc) XFreeGC (st->dpy, st->fgc);
free (st);
}
XSCREENSAVER_MODULE ("Interaggregate", interaggregate)
|