1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* forest --- binary trees in a fractal forest */
3
4 #if 0
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 DEFAULTS "*delay: 400000 \n" \
35 "*count: 100 \n" \
36 "*cycles: 200 \n" \
37 "*ncolors: 100 \n" \
38
39 # define free_forest 0
40 # define reshape_forest 0
41 # define forest_handle_event 0
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 ENTRYPOINT 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
draw_tree(ModeInfo * mi,short int x,short int y,short int len,float a,float as,short int c,short int level)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 ENTRYPOINT void
init_forest(ModeInfo * mi)145 init_forest(ModeInfo * mi)
146 {
147 foreststruct *fp;
148
149 MI_INIT(mi, forests);
150 fp = &forests[MI_SCREEN(mi)];
151
152 fp->width = MI_WIDTH(mi);
153 fp->height = MI_HEIGHT(mi);
154 fp->time = 0;
155
156 fp->ntrees = MI_COUNT(mi);
157 if (fp->ntrees < -MINTREES)
158 fp->ntrees = NRAND(-fp->ntrees - MINTREES + 1) + MINTREES;
159 else if (fp->ntrees < MINTREES)
160 fp->ntrees = MINTREES;
161
162 MI_CLEARWINDOW(mi);
163 }
164
165 ENTRYPOINT void
draw_forest(ModeInfo * mi)166 draw_forest(ModeInfo * mi)
167 {
168 Display *display = MI_DISPLAY(mi);
169 GC gc = MI_GC(mi);
170 short x, y, x_2, y_2, len, c = 0;
171 float a, as;
172 foreststruct *fp;
173
174 if (forests == NULL)
175 return;
176 fp = &forests[MI_SCREEN(mi)];
177
178 MI_IS_DRAWN(mi) = True;
179 if (fp->time < fp->ntrees) {
180
181 x = RANGE_RAND(0, fp->width);
182 y = RANGE_RAND(0, fp->height + MAXHEIGHT);
183 a = -M_PI / 2.0 + DEGTORAD(NRAND(2 * RANDANGLE) - RANDANGLE);
184 as = DEGTORAD(RANGE_RAND(MINANGLE, MAXANGLE));
185 len = ((RANGE_RAND(MINHEIGHT, MAXHEIGHT) * (fp->width / 20)) / 50) + 2;
186
187 if (MI_NPIXELS(mi) > 2) {
188 c = NRAND(MI_NPIXELS(mi));
189 XSetForeground(display, gc, MI_PIXEL(mi, c));
190 c = (c + COLORSPEED) % MI_NPIXELS(mi);
191 } else
192 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
193
194 x_2 = x + (short) (COSF(a) * ((float) len));
195 y_2 = y + (short) (SINF(a) * ((float) len));
196
197 XDrawLine(display, MI_WINDOW(mi), gc, x, y, x_2, y_2);
198 XDrawLine(display, MI_WINDOW(mi), gc, x + 1, y, x_2 + 1, y_2);
199
200 draw_tree(mi, x_2, y_2, (len * REDUCE) / 100, a, as, c, 1);
201 }
202 if (++fp->time > MI_CYCLES(mi)) {
203 init_forest(mi);
204 }
205 }
206
207 ENTRYPOINT void
release_forest(ModeInfo * mi)208 release_forest(ModeInfo * mi)
209 {
210 if (forests != NULL) {
211 free(forests);
212 forests = (foreststruct *) NULL;
213 }
214 }
215
216 #ifndef STANDALONE
217 ENTRYPOINT void
refresh_forest(ModeInfo * mi)218 refresh_forest(ModeInfo * mi)
219 {
220 MI_CLEARWINDOW(mi);
221 }
222 #endif
223
224 XSCREENSAVER_MODULE ("Forest", forest)
225
226 #endif /* MODE_forest */
227