0a64c0609a11087f58db86dba8ac4d83a6d7b86e
[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 #include "config.h"
18
19 #ifdef STANDALONE
20 # define PROGCLASS          "Forest" /*"XTree"*/
21 # define HACK_INIT          init_trees
22 # define HACK_DRAW          draw_trees
23 # define trees_opts         xlockmore_opts 
24 # define DEFAULTS   "*delay:            500000 \n"  \
25                     "*ncolors:          20     \n"  \
26                     "*eraseSpeed:       400    \n"  \
27                     "*eraseMode:        -1     \n"  \
28                     "*installColormap   False"
29 # include "xlockmore.h"     /* from the xscreensaver distribution */
30 #else  /* !STANDALONE */
31 # include "xlock.h"                                     /* from the xlockmore distribution */
32 #endif /* !STANDALONE */
33
34 ModeSpecOpt trees_opts = {0, NULL, 0, NULL, NULL};
35
36 typedef struct {
37   int     x;
38   int     y;
39   int     thick;
40   double  size;
41   long    color;
42   int     toDo;
43   int     season;
44 } treestruct;
45
46 static treestruct *trees = NULL;
47
48 XColor colors[20];
49 int color;
50
51 static long colorM[12] = {0xff0000, 0xff8000, 0xffff00, 0x80ff00,
52                           0x00ff00, 0x00ff80, 0x00ffff, 0x0080ff,
53                           0x0000ff, 0x8000ff, 0xff00ff, 0xff0080};
54
55 static long colorV[12] = {0x0a0000, 0x0a0500, 0x0a0a00, 0x050a00,
56                           0x000a00, 0x000a05, 0x000a0a, 0x00050a,
57                           0x00000a, 0x05000a, 0x0a000a, 0x0a0005};
58
59 void init_trees(ModeInfo * mi) {
60         unsigned long                   pixels[20];
61   treestruct       *tree;
62   Display          *display = MI_DISPLAY(mi);
63   GC                gc = MI_GC(mi);
64   int               i;
65
66   if (trees == NULL) {
67     trees = (treestruct *) calloc(MI_NUM_SCREENS(mi), sizeof (treestruct));
68     if (trees == NULL) {
69       return;
70     }
71                 
72     if (mi->npixels > 20) {
73                         printf("%d colors selected. Setting limit to 20...\n", mi->npixels);
74                         mi->npixels = 20;
75                 }
76                         
77                 if (mi->npixels < 4) {
78       for (i = 0; i < mi->npixels; i++) {
79         colors[i].red   = 65535 * (i & 1);
80         colors[i].green = 65535 * (i & 1);
81         colors[i].blue  = 65535 * (i & 1);
82         colors[i].flags = DoRed | DoGreen | DoBlue;
83       }
84     } else {
85       if (mi->npixels < 8) {
86         for (i = 0; i < mi->npixels; i++) {
87           colors[i].red   = 32768 + 4096 * (i % 4);
88           colors[i].green = 32768 + 4096 * (i % 4);
89           colors[i].blue  = 32768 + 4096 * (i % 4);
90           colors[i].flags = DoRed | DoGreen | DoBlue;
91         }
92       } else {
93         for (i = 0; i < mi->npixels; i++) {
94           colors[i].red   = 24576 + 4096 * (i % 4);
95           colors[i].green = 10240 + 2048 * (i % 4);
96           colors[i].blue  =  0;
97           colors[i].flags = DoRed | DoGreen | DoBlue;
98         }
99       }
100     }
101
102     for (i = 0; i < mi->npixels; i++)
103       if (!XAllocColor(display, mi->xgwa.colormap, &colors[i])) break;
104     color = i;
105
106     XSetForeground(display, gc, colors[1].pixel);
107   }
108
109   XClearWindow(display, MI_WINDOW(mi));
110   XSetLineAttributes(display, gc, 2, LineSolid, CapButt, JoinMiter);
111   tree = &trees[MI_SCREEN(mi)];
112   tree->toDo   = 25;
113         tree->season = NRAND(12);
114
115   for (i = 4; i < mi->npixels; i++) {
116     int sIndex = (tree->season + (i-4) / 4) % 12;
117     long color = colorM[sIndex] - 2 * colorV[sIndex] * (i % 4);
118     colors[i].red = (color & 0xff0000) / 256;
119     colors[i].green = (color & 0x00ff00);
120     colors[i].blue = (color & 0x0000ff) * 256;
121     colors[i].flags = DoRed | DoGreen | DoBlue;
122   }
123
124         for (i = 0; i < color; i++)
125                 pixels[i] = colors[i].pixel;
126                         
127   XFreeColors(display, mi->xgwa.colormap, pixels, mi->npixels, 0L);
128
129         for (i = 0; i < mi->npixels; i++)
130     if (!XAllocColor(display, mi->xgwa.colormap, &colors[i])) break;
131
132   color = i;
133 }
134
135 double rRand(double a, double b) {
136   return (a+(b-a)*NRAND(10001)/10000.0);
137 }
138
139 void draw_line(ModeInfo * mi,
140                int x1, int y1, int x2, int y2,
141                double angle, int widths, int widthe) {
142
143   Display    *display = MI_DISPLAY(mi);
144   GC          gc = MI_GC(mi);
145   double      sns = 0.5*widths*sin(angle + M_PI_2);
146   double      css = 0.5*widths*cos(angle + M_PI_2);
147   double      sne = 0.5*widthe*sin(angle + M_PI_2);
148   double      cse = 0.5*widthe*cos(angle + M_PI_2);
149
150   int         xs1 = (int) (x1-sns);
151   int         xs2 = (int) (x1+sns);
152   int         ys1 = (int) (y1-css);
153   int         ys2 = (int) (y1+css);
154   int         xe1 = (int) (x2-sne);
155   int         xe2 = (int) (x2+sne);
156   int         ye1 = (int) (y2-cse);
157   int         ye2 = (int) (y2+cse);
158   int         i;
159
160   for (i = 0; i < widths; i++) {
161     if (color >= 4)
162       XSetForeground(display, gc, colors[i*4/widths].pixel);
163     XDrawLine(display, MI_WINDOW(mi), gc,
164       xs1+(xs2-xs1)*i/widths, ys1+(ys2-ys1)*i/widths,
165       xe1+(xe2-xe1)*i/widths, ye1+(ye2-ye1)*i/widths);
166   }
167 }
168
169 void draw_tree_rec(ModeInfo * mi, double thick, int x, int y, double angle) {
170   treestruct *tree = &trees[MI_SCREEN(mi)];
171   Display    *display = MI_DISPLAY(mi);
172   GC          gc = MI_GC(mi);
173   int         length = (24+NRAND(12))*tree->size;
174   int         a = (int) (x - length*sin(angle));
175   int         b = (int) (y - length*cos(angle));
176   int         i;
177
178   draw_line(mi, x, y, a, b, angle, thick*tree->size, 0.68*thick*tree->size);
179
180   if (thick > 2) {
181     draw_tree_rec(mi, 0.68*thick, a, b, 0.8*angle+rRand(-0.2, 0.2));
182     if (thick < tree->thick-1) {
183       draw_tree_rec(mi, 0.68*thick, a, b, angle+rRand(0.2, 0.9));
184       draw_tree_rec(mi, 0.68*thick, (a+x)/2, (b+y)/2, angle-rRand(0.2, 0.9));
185     }
186   }
187
188   if (thick < 0.5*tree->thick) {
189         int     nleaf = 12 + NRAND(4);
190           XArc    leaf[16];
191     for (i = 0; i < nleaf; i++) {
192       leaf[i].x = a + (int) (tree->size * rRand(-12, 12));
193       leaf[i].y = b + (int) (tree->size * rRand(-12, 12));
194       leaf[i].width = (int) (tree->size * rRand(2, 6));
195       leaf[i].height = leaf[i].width;
196       leaf[i].angle1 = 0;
197       leaf[i].angle2 = 360 * 64;
198     }
199     if (mi->npixels >= 4)
200       XSetForeground(display, gc, colors[tree->color+NRAND(4)].pixel);
201     XFillArcs(display, MI_WINDOW(mi), gc, leaf, nleaf);
202   }
203 }
204
205 void draw_trees(ModeInfo * mi) {
206   treestruct *tree = &trees[MI_SCREEN(mi)];
207         int                                     width = MI_WIN_WIDTH(mi);
208         int                                     height = MI_WIN_HEIGHT(mi);
209
210   if (--(tree->toDo) == 0) {
211     usleep(3000000);
212     init_trees(mi);
213   }
214
215   tree->x        = NRAND(width);
216   tree->y        = (int) (1.25 * height * (1 - tree->toDo / 23.0));
217   tree->thick    = rRand(7, 12);
218   tree->size     = height / 480.0;
219   if (color < 8) {
220     tree->color = 0;
221   } else {
222     tree->color    = 4 * (1 + NRAND(color / 4 - 1));
223   }
224
225   draw_tree_rec(mi, tree->thick, tree->x, tree->y, rRand(-0.1, 0.1));
226 }
227
228 void release_trees(ModeInfo * mi) {
229   if (trees != NULL) {
230     (void) free((void *) trees);
231     trees = NULL;
232   }
233 }