1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* mountain -- square grid mountains */
5 static const char sccsid[] = "@(#)mountain.c 5.00 2000/11/01 xlockmore";
9 * Copyright (c) 1995 Pascal Pensa <pensa@aurora.unice.fr>
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.
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.
24 * 01-Nov-2000: Allocation checks
25 * 10-May-1997: Compatible with xscreensaver
30 # define MODE_mountain
31 #define DEFAULTS "*delay: 20000 \n" \
35 "*fpsSolid: true \n" \
37 # define SMOOTH_COLORS
38 # define reshape_mountain 0
39 # define mountain_handle_event 0
40 # include "xlockmore.h" /* in xscreensaver distribution */
41 #else /* STANDALONE */
42 # include "xlock.h" /* in xlockmore distribution */
43 #endif /* STANDALONE */
47 ENTRYPOINT ModeSpecOpt mountain_opts =
48 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
51 ModStruct mountain_description =
52 {"mountain", "init_mountain", "draw_mountain", "release_mountain",
53 "refresh_mountain", "init_mountain", (char *) NULL, &mountain_opts,
54 1000, 30, 4000, 1, 64, 1.0, "",
55 "Shows Papo's mountain range", 0, NULL};
59 /* ~ 5000 Max mountain height (1000 - 10000) */
60 #define MAXHEIGHT (3 * (mp->width + mp->height))
62 #define WORLDWIDTH 50 /* World size x * y */
64 #define RANGE_RAND(min,max) ((min) + NRAND((max) - (min)))
73 int h[WORLDWIDTH][WORLDWIDTH];
74 long time; /* up time */
80 static mountainstruct *mountains = (mountainstruct *) NULL;
83 spread(int (*m)[50], int x, int y)
88 for (y2 = y - 1; y2 <= y + 1; y2++)
89 for (x2 = x - 1; x2 <= x + 1; x2++)
90 if (x2 >= 0 && y2 >= 0 && x2 < WORLDWIDTH && y2 < WORLDWIDTH)
91 m[x2][y2] = (m[x2][y2] + h) / 2;
95 drawamountain(ModeInfo * mi)
97 Display *display = MI_DISPLAY(mi);
98 Window window = MI_WINDOW(mi);
100 mountainstruct *mp = &mountains[MI_SCREEN(mi)];
101 int x2, y2, x3, y3, y4, y5, c = 0;
104 if (MI_NPIXELS(mi) > 2) {
105 c = (mp->h[mp->x][mp->y] + mp->h[mp->x + 1][mp->y] +
106 mp->h[mp->x][mp->y + 1] + mp->h[mp->x + 1][mp->y + 1]) / 4;
107 c = (c / 10 + mp->offset) % MI_NPIXELS(mi);
109 x2 = mp->x * (2 * mp->width) / (3 * WORLDWIDTH);
110 y2 = mp->y * (2 * mp->height) / (3 * WORLDWIDTH);
111 p[0].x = (x2 - y2 / 2) + (mp->width / 4);
112 p[0].y = (y2 - mp->h[mp->x][mp->y]) + mp->height / 4;
114 x3 = (mp->x + 1) * (2 * mp->width) / (3 * WORLDWIDTH);
115 y3 = mp->y * (2 * mp->height) / (3 * WORLDWIDTH);
116 p[1].x = (x3 - y3 / 2) + (mp->width / 4);
117 p[1].y = (y3 - mp->h[mp->x + 1][mp->y]) + mp->height / 4;
119 y4 = (mp->y + 1) * (2 * mp->height) / (3 * WORLDWIDTH);
120 p[2].x = (x3 - y4 / 2) + (mp->width / 4);
121 p[2].y = (y4 - mp->h[mp->x + 1][mp->y + 1]) + mp->height / 4;
123 y5 = (mp->y + 1) * (2 * mp->height) / (3 * WORLDWIDTH);
124 p[3].x = (x2 - y5 / 2) + (mp->width / 4);
125 p[3].y = (y5 - mp->h[mp->x][mp->y + 1]) + mp->height / 4;
130 if (MI_NPIXELS(mi) > 2)
131 XSetForeground(display, gc, MI_PIXEL(mi, c));
133 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
136 if ((Bool) (LRAND() & 1))
137 XDrawLines(display, window, gc, p, 5, CoordModeOrigin);
139 XFillPolygon(display, window, gc, p, 4, Complex, CoordModeOrigin);
140 if (!mp->pixelmode) {
141 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
142 XDrawLines(display, window, gc, p, 5, CoordModeOrigin);
147 XDrawLines(display, window, gc, p, 5, CoordModeOrigin);
149 XFillPolygon(display, window, gc, p, 4, Complex, CoordModeOrigin);
151 if (!mp->pixelmode) {
152 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
153 XDrawLines(display, window, gc, p, 5, CoordModeOrigin);
158 if (mp->x == WORLDWIDTH - 1) {
162 if (mp->y == WORLDWIDTH - 1)
167 init_mountain (ModeInfo * mi)
173 if (mountains == NULL) {
174 if ((mountains = (mountainstruct *) calloc(MI_NUM_SCREENS(mi),
175 sizeof (mountainstruct))) == NULL)
178 mp = &mountains[MI_SCREEN(mi)];
180 mp->width = MI_WIDTH(mi);
181 mp->height = MI_HEIGHT(mi);
182 mp->pixelmode = (mp->width + mp->height < 200);
186 if (MI_IS_FULLRANDOM(mi)) {
187 mp->joke = (Bool) (NRAND(10) == 0);
188 mp->wireframe = (Bool) (LRAND() & 1);
191 mp->wireframe = MI_IS_WIREFRAME(mi);
194 if (mp->stippledGC == None) {
195 gcv.foreground = MI_WHITE_PIXEL(mi);
196 gcv.background = MI_BLACK_PIXEL(mi);
197 if ((mp->stippledGC = XCreateGC(MI_DISPLAY(mi), MI_WINDOW(mi),
198 GCForeground | GCBackground, &gcv)) == None)
203 for (y = 0; y < (int) WORLDWIDTH; y++)
204 for (x = 0; x < (int) WORLDWIDTH; x++)
210 for (i = 0; i < j; i++)
211 mp->h[RANGE_RAND(1, WORLDWIDTH - 1)][RANGE_RAND(1, WORLDWIDTH - 1)] =
214 for (y = 0; y < WORLDWIDTH; y++)
215 for (x = 0; x < WORLDWIDTH; x++)
218 for (y = 0; y < WORLDWIDTH; y++)
219 for (x = 0; x < WORLDWIDTH; x++) {
220 mp->h[x][y] = mp->h[x][y] + NRAND(10) - 5;
221 if (mp->h[x][y] < 10)
225 if (MI_NPIXELS(mi) > 2)
226 mp->offset = NRAND(MI_NPIXELS(mi));
232 draw_mountain (ModeInfo * mi)
236 if (mountains == NULL)
238 mp = &mountains[MI_SCREEN(mi)];
239 if (mp->stippledGC == NULL)
242 MI_IS_DRAWN(mi) = True;
249 if (++mp->time > MI_CYCLES(mi))
259 release_mountain (ModeInfo * mi)
261 if (mountains != NULL) {
264 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
265 mountainstruct *mp = &mountains[screen];
268 XFreeGC(MI_DISPLAY(mi), mp->stippledGC);
270 (void) free((void *) mountains);
271 mountains = (mountainstruct *) NULL;
276 refresh_mountain(ModeInfo * mi)
280 if (mountains == NULL)
282 mp = &mountains[MI_SCREEN(mi)];
289 XSCREENSAVER_MODULE ("Mountain", mountain)
291 #endif /* MODE_mountain */