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