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