http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.01.tar.gz
[xscreensaver] / hacks / forest.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* forest --- binary trees in a fractal forest */
3
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)forest.c      5.00 2000/11/01 xlockmore";
6
7 #endif
8
9 /*-
10  * Copyright (c) 1995 Pascal Pensa <pensa@aurora.unice.fr>
11  *
12  * Original idea : Guillaume Ramey <ramey@aurora.unice.fr>
13  *
14  * Permission to use, copy, modify, and distribute this software and its
15  * documentation for any purpose and without fee is hereby granted,
16  * provided that the above copyright notice appear in all copies and that
17  * both that copyright notice and this permission notice appear in
18  * supporting documentation.
19  *
20  * This file is provided AS IS with no warranties of any kind.  The author
21  * shall have no liability with respect to the infringement of copyrights,
22  * trade secrets or any patents by this file or any part thereof.  In no
23  * event will the author be liable for any lost revenue or profits or
24  * other special, indirect and consequential damages.
25  *
26  * Revision History:
27  * 01-Nov-2000: Allocation checks
28  * 10-May-1997: Compatible with xscreensaver
29  *
30  */
31
32 #ifdef STANDALONE
33 #define MODE_forest
34 #define PROGCLASS "Forest"
35 #define HACK_INIT init_forest
36 #define HACK_DRAW draw_forest
37 #define forest_opts xlockmore_opts
38 #define DEFAULTS "*delay: 500000 \n" \
39  "*count: 100 \n" \
40  "*cycles: 200 \n" \
41  "*ncolors: 20 \n"
42 #define UNIFORM_COLORS
43 #include "xlockmore.h"          /* in xscreensaver distribution */
44 #else /* STANDALONE */
45 #include "xlock.h"              /* in xlockmore distribution */
46
47 #endif /* STANDALONE */
48
49 #ifdef MODE_forest
50
51 ModeSpecOpt forest_opts =
52 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
53
54 #ifdef USE_MODULES
55 ModStruct   forest_description =
56 {"forest", "init_forest", "draw_forest", "release_forest",
57  "refresh_forest", "init_forest", (char *) NULL, &forest_opts,
58  400000, 100, 200, 1, 64, 1.0, "",
59  "Shows binary trees of a fractal forest", 0, NULL};
60
61 #endif
62
63 #define MINTREES   1
64
65 #define MINHEIGHT  20           /* Tree height range */
66 #define MAXHEIGHT  40
67
68 #define MINANGLE   15           /* (degree) angle between soon */
69 #define MAXANGLE   35
70 #define RANDANGLE  15           /* (degree) Max random angle from default */
71
72 #define REDUCE     90           /* Height % from father */
73
74 #define ITERLEVEL  10           /* Tree iteration */
75
76 #define COLORSPEED  2           /* Color increment */
77
78 /* degree to radian */
79 #define DEGTORAD(x) (((float)(x)) * M_PI / 180.0)
80
81 #define RANGE_RAND(min,max) ((min) + NRAND((max) - (min)))
82
83 typedef struct {
84         int         width;
85         int         height;
86         int         time;       /* up time */
87         int         ntrees;
88 } foreststruct;
89
90 static foreststruct *forests = (foreststruct *) NULL;
91
92 static void
93 draw_tree(ModeInfo * mi,
94           short int x, short int y, short int len,
95           float a, float as, short int c, short int level)
96                                 /* Father's end */
97                                 /* Length */
98                                 /* color */
99                                 /* Height level */
100                                 /* Father's angle */
101                                 /* Father's angle step */
102 {
103         Display    *display = MI_DISPLAY(mi);
104         Window      window = MI_WINDOW(mi);
105         GC          gc = MI_GC(mi);
106         short       x_1, y_1, x_2, y_2;
107         float       a1, a2;
108
109         /* left */
110
111         a1 = a + as + DEGTORAD(NRAND(2 * RANDANGLE) - RANDANGLE);
112
113         x_1 = x + (short) (COSF(a1) * ((float) len));
114         y_1 = y + (short) (SINF(a1) * ((float) len));
115
116         /* right */
117
118         a2 = a - as + DEGTORAD(NRAND(2 * RANDANGLE) - RANDANGLE);
119
120         x_2 = x + (short) (COSF(a2) * ((float) len));
121         y_2 = y + (short) (SINF(a2) * ((float) len));
122
123         if (MI_NPIXELS(mi) > 2) {
124                 XSetForeground(display, gc, MI_PIXEL(mi, c));
125                 c = (c + COLORSPEED) % MI_NPIXELS(mi);
126         } else
127                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
128
129         XDrawLine(display, window, gc, x, y, x_1, y_1);
130         XDrawLine(display, window, gc, x, y, x_2, y_2);
131
132         if (level < 2) {
133                 XDrawLine(display, window, gc, x + 1, y, x_1 + 1, y_1);
134                 XDrawLine(display, window, gc, x + 1, y, x_2 + 1, y_2);
135         }
136         len = (len * REDUCE * 10) / 1000;
137
138         if (level < ITERLEVEL) {
139                 draw_tree(mi, x_1, y_1, len, a1, as, c, level + 1);
140                 draw_tree(mi, x_2, y_2, len, a2, as, c, level + 1);
141         }
142 }
143
144 void
145 init_forest(ModeInfo * mi)
146 {
147         foreststruct *fp;
148
149         if (forests == NULL) {
150                 if ((forests = (foreststruct *) calloc(MI_NUM_SCREENS(mi),
151                                              sizeof (foreststruct))) == NULL)
152                         return;
153         }
154         fp = &forests[MI_SCREEN(mi)];
155
156         fp->width = MI_WIDTH(mi);
157         fp->height = MI_HEIGHT(mi);
158         fp->time = 0;
159
160         fp->ntrees = MI_COUNT(mi);
161         if (fp->ntrees < -MINTREES)
162                 fp->ntrees = NRAND(-fp->ntrees - MINTREES + 1) + MINTREES;
163         else if (fp->ntrees < MINTREES)
164                 fp->ntrees = MINTREES;
165
166         MI_CLEARWINDOW(mi);
167 }
168
169 void
170 draw_forest(ModeInfo * mi)
171 {
172         Display    *display = MI_DISPLAY(mi);
173         GC          gc = MI_GC(mi);
174         short       x, y, x_2, y_2, len, c = 0;
175         float       a, as;
176         foreststruct *fp;
177
178         if (forests == NULL)
179                 return;
180         fp = &forests[MI_SCREEN(mi)];
181
182         MI_IS_DRAWN(mi) = True;
183         if (fp->time < fp->ntrees) {
184
185                 x = RANGE_RAND(0, fp->width);
186                 y = RANGE_RAND(0, fp->height + MAXHEIGHT);
187                 a = -M_PI / 2.0 + DEGTORAD(NRAND(2 * RANDANGLE) - RANDANGLE);
188                 as = DEGTORAD(RANGE_RAND(MINANGLE, MAXANGLE));
189                 len = ((RANGE_RAND(MINHEIGHT, MAXHEIGHT) * (fp->width / 20)) / 50) + 2;
190
191                 if (MI_NPIXELS(mi) > 2) {
192                         c = NRAND(MI_NPIXELS(mi));
193                         XSetForeground(display, gc, MI_PIXEL(mi, c));
194                         c = (c + COLORSPEED) % MI_NPIXELS(mi);
195                 } else
196                         XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
197
198                 x_2 = x + (short) (COSF(a) * ((float) len));
199                 y_2 = y + (short) (SINF(a) * ((float) len));
200
201                 XDrawLine(display, MI_WINDOW(mi), gc, x, y, x_2, y_2);
202                 XDrawLine(display, MI_WINDOW(mi), gc, x + 1, y, x_2 + 1, y_2);
203
204                 draw_tree(mi, x_2, y_2, (len * REDUCE) / 100, a, as, c, 1);
205         }
206         if (++fp->time > MI_CYCLES(mi)) {
207                 init_forest(mi);
208         }
209 }
210
211 void
212 release_forest(ModeInfo * mi)
213 {
214         if (forests != NULL) {
215                 (void) free((void *) forests);
216                 forests = (foreststruct *) NULL;
217         }
218 }
219
220 void
221 refresh_forest(ModeInfo * mi)
222 {
223         MI_CLEARWINDOW(mi);
224 }
225
226 #endif /* MODE_forest */