http://www.jwz.org/xscreensaver/xscreensaver-5.07.tar.gz
[xscreensaver] / hacks / forest.c
1 /* forest.c (aka xtree.c), Copyright (c) 1999
2  *  Peter Baumung <unn6@rz.uni-karlsruhe.de>
3  *
4  * Most code taken from
5  *  xscreensaver, Copyright (c) 1992, 1995, 1997 
6  *  Jamie Zawinski <jwz@netscape.com>
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that
11  * copyright notice and this permission notice appear in supporting
12  * documentation.  No representations are made about the suitability of this
13  * software for any purpose.  It is provided "as is" without express or 
14  * implied warranty.
15  */
16
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20
21 /* ******************************   NOTE   ******************************
22
23      This is not the xlockmore version of forest, but a much better
24      looking rewrite.  Be careful not to delete it in a merging frenzy...
25
26    **********************************************************************
27  */
28
29 #ifdef STANDALONE
30 # define DEFAULTS   "*delay:            500000 \n"  \
31                     "*ncolors:          20     \n" \
32                     "*fpsSolid:          true     \n" \
33
34 # include "xlockmore.h"     /* from the xscreensaver distribution */
35 # define refresh_trees 0
36 # define reshape_trees 0
37 # define trees_handle_event 0
38 #else  /* !STANDALONE */
39 # include "xlock.h"                                     /* from the xlockmore distribution */
40 #endif /* !STANDALONE */
41
42 ENTRYPOINT ModeSpecOpt trees_opts = {0, NULL, 0, NULL, NULL};
43
44 typedef struct {
45   int     x;
46   int     y;
47   int     thick;
48   double  size;
49   long    color;
50   int     toDo;
51   int     pause;
52   int     season;
53 } treestruct;
54
55 static treestruct *trees = NULL;
56
57 static XColor colors[20];
58 static int color;
59
60 static long colorM[12] = {0xff0000, 0xff8000, 0xffff00, 0x80ff00,
61                           0x00ff00, 0x00ff80, 0x00ffff, 0x0080ff,
62                           0x0000ff, 0x8000ff, 0xff00ff, 0xff0080};
63
64 static long colorV[12] = {0x0a0000, 0x0a0500, 0x0a0a00, 0x050a00,
65                           0x000a00, 0x000a05, 0x000a0a, 0x00050a,
66                           0x00000a, 0x05000a, 0x0a000a, 0x0a0005};
67
68 ENTRYPOINT void
69 init_trees(ModeInfo * mi)
70 {
71         unsigned long                   pixels[20];
72   treestruct       *tree;
73   Display          *display = MI_DISPLAY(mi);
74   GC                gc = MI_GC(mi);
75   int               i;
76
77   if (trees == NULL) {
78     trees = (treestruct *) calloc(MI_NUM_SCREENS(mi), sizeof (treestruct));
79     if (trees == NULL) {
80       return;
81     }
82                 
83     if (mi->npixels > 20) {
84                         printf("%d colors selected. Setting limit to 20...\n", mi->npixels);
85                         mi->npixels = 20;
86                 }
87                         
88                 if (mi->npixels < 4) {
89       for (i = 0; i < mi->npixels; i++) {
90         colors[i].red   = 65535 * (i & 1);
91         colors[i].green = 65535 * (i & 1);
92         colors[i].blue  = 65535 * (i & 1);
93         colors[i].flags = DoRed | DoGreen | DoBlue;
94       }
95     } else {
96       if (mi->npixels < 8) {
97         for (i = 0; i < mi->npixels; i++) {
98           colors[i].red   = 32768 + 4096 * (i % 4);
99           colors[i].green = 32768 + 4096 * (i % 4);
100           colors[i].blue  = 32768 + 4096 * (i % 4);
101           colors[i].flags = DoRed | DoGreen | DoBlue;
102         }
103       } else {
104         for (i = 0; i < mi->npixels; i++) {
105           colors[i].red   = 24576 + 4096 * (i % 4);
106           colors[i].green = 10240 + 2048 * (i % 4);
107           colors[i].blue  =  0;
108           colors[i].flags = DoRed | DoGreen | DoBlue;
109         }
110       }
111     }
112
113     for (i = 0; i < mi->npixels; i++)
114       if (!XAllocColor(display, mi->xgwa.colormap, &colors[i])) break;
115     color = i;
116
117     XSetForeground(display, gc, colors[1].pixel);
118   }
119
120   XClearWindow(display, MI_WINDOW(mi));
121   XSetLineAttributes(display, gc, 2, LineSolid, CapButt, JoinMiter);
122   tree = &trees[MI_SCREEN(mi)];
123   tree->toDo   = 25;
124         tree->season = NRAND(12);
125
126   for (i = 4; i < mi->npixels; i++) {
127     int sIndex = (tree->season + (i-4) / 4) % 12;
128     long color = colorM[sIndex] - 2 * colorV[sIndex] * (i % 4);
129     colors[i].red = (color & 0xff0000) / 256;
130     colors[i].green = (color & 0x00ff00);
131     colors[i].blue = (color & 0x0000ff) * 256;
132     colors[i].flags = DoRed | DoGreen | DoBlue;
133   }
134
135         for (i = 0; i < color; i++)
136                 pixels[i] = colors[i].pixel;
137                         
138   XFreeColors(display, mi->xgwa.colormap, pixels, mi->npixels, 0L);
139
140         for (i = 0; i < mi->npixels; i++)
141     if (!XAllocColor(display, mi->xgwa.colormap, &colors[i])) break;
142
143   color = i;
144 }
145
146 static double rRand(double a, double b)
147 {
148   return (a+(b-a)*NRAND(10001)/10000.0);
149 }
150
151 static void draw_line(ModeInfo * mi,
152                       int x1, int y1, int x2, int y2,
153                       double angle, int widths, int widthe)
154 {
155
156   Display    *display = MI_DISPLAY(mi);
157   GC          gc = MI_GC(mi);
158   double      sns = 0.5*widths*sin(angle + M_PI_2);
159   double      css = 0.5*widths*cos(angle + M_PI_2);
160   double      sne = 0.5*widthe*sin(angle + M_PI_2);
161   double      cse = 0.5*widthe*cos(angle + M_PI_2);
162
163   int         xs1 = (int) (x1-sns);
164   int         xs2 = (int) (x1+sns);
165   int         ys1 = (int) (y1-css);
166   int         ys2 = (int) (y1+css);
167   int         xe1 = (int) (x2-sne);
168   int         xe2 = (int) (x2+sne);
169   int         ye1 = (int) (y2-cse);
170   int         ye2 = (int) (y2+cse);
171   int         i;
172
173   for (i = 0; i < widths; i++) {
174     if (color >= 4)
175       XSetForeground(display, gc, colors[i*4/widths].pixel);
176     XDrawLine(display, MI_WINDOW(mi), gc,
177       xs1+(xs2-xs1)*i/widths, ys1+(ys2-ys1)*i/widths,
178       xe1+(xe2-xe1)*i/widths, ye1+(ye2-ye1)*i/widths);
179   }
180 }
181
182 static void draw_tree_rec(ModeInfo * mi, double thick, int x, int y, double angle)
183 {
184   treestruct *tree = &trees[MI_SCREEN(mi)];
185   Display    *display = MI_DISPLAY(mi);
186   GC          gc = MI_GC(mi);
187   int         length = (24+NRAND(12))*tree->size;
188   int         a = (int) (x - length*sin(angle));
189   int         b = (int) (y - length*cos(angle));
190   int         i;
191
192   draw_line(mi, x, y, a, b, angle, thick*tree->size, 0.68*thick*tree->size);
193
194   if (thick > 2) {
195     draw_tree_rec(mi, 0.68*thick, a, b, 0.8*angle+rRand(-0.2, 0.2));
196     if (thick < tree->thick-1) {
197       draw_tree_rec(mi, 0.68*thick, a, b, angle+rRand(0.2, 0.9));
198       draw_tree_rec(mi, 0.68*thick, (a+x)/2, (b+y)/2, angle-rRand(0.2, 0.9));
199     }
200   }
201
202   if (thick < 0.5*tree->thick) {
203         int     nleaf = 12 + NRAND(4);
204           XArc    leaf[16];
205     for (i = 0; i < nleaf; i++) {
206       leaf[i].x = a + (int) (tree->size * rRand(-12, 12));
207       leaf[i].y = b + (int) (tree->size * rRand(-12, 12));
208       leaf[i].width = (int) (tree->size * rRand(2, 6));
209       leaf[i].height = leaf[i].width;
210       leaf[i].angle1 = 0;
211       leaf[i].angle2 = 360 * 64;
212     }
213     if (mi->npixels >= 4)
214       XSetForeground(display, gc, colors[tree->color+NRAND(4)].pixel);
215     XFillArcs(display, MI_WINDOW(mi), gc, leaf, nleaf);
216   }
217 }
218
219 ENTRYPOINT void
220 draw_trees(ModeInfo * mi)
221 {
222   treestruct *tree = &trees[MI_SCREEN(mi)];
223         int                                     width = MI_WIN_WIDTH(mi);
224         int                                     height = MI_WIN_HEIGHT(mi);
225
226   if (tree->pause == 1) {
227     tree->pause--;
228     init_trees(mi);
229   } else if (tree->pause > 1) {
230     tree->pause--;
231     return;
232   } else if (--(tree->toDo) == 0) {
233     tree->pause = 6;
234     return;
235   }
236
237   tree->x        = NRAND(width);
238   tree->y        = (int) (1.25 * height * (1 - tree->toDo / 23.0));
239   tree->thick    = rRand(7, 12);
240   tree->size     = height / 480.0;
241   if (color < 8) {
242     tree->color = 0;
243   } else {
244     tree->color    = 4 * (1 + NRAND(color / 4 - 1));
245   }
246
247   draw_tree_rec(mi, tree->thick, tree->x, tree->y, rRand(-0.1, 0.1));
248 }
249
250
251 ENTRYPOINT void
252 release_trees(ModeInfo * mi)
253 {
254   if (trees != NULL) {
255     (void) free((void *) trees);
256     trees = NULL;
257   }
258 }
259
260 XSCREENSAVER_MODULE_2 ("Forest", forest, trees)