From http://www.jwz.org/xscreensaver/xscreensaver-5.37.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 release_triangle 0
55 # include "xlockmore.h"         /* in xscreensaver distribution */
56 #else /* STANDALONE */
57 # include "xlock.h"                     /* in xlockmore distribution */
58 #endif /* STANDALONE */
59
60 ENTRYPOINT ModeSpecOpt triangle_opts =
61 {0, NULL, 0, NULL, NULL};
62
63 #define MAX_STEPS 8
64 #define MAX_SIZE  (1<<MAX_STEPS)
65 #define MAX_LEVELS 1000
66
67 #undef TOP  /* FTSO AIX */
68
69 #define DELTA  0.4
70 #define LEFT   (-0.25)
71 #define RIGHT  1.25
72 #define TOP    0.3
73 #define BOTTOM 1.0
74 #define BLUE   45               /* Just the right shade of blue */
75
76 #define BACKFACE_REMOVAL
77
78 #define DISPLACE(h,d) ((h)/2+LRAND()/(MAXRAND/(2*(d)+1))-d)
79
80 typedef struct {
81         int         width;
82         int         height;
83         int         size;
84         int         steps;
85         int         stage;
86         int         init_now;
87         int         fast;
88         int         i;
89         int         j;
90         int         d;
91         short       level[MAX_LEVELS];
92         int         xpos[2 * MAX_SIZE + 1];
93         int         ypos[MAX_SIZE + 1];
94         short       H[(MAX_SIZE + 1) * (MAX_SIZE + 2) / 2];
95         short      *h[MAX_SIZE + 1];
96         short       delta[MAX_STEPS];
97 } trianglestruct;
98
99 static trianglestruct *triangles = NULL;
100
101 static
102 void
103 draw_atriangle(ModeInfo * mi, XPoint * p, int y_0, int y_1, int y_2, double dinv)
104 {
105         Display    *display = MI_DISPLAY(mi);
106         Window      window = MI_WINDOW(mi);
107         GC          gc = MI_GC(mi);
108
109         if (MI_NCOLORS(mi) > 2) {       /* color */
110                 int         dmax, dmin;
111                 long        color;
112
113                 dmin = MIN(y_0, y_1);
114                 dmin = MIN(dmin, y_2);
115                 dmax = MAX(y_0, y_1);
116                 dmax = MAX(dmax, y_2);
117
118                 if (dmax == 0) {
119                         color = BLUE;
120                 } else {
121                         color = MI_NCOLORS(mi) -
122                                 (int) ((double) MI_NCOLORS(mi) / M_PI_2 * atan(dinv * (dmax - dmin)));
123                 }
124
125                 XSetForeground(display, gc, mi->colors[color % MI_NCOLORS(mi)].pixel);
126                 XFillPolygon(display, window, gc, p, 3, Convex, CoordModeOrigin);
127         } else {
128                 /* mono */
129 #ifdef BACKFACE_REMOVAL
130                 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
131                 XFillPolygon(display, window, gc, p, 3, Convex, CoordModeOrigin);
132 #endif
133                 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
134                 XDrawLine(display, window, gc, p[0].x, p[0].y, p[1].x, p[1].y);
135                 XDrawLine(display, window, gc, p[1].x, p[1].y, p[2].x, p[2].y);
136                 XDrawLine(display, window, gc, p[2].x, p[2].y, p[0].x, p[0].y);
137         }
138 }
139
140 static
141 void
142 calc_points1(trianglestruct * tp, int d, int *y0_p, int *y1_p, int *y2_p, XPoint * p)
143 {
144         *y0_p = tp->level[MAX(tp->h[tp->i][tp->j], 0)];
145         *y1_p = tp->level[MAX(tp->h[tp->i + d][tp->j], 0)];
146         *y2_p = tp->level[MAX(tp->h[tp->i][tp->j + d], 0)];
147
148         p[0].x = tp->xpos[2 * tp->i + tp->j];
149         p[1].x = tp->xpos[2 * (tp->i + d) + tp->j];
150         p[2].x = tp->xpos[2 * tp->i + (tp->j + d)];
151
152         p[0].y = tp->ypos[tp->j] - *y0_p;
153         p[1].y = tp->ypos[tp->j] - *y1_p;
154         p[2].y = tp->ypos[tp->j + d] - *y2_p;
155 }
156
157 static
158 void
159 calc_points2(trianglestruct * tp, int d, int *y0_p, int *y1_p, int *y2_p, XPoint * p)
160 {
161         *y0_p = tp->level[MAX(tp->h[tp->i + d][tp->j], 0)];
162         *y1_p = tp->level[MAX(tp->h[tp->i + d][tp->j + d], 0)];
163         *y2_p = tp->level[MAX(tp->h[tp->i][tp->j + d], 0)];
164
165         p[0].x = tp->xpos[2 * (tp->i + d) + tp->j];
166         p[1].x = tp->xpos[2 * (tp->i + d) + (tp->j + d)];
167         p[2].x = tp->xpos[2 * tp->i + (tp->j + d)];
168
169         p[0].y = tp->ypos[tp->j] - *y0_p;
170         p[1].y = tp->ypos[tp->j + d] - *y1_p;
171         p[2].y = tp->ypos[tp->j + d] - *y2_p;
172 }
173
174
175 static
176 void
177 draw_mesh(ModeInfo * mi, trianglestruct * tp, int d, int count)
178 {
179         XPoint      p[3];
180         int         first = 1;
181         int         y_0, y_1, y_2;
182         double      dinv = 0.2 / d;
183
184         if ((tp->j == 0) && (tp->i == 0)) {
185 #if 0 /* jwz */
186                 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
187 #else
188                 {
189                   int x = 0;
190                   int y = 0;
191                   int x2 = MI_WIN_WIDTH(mi);
192                   int y2 = tp->ypos[0];
193                   XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
194                   XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
195                                                  x, y, x2, y2);
196                 }
197 #endif
198         }
199         for (; (tp->j < tp->size) && (count > 0); tp->j += ((count) ? d : 0)) {
200                 for (tp->i = (first) ? tp->i : 0, first = 0;
201                      (tp->i < MAX_SIZE - tp->j) && (count > 0);
202                      tp->i += d, count--) {
203                         if (tp->i + tp->j < tp->size) {
204                                 calc_points1(tp, d, &y_0, &y_1, &y_2, p);
205                                 draw_atriangle(mi, p, y_0, y_1, y_2, dinv);
206                         }
207                         if (tp->i + tp->j + d < tp->size) {
208                                 calc_points2(tp, d, &y_0, &y_1, &y_2, p);
209                                 draw_atriangle(mi, p, y_0, y_1, y_2, dinv);
210                         }
211                 }
212         }
213
214         if (tp->j == tp->size) {
215                 tp->init_now = 1;
216         }
217 }
218
219 ENTRYPOINT void
220 init_triangle (ModeInfo * mi)
221 {
222         trianglestruct *tp;
223         short      *tmp;
224         int         i, dim, one;
225
226         MI_INIT (mi, triangles, 0);
227         tp = &triangles[MI_SCREEN(mi)];
228
229         tp->width = MI_WIN_WIDTH(mi);
230         tp->height = MI_WIN_HEIGHT(mi);
231         tp->init_now = 1;
232         tp->fast = 2;
233
234         XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
235
236
237
238         tp->steps = MAX_STEPS;
239         do {
240                 tp->size = 1 << --tp->steps;
241         } while (tp->size * 5 > tp->width);
242         tmp = tp->H;
243         for (i = 0; i < tp->size + 1; i++) {
244                 tp->h[i] = tmp;
245                 tmp += (tp->size) + 1 - i;
246         }
247
248         tp->stage = -1;
249         dim = MIN(tp->width, tp->height);
250
251         for (i = 0; i < 2 * tp->size + 1; i++) {
252                 tp->xpos[i] = (short) ((((double) i)
253                          / ((double) (2 * tp->size)) * (RIGHT - LEFT) + LEFT)
254                                        * dim) + (tp->width - dim) / 2;
255         }
256
257         for (i = 0; i < (tp->size + 1); i++) {
258                 tp->ypos[i] = (short) ((((double) i)
259                          / ((double) tp->size) * (BOTTOM - TOP) + TOP) * dim)
260                         + (tp->height - dim) / 2;
261         }
262
263         for (i = 0; i < tp->steps; i++) {
264                 tp->delta[i] = ((short) (DELTA * dim)) >> i;
265         }
266
267         one = tp->delta[0];
268
269         if (one > 0)
270                 for (i = 0; i < MAX_LEVELS; i++) {
271                         tp->level[i] = (i * i) / one;
272                 }
273 }
274
275 ENTRYPOINT void
276 draw_triangle (ModeInfo * mi)
277 {
278         trianglestruct *tp = &triangles[MI_SCREEN(mi)];
279         int         d, d2, i, j, delta;
280
281         if (!tp->init_now) {
282                 draw_mesh(mi, tp, tp->d / 2, MAX_SIZE / tp->d);
283
284                 /* The init_now flag will pop up when the scene is complete.
285                  * Cycles specifies how long to wait, in 1/10 secs.
286                  TODO: This is wrong for multi-screens ***
287                  */
288                 if (tp->init_now) {
289 #ifndef STANDALONE
290                         MI_PAUSE(mi) = 2000000;
291 #else
292                         if (tp->stage == -1)
293                           {
294                                 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
295                                 if (!mono_p)
296                                   {
297                                         free_colors(mi->xgwa.screen, mi->xgwa.colormap, mi->colors,
298                                                                 mi->npixels);
299                     mi->npixels = 
300                       get_integer_resource (mi->dpy, "ncolors", "Integer");
301                                         make_smooth_colormap (mi->xgwa.screen,
302                                                                                   mi->xgwa.visual, mi->xgwa.colormap,
303                                                                                   mi->colors, &mi->npixels,
304                                                                                   True, &mi->writable_p, True);
305                                   }
306                           }
307 #endif
308                 }
309                 return;
310         }
311         if (tp->delta[0] > 0) {
312                 if (!(++tp->stage)) {
313                         tp->h[0][0] = (short int) MAX(0, DISPLACE(0, tp->delta[0]));
314                         tp->h[tp->size][0] = (short int) MAX(0, DISPLACE(0, tp->delta[0]));
315                         tp->h[0][tp->size] = (short int) MAX(0, DISPLACE(0, tp->delta[0]));
316                 } else {
317                         d = 2 << (tp->steps - tp->stage);
318                         d2 = d / 2;
319                         delta = tp->delta[tp->stage - 1];
320
321                         for (i = 0; i < tp->size; i += d) {
322                                 for (j = 0; j < (tp->size - i); j += d) {
323                                         tp->h[i + d2][j] = (short int) DISPLACE(tp->h[i][j] +
324                                                      tp->h[i + d][j], delta);
325                                         tp->h[i][j + d2] = (short int) DISPLACE(tp->h[i][j] +
326                                                      tp->h[i][j + d], delta);
327                                         tp->h[i + d2][j + d2] = (short int) DISPLACE(tp->h[i + d][j] +
328                                                      tp->h[i][j + d], delta);
329                                 }
330
331                                 tp->init_now = 0;
332                                 tp->i = 0;
333                                 tp->j = 0;
334                                 tp->d = d;
335                         }
336                 }
337         }
338         if (tp->stage == tp->steps) {
339                 tp->stage = -1;
340         }
341 }
342
343 ENTRYPOINT void
344 reshape_triangle(ModeInfo * mi, int width, int height)
345 {
346   XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
347   init_triangle (mi);
348 }
349
350 ENTRYPOINT void
351 refresh_triangle (ModeInfo * mi)
352 {
353         /* Do nothing, it will refresh by itself */
354 }
355
356 ENTRYPOINT Bool
357 triangle_handle_event (ModeInfo *mi, XEvent *event)
358 {
359   if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
360     {
361       reshape_triangle (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
362       return True;
363     }
364   return False;
365 }
366
367
368 XSCREENSAVER_MODULE ("Triangle", triangle)