1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* forest --- binary trees in a fractal forest */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)forest.c 5.00 2000/11/01 xlockmore";
10 * Copyright (c) 1995 Pascal Pensa <pensa@aurora.unice.fr>
12 * Original idea : Guillaume Ramey <ramey@aurora.unice.fr>
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.
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.
27 * 01-Nov-2000: Allocation checks
28 * 10-May-1997: Compatible with xscreensaver
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" \
42 #define UNIFORM_COLORS
43 #include "xlockmore.h" /* in xscreensaver distribution */
44 #else /* STANDALONE */
45 #include "xlock.h" /* in xlockmore distribution */
47 #endif /* STANDALONE */
51 ModeSpecOpt forest_opts =
52 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
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};
65 #define MINHEIGHT 20 /* Tree height range */
68 #define MINANGLE 15 /* (degree) angle between soon */
70 #define RANDANGLE 15 /* (degree) Max random angle from default */
72 #define REDUCE 90 /* Height % from father */
74 #define ITERLEVEL 10 /* Tree iteration */
76 #define COLORSPEED 2 /* Color increment */
78 /* degree to radian */
79 #define DEGTORAD(x) (((float)(x)) * M_PI / 180.0)
81 #define RANGE_RAND(min,max) ((min) + NRAND((max) - (min)))
86 int time; /* up time */
90 static foreststruct *forests = (foreststruct *) NULL;
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)
101 /* Father's angle step */
103 Display *display = MI_DISPLAY(mi);
104 Window window = MI_WINDOW(mi);
106 short x_1, y_1, x_2, y_2;
111 a1 = a + as + DEGTORAD(NRAND(2 * RANDANGLE) - RANDANGLE);
113 x_1 = x + (short) (COSF(a1) * ((float) len));
114 y_1 = y + (short) (SINF(a1) * ((float) len));
118 a2 = a - as + DEGTORAD(NRAND(2 * RANDANGLE) - RANDANGLE);
120 x_2 = x + (short) (COSF(a2) * ((float) len));
121 y_2 = y + (short) (SINF(a2) * ((float) len));
123 if (MI_NPIXELS(mi) > 2) {
124 XSetForeground(display, gc, MI_PIXEL(mi, c));
125 c = (c + COLORSPEED) % MI_NPIXELS(mi);
127 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
129 XDrawLine(display, window, gc, x, y, x_1, y_1);
130 XDrawLine(display, window, gc, x, y, x_2, y_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);
136 len = (len * REDUCE * 10) / 1000;
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);
145 init_forest(ModeInfo * mi)
149 if (forests == NULL) {
150 if ((forests = (foreststruct *) calloc(MI_NUM_SCREENS(mi),
151 sizeof (foreststruct))) == NULL)
154 fp = &forests[MI_SCREEN(mi)];
156 fp->width = MI_WIDTH(mi);
157 fp->height = MI_HEIGHT(mi);
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;
170 draw_forest(ModeInfo * mi)
172 Display *display = MI_DISPLAY(mi);
174 short x, y, x_2, y_2, len, c = 0;
180 fp = &forests[MI_SCREEN(mi)];
182 MI_IS_DRAWN(mi) = True;
183 if (fp->time < fp->ntrees) {
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;
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);
196 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
198 x_2 = x + (short) (COSF(a) * ((float) len));
199 y_2 = y + (short) (SINF(a) * ((float) len));
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);
204 draw_tree(mi, x_2, y_2, (len * REDUCE) / 100, a, as, c, 1);
206 if (++fp->time > MI_CYCLES(mi)) {
212 release_forest(ModeInfo * mi)
214 if (forests != NULL) {
215 (void) free((void *) forests);
216 forests = (foreststruct *) NULL;
221 refresh_forest(ModeInfo * mi)
226 #endif /* MODE_forest */