From http://www.jwz.org/xscreensaver/xscreensaver-5.18.tar.gz
[xscreensaver] / hacks / mountain.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* mountain -- square grid mountains */
3
4 #if 0
5 static const char sccsid[] = "@(#)mountain.c    5.00 2000/11/01 xlockmore";
6 #endif
7
8 /*-
9  * Copyright (c) 1995 Pascal Pensa <pensa@aurora.unice.fr>
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  * 01-Nov-2000: Allocation checks
25  * 10-May-1997: Compatible with xscreensaver
26  * 1995: Written
27  */
28
29 #ifdef STANDALONE
30 # define MODE_mountain
31 #define DEFAULTS        "*delay: 20000 \n" \
32                                         "*count: 30 \n" \
33                                         "*cycles: 4000 \n" \
34                                         "*ncolors: 64 \n" \
35                                         "*fpsSolid: true \n" \
36
37 # define SMOOTH_COLORS
38 # define mountain_handle_event 0
39 # include "xlockmore.h"         /* in xscreensaver distribution */
40 #else /* STANDALONE */
41 # include "xlock.h"             /* in xlockmore distribution */
42 #endif /* STANDALONE */
43
44 #ifdef MODE_mountain
45
46 ENTRYPOINT ModeSpecOpt mountain_opts =
47 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
48
49 #ifdef USE_MODULES
50 ModStruct   mountain_description =
51 {"mountain", "init_mountain", "draw_mountain", "release_mountain",
52  "refresh_mountain", "init_mountain", (char *) NULL, &mountain_opts,
53  1000, 30, 4000, 1, 64, 1.0, "",
54  "Shows Papo's mountain range", 0, NULL};
55
56 #endif
57
58 /* ~ 5000 Max mountain height (1000 - 10000) */
59 #define MAXHEIGHT  (3 * (mp->width + mp->height))
60
61 #define WORLDWIDTH 50           /* World size x * y */
62
63 #define RANGE_RAND(min,max) ((min) + NRAND((max) - (min)))
64
65 typedef struct {
66         int         pixelmode;
67         int         width;
68         int         height;
69         int         x, y;
70         int         offset;
71         int         stage;
72         int         h[WORLDWIDTH][WORLDWIDTH];
73         long        time;       /* up time */
74         Bool        wireframe;
75         Bool        joke;
76         GC          stippledGC;
77 } mountainstruct;
78
79 static mountainstruct *mountains = (mountainstruct *) NULL;
80
81 static void
82 spread(int  (*m)[50], int x, int y)
83 {
84         int         x2, y2;
85         int         h = m[x][y];
86
87         for (y2 = y - 1; y2 <= y + 1; y2++)
88                 for (x2 = x - 1; x2 <= x + 1; x2++)
89                         if (x2 >= 0 && y2 >= 0 && x2 < WORLDWIDTH && y2 < WORLDWIDTH)
90                                 m[x2][y2] = (m[x2][y2] + h) / 2;
91 }
92
93 static void
94 drawamountain(ModeInfo * mi)
95 {
96         Display    *display = MI_DISPLAY(mi);
97         Window      window = MI_WINDOW(mi);
98         GC          gc = MI_GC(mi);
99         mountainstruct *mp = &mountains[MI_SCREEN(mi)];
100         int         x2, y2, x3, y3, y4, y5, c = 0;
101         XPoint      p[5];
102
103         if (MI_NPIXELS(mi) > 2) {
104                 c = (mp->h[mp->x][mp->y] + mp->h[mp->x + 1][mp->y] +
105                   mp->h[mp->x][mp->y + 1] + mp->h[mp->x + 1][mp->y + 1]) / 4;
106                 c = (c / 10 + mp->offset) % MI_NPIXELS(mi);
107         }
108         x2 = mp->x * (2 * mp->width) / (3 * WORLDWIDTH);
109         y2 = mp->y * (2 * mp->height) / (3 * WORLDWIDTH);
110         p[0].x = (x2 - y2 / 2) + (mp->width / 4);
111         p[0].y = (y2 - mp->h[mp->x][mp->y]) + mp->height / 4;
112
113         x3 = (mp->x + 1) * (2 * mp->width) / (3 * WORLDWIDTH);
114         y3 = mp->y * (2 * mp->height) / (3 * WORLDWIDTH);
115         p[1].x = (x3 - y3 / 2) + (mp->width / 4);
116         p[1].y = (y3 - mp->h[mp->x + 1][mp->y]) + mp->height / 4;
117
118         y4 = (mp->y + 1) * (2 * mp->height) / (3 * WORLDWIDTH);
119         p[2].x = (x3 - y4 / 2) + (mp->width / 4);
120         p[2].y = (y4 - mp->h[mp->x + 1][mp->y + 1]) + mp->height / 4;
121
122         y5 = (mp->y + 1) * (2 * mp->height) / (3 * WORLDWIDTH);
123         p[3].x = (x2 - y5 / 2) + (mp->width / 4);
124         p[3].y = (y5 - mp->h[mp->x][mp->y + 1]) + mp->height / 4;
125
126         p[4].x = p[0].x;
127         p[4].y = p[0].y;
128
129         if (MI_NPIXELS(mi) > 2)
130                 XSetForeground(display, gc, MI_PIXEL(mi, c));
131         else
132                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
133
134         if (mp->joke) {
135                 if ((Bool) (LRAND() & 1))
136                         XDrawLines(display, window, gc, p, 5, CoordModeOrigin);
137                 else {
138                         XFillPolygon(display, window, gc, p, 4, Complex, CoordModeOrigin);
139                         if (!mp->pixelmode) {
140                                 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
141                                 XDrawLines(display, window, gc, p, 5, CoordModeOrigin);
142                         }
143                 }
144         } else {
145                 if (mp->wireframe) {
146                         XDrawLines(display, window, gc, p, 5, CoordModeOrigin);
147                 } else {
148                         XFillPolygon(display, window, gc, p, 4, Complex, CoordModeOrigin);
149
150                         if (!mp->pixelmode) {
151                                 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
152                                 XDrawLines(display, window, gc, p, 5, CoordModeOrigin);
153                         }
154                 }
155         }
156         mp->x++;
157         if (mp->x == WORLDWIDTH - 1) {
158                 mp->y++;
159                 mp->x = 0;
160         }
161         if (mp->y == WORLDWIDTH - 1)
162                 mp->stage++;
163 }
164
165 ENTRYPOINT void
166 init_mountain (ModeInfo * mi)
167 {
168         int         i, j, x, y;
169         XGCValues   gcv;
170         mountainstruct *mp;
171
172         if (mountains == NULL) {
173                 if ((mountains = (mountainstruct *) calloc(MI_NUM_SCREENS(mi),
174                                            sizeof (mountainstruct))) == NULL)
175                         return;
176         }
177         mp = &mountains[MI_SCREEN(mi)];
178
179         mp->width = MI_WIDTH(mi);
180         mp->height = MI_HEIGHT(mi);
181         mp->pixelmode = (mp->width + mp->height < 200);
182         mp->stage = 0;
183         mp->time = 0;
184         mp->x = mp->y = 0;
185         if (MI_IS_FULLRANDOM(mi)) {
186                 mp->joke = (Bool) (NRAND(10) == 0);
187                 mp->wireframe = (Bool) (LRAND() & 1);
188         } else {
189                 mp->joke = False;
190                 mp->wireframe = MI_IS_WIREFRAME(mi);
191         }
192
193         if (mp->stippledGC == None) {
194                 gcv.foreground = MI_WHITE_PIXEL(mi);
195                 gcv.background = MI_BLACK_PIXEL(mi);
196                 if ((mp->stippledGC = XCreateGC(MI_DISPLAY(mi), MI_WINDOW(mi),
197                                           GCForeground | GCBackground, &gcv)) == None)
198                         return;
199         }
200         MI_CLEARWINDOW(mi);
201
202         for (y = 0; y < (int) WORLDWIDTH; y++)
203                 for (x = 0; x < (int) WORLDWIDTH; x++)
204                         mp->h[x][y] = 0;
205
206         j = MI_COUNT(mi);
207         if (j < 0)
208                 j = NRAND(-j) + 1;
209         for (i = 0; i < j; i++)
210                 mp->h[RANGE_RAND(1, WORLDWIDTH - 1)][RANGE_RAND(1, WORLDWIDTH - 1)] =
211                         NRAND(MAXHEIGHT);
212
213         for (y = 0; y < WORLDWIDTH; y++)
214                 for (x = 0; x < WORLDWIDTH; x++)
215                         spread(mp->h, x, y);
216
217         for (y = 0; y < WORLDWIDTH; y++)
218                 for (x = 0; x < WORLDWIDTH; x++) {
219                         mp->h[x][y] = mp->h[x][y] + NRAND(10) - 5;
220                         if (mp->h[x][y] < 10)
221                                 mp->h[x][y] = 0;
222                 }
223
224         if (MI_NPIXELS(mi) > 2)
225                 mp->offset = NRAND(MI_NPIXELS(mi));
226         else
227                 mp->offset = 0;
228 }
229
230 ENTRYPOINT void
231 draw_mountain (ModeInfo * mi)
232 {
233         mountainstruct *mp;
234
235         if (mountains == NULL)
236                         return;
237         mp = &mountains[MI_SCREEN(mi)];
238         if (mp->stippledGC == NULL)
239                         return;
240
241         MI_IS_DRAWN(mi) = True;
242
243         switch (mp->stage) {
244                 case 0:
245                         drawamountain(mi);
246                         break;
247                 case 1:
248                         if (++mp->time > MI_CYCLES(mi))
249                                 mp->stage++;
250                         break;
251                 case 2:
252                         init_mountain(mi);
253                         break;
254         }
255 }
256
257 ENTRYPOINT void
258 reshape_mountain(ModeInfo * mi, int width, int height)
259 {
260   XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
261   init_mountain (mi);
262 }
263
264
265 ENTRYPOINT void
266 release_mountain (ModeInfo * mi)
267 {
268         if (mountains != NULL) {
269                 int         screen;
270
271                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
272                         mountainstruct *mp = &mountains[screen];
273
274                         if (mp->stippledGC)
275                                 XFreeGC(MI_DISPLAY(mi), mp->stippledGC);
276                 }
277                 (void) free((void *) mountains);
278                 mountains = (mountainstruct *) NULL;
279         }
280 }
281
282 ENTRYPOINT void
283 refresh_mountain(ModeInfo * mi)
284 {
285         mountainstruct *mp;
286
287         if (mountains == NULL)
288                         return;
289         mp = &mountains[MI_SCREEN(mi)];
290
291         MI_CLEARWINDOW(mi);
292         mp->x = 0;
293         mp->y = 0;
294 }
295
296 XSCREENSAVER_MODULE ("Mountain", mountain)
297
298 #endif /* MODE_mountain */