1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* mountain -- square grid mountains */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)mountain.c 4.04 97/07/28 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 * 10-May-97: Compatible with xscreensaver
29 # define PROGCLASS "Mountain"
30 # define HACK_INIT init_mountain
31 # define HACK_DRAW draw_mountain
32 # define mountain_opts xlockmore_opts
33 # define DEFAULTS "*delay: 0 \n" \
36 # define SMOOTH_COLORS
37 #include "xlockmore.h" /* in xscreensaver distribution */
38 #else /* STANDALONE */
39 #include "xlock.h" /* in xlockmore distribution */
40 #endif /* STANDALONE */
42 ModeSpecOpt mountain_opts =
43 {0, NULL, 0, NULL, NULL};
45 /* ~ 5000 Max mountain height (1000 - 10000) */
46 #define MAXHEIGHT (3 * (mp->width + mp->height))
48 #define WORLDWIDTH 50 /* World size x * y */
50 #define RANGE_RAND(min,max) ((min) + NRAND((max) - (min)))
59 int h[WORLDWIDTH][WORLDWIDTH];
60 int time; /* up time */
65 static mountainstruct *mountains = NULL;
68 spread(int (*m)[50], int x, int y)
73 for (y2 = y - 1; y2 <= y + 1; y2++)
74 for (x2 = x - 1; x2 <= x + 1; x2++)
75 if (x2 >= 0 && y2 >= 0 && x2 < WORLDWIDTH && y2 < WORLDWIDTH)
76 m[x2][y2] = (m[x2][y2] + h) / 2;
80 drawamountain(ModeInfo * mi)
82 Display *display = MI_DISPLAY(mi);
83 Window window = MI_WINDOW(mi);
85 mountainstruct *mp = &mountains[MI_SCREEN(mi)];
86 int x2, y2, x3, y3, y4, y5, c = 0;
89 if (MI_NPIXELS(mi) > 2) {
90 c = (mp->h[mp->x][mp->y] + mp->h[mp->x + 1][mp->y] +
91 mp->h[mp->x][mp->y + 1] + mp->h[mp->x + 1][mp->y + 1]) / 4;
92 c = (c / 10 + mp->offset) % MI_NPIXELS(mi);
94 x2 = mp->x * (2 * mp->width) / (3 * WORLDWIDTH);
95 y2 = mp->y * (2 * mp->height) / (3 * WORLDWIDTH);
96 p[0].x = (x2 - y2 / 2) + (mp->width / 4);
97 p[0].y = (y2 - mp->h[mp->x][mp->y]) + mp->height / 4;
99 x3 = (mp->x + 1) * (2 * mp->width) / (3 * WORLDWIDTH);
100 y3 = mp->y * (2 * mp->height) / (3 * WORLDWIDTH);
101 p[1].x = (x3 - y3 / 2) + (mp->width / 4);
102 p[1].y = (y3 - mp->h[mp->x + 1][mp->y]) + mp->height / 4;
104 y4 = (mp->y + 1) * (2 * mp->height) / (3 * WORLDWIDTH);
105 p[2].x = (x3 - y4 / 2) + (mp->width / 4);
106 p[2].y = (y4 - mp->h[mp->x + 1][mp->y + 1]) + mp->height / 4;
108 y5 = (mp->y + 1) * (2 * mp->height) / (3 * WORLDWIDTH);
109 p[3].x = (x2 - y5 / 2) + (mp->width / 4);
110 p[3].y = (y5 - mp->h[mp->x][mp->y + 1]) + mp->height / 4;
115 if (MI_NPIXELS(mi) > 2)
116 XSetForeground(display, gc, MI_PIXEL(mi, c));
118 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
119 XFillPolygon(display, window, gc, p, 4, Complex, CoordModeOrigin);
121 if (!mp->pixelmode) {
122 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
123 XDrawLines(display, window, gc, p, 5, CoordModeOrigin);
126 if (mp->x == WORLDWIDTH - 1) {
130 if (mp->y == WORLDWIDTH - 1)
135 init_mountain(ModeInfo * mi)
141 if (mountains == NULL) {
142 if ((mountains = (mountainstruct *) calloc(MI_NUM_SCREENS(mi),
143 sizeof (mountainstruct))) == NULL)
146 mp = &mountains[MI_SCREEN(mi)];
147 mp->width = MI_WIN_WIDTH(mi);
148 mp->height = MI_WIN_HEIGHT(mi);
149 mp->pixelmode = (mp->width + mp->height < 200);
155 gcv.foreground = MI_WIN_WHITE_PIXEL(mi);
156 gcv.background = MI_WIN_BLACK_PIXEL(mi);
158 mp->stippled_GC = XCreateGC(MI_DISPLAY(mi), MI_WINDOW(mi),
159 GCForeground | GCBackground, &gcv);
161 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
163 for (y = 0; y < WORLDWIDTH; y++)
164 for (x = 0; x < WORLDWIDTH; x++)
167 j = MI_BATCHCOUNT(mi);
170 for (i = 0; i < j; i++)
171 mp->h[RANGE_RAND(1, WORLDWIDTH - 1)][RANGE_RAND(1, WORLDWIDTH - 1)] =
174 for (y = 0; y < WORLDWIDTH; y++)
175 for (x = 0; x < WORLDWIDTH; x++)
178 for (y = 0; y < WORLDWIDTH; y++)
179 for (x = 0; x < WORLDWIDTH; x++) {
180 mp->h[x][y] = mp->h[x][y] + NRAND(10) - 5;
181 if (mp->h[x][y] < 10)
185 if (MI_NPIXELS(mi) > 2)
186 mp->offset = NRAND(MI_NPIXELS(mi));
192 draw_mountain(ModeInfo * mi)
194 mountainstruct *mp = &mountains[MI_SCREEN(mi)];
201 MI_PAUSE(mi) = 2000000;
202 /*if (++mp->time > MI_CYCLES(mi)); */
212 release_mountain(ModeInfo * mi)
214 if (mountains != NULL) {
217 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
218 mountainstruct *mp = &mountains[screen];
220 XFreeGC(MI_DISPLAY(mi), mp->stippled_GC);
222 (void) free((void *) mountains);
228 refresh_mountain(ModeInfo * mi)
230 mountainstruct *mp = &mountains[MI_SCREEN(mi)];