From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[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                                         "*fpsSolid: true \n" \
52                                     "*lowrez: True \n" \
53
54 # include "xlockmore.h"         /* in xscreensaver distribution */
55 # define free_vines 0
56 # define release_vines 0
57 # define reshape_vines 0
58 # define vines_handle_event 0
59 #else /* STANDALONE */
60 # include "xlock.h"             /* in xlockmore distribution */
61 #endif /* STANDALONE */
62
63 #ifdef MODE_vines
64
65 ENTRYPOINT ModeSpecOpt vines_opts =
66 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
67
68 #ifdef USE_MODULES
69 ModStruct   vines_description =
70 {"vines", "init_vines", "draw_vines", (char *) NULL,
71  "refresh_vines", "init_vines", (char *) NULL, &vines_opts,
72  200000, 0, 1, 1, 64, 1.0, "",
73  "Shows fractals", 0, NULL};
74
75 #endif
76
77 typedef struct {
78         int         a;
79         int         x1;
80         int         y1;
81         int         x2;
82         int         y2;
83         int         i;
84         int         length;
85         int         iterations;
86         int         constant;
87         int         ang;
88         int         centerx;
89         int         centery;
90 } vinestruct;
91
92 static vinestruct *vines = (vinestruct *) NULL;
93
94 #ifndef STANDALONE
95 ENTRYPOINT void
96 refresh_vines(ModeInfo * mi)
97 {
98         MI_CLEARWINDOW(mi);
99 }                               /* refresh_vines */
100 #endif
101
102 ENTRYPOINT void
103 init_vines(ModeInfo * mi)
104 {
105         vinestruct *fp;
106
107         MI_INIT (mi, vines);
108         fp = &vines[MI_SCREEN(mi)];
109
110         fp->i = 0;
111         fp->length = 0;
112         fp->iterations = 30 + NRAND(100);
113
114         MI_CLEARWINDOW(mi);
115 }                               /* init_vines */
116
117 ENTRYPOINT void
118 draw_vines(ModeInfo * mi)
119 {
120         Display    *display = MI_DISPLAY(mi);
121         GC          gc = MI_GC(mi);
122         int         count;
123         vinestruct *fp;
124
125         if (vines == NULL)
126                 return;
127         fp = &vines[MI_SCREEN(mi)];
128
129         MI_IS_DRAWN(mi) = True;
130         if (fp->i >= fp->length) {
131                 if (--(fp->iterations) == 0) {
132                         init_vines(mi);
133                         return;
134                 }
135                 fp->centerx = NRAND(MI_WIDTH(mi));
136                 fp->centery = NRAND(MI_HEIGHT(mi));
137
138                 fp->ang = 60 + NRAND(720);
139                 fp->length = 100 + NRAND(3000);
140                 fp->constant = fp->length * (10 + NRAND(10));
141
142                 fp->i = 0;
143                 fp->a = 0;
144                 fp->x1 = 0;
145                 fp->y1 = 0;
146                 fp->x2 = 1;
147                 fp->y2 = 0;
148
149                 if (MI_NPIXELS(mi) > 2)
150                         XSetForeground(display, gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
151                 else
152                         XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
153         }
154         count = fp->i + MI_COUNT(mi);
155         if ((count <= fp->i) || (count > fp->length))
156                 count = fp->length;
157
158         while (fp->i < count) {
159                 XDrawLine(display, MI_WINDOW(mi), gc,
160                           fp->centerx + (fp->x1 / fp->constant),
161                           fp->centery - (fp->y1 / fp->constant),
162                           fp->centerx + (fp->x2 / fp->constant),
163                           fp->centery - (fp->y2 / fp->constant));
164
165                 fp->a += (fp->ang * fp->i);
166
167                 fp->x1 = fp->x2;
168                 fp->y1 = fp->y2;
169
170                 fp->x2 += (int) (fp->i * (cos((double) fp->a) * 360.0) / (2.0 * M_PI));
171                 fp->y2 += (int) (fp->i * (sin((double) fp->a) * 360.0) / (2.0 * M_PI));
172                 fp->i++;
173         }
174 }                               /* draw_vines */
175
176
177 XSCREENSAVER_MODULE ("Vines", vines)
178
179 #endif /* MODE_vines */