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