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