1 /* -*- Mode: C; tab-width: 4 -*-
2 * forest.c --- draw a fractal forest.
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)forest.c 4.03 97/05/10 xlockmore";
8 /* Copyright (c) 1995 Pascal Pensa <pensa@aurora.unice.fr>
10 * Original idea : Guillaume Ramey <ramey@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 "Forest"
30 # define HACK_INIT init_forest
31 # define HACK_DRAW draw_forest
32 # define forest_opts xlockmore_opts
33 # define DEFAULTS "*count: 100 \n" \
37 # define UNIFORM_COLORS
38 # include "xlockmore.h" /* from the xscreensaver distribution */
40 #else /* !STANDALONE */
41 # include "xlock.h" /* from the xlockmore distribution */
42 #endif /* !STANDALONE */
44 ModeSpecOpt forest_opts = {
45 0, NULL, 0, NULL, NULL };
50 #define MINHEIGHT 20 /* Tree height range */
53 #define MINANGLE 15 /* (degree) angle between soon */
55 #define RANDANGLE 15 /* (degree) Max random angle from default */
57 #define REDUCE 90 /* Height % from father */
59 #define ITERLEVEL 10 /* Tree iteration */
61 #define COLORSPEED 2 /* Color increment */
63 /* degree to radian */
64 #define DEGTORAD(x) (((float)(x)) * M_PI / 180.0)
66 #define RANGE_RAND(min,max) ((min) + NRAND((max) - (min)))
71 int time; /* up time */
75 static foreststruct *forests = NULL;
78 draw_tree(ModeInfo * mi,
79 short int x, short int y, short int len,
80 float a, float as, short int c, short int level)
86 /* Father's angle step */
88 Display *display = MI_DISPLAY(mi);
89 Window window = MI_WINDOW(mi);
91 short x_1, y_1, x_2, y_2;
96 a1 = a + as + DEGTORAD(NRAND(2 * RANDANGLE) - RANDANGLE);
98 x_1 = x + (short) (COSF(a1) * ((float) len));
99 y_1 = y + (short) (SINF(a1) * ((float) len));
103 a2 = a - as + DEGTORAD(NRAND(2 * RANDANGLE) - RANDANGLE);
105 x_2 = x + (short) (COSF(a2) * ((float) len));
106 y_2 = y + (short) (SINF(a2) * ((float) len));
108 if (MI_NPIXELS(mi) > 2) {
109 XSetForeground(display, gc, MI_PIXEL(mi, c));
110 c = (c + COLORSPEED) % MI_NPIXELS(mi);
112 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
114 XDrawLine(display, window, gc, x, y, x_1, y_1);
115 XDrawLine(display, window, gc, x, y, x_2, y_2);
118 XDrawLine(display, window, gc, x + 1, y, x_1 + 1, y_1);
119 XDrawLine(display, window, gc, x + 1, y, x_2 + 1, y_2);
121 len = (len * REDUCE * 10) / 1000;
123 if (level < ITERLEVEL) {
124 draw_tree(mi, x_1, y_1, len, a1, as, c, level + 1);
125 draw_tree(mi, x_2, y_2, len, a2, as, c, level + 1);
130 init_forest(ModeInfo * mi)
134 if (forests == NULL) {
135 if ((forests = (foreststruct *) calloc(MI_NUM_SCREENS(mi),
136 sizeof (foreststruct))) == NULL)
139 fp = &forests[MI_SCREEN(mi)];
141 fp->width = MI_WIN_WIDTH(mi);
142 fp->height = MI_WIN_HEIGHT(mi);
145 fp->ntrees = MI_BATCHCOUNT(mi);
146 if (fp->ntrees < -MINTREES)
147 fp->ntrees = NRAND(-fp->ntrees - MINTREES + 1) + MINTREES;
148 else if (fp->ntrees < MINTREES)
149 fp->ntrees = MINTREES;
150 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
154 draw_forest(ModeInfo * mi)
156 Display *display = MI_DISPLAY(mi);
158 foreststruct *fp = &forests[MI_SCREEN(mi)];
159 short x, y, x_2, y_2, len, c = 0;
162 if (fp->time < fp->ntrees) {
164 x = RANGE_RAND(0, fp->width);
165 y = RANGE_RAND(0, fp->height + MAXHEIGHT);
166 a = -M_PI / 2.0 + DEGTORAD(NRAND(2 * RANDANGLE) - RANDANGLE);
167 as = DEGTORAD(RANGE_RAND(MINANGLE, MAXANGLE));
168 len = ((RANGE_RAND(MINHEIGHT, MAXHEIGHT) * (fp->width / 20)) / 50) + 2;
170 if (MI_NPIXELS(mi) > 2) {
171 c = NRAND(MI_NPIXELS(mi));
172 XSetForeground(display, gc, MI_PIXEL(mi, c));
173 c = (c + COLORSPEED) % MI_NPIXELS(mi);
175 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
177 x_2 = x + (short) (COSF(a) * ((float) len));
178 y_2 = y + (short) (SINF(a) * ((float) len));
180 XDrawLine(display, MI_WINDOW(mi), gc, x, y, x_2, y_2);
181 XDrawLine(display, MI_WINDOW(mi), gc, x + 1, y, x_2 + 1, y_2);
183 draw_tree(mi, x_2, y_2, (len * REDUCE) / 100, a, as, c, 1);
185 if (++fp->time > MI_CYCLES(mi)) {
187 erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
188 #endif /* STANDALONE */
194 release_forest(ModeInfo * mi)
196 if (forests != NULL) {
197 (void) free((void *) forests);
203 refresh_forest(ModeInfo * mi)
205 foreststruct *fp = &forests[MI_SCREEN(mi)];
207 if (fp->time < fp->ntrees)
208 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));