1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* mountain -- square grid mountains */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)mountain.c 5.00 2000/11/01 xlockmore";
10 * Copyright (c) 1995 Pascal Pensa <pensa@aurora.unice.fr>
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 * 01-Nov-2000: Allocation checks
26 * 10-May-1997: Compatible with xscreensaver
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" \
41 #include "xlockmore.h" /* in xscreensaver distribution */
42 #else /* STANDALONE */
43 #include "xlock.h" /* in xlockmore distribution */
44 #endif /* STANDALONE */
48 ModeSpecOpt mountain_opts =
49 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
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};
60 /* ~ 5000 Max mountain height (1000 - 10000) */
61 #define MAXHEIGHT (3 * (mp->width + mp->height))
63 #define WORLDWIDTH 50 /* World size x * y */
65 #define RANGE_RAND(min,max) ((min) + NRAND((max) - (min)))
74 int h[WORLDWIDTH][WORLDWIDTH];
75 long time; /* up time */
81 static mountainstruct *mountains = (mountainstruct *) NULL;
84 spread(int (*m)[50], int x, int y)
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;
96 drawamountain(ModeInfo * mi)
98 Display *display = MI_DISPLAY(mi);
99 Window window = MI_WINDOW(mi);
101 mountainstruct *mp = &mountains[MI_SCREEN(mi)];
102 int x2, y2, x3, y3, y4, y5, c = 0;
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);
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;
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;
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;
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;
131 if (MI_NPIXELS(mi) > 2)
132 XSetForeground(display, gc, MI_PIXEL(mi, c));
134 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
137 if ((Bool) (LRAND() & 1))
138 XDrawLines(display, window, gc, p, 5, CoordModeOrigin);
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);
148 XDrawLines(display, window, gc, p, 5, CoordModeOrigin);
150 XFillPolygon(display, window, gc, p, 4, Complex, CoordModeOrigin);
152 if (!mp->pixelmode) {
153 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
154 XDrawLines(display, window, gc, p, 5, CoordModeOrigin);
159 if (mp->x == WORLDWIDTH - 1) {
163 if (mp->y == WORLDWIDTH - 1)
168 init_mountain(ModeInfo * mi)
174 if (mountains == NULL) {
175 if ((mountains = (mountainstruct *) calloc(MI_NUM_SCREENS(mi),
176 sizeof (mountainstruct))) == NULL)
179 mp = &mountains[MI_SCREEN(mi)];
181 mp->width = MI_WIDTH(mi);
182 mp->height = MI_HEIGHT(mi);
183 mp->pixelmode = (mp->width + mp->height < 200);
187 if (MI_IS_FULLRANDOM(mi)) {
188 mp->joke = (Bool) (NRAND(10) == 0);
189 mp->wireframe = (Bool) (LRAND() & 1);
192 mp->wireframe = MI_IS_WIREFRAME(mi);
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)
204 for (y = 0; y < (int) WORLDWIDTH; y++)
205 for (x = 0; x < (int) WORLDWIDTH; x++)
211 for (i = 0; i < j; i++)
212 mp->h[RANGE_RAND(1, WORLDWIDTH - 1)][RANGE_RAND(1, WORLDWIDTH - 1)] =
215 for (y = 0; y < WORLDWIDTH; y++)
216 for (x = 0; x < WORLDWIDTH; x++)
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)
226 if (MI_NPIXELS(mi) > 2)
227 mp->offset = NRAND(MI_NPIXELS(mi));
233 draw_mountain(ModeInfo * mi)
237 if (mountains == NULL)
239 mp = &mountains[MI_SCREEN(mi)];
240 if (mp->stippledGC == NULL)
243 MI_IS_DRAWN(mi) = True;
250 if (++mp->time > MI_CYCLES(mi))
260 release_mountain(ModeInfo * mi)
262 if (mountains != NULL) {
265 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
266 mountainstruct *mp = &mountains[screen];
269 XFreeGC(MI_DISPLAY(mi), mp->stippledGC);
271 (void) free((void *) mountains);
272 mountains = (mountainstruct *) NULL;
277 refresh_mountain(ModeInfo * mi)
281 if (mountains == NULL)
283 mp = &mountains[MI_SCREEN(mi)];
290 #endif /* MODE_mountain */