1 /* xscreensaver, Copyright (c) 1992, 1995, 1996, 1997
2 * Jamie Zawinski <jwz@netscape.com>
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
13 /* Flying through an asteroid field. Based on TI Explorer Lisp code by
14 John Nguyen <johnn@hx.lcs.mit.edu>
19 #include "screenhack.h"
22 #define MIN_DEPTH 2 /* rocks disappear when they get this close */
23 #define MAX_DEPTH 60 /* this is where rocks appear */
24 #define MIN_SIZE 3 /* how small where pixmaps are not used */
25 #define MAX_SIZE 200 /* how big (in pixels) rocks are at depth 1 */
26 #define DEPTH_SCALE 100 /* how many ticks there are between depths */
27 #define SIN_RESOLUTION 1000
29 #define MAX_DEP 0.3 /* how far the displacement can be (percent) */
30 #define DIRECTION_CHANGE_RATE 60
31 #define MAX_DEP_SPEED 5 /* Maximum speed for movement */
32 #define MOVE_STYLE 0 /* Only 0 and 1. Distinguishes the fact that
33 these are the rocks that are moving (1)
34 or the rocks source (0). */
36 /* there's not much point in the above being user-customizable, but those
37 numbers might want to be tweaked for displays with an order of magnitude
38 higher resolution or compute power.
41 static double sins [SIN_RESOLUTION];
42 static double coss [SIN_RESOLUTION];
43 static double depths [(MAX_DEPTH + 1) * DEPTH_SCALE];
47 static int width, height, midx, midy;
48 static int dep_x, dep_y;
50 static GC draw_gc, erase_gc;
55 static GC threed_left_gc, threed_right_gc;
56 static double threed_delta;
59 (threed_delta * 40.0 * \
60 (1.0 - ((MAX_DEPTH * DEPTH_SCALE / 2) / \
61 ((z) + 20.0 * DEPTH_SCALE))))
72 static struct rock *rocks;
74 static Pixmap pixmaps [MAX_SIZE];
77 static void rock_compute (struct rock *);
78 static void rock_draw (struct rock *, Bool draw_p);
81 rock_reset (struct rock *rock)
83 rock->real_size = MAX_SIZE;
84 rock->r = (SIN_RESOLUTION * 0.7) + (random () % (30 * SIN_RESOLUTION));
85 rock->theta = random () % SIN_RESOLUTION;
86 rock->depth = MAX_DEPTH * DEPTH_SCALE;
88 rock_draw (rock, True);
92 rock_tick (struct rock *rock, int d)
96 rock_draw (rock, False);
100 rock->theta = (rock->theta + d) % SIN_RESOLUTION;
102 while (rock->theta < 0)
103 rock->theta += SIN_RESOLUTION;
104 if (rock->depth < (MIN_DEPTH * DEPTH_SCALE))
109 rock_draw (rock, True);
112 else if ((random () % 40) == 0)
117 rock_compute (struct rock *rock)
119 double factor = depths [rock->depth];
120 double rsize = rock->real_size * factor;
122 rock->size = (int) (rsize + 0.5);
123 rock->diff = (int) GETZDIFF(rock->depth);
124 rock->x = midx + (coss [rock->theta] * rock->r * factor);
125 rock->y = midy + (sins [rock->theta] * rock->r * factor);
129 double move_factor = (((double) MOVE_STYLE) -
130 (((double) rock->depth) /
131 (((double) (MAX_DEPTH + 1)) *
132 ((double) DEPTH_SCALE))));
133 /* move_factor is 0 when the rock is close, 1 when far */
134 rock->x += (((double) dep_x) * move_factor);
135 rock->y += (((double) dep_y) * move_factor);
140 rock_draw (rock, draw_p)
145 ? (threed ? erase_gc : draw_gc)
148 if (rock->x <= 0 || rock->y <= 0 || rock->x >= width || rock->y >= height)
150 /* this means that if a rock were to go off the screen at 12:00, but
151 would have been visible at 3:00, it won't come back once the observer
152 rotates around so that the rock would have been visible again.
163 if (draw_p) gc = threed_left_gc;
164 XDrawPoint (dpy, window, gc, rock->x - rock->diff, rock->y);
165 if (draw_p) gc = threed_right_gc;
166 XDrawPoint (dpy, window, gc, rock->x + rock->diff, rock->y);
170 XDrawPoint (dpy, window, gc, rock->x, rock->y);
173 else if (rock->size <= MIN_SIZE || !draw_p)
177 if (draw_p) gc = threed_left_gc;
178 XFillRectangle(dpy, window, gc,
179 rock->x - rock->size / 2 - rock->diff,
180 rock->y - rock->size / 2,
181 rock->size, rock->size);
182 if (draw_p) gc = threed_right_gc;
183 XFillRectangle(dpy, window, gc,
184 rock->x - rock->size / 2 + rock->diff,
185 rock->y - rock->size / 2,
186 rock->size, rock->size);
190 XFillRectangle (dpy, window, gc,
191 rock->x - rock->size/2, rock->y - rock->size/2,
192 rock->size, rock->size);
195 else if (rock->size < MAX_SIZE)
200 XCopyPlane(dpy, pixmaps[rock->size], window, gc,
201 0, 0, rock->size, rock->size,
202 rock->x - rock->size / 2 - rock->diff,
203 rock->y - rock->size / 2, 1L);
204 gc = threed_right_gc;
205 XCopyPlane(dpy, pixmaps[rock->size], window, gc,
206 0, 0, rock->size, rock->size,
207 rock->x - rock->size / 2 + rock->diff,
208 rock->y - rock->size / 2, 1L);
212 XCopyPlane (dpy, pixmaps [rock->size], window, gc,
213 0, 0, rock->size, rock->size,
214 rock->x - rock->size/2, rock->y - rock->size/2,
222 init_pixmaps (Display *dpy, Window window)
226 GC fg_gc = 0, bg_gc = 0;
227 pixmaps [0] = pixmaps [1] = 0;
228 for (i = MIN_DEPTH; i < MAX_SIZE; i++)
230 int w = (1+(i/32))<<5; /* server might be faster if word-aligned */
232 Pixmap p = XCreatePixmap (dpy, window, w, h, 1);
237 fprintf (stderr, "%s: couldn't allocate pixmaps", progname);
241 { /* must use drawable of pixmap, not window (fmh) */
243 fg_gc = XCreateGC (dpy, p, GCForeground, &gcv);
245 bg_gc = XCreateGC (dpy, p, GCForeground, &gcv);
247 XFillRectangle (dpy, p, bg_gc, 0, 0, w, h);
248 points [0].x = i * 0.15; points [0].y = i * 0.85;
249 points [1].x = i * 0.00; points [1].y = i * 0.20;
250 points [2].x = i * 0.30; points [2].y = i * 0.00;
251 points [3].x = i * 0.40; points [3].y = i * 0.10;
252 points [4].x = i * 0.90; points [4].y = i * 0.10;
253 points [5].x = i * 1.00; points [5].y = i * 0.55;
254 points [6].x = i * 0.45; points [6].y = i * 1.00;
255 XFillPolygon (dpy, p, fg_gc, points, 7, Nonconvex, CoordModeOrigin);
257 XFreeGC (dpy, fg_gc);
258 XFreeGC (dpy, bg_gc);
263 compute_move(int axe) /* 0 for x, 1 for y */
265 static int current_dep[2] = {0, 0};
266 static int speed[2] = {0, 0};
267 static short direction[2] = {0, 0};
268 static int limit[2] = {0, 0};
274 current_dep[axe] += speed[axe]; /* We adjust the displacement */
276 if (current_dep[axe] > (int) (limit[axe] * max_dep))
278 if (current_dep[axe] > limit[axe])
279 current_dep[axe] = limit[axe];
281 } /* This is when we reach the upper screen limit */
282 if (current_dep[axe] < (int) (-limit[axe] * max_dep))
284 if (current_dep[axe] < -limit[axe])
285 current_dep[axe] = -limit[axe];
287 } /* This is when we reach the lower screen limit */
288 if (direction[axe] == 1) /* We adjust the speed */
290 else if (direction[axe] == -1)
293 if (speed[axe] > MAX_DEP_SPEED)
294 speed[axe] = MAX_DEP_SPEED;
295 else if (speed[axe] < -MAX_DEP_SPEED)
296 speed[axe] = -MAX_DEP_SPEED;
298 if (move_p && !(random() % DIRECTION_CHANGE_RATE))
300 /* We change direction */
301 change = random() & 1;
304 if (direction[axe] == 0)
305 direction[axe] = change - 1; /* 0 becomes either 1 or -1 */
307 direction[axe] = 0; /* -1 or 1 become 0 */
310 return (current_dep[axe]);
320 dep_x = compute_move(0);
321 dep_y = compute_move(1);
324 for (i = 0; i < nrocks; i++)
325 rock_tick (&rocks [i], d);
332 static int current_delta = 0; /* observer Z rotation */
333 static int window_tick = 50;
334 static int new_delta = 0;
335 static int dchange_tick = 0;
337 if (window_tick++ == 50)
339 XWindowAttributes xgwa;
340 XGetWindowAttributes (dpy, window, &xgwa);
343 height = xgwa.height;
348 if (current_delta != new_delta)
350 if (dchange_tick++ == 5)
353 if (current_delta < new_delta)
361 if (! (random() % 50))
363 new_delta = ((random() % 11) - 5);
364 if (! (random() % 10))
368 tick_rocks (current_delta);
372 init_rocks (Display *d, Window w)
377 XWindowAttributes xgwa;
381 XGetWindowAttributes (dpy, window, &xgwa);
382 cmap = xgwa.colormap;
383 delay = get_integer_resource ("delay", "Integer");
384 if (delay < 0) delay = 0;
385 speed = get_integer_resource ("speed", "Integer");
386 if (speed < 1) speed = 1;
387 if (speed > 100) speed = 100;
388 rotate_p = get_boolean_resource ("rotate", "Boolean");
389 move_p = get_boolean_resource ("move", "Boolean");
390 fg = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
391 bg = get_pixel_resource ("background", "Background", dpy, cmap);
394 draw_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
397 erase_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
399 max_dep = (move_p ? MAX_DEP : 0);
401 for (i = 0; i < SIN_RESOLUTION; i++)
403 sins [i] = sin ((((double) i) / (SIN_RESOLUTION / 2)) * M_PI);
404 coss [i] = cos ((((double) i) / (SIN_RESOLUTION / 2)) * M_PI);
406 /* we actually only need i/speed of these, but wtf */
407 for (i = 1; i < (sizeof (depths) / sizeof (depths[0])); i++)
408 depths [i] = atan (((double) 0.5) / (((double) i) / DEPTH_SCALE));
409 depths [0] = M_PI/2; /* avoid division by 0 */
411 threed = get_boolean_resource("use3d", "Boolean");
415 gcv.foreground = get_pixel_resource ("left3d", "Foreground", dpy, cmap);
416 threed_left_gc = XCreateGC (dpy, window, GCForeground|GCBackground,&gcv);
417 gcv.foreground = get_pixel_resource ("right3d", "Foreground", dpy, cmap);
418 threed_right_gc = XCreateGC (dpy, window,GCForeground|GCBackground,&gcv);
419 threed_delta = get_float_resource("delta3d", "Integer");
422 /* don't want any exposure events from XCopyPlane */
423 XSetGraphicsExposures (dpy, draw_gc, False);
424 XSetGraphicsExposures (dpy, erase_gc, False);
426 nrocks = get_integer_resource ("count", "Count");
427 if (nrocks < 1) nrocks = 1;
428 rocks = (struct rock *) calloc (nrocks, sizeof (struct rock));
429 init_pixmaps (dpy, window);
430 XClearWindow (dpy, window);
435 char *progclass = "Rocks";
437 char *defaults [] = {
438 "Rocks.background: Black", /* to placate SGI */
439 "Rocks.foreground: #E9967A",
452 XrmOptionDescRec options [] = {
453 { "-count", ".count", XrmoptionSepArg, 0 },
454 { "-rotate", ".rotate", XrmoptionNoArg, "true" },
455 { "-norotate", ".rotate", XrmoptionNoArg, "false" },
456 { "-move", ".move", XrmoptionNoArg, "true" },
457 { "-nomove", ".move", XrmoptionNoArg, "false" },
458 { "-delay", ".delay", XrmoptionSepArg, 0 },
459 { "-speed", ".speed", XrmoptionSepArg, 0 },
460 {"-3d", ".use3d", XrmoptionNoArg, "True"},
461 {"-no-3d", ".use3d", XrmoptionNoArg, "False"},
462 {"-left3d", ".left3d", XrmoptionSepArg, 0 },
463 {"-right3d", ".right3d", XrmoptionSepArg, 0 },
464 {"-delta3d", ".delta3d", XrmoptionSepArg, 0 },
469 screenhack (Display *dpy, Window window)
471 init_rocks (dpy, window);
476 if (delay) usleep (delay);