From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[xscreensaver] / hacks / triangle.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* triangle --- create a triangle-mountain */
3
4 #if 0
5 static const char sccsid[] = "@(#)triangle.c    4.04 97/07/28 xlockmore";
6 #endif
7
8 /*-
9  * Copyright (c) 1995 by Tobias Gloth
10  *
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation for any purpose and without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and that
14  * both that copyright notice and this permission notice appear in
15  * supporting documentation.
16  *
17  * This file is provided AS IS with no warranties of any kind.  The author
18  * shall have no liability with respect to the infringement of copyrights,
19  * trade secrets or any patents by this file or any part thereof.  In no
20  * event will the author be liable for any lost revenue or profits or
21  * other special, indirect and consequential damages.
22  *
23  * Revision History:
24  * 10-May-97: Compatible with xscreensaver
25  * 10-Mar-96: re-arranged and re-formatted the code for appearance and
26  *            to make common subroutines.  Simplified.
27  *                Ron Hitchens <ron@idiom.com>
28  * 07-Mar-96: Removed internal delay code, set MI_PAUSE(mi) for inter-scene
29  *            delays.  No other delays are needed here.
30  *            Made pause time sensitive to value of cycles (in 10ths of a
31  *            second).  Removed (hopefully) all references to globals.
32  *                Ron Hitchens <ron@idiom.com>
33  * 27-Feb-96: Undid the changes listed below.  Added ModeInfo argument.
34  *                Implemented delay between scenes using the MI_PAUSE(mi)
35  *            scheme.  Ron Hitchens <ron@idiom.com>
36  * 27-Dec-95: Ron Hitchens <ron@idiom.com>
37  *            Modified logic of draw_triangle() to provide a delay
38  *            (sensitive to the value of cycles) between each iteration.
39  *            Because this mode is so compute intensive, when the new
40  *            event loop adjusted the delay to compensate, this mode had
41  *            almost no delay time left.  This change pauses between each
42  *            new landscape, but could still be done better (it is not
43  *            sensitive to input events while drawing, for example).
44  * 03-Nov-95: Many changes (hopefully some good ones) by David Bagley
45  * 01-Oct-95: Written by Tobias Gloth
46  */
47
48 #ifdef STANDALONE
49 # define DEFAULTS       "*delay: 10000 \n"      \
50                                         "*ncolors: 128 \n" \
51                                         "*fpsSolid: true \n" \
52
53 # define SMOOTH_COLORS
54 # define free_triangle 0
55 # define release_triangle 0
56 # define reshape_triangle 0
57 # define triangle_handle_event 0
58 # include "xlockmore.h"         /* in xscreensaver distribution */
59 #else /* STANDALONE */
60 # include "xlock.h"                     /* in xlockmore distribution */
61 #endif /* STANDALONE */
62
63 ENTRYPOINT ModeSpecOpt triangle_opts =
64 {0, NULL, 0, NULL, NULL};
65
66 #define MAX_STEPS 8
67 #define MAX_SIZE  (1<<MAX_STEPS)
68 #define MAX_LEVELS 1000
69
70 #undef TOP  /* FTSO AIX */
71
72 #define DELTA  0.4
73 #define LEFT   (-0.25)
74 #define RIGHT  1.25
75 #define TOP    0.3
76 #define BOTTOM 1.0
77 #define BLUE   45               /* Just the right shade of blue */
78
79 #define BACKFACE_REMOVAL
80
81 #define DISPLACE(h,d) ((h)/2+LRAND()/(MAXRAND/(2*(d)+1))-d)
82
83 typedef struct {
84         int         width;
85         int         height;
86         int         size;
87         int         steps;
88         int         stage;
89         int         init_now;
90         int         fast;
91         int         i;
92         int         j;
93         int         d;
94         short       level[MAX_LEVELS];
95         int         xpos[2 * MAX_SIZE + 1];
96         int         ypos[MAX_SIZE + 1];
97         short       H[(MAX_SIZE + 1) * (MAX_SIZE + 2) / 2];
98         short      *h[MAX_SIZE + 1];
99         short       delta[MAX_STEPS];
100 } trianglestruct;
101
102 static trianglestruct *triangles = NULL;
103
104 static
105 void
106 draw_atriangle(ModeInfo * mi, XPoint * p, int y_0, int y_1, int y_2, double dinv)
107 {
108         Display    *display = MI_DISPLAY(mi);
109         Window      window = MI_WINDOW(mi);
110         GC          gc = MI_GC(mi);
111
112         if (MI_NCOLORS(mi) > 2) {       /* color */
113                 int         dmax, dmin;
114                 long        color;
115
116                 dmin = MIN(y_0, y_1);
117                 dmin = MIN(dmin, y_2);
118                 dmax = MAX(y_0, y_1);
119                 dmax = MAX(dmax, y_2);
120
121                 if (dmax == 0) {
122                         color = BLUE;
123                 } else {
124                         color = MI_NCOLORS(mi) -
125                                 (int) ((double) MI_NCOLORS(mi) / M_PI_2 * atan(dinv * (dmax - dmin)));
126                 }
127
128                 XSetForeground(display, gc, mi->colors[color % MI_NCOLORS(mi)].pixel);
129                 XFillPolygon(display, window, gc, p, 3, Convex, CoordModeOrigin);
130         } else {
131                 /* mono */
132 #ifdef BACKFACE_REMOVAL
133                 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
134                 XFillPolygon(display, window, gc, p, 3, Convex, CoordModeOrigin);
135 #endif
136                 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
137                 XDrawLine(display, window, gc, p[0].x, p[0].y, p[1].x, p[1].y);
138                 XDrawLine(display, window, gc, p[1].x, p[1].y, p[2].x, p[2].y);
139                 XDrawLine(display, window, gc, p[2].x, p[2].y, p[0].x, p[0].y);
140         }
141 }
142
143 static
144 void
145 calc_points1(trianglestruct * tp, int d, int *y0_p, int *y1_p, int *y2_p, XPoint * p)
146 {
147         *y0_p = tp->level[MAX(tp->h[tp->i][tp->j], 0)];
148         *y1_p = tp->level[MAX(tp->h[tp->i + d][tp->j], 0)];
149         *y2_p = tp->level[MAX(tp->h[tp->i][tp->j + d], 0)];
150
151         p[0].x = tp->xpos[2 * tp->i + tp->j];
152         p[1].x = tp->xpos[2 * (tp->i + d) + tp->j];
153         p[2].x = tp->xpos[2 * tp->i + (tp->j + d)];
154
155         p[0].y = tp->ypos[tp->j] - *y0_p;
156         p[1].y = tp->ypos[tp->j] - *y1_p;
157         p[2].y = tp->ypos[tp->j + d] - *y2_p;
158 }
159
160 static
161 void
162 calc_points2(trianglestruct * tp, int d, int *y0_p, int *y1_p, int *y2_p, XPoint * p)
163 {
164         *y0_p = tp->level[MAX(tp->h[tp->i + d][tp->j], 0)];
165         *y1_p = tp->level[MAX(tp->h[tp->i + d][tp->j + d], 0)];
166         *y2_p = tp->level[MAX(tp->h[tp->i][tp->j + d], 0)];
167
168         p[0].x = tp->xpos[2 * (tp->i + d) + tp->j];
169         p[1].x = tp->xpos[2 * (tp->i + d) + (tp->j + d)];
170         p[2].x = tp->xpos[2 * tp->i + (tp->j + d)];
171
172         p[0].y = tp->ypos[tp->j] - *y0_p;
173         p[1].y = tp->ypos[tp->j + d] - *y1_p;
174         p[2].y = tp->ypos[tp->j + d] - *y2_p;
175 }
176
177
178 static
179 void
180 draw_mesh(ModeInfo * mi, trianglestruct * tp, int d, int count)
181 {
182         XPoint      p[3];
183         int         first = 1;
184         int         y_0, y_1, y_2;
185         double      dinv = 0.2 / d;
186
187         if ((tp->j == 0) && (tp->i == 0)) {
188 #if 0 /* jwz */
189                 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
190 #else
191                 {
192                   int x = 0;
193                   int y = 0;
194                   int x2 = MI_WIN_WIDTH(mi);
195                   int y2 = tp->ypos[0];
196                   XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
197                   XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
198                                                  x, y, x2, y2);
199                 }
200 #endif
201         }
202         for (; (tp->j < tp->size) && (count > 0); tp->j += ((count) ? d : 0)) {
203                 for (tp->i = (first) ? tp->i : 0, first = 0;
204                      (tp->i < MAX_SIZE - tp->j) && (count > 0);
205                      tp->i += d, count--) {
206                         if (tp->i + tp->j < tp->size) {
207                                 calc_points1(tp, d, &y_0, &y_1, &y_2, p);
208                                 draw_atriangle(mi, p, y_0, y_1, y_2, dinv);
209                         }
210                         if (tp->i + tp->j + d < tp->size) {
211                                 calc_points2(tp, d, &y_0, &y_1, &y_2, p);
212                                 draw_atriangle(mi, p, y_0, y_1, y_2, dinv);
213                         }
214                 }
215         }
216
217         if (tp->j == tp->size) {
218                 tp->init_now = 1;
219         }
220 }
221
222 ENTRYPOINT void
223 init_triangle (ModeInfo * mi)
224 {
225         trianglestruct *tp;
226         short      *tmp;
227         int         i, dim, one;
228
229         MI_INIT (mi, triangles);
230         tp = &triangles[MI_SCREEN(mi)];
231
232         tp->width = MI_WIN_WIDTH(mi);
233         tp->height = MI_WIN_HEIGHT(mi);
234         tp->init_now = 1;
235         tp->fast = 2;
236
237         XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
238
239
240
241         tp->steps = MAX_STEPS;
242         do {
243                 tp->size = 1 << --tp->steps;
244         } while (tp->size * 5 > tp->width);
245         tmp = tp->H;
246         for (i = 0; i < tp->size + 1; i++) {
247                 tp->h[i] = tmp;
248                 tmp += (tp->size) + 1 - i;
249         }
250
251         tp->stage = -1;
252         dim = MIN(tp->width, tp->height);
253
254         for (i = 0; i < 2 * tp->size + 1; i++) {
255                 tp->xpos[i] = (short) ((((double) i)
256                          / ((double) (2 * tp->size)) * (RIGHT - LEFT) + LEFT)
257                                        * dim) + (tp->width - dim) / 2;
258         }
259
260         for (i = 0; i < (tp->size + 1); i++) {
261                 tp->ypos[i] = (short) ((((double) i)
262                          / ((double) tp->size) * (BOTTOM - TOP) + TOP) * dim)
263                         + (tp->height - dim) / 2;
264         }
265
266         for (i = 0; i < tp->steps; i++) {
267                 tp->delta[i] = ((short) (DELTA * dim)) >> i;
268         }
269
270         one = tp->delta[0];
271
272         if (one > 0)
273                 for (i = 0; i < MAX_LEVELS; i++) {
274                         tp->level[i] = (i * i) / one;
275                 }
276 }
277
278 ENTRYPOINT void
279 draw_triangle (ModeInfo * mi)
280 {
281         trianglestruct *tp = &triangles[MI_SCREEN(mi)];
282         int         d, d2, i, j, delta;
283
284         if (!tp->init_now) {
285                 draw_mesh(mi, tp, tp->d / 2, MAX_SIZE / tp->d);
286
287                 /* The init_now flag will pop up when the scene is complete.
288                  * Cycles specifies how long to wait, in 1/10 secs.
289                  TODO: This is wrong for multi-screens ***
290                  */
291                 if (tp->init_now) {
292 #ifndef STANDALONE
293                         MI_PAUSE(mi) = 2000000;
294 #else
295                         if (tp->stage == -1)
296                           {
297                                 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
298                                 if (!mono_p)
299                                   {
300                                         free_colors(mi->xgwa.screen, mi->xgwa.colormap, mi->colors,
301                                                                 mi->npixels);
302                     mi->npixels = 
303                       get_integer_resource (mi->dpy, "ncolors", "Integer");
304                                         make_smooth_colormap (mi->xgwa.screen,
305                                                                                   mi->xgwa.visual, mi->xgwa.colormap,
306                                                                                   mi->colors, &mi->npixels,
307                                                                                   True, &mi->writable_p, True);
308                                   }
309                           }
310 #endif
311                 }
312                 return;
313         }
314         if (tp->delta[0] > 0) {
315                 if (!(++tp->stage)) {
316                         tp->h[0][0] = (short int) MAX(0, DISPLACE(0, tp->delta[0]));
317                         tp->h[tp->size][0] = (short int) MAX(0, DISPLACE(0, tp->delta[0]));
318                         tp->h[0][tp->size] = (short int) MAX(0, DISPLACE(0, tp->delta[0]));
319                 } else {
320                         d = 2 << (tp->steps - tp->stage);
321                         d2 = d / 2;
322                         delta = tp->delta[tp->stage - 1];
323
324                         for (i = 0; i < tp->size; i += d) {
325                                 for (j = 0; j < (tp->size - i); j += d) {
326                                         tp->h[i + d2][j] = (short int) DISPLACE(tp->h[i][j] +
327                                                      tp->h[i + d][j], delta);
328                                         tp->h[i][j + d2] = (short int) DISPLACE(tp->h[i][j] +
329                                                      tp->h[i][j + d], delta);
330                                         tp->h[i + d2][j + d2] = (short int) DISPLACE(tp->h[i + d][j] +
331                                                      tp->h[i][j + d], delta);
332                                 }
333
334                                 tp->init_now = 0;
335                                 tp->i = 0;
336                                 tp->j = 0;
337                                 tp->d = d;
338                         }
339                 }
340         }
341         if (tp->stage == tp->steps) {
342                 tp->stage = -1;
343         }
344 }
345
346 #ifndef STANDALONE
347 ENTRYPOINT void
348 refresh_triangle (ModeInfo * mi)
349 {
350         /* Do nothing, it will refresh by itself */
351 }
352 #endif
353
354 XSCREENSAVER_MODULE ("Triangle", triangle)