From http://www.jwz.org/xscreensaver/xscreensaver-5.22.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 fadeplot_handle_event 0
43 # include "xlockmore.h"         /* in xscreensaver distribution */
44 #else /* STANDALONE */
45 # include "xlock.h"             /* in xlockmore distribution */
46 #endif /* STANDALONE */
47
48 #ifdef MODE_fadeplot
49
50 ENTRYPOINT ModeSpecOpt fadeplot_opts =
51 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
52
53 #ifdef USE_MODULES
54 ModStruct   fadeplot_description =
55 {"fadeplot", "init_fadeplot", "draw_fadeplot", "release_fadeplot",
56  "refresh_fadeplot", "init_fadeplot", (char *) NULL, &fadeplot_opts,
57  30000, 10, 1500, 1, 64, 0.6, "",
58  "Shows a fading plot of sine squared", 0, NULL};
59
60 #endif
61
62 #define MINSTEPS 1
63
64 typedef struct {
65         XPoint      speed, step, factor, st;
66         int         temps, maxpts, nbstep;
67         int         min;
68         int         width, height;
69         int         pix;
70         int         angles;
71         int        *stab;
72         XPoint     *pts;
73 } fadeplotstruct;
74
75 static fadeplotstruct *fadeplots = (fadeplotstruct *) NULL;
76
77 static void
78 free_fadeplot(fadeplotstruct *fp)
79 {
80         if (fp->pts != NULL) {
81                 (void) free((void *) fp->pts);
82                 fp->pts = (XPoint *) NULL;
83         }
84         if (fp->stab != NULL) {
85                 (void) free((void *) fp->stab);
86                 fp->stab = (int *) NULL;
87         }
88 }
89
90 static Bool
91 initSintab(ModeInfo * mi)
92 {
93         fadeplotstruct *fp = &fadeplots[MI_SCREEN(mi)];
94         int         i;
95         float       x;
96
97         fp->angles = NRAND(950) + 250;
98         if ((fp->stab = (int *) malloc(fp->angles * sizeof (int))) == NULL) {
99                 free_fadeplot(fp);
100                 return False;
101         }
102         for (i = 0; i < fp->angles; i++) {
103                 x = SINF(2.0 * M_PI * i / fp->angles);
104                 fp->stab[i] = (int) (x * ABS(x) * fp->min) + fp->min;
105         }
106         return True;
107 }
108
109 ENTRYPOINT void
110 init_fadeplot (ModeInfo * mi)
111 {
112         fadeplotstruct *fp;
113
114         if (fadeplots == NULL) {
115                 if ((fadeplots = (fadeplotstruct *) calloc(MI_NUM_SCREENS(mi),
116                                            sizeof (fadeplotstruct))) == NULL)
117                         return;
118         }
119         fp = &fadeplots[MI_SCREEN(mi)];
120
121         fp->width = MI_WIDTH(mi);
122         fp->height = MI_HEIGHT(mi);
123         fp->min = MAX(MIN(fp->width, fp->height) / 2, 1);
124
125         fp->speed.x = 8;
126         fp->speed.y = 10;
127         fp->step.x = 1;
128         fp->step.y = 1;
129         fp->temps = 0;
130         fp->factor.x = MAX(fp->width / (2 * fp->min), 1);
131         fp->factor.y = MAX(fp->height / (2 * fp->min), 1);
132
133         fp->nbstep = MI_COUNT(mi);
134         if (fp->nbstep < -MINSTEPS) {
135                 fp->nbstep = NRAND(-fp->nbstep - MINSTEPS + 1) + MINSTEPS;
136         } else if (fp->nbstep < MINSTEPS)
137                 fp->nbstep = MINSTEPS;
138
139         fp->maxpts = MI_CYCLES(mi);
140         if (fp->maxpts < 1)
141                 fp->maxpts = 1;
142
143         if (fp->pts == NULL) {
144                 if ((fp->pts = (XPoint *) calloc(fp->maxpts, sizeof (XPoint))) ==
145                                  NULL) {
146                         free_fadeplot(fp);
147                         return;
148                 }
149         }
150         if (MI_NPIXELS(mi) > 2)
151                 fp->pix = NRAND(MI_NPIXELS(mi));
152
153         if (fp->stab != NULL)
154                 (void) free((void *) fp->stab);
155         if (!initSintab(mi))
156                 return;
157         MI_CLEARWINDOW(mi);
158 }
159
160 ENTRYPOINT void
161 draw_fadeplot (ModeInfo * mi)
162 {
163         Display    *display = MI_DISPLAY(mi);
164         Window      window = MI_WINDOW(mi);
165         GC          gc = MI_GC(mi);
166         int         i, j, temp;
167         fadeplotstruct *fp;
168
169         if (fadeplots == NULL)
170                 return;
171         fp = &fadeplots[MI_SCREEN(mi)];
172         if (fp->stab == NULL)
173                 return;
174
175         MI_IS_DRAWN(mi) = True;
176         XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
177         XDrawPoints(display, window, gc, fp->pts, fp->maxpts, CoordModeOrigin);
178
179         if (MI_NPIXELS(mi) > 2) {
180                 XSetForeground(display, gc, MI_PIXEL(mi, fp->pix));
181                 if (++fp->pix >= MI_NPIXELS(mi))
182                         fp->pix = 0;
183         } else
184                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
185
186         temp = 0;
187         for (j = 0; j < fp->nbstep; j++) {
188                 for (i = 0; i < fp->maxpts / fp->nbstep; i++) {
189                         fp->pts[temp].x =
190                                 fp->stab[(fp->st.x + fp->speed.x * j + i * fp->step.x) % fp->angles] *
191                                 fp->factor.x + fp->width / 2 - fp->min;
192                         fp->pts[temp].y =
193                                 fp->stab[(fp->st.y + fp->speed.y * j + i * fp->step.y) % fp->angles] *
194                                 fp->factor.y + fp->height / 2 - fp->min;
195                         temp++;
196                 }
197         }
198         XDrawPoints(display, window, gc, fp->pts, temp, CoordModeOrigin);
199         fp->st.x = (fp->st.x + fp->speed.x) % fp->angles;
200         fp->st.y = (fp->st.y + fp->speed.y) % fp->angles;
201         fp->temps++;
202         if ((fp->temps % (fp->angles / 2)) == 0) {
203                 fp->temps = fp->temps % fp->angles * 5;
204                 if ((fp->temps % (fp->angles)) == 0)
205                         fp->speed.y = (fp->speed.y + 1) % 30 + 1;
206                 if ((fp->temps % (fp->angles * 2)) == 0)
207                         fp->speed.x = (fp->speed.x) % 20;
208                 if ((fp->temps % (fp->angles * 3)) == 0)
209                         fp->step.y = (fp->step.y + 1) % 2 + 1;
210
211                 MI_CLEARWINDOW(mi);
212         }
213 }
214
215 ENTRYPOINT void
216 reshape_fadeplot(ModeInfo * mi, int width, int height)
217 {
218         fadeplotstruct *fp = &fadeplots[MI_SCREEN(mi)];
219     fp->width  = width;
220     fp->height = height;
221         fp->min = MAX(MIN(fp->width, fp->height) / 2, 1);
222         fp->factor.x = MAX(fp->width / (2 * fp->min), 1);
223         fp->factor.y = MAX(fp->height / (2 * fp->min), 1);
224 }
225
226 ENTRYPOINT void
227 refresh_fadeplot (ModeInfo * mi)
228 {
229 }
230
231 ENTRYPOINT void
232 release_fadeplot (ModeInfo * mi)
233 {
234         if (fadeplots != NULL) {
235                 int         screen;
236
237                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
238                         free_fadeplot(&fadeplots[screen]);
239                 (void) free((void *) fadeplots);
240                 fadeplots = (fadeplotstruct *) NULL;
241         }
242 }
243
244 XSCREENSAVER_MODULE ("FadePlot", fadeplot)
245
246 #endif /* MODE_fadeplot */