http://ftp.x.org/contrib/applications/xscreensaver-2.23.tar.gz
[xscreensaver] / hacks / hypercube.c
1 /* xscreensaver, Copyright (c) 1992, 1995, 1996
2  *  Jamie Zawinski <jwz@netscape.com>
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  * This code derived from TI Explorer Lisp code by Joe Keane, Fritz Mueller,
13  * and Jamie Zawinski.
14  */
15
16 #include <math.h>
17 #include "screenhack.h"
18
19 static Display *dpy;
20 static Window window;
21 static GC color0, color1, color2, color3, color4, color5, color6, color7;
22 static GC black;
23
24 static int delay;
25
26 static int observer_z;
27 static int x_offset, y_offset;
28 static int unit_pixels;
29
30 struct point_state {
31   int old_x, old_y;
32   int new_x, new_y;
33   Bool same_p;
34 };
35
36 static void
37 move_line (struct point_state *state0, struct point_state *state1, GC gc)
38 {
39   if (state0->same_p && state1->same_p)
40     return;
41   if (mono_p)
42     {
43       XDrawLine (dpy, window, black,
44                  state0->old_x, state0->old_y, state1->old_x, state1->old_y);
45       XDrawLine (dpy, window, gc,
46                  state0->new_x, state0->new_y, state1->new_x, state1->new_y);
47     }
48   else
49     {
50       XSegment segments [2];
51       segments [0].x1 = state0->old_x; segments [0].y1 = state0->old_y;
52       segments [0].x2 = state1->old_x; segments [0].y2 = state1->old_y;
53       segments [1].x1 = state0->new_x; segments [1].y1 = state0->new_y;
54       segments [1].x2 = state1->new_x; segments [1].y2 = state1->new_y;
55       XDrawSegments (dpy, window, gc, segments, 2);
56     }
57 }
58
59 static void
60 hyper (double xy, double xz, double yz, double xw, double yw, double zw)
61 {
62   double cos_xy = cos (xy), sin_xy = sin (xy);
63   double cos_xz = cos (xz), sin_xz = sin (xz);
64   double cos_yz = cos (yz), sin_yz = sin (yz);
65   double cos_xw = cos (xw), sin_xw = sin (xw);
66   double cos_yw = cos (yw), sin_yw = sin (yw);
67   double cos_zw = cos (zw), sin_zw = sin (zw);
68
69   double ax = 1.0, ay = 0.0, az = 0.0, aw = 0.0;
70   double bx = 0.0, by = 1.0, bz = 0.0, bw = 0.0;
71   double cx = 0.0, cy = 0.0, cz = 1.0, cw = 0.0;
72   double dx = 0.0, dy = 0.0, dz = 0.0, dw = 1.0;
73
74   double _tmp0_, _tmp1_;
75
76   struct point_state points [16];
77   memset (points, 0, sizeof (points));
78
79 #define mmmm (&points[0])
80 #define mmmp (&points[1])
81 #define mmpm (&points[2])
82 #define mmpp (&points[3])
83 #define mpmm (&points[4])
84 #define mpmp (&points[5])
85 #define mppm (&points[6])
86 #define mppp (&points[7])
87 #define pmmm (&points[8])
88 #define pmmp (&points[9])
89 #define pmpm (&points[10])
90 #define pmpp (&points[11])
91 #define ppmm (&points[12])
92 #define ppmp (&points[13])
93 #define pppm (&points[14])
94 #define pppp (&points[15])
95
96   while (1)
97     {
98       double temp_mult;
99
100 #define compute(a,b,c,d,point_state) \
101       temp_mult = (unit_pixels / (((a*az) + (b*bz) + (c*cz) + (d*dz) +    \
102                                    (a*aw) + (b*bw) + (c*cw) + (d*dw))     \
103                                   - observer_z));                         \
104   point_state->old_x = point_state->new_x;                                \
105   point_state->old_y = point_state->new_y;                                \
106   point_state->new_x = ((((a*ax) + (b*bx) + (c*cx) + (d*dx)) * temp_mult) \
107                         + x_offset);                                      \
108   point_state->new_y = ((((a*ay) + (b*by) + (c*cy) + (d*dy)) * temp_mult) \
109                         + y_offset);                                      \
110   point_state->same_p = (point_state->old_x == point_state->new_x &&      \
111                          point_state->old_y == point_state->new_y);
112
113       compute (-1, -1, -1, -1, mmmm);
114       compute (-1, -1, -1,  1, mmmp);
115       compute (-1, -1,  1, -1, mmpm);
116       compute (-1, -1,  1,  1, mmpp);
117       compute (-1,  1, -1, -1, mpmm);
118       compute (-1,  1, -1,  1, mpmp);
119       compute (-1,  1,  1, -1, mppm);
120       compute (-1,  1,  1,  1, mppp);
121       compute ( 1, -1, -1, -1, pmmm);
122       compute ( 1, -1, -1,  1, pmmp);
123       compute ( 1, -1,  1, -1, pmpm);
124       compute ( 1, -1,  1,  1, pmpp);
125       compute ( 1,  1, -1, -1, ppmm);
126       compute ( 1,  1, -1,  1, ppmp);
127       compute ( 1,  1,  1, -1, pppm);
128       compute ( 1,  1,  1,  1, pppp);
129
130       move_line (mmmm, mmmp, color0);
131       move_line (mmmm, mmpm, color0);
132       move_line (mmpm, mmpp, color0);
133       move_line (mmmp, mmpp, color0);
134       
135       move_line (pmmm, pmmp, color1);
136       move_line (pmmm, pmpm, color1);
137       move_line (pmpm, pmpp, color1);
138       move_line (pmmp, pmpp, color1);
139       
140       move_line (mpmm, mpmp, color2);
141       move_line (mpmm, mppm, color2);
142       move_line (mppm, mppp, color2);
143       move_line (mpmp, mppp, color2);
144       
145       move_line (mmpp, mppp, color3);
146       move_line (mmpp, pmpp, color3);
147       move_line (pmpp, pppp, color3);
148       move_line (mppp, pppp, color3);
149       
150       move_line (mmmm, mpmm, color4);
151       move_line (mmmm, pmmm, color4);
152       move_line (mpmm, ppmm, color4);
153       move_line (pmmm, ppmm, color4);
154       
155       move_line (mmmp, mpmp, color5);
156       move_line (mmmp, pmmp, color5);
157       move_line (pmmp, ppmp, color5);
158       move_line (mpmp, ppmp, color5);
159       
160       move_line (mmpm, mppm, color6);
161       move_line (mmpm, pmpm, color6);
162       move_line (pmpm, pppm, color6);
163       move_line (mppm, pppm, color6);
164       
165       move_line (ppmm, ppmp, color7);
166       move_line (ppmm, pppm, color7);
167       move_line (pppm, pppp, color7);
168       move_line (ppmp, pppp, color7);
169
170  /* If you get error messages about the following forms, and you think you're
171     using an ANSI C conforming compiler, then you're mistaken.  Possibly you're
172     mixing an ANSI compiler with a non-ANSI preprocessor, or vice versa.
173     Regardless, your system is broken; it's not a bug in this program.
174   */
175 #if defined(__STDC__) || defined(__ANSI_CPP__)
176 # define rotate(name,dim0,dim1,cos,sin) \
177       _tmp0_ = ((name##dim0 * cos) + (name##dim1 * sin)); \
178       _tmp1_ = ((name##dim1 * cos) - (name##dim0 * sin)); \
179       name##dim0 = _tmp0_; \
180       name##dim1 = _tmp1_;
181
182 # define rotates(dim0,dim1) \
183       if (sin_##dim0##dim1 != 0) {                                 \
184         rotate(a, dim0, dim1, cos_##dim0##dim1, sin_##dim0##dim1); \
185         rotate(b, dim0, dim1, cos_##dim0##dim1, sin_##dim0##dim1); \
186         rotate(c, dim0, dim1, cos_##dim0##dim1, sin_##dim0##dim1); \
187         rotate(d, dim0, dim1, cos_##dim0##dim1, sin_##dim0##dim1); \
188       }
189
190 #else /* !__STDC__, courtesy of Andreas Luik <luik@isa.de> */
191 # define rotate(name,dim0,dim1,cos,sin) \
192       _tmp0_ = ((name/**/dim0 * cos) + (name/**/dim1 * sin)); \
193       _tmp1_ = ((name/**/dim1 * cos) - (name/**/dim0 * sin)); \
194       name/**/dim0 = _tmp0_; \
195       name/**/dim1 = _tmp1_;
196
197 # define rotates(dim0,dim1) \
198       if (sin_/**/dim0/**/dim1 != 0) {                                 \
199         rotate(a,dim0,dim1,cos_/**/dim0/**/dim1,sin_/**/dim0/**/dim1); \
200         rotate(b,dim0,dim1,cos_/**/dim0/**/dim1,sin_/**/dim0/**/dim1); \
201         rotate(c,dim0,dim1,cos_/**/dim0/**/dim1,sin_/**/dim0/**/dim1); \
202         rotate(d,dim0,dim1,cos_/**/dim0/**/dim1,sin_/**/dim0/**/dim1); \
203       }
204 #endif /* !__STDC__ */
205
206       rotates (x,y);
207       rotates (x,z);
208       rotates (y,z);
209       rotates (x,w);
210       rotates (y,w);
211       rotates (z,w);
212
213       XSync (dpy, True);
214       if (delay) usleep (delay);
215     }
216 }
217
218 \f
219 char *progclass = "Hypercube";
220
221 char *defaults [] = {
222   ".background: black",
223   ".foreground: white",
224   "*color0:     red",
225   "*color1:     orange",
226   "*color2:     yellow",
227   "*color3:     white",
228   "*color4:     green",
229   "*color5:     cyan",
230   "*color6:     dodgerblue",
231   "*color7:     magenta",
232
233   "*xw:         0.000",
234   "*xy:         0.010",
235   "*xz:         0.005",
236   "*yw:         0.010",
237   "*yz:         0.000",
238   "*zw:         0.000",
239
240   "*observer-z: 5",
241   "*delay:      30000",
242   0
243 };
244
245 XrmOptionDescRec options [] = {
246   { "-color0",          ".color0",      XrmoptionSepArg, 0 },
247   { "-color1",          ".color1",      XrmoptionSepArg, 0 },
248   { "-color2",          ".color2",      XrmoptionSepArg, 0 },
249   { "-color3",          ".color3",      XrmoptionSepArg, 0 },
250   { "-color4",          ".color4",      XrmoptionSepArg, 0 },
251   { "-color5",          ".color5",      XrmoptionSepArg, 0 },
252   { "-color6",          ".color6",      XrmoptionSepArg, 0 },
253   { "-color7",          ".color7",      XrmoptionSepArg, 0 },
254
255   { "-xw",              ".xw",          XrmoptionSepArg, 0 },
256   { "-xy",              ".xy",          XrmoptionSepArg, 0 },
257   { "-xz",              ".xz",          XrmoptionSepArg, 0 },
258   { "-yw",              ".yw",          XrmoptionSepArg, 0 },
259   { "-yz",              ".yz",          XrmoptionSepArg, 0 },
260   { "-zw",              ".zw",          XrmoptionSepArg, 0 },
261
262   { "-observer-z",      ".observer-z",  XrmoptionSepArg, 0 },
263   { "-delay",           ".delay",       XrmoptionSepArg, 0 },
264   { 0, 0, 0, 0 }
265 };
266
267 void
268 screenhack (Display *d, Window w)
269 {
270   XGCValues gcv;
271   XWindowAttributes xgwa;
272   Colormap cmap;
273   double xy, xz, yz, xw, yw, zw;
274   unsigned long bg;
275
276   dpy = d;
277   window = w;
278   XGetWindowAttributes (dpy, window, &xgwa);
279   cmap = xgwa.colormap;
280
281   x_offset = xgwa.width / 2;
282   y_offset = xgwa.height / 2;
283   unit_pixels = xgwa.width < xgwa.height ? xgwa.width : xgwa.height;
284
285   xy = get_float_resource ("xy", "Float");
286   xz = get_float_resource ("xz", "Float");
287   yz = get_float_resource ("yz", "Float");
288   xw = get_float_resource ("xw", "Float");
289   yw = get_float_resource ("yw", "Float");
290   zw = get_float_resource ("zw", "Float");
291
292   observer_z = get_integer_resource ("observer-z", "Integer");
293
294   delay = get_integer_resource ("delay", "Integer");
295
296   bg = get_pixel_resource ("background", "Background", dpy, cmap);
297
298   if (mono_p)
299     {
300       gcv.function = GXcopy;
301       gcv.foreground = bg;
302       black = XCreateGC (dpy, window, GCForeground|GCFunction, &gcv);
303       gcv.foreground = get_pixel_resource ("foreground", "Foreground",
304                                            dpy, cmap);
305       color0 = color1 = color2 = color3 = color4 = color5 = color6 = color7 =
306         XCreateGC (dpy, window, GCForeground|GCFunction, &gcv);
307     }
308   else
309     {
310       black = 0;
311       gcv.function = GXxor;
312 #define make_gc(color,name) \
313         gcv.foreground = bg ^ get_pixel_resource ((name), "Foreground", \
314                                                   dpy, cmap);           \
315         color = XCreateGC (dpy, window, GCForeground|GCFunction, &gcv)
316
317       make_gc (color0,"color0");
318       make_gc (color1,"color1");
319       make_gc (color2,"color2");
320       make_gc (color3,"color3");
321       make_gc (color4,"color4");
322       make_gc (color5,"color5");
323       make_gc (color6,"color6");
324       make_gc (color7,"color7");
325     }
326
327   hyper (xy, xz, yz, xw, yw, zw);
328 }