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