1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* triangle --- create a triangle-mountain */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)triangle.c 4.04 97/07/28 xlockmore";
10 * Copyright (c) 1995 by Tobias Gloth
12 * Permission to use, copy, modify, and distribute this software and its
13 * documentation for any purpose and without fee is hereby granted,
14 * provided that the above copyright notice appear in all copies and that
15 * both that copyright notice and this permission notice appear in
16 * supporting documentation.
18 * This file is provided AS IS with no warranties of any kind. The author
19 * shall have no liability with respect to the infringement of copyrights,
20 * trade secrets or any patents by this file or any part thereof. In no
21 * event will the author be liable for any lost revenue or profits or
22 * other special, indirect and consequential damages.
25 * 10-May-97: Compatible with xscreensaver
26 * 10-Mar-96: re-arranged and re-formatted the code for appearance and
27 * to make common subroutines. Simplified.
28 * Ron Hitchens <ron@idiom.com>
29 * 07-Mar-96: Removed internal delay code, set MI_PAUSE(mi) for inter-scene
30 * delays. No other delays are needed here.
31 * Made pause time sensitive to value of cycles (in 10ths of a
32 * second). Removed (hopefully) all references to globals.
33 * Ron Hitchens <ron@idiom.com>
34 * 27-Feb-96: Undid the changes listed below. Added ModeInfo argument.
35 * Implemented delay between scenes using the MI_PAUSE(mi)
36 * scheme. Ron Hitchens <ron@idiom.com>
37 * 27-Dec-95: Ron Hitchens <ron@idiom.com>
38 * Modified logic of draw_triangle() to provide a delay
39 * (sensitive to the value of cycles) between each iteration.
40 * Because this mode is so compute intensive, when the new
41 * event loop adjusted the delay to compensate, this mode had
42 * almost no delay time left. This change pauses between each
43 * new landscape, but could still be done better (it is not
44 * sensitive to input events while drawing, for example).
45 * 03-Nov-95: Many changes (hopefully some good ones) by David Bagley
46 * 01-Oct-95: Written by Tobias Gloth
50 # define PROGCLASS "Triangle"
51 # define HACK_INIT init_triangle
52 # define HACK_DRAW draw_triangle
53 # define triangle_opts xlockmore_opts
54 # define DEFAULTS "*delay: 10000 \n" \
56 # define SMOOTH_COLORS
57 # include "xlockmore.h" /* in xscreensaver distribution */
58 #else /* STANDALONE */
59 # include "xlock.h" /* in xlockmore distribution */
60 #endif /* STANDALONE */
62 ModeSpecOpt triangle_opts =
63 {0, NULL, 0, NULL, NULL};
66 #define MAX_SIZE (1<<MAX_STEPS)
67 #define MAX_LEVELS 1000
74 #define BLUE 45 /* Just the right shade of blue */
76 #define BACKFACE_REMOVAL
78 #define DISPLACE(h,d) ((h)/2+LRAND()/(MAXRAND/(2*(d)+1))-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];
99 static trianglestruct *triangles = NULL;
103 draw_atriangle(ModeInfo * mi, XPoint * p, int y_0, int y_1, int y_2, double dinv)
105 Display *display = MI_DISPLAY(mi);
106 Window window = MI_WINDOW(mi);
109 if (MI_NPIXELS(mi) > 2) { /* color */
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);
121 color = MI_NPIXELS(mi) -
122 (int) ((double) MI_NPIXELS(mi) / M_PI_2 * atan(dinv * (dmax - dmin)));
125 XSetForeground(display, gc, MI_PIXEL(mi, color % MI_NPIXELS(mi)));
126 XFillPolygon(display, window, gc, p, 3, Convex, CoordModeOrigin);
129 #ifdef BACKFACE_REMOVAL
130 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
131 XFillPolygon(display, window, gc, p, 3, Convex, CoordModeOrigin);
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);
142 calc_points1(trianglestruct * tp, int d, int *y0_p, int *y1_p, int *y2_p, XPoint * p)
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)];
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)];
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;
159 calc_points2(trianglestruct * tp, int d, int *y0_p, int *y1_p, int *y2_p, XPoint * p)
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)];
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)];
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;
177 draw_mesh(ModeInfo * mi, trianglestruct * tp, int d, int count)
182 double dinv = 0.2 / d;
184 if ((tp->j == 0) && (tp->i == 0)) {
186 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
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),
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);
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);
214 if (tp->j == tp->size) {
220 init_triangle(ModeInfo * mi)
226 if (triangles == NULL) {
227 if ((triangles = (trianglestruct *) calloc(MI_NUM_SCREENS(mi),
228 sizeof (trianglestruct))) == NULL)
231 tp = &triangles[MI_SCREEN(mi)];
233 tp->width = MI_WIN_WIDTH(mi);
234 tp->height = MI_WIN_HEIGHT(mi);
238 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
242 tp->steps = MAX_STEPS;
244 tp->size = 1 << --tp->steps;
245 } while (tp->size * 5 > tp->width);
247 for (i = 0; i < tp->size + 1; i++) {
249 tmp += (tp->size) + 1 - i;
253 dim = MIN(tp->width, tp->height);
255 for (i = 0; i < 2 * tp->size + 1; i++) {
256 tp->xpos[i] = (short) ((((double) i)
257 / ((double) (2 * tp->size)) * (RIGHT - LEFT) + LEFT)
258 * dim) + (tp->width - dim) / 2;
261 for (i = 0; i < (tp->size + 1); i++) {
262 tp->ypos[i] = (short) ((((double) i)
263 / ((double) tp->size) * (BOTTOM - TOP) + TOP) * dim)
264 + (tp->height - dim) / 2;
267 for (i = 0; i < tp->steps; i++) {
268 tp->delta[i] = ((short) (DELTA * dim)) >> i;
274 for (i = 0; i < MAX_LEVELS; i++) {
275 tp->level[i] = (i * i) / one;
280 draw_triangle(ModeInfo * mi)
282 trianglestruct *tp = &triangles[MI_SCREEN(mi)];
283 int d, d2, i, j, delta;
286 draw_mesh(mi, tp, tp->d / 2, MAX_SIZE / tp->d);
288 /* The init_now flag will pop up when the scene is complete.
289 * Cycles specifies how long to wait, in 1/10 secs.
290 TODO: This is wrong for multi-screens ***
294 MI_PAUSE(mi) = 2000000;
298 XSync(MI_DISPLAY(mi), False);
300 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
304 free_colors(mi->dpy, mi->xgwa.colormap, mi->colors,
306 make_smooth_colormap (mi->dpy,
307 mi->xgwa.visual, mi->xgwa.colormap,
308 mi->colors, &mi->npixels,
309 True, &mi->writable_p, True);
317 if (tp->delta[0] > 0) {
318 if (!(++tp->stage)) {
319 tp->h[0][0] = (short int) MAX(0, DISPLACE(0, tp->delta[0]));
320 tp->h[tp->size][0] = (short int) MAX(0, DISPLACE(0, tp->delta[0]));
321 tp->h[0][tp->size] = (short int) MAX(0, DISPLACE(0, tp->delta[0]));
323 d = 2 << (tp->steps - tp->stage);
325 delta = tp->delta[tp->stage - 1];
327 for (i = 0; i < tp->size; i += d) {
328 for (j = 0; j < (tp->size - i); j += d) {
329 tp->h[i + d2][j] = (short int) DISPLACE(tp->h[i][j] +
330 tp->h[i + d][j], delta);
331 tp->h[i][j + d2] = (short int) DISPLACE(tp->h[i][j] +
332 tp->h[i][j + d], delta);
333 tp->h[i + d2][j + d2] = (short int) DISPLACE(tp->h[i + d][j] +
334 tp->h[i][j + d], delta);
344 if (tp->stage == tp->steps) {
350 release_triangle(ModeInfo * mi)
352 if (triangles != NULL) {
353 (void) free((void *) triangles);
359 refresh_triangle(ModeInfo * mi)
361 /* Do nothing, it will refresh by itself */