From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / fadeplot.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* fadeplot --- a fading plot of sine squared */
3
4 #if 0
5 static const char sccsid[] = "@(#)fadeplot.c    5.00 2000/11/01 xlockmore";
6 #endif
7
8 /*-
9  * Some easy plotting stuff, by Bas van Gaalen, Holland, PD
10  *
11  * Copyright (c) 1996 by Charles Vidal
12  *
13  * Permission to use, copy, modify, and distribute this software and its
14  * documentation for any purpose and without fee is hereby granted,
15  * provided that the above copyright notice appear in all copies and that
16  * both that copyright notice and this permission notice appear in
17  * supporting documentation.
18  *
19  * This file is provided AS IS with no warranties of any kind.  The author
20  * shall have no liability with respect to the infringement of copyrights,
21  * trade secrets or any patents by this file or any part thereof.  In no
22  * event will the author be liable for any lost revenue or profits or
23  * other special, indirect and consequential damages.
24  *
25  * Revision History:
26  * 01-Nov-2000: Allocation checks
27  * 10-May-1997: Compatible with screensaver
28  * 1996: Written by Charles Vidal based on work by Bas van Gaalen
29  */
30
31 #ifdef STANDALONE
32 # define MODE_fadeplot
33 # define DEFAULTS       "*delay: 30000 \n" \
34                                         "*count: 10 \n" \
35                                         "*cycles: 1500 \n" \
36                                         "*ncolors: 64 \n" \
37                                         "*fpsSolid: true \n" \
38                                         "*ignoreRotation: True \n" \
39
40 # define BRIGHT_COLORS
41 # define UNIFORM_COLORS
42 # define release_fadeplot 0
43 # define fadeplot_handle_event 0
44 # include "xlockmore.h"         /* in xscreensaver distribution */
45 #else /* STANDALONE */
46 # include "xlock.h"             /* in xlockmore distribution */
47 #endif /* STANDALONE */
48
49 #ifdef MODE_fadeplot
50
51 ENTRYPOINT ModeSpecOpt fadeplot_opts =
52 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
53
54 #ifdef USE_MODULES
55 ModStruct   fadeplot_description =
56 {"fadeplot", "init_fadeplot", "draw_fadeplot", (char *) NULL,
57  "refresh_fadeplot", "init_fadeplot", (char *) NULL, &fadeplot_opts,
58  30000, 10, 1500, 1, 64, 0.6, "",
59  "Shows a fading plot of sine squared", 0, NULL};
60
61 #endif
62
63 #define MINSTEPS 1
64
65 typedef struct {
66         XPoint      speed, step, factor, st;
67         int         temps, maxpts, nbstep;
68         int         min;
69         int         width, height;
70         int         pix;
71         int         angles;
72         int        *stab;
73         XPoint     *pts;
74 } fadeplotstruct;
75
76 static fadeplotstruct *fadeplots = (fadeplotstruct *) NULL;
77
78 static void
79 free_fadeplot(ModeInfo * mi)
80 {
81         fadeplotstruct *fp = &fadeplots[MI_SCREEN(mi)];
82         if (fp->pts != NULL) {
83                 (void) free((void *) fp->pts);
84                 fp->pts = (XPoint *) NULL;
85         }
86         if (fp->stab != NULL) {
87                 (void) free((void *) fp->stab);
88                 fp->stab = (int *) NULL;
89         }
90 }
91
92 static Bool
93 initSintab(ModeInfo * mi)
94 {
95         fadeplotstruct *fp = &fadeplots[MI_SCREEN(mi)];
96         int         i;
97         float       x;
98
99         fp->angles = NRAND(950) + 250;
100         if ((fp->stab = (int *) malloc(fp->angles * sizeof (int))) == NULL) {
101                 free_fadeplot(mi);
102                 return False;
103         }
104         for (i = 0; i < fp->angles; i++) {
105                 x = SINF(2.0 * M_PI * i / fp->angles);
106                 fp->stab[i] = (int) (x * ABS(x) * fp->min) + fp->min;
107         }
108         return True;
109 }
110
111 ENTRYPOINT void
112 init_fadeplot (ModeInfo * mi)
113 {
114         fadeplotstruct *fp;
115
116         MI_INIT (mi, fadeplots, free_fadeplot);
117         fp = &fadeplots[MI_SCREEN(mi)];
118
119         fp->width = MI_WIDTH(mi);
120         fp->height = MI_HEIGHT(mi);
121         fp->min = MAX(MIN(fp->width, fp->height) / 2, 1);
122
123         fp->speed.x = 8;
124         fp->speed.y = 10;
125         fp->step.x = 1;
126         fp->step.y = 1;
127         fp->temps = 0;
128         fp->factor.x = MAX(fp->width / (2 * fp->min), 1);
129         fp->factor.y = MAX(fp->height / (2 * fp->min), 1);
130
131         fp->nbstep = MI_COUNT(mi);
132         if (fp->nbstep < -MINSTEPS) {
133                 fp->nbstep = NRAND(-fp->nbstep - MINSTEPS + 1) + MINSTEPS;
134         } else if (fp->nbstep < MINSTEPS)
135                 fp->nbstep = MINSTEPS;
136
137         fp->maxpts = MI_CYCLES(mi);
138         if (fp->maxpts < 1)
139                 fp->maxpts = 1;
140
141         if (fp->pts == NULL) {
142                 if ((fp->pts = (XPoint *) calloc(fp->maxpts, sizeof (XPoint))) ==
143                                  NULL) {
144                         free_fadeplot(mi);
145                         return;
146                 }
147         }
148         if (MI_NPIXELS(mi) > 2)
149                 fp->pix = NRAND(MI_NPIXELS(mi));
150
151         if (fp->stab != NULL)
152                 (void) free((void *) fp->stab);
153         if (!initSintab(mi))
154                 return;
155         MI_CLEARWINDOW(mi);
156 }
157
158 ENTRYPOINT void
159 draw_fadeplot (ModeInfo * mi)
160 {
161         Display    *display = MI_DISPLAY(mi);
162         Window      window = MI_WINDOW(mi);
163         GC          gc = MI_GC(mi);
164         int         i, j, temp;
165         fadeplotstruct *fp;
166
167         if (fadeplots == NULL)
168                 return;
169         fp = &fadeplots[MI_SCREEN(mi)];
170         if (fp->stab == NULL)
171                 return;
172
173         MI_IS_DRAWN(mi) = True;
174         XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
175         XDrawPoints(display, window, gc, fp->pts, fp->maxpts, CoordModeOrigin);
176
177         if (MI_NPIXELS(mi) > 2) {
178                 XSetForeground(display, gc, MI_PIXEL(mi, fp->pix));
179                 if (++fp->pix >= MI_NPIXELS(mi))
180                         fp->pix = 0;
181         } else
182                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
183
184         temp = 0;
185         for (j = 0; j < fp->nbstep; j++) {
186                 for (i = 0; i < fp->maxpts / fp->nbstep; i++) {
187                         fp->pts[temp].x =
188                                 fp->stab[(fp->st.x + fp->speed.x * j + i * fp->step.x) % fp->angles] *
189                                 fp->factor.x + fp->width / 2 - fp->min;
190                         fp->pts[temp].y =
191                                 fp->stab[(fp->st.y + fp->speed.y * j + i * fp->step.y) % fp->angles] *
192                                 fp->factor.y + fp->height / 2 - fp->min;
193                         temp++;
194                 }
195         }
196         XDrawPoints(display, window, gc, fp->pts, temp, CoordModeOrigin);
197         fp->st.x = (fp->st.x + fp->speed.x) % fp->angles;
198         fp->st.y = (fp->st.y + fp->speed.y) % fp->angles;
199         fp->temps++;
200         if ((fp->temps % (fp->angles / 2)) == 0) {
201                 fp->temps = fp->temps % fp->angles * 5;
202                 if ((fp->temps % (fp->angles)) == 0)
203                         fp->speed.y = (fp->speed.y + 1) % 30 + 1;
204                 if ((fp->temps % (fp->angles * 2)) == 0)
205                         fp->speed.x = (fp->speed.x) % 20;
206                 if ((fp->temps % (fp->angles * 3)) == 0)
207                         fp->step.y = (fp->step.y + 1) % 2 + 1;
208
209                 MI_CLEARWINDOW(mi);
210         }
211 }
212
213 ENTRYPOINT void
214 reshape_fadeplot(ModeInfo * mi, int width, int height)
215 {
216         fadeplotstruct *fp = &fadeplots[MI_SCREEN(mi)];
217     fp->width  = width;
218     fp->height = height;
219         fp->min = MAX(MIN(fp->width, fp->height) / 2, 1);
220         fp->factor.x = MAX(fp->width / (2 * fp->min), 1);
221         fp->factor.y = MAX(fp->height / (2 * fp->min), 1);
222 }
223
224 ENTRYPOINT void
225 refresh_fadeplot (ModeInfo * mi)
226 {
227 }
228
229 XSCREENSAVER_MODULE ("FadePlot", fadeplot)
230
231 #endif /* MODE_fadeplot */