http://slackware.bholcomb.com/slackware/slackware-11.0/source/xap/xscreensaver/xscree...
[xscreensaver] / hacks / vines.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* vines --- vine fractals */
3
4 #if 0
5 static const char sccsid[] = "@(#)vines.c       5.00 2000/11/01 xlockmore";
6 #endif
7
8 /*-
9  * Copyright (c) 1997 by Tracy Camp campt@hurrah.com
10  *
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation for any purpose and without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and that
14  * both that copyright notice and this permission notice appear in
15  * supporting documentation.
16  *
17  * This file is provided AS IS with no warranties of any kind.  The author
18  * shall have no liability with respect to the infringement of copyrights,
19  * trade secrets or any patents by this file or any part thereof.  In no
20  * event will the author be liable for any lost revenue or profits or
21  * other special, indirect and consequential damages.
22  *
23  * If you make a modification I would of course appreciate a copy.
24  *
25  * Revision History:
26  * 01-Nov-2000: Allocation checks
27  * 11-Jul-1997: David Hansen <dhansen@metapath.com>
28  *              Changed names to vines and modified draw loop
29  *              to honor batchcount so vines can be grown or plotted.
30  * 10-May-1997: Compatible with xscreensaver
31  * 21-Mar-1997: David Hansen <dhansen@metapath.com>
32  *              Updated mode to draw complete patterns on every
33  *              iteration instead of growing the vine.  Also made
34  *              adjustments to randomization and changed variable
35  *              names to make logic easier to follow.
36  */
37
38 /*-
39  * This was modifed from a 'screen saver' that a friend and I
40  * wrote on our TI-8x calculators in high school physics one day
41  * Basically another geometric pattern generator, this ones claim
42  * to fame is a pseudo-fractal looking vine like pattern that creates
43  * nifty whorls and loops.
44  */
45
46 #ifdef STANDALONE
47 # define MODE_vines
48 # define DEFAULTS       "*delay: 200000 \n" \
49                                         "*count: 0 \n" \
50                                         "*ncolors: 64 \n"
51 # include "xlockmore.h"         /* in xscreensaver distribution */
52 # define reshape_vines 0
53 # define vines_handle_event 0
54 # include "erase.h"
55 #else /* STANDALONE */
56 # include "xlock.h"             /* in xlockmore distribution */
57 #endif /* STANDALONE */
58
59 #ifdef MODE_vines
60
61 ENTRYPOINT ModeSpecOpt vines_opts =
62 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
63
64 #ifdef USE_MODULES
65 ModStruct   vines_description =
66 {"vines", "init_vines", "draw_vines", "release_vines",
67  "refresh_vines", "init_vines", (char *) NULL, &vines_opts,
68  200000, 0, 1, 1, 64, 1.0, "",
69  "Shows fractals", 0, NULL};
70
71 #endif
72
73 typedef struct {
74         int         a;
75         int         x1;
76         int         y1;
77         int         x2;
78         int         y2;
79         int         i;
80         int         length;
81         int         iterations;
82         int         constant;
83         int         ang;
84         int         centerx;
85         int         centery;
86 #ifdef STANDALONE
87   eraser_state *eraser;
88 #endif
89 } vinestruct;
90
91 static vinestruct *vines = (vinestruct *) NULL;
92
93 ENTRYPOINT void
94 refresh_vines(ModeInfo * mi)
95 {
96         MI_CLEARWINDOW(mi);
97 }                               /* refresh_vines */
98
99 ENTRYPOINT void
100 init_vines(ModeInfo * mi)
101 {
102         vinestruct *fp;
103
104         if (vines == NULL) {
105                 if ((vines = (vinestruct *) calloc(MI_NUM_SCREENS(mi),
106                                              sizeof (vinestruct))) == NULL) {
107                         return;
108                 }
109         }
110         fp = &vines[MI_SCREEN(mi)];
111
112         fp->i = 0;
113         fp->length = 0;
114         fp->iterations = 30 + NRAND(100);
115
116 #ifndef STANDALONE
117         MI_CLEARWINDOW(mi);
118 #endif
119 }                               /* init_vines */
120
121 ENTRYPOINT void
122 draw_vines(ModeInfo * mi)
123 {
124         Display    *display = MI_DISPLAY(mi);
125         GC          gc = MI_GC(mi);
126         int         count;
127         vinestruct *fp;
128
129         if (vines == NULL)
130                 return;
131         fp = &vines[MI_SCREEN(mi)];
132
133 #ifdef STANDALONE
134     if (fp->eraser) {
135       fp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), fp->eraser);
136       return;
137     }
138 #endif
139
140         /* MI_IS_DRAWN(mi) = True; */
141         if (fp->i >= fp->length) {
142                 if (--(fp->iterations) == 0) {
143 #ifdef STANDALONE
144           fp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), fp->eraser);
145 #endif /* STANDALONE */
146                         init_vines(mi);
147                 }
148                 fp->centerx = NRAND(MI_WIDTH(mi));
149                 fp->centery = NRAND(MI_HEIGHT(mi));
150
151                 fp->ang = 60 + NRAND(720);
152                 fp->length = 100 + NRAND(3000);
153                 fp->constant = fp->length * (10 + NRAND(10));
154
155                 fp->i = 0;
156                 fp->a = 0;
157                 fp->x1 = 0;
158                 fp->y1 = 0;
159                 fp->x2 = 1;
160                 fp->y2 = 0;
161
162                 if (MI_NPIXELS(mi) > 2)
163                         XSetForeground(display, gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
164                 else
165                         XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
166         }
167         count = fp->i + MI_COUNT(mi);
168         if ((count <= fp->i) || (count > fp->length))
169                 count = fp->length;
170
171         while (fp->i < count) {
172                 XDrawLine(display, MI_WINDOW(mi), gc,
173                           fp->centerx + (fp->x1 / fp->constant),
174                           fp->centery - (fp->y1 / fp->constant),
175                           fp->centerx + (fp->x2 / fp->constant),
176                           fp->centery - (fp->y2 / fp->constant));
177
178                 fp->a += (fp->ang * fp->i);
179
180                 fp->x1 = fp->x2;
181                 fp->y1 = fp->y2;
182
183                 fp->x2 += (int) (fp->i * (cos((double) fp->a) * 360.0) / (2.0 * M_PI));
184                 fp->y2 += (int) (fp->i * (sin((double) fp->a) * 360.0) / (2.0 * M_PI));
185                 fp->i++;
186         }
187 }                               /* draw_vines */
188
189 ENTRYPOINT void
190 release_vines(ModeInfo * mi)
191 {
192         if (vines != NULL) {
193                 (void) free((void *) vines);
194                 vines = (vinestruct *) NULL;
195         }
196 }                               /* release_vines */
197
198
199 XSCREENSAVER_MODULE ("Vines", vines)
200
201 #endif /* MODE_vines */