1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* fadeplot --- a fading plot of sine squared */
5 static const char sccsid[] = "@(#)fadeplot.c 5.00 2000/11/01 xlockmore";
9 * Some easy plotting stuff, by Bas van Gaalen, Holland, PD
11 * Copyright (c) 1996 by Charles Vidal
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.
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.
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
32 # define MODE_fadeplot
33 # define DEFAULTS "*delay: 30000 \n" \
37 "*fpsSolid: true \n" \
39 # define BRIGHT_COLORS
40 # define UNIFORM_COLORS
41 # define fadeplot_handle_event 0
42 # include "xlockmore.h" /* in xscreensaver distribution */
43 #else /* STANDALONE */
44 # include "xlock.h" /* in xlockmore distribution */
45 #endif /* STANDALONE */
49 ENTRYPOINT ModeSpecOpt fadeplot_opts =
50 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
53 ModStruct fadeplot_description =
54 {"fadeplot", "init_fadeplot", "draw_fadeplot", "release_fadeplot",
55 "refresh_fadeplot", "init_fadeplot", (char *) NULL, &fadeplot_opts,
56 30000, 10, 1500, 1, 64, 0.6, "",
57 "Shows a fading plot of sine squared", 0, NULL};
64 XPoint speed, step, factor, st;
65 int temps, maxpts, nbstep;
74 static fadeplotstruct *fadeplots = (fadeplotstruct *) NULL;
77 free_fadeplot(fadeplotstruct *fp)
79 if (fp->pts != NULL) {
80 (void) free((void *) fp->pts);
81 fp->pts = (XPoint *) NULL;
83 if (fp->stab != NULL) {
84 (void) free((void *) fp->stab);
85 fp->stab = (int *) NULL;
90 initSintab(ModeInfo * mi)
92 fadeplotstruct *fp = &fadeplots[MI_SCREEN(mi)];
96 fp->angles = NRAND(950) + 250;
97 if ((fp->stab = (int *) malloc(fp->angles * sizeof (int))) == NULL) {
101 for (i = 0; i < fp->angles; i++) {
102 x = SINF(2.0 * M_PI * i / fp->angles);
103 fp->stab[i] = (int) (x * ABS(x) * fp->min) + fp->min;
109 init_fadeplot (ModeInfo * mi)
113 if (fadeplots == NULL) {
114 if ((fadeplots = (fadeplotstruct *) calloc(MI_NUM_SCREENS(mi),
115 sizeof (fadeplotstruct))) == NULL)
118 fp = &fadeplots[MI_SCREEN(mi)];
120 fp->width = MI_WIDTH(mi);
121 fp->height = MI_HEIGHT(mi);
122 fp->min = MAX(MIN(fp->width, fp->height) / 2, 1);
129 fp->factor.x = MAX(fp->width / (2 * fp->min), 1);
130 fp->factor.y = MAX(fp->height / (2 * fp->min), 1);
132 fp->nbstep = MI_COUNT(mi);
133 if (fp->nbstep < -MINSTEPS) {
134 fp->nbstep = NRAND(-fp->nbstep - MINSTEPS + 1) + MINSTEPS;
135 } else if (fp->nbstep < MINSTEPS)
136 fp->nbstep = MINSTEPS;
138 fp->maxpts = MI_CYCLES(mi);
142 if (fp->pts == NULL) {
143 if ((fp->pts = (XPoint *) calloc(fp->maxpts, sizeof (XPoint))) ==
149 if (MI_NPIXELS(mi) > 2)
150 fp->pix = NRAND(MI_NPIXELS(mi));
152 if (fp->stab != NULL)
153 (void) free((void *) fp->stab);
160 draw_fadeplot (ModeInfo * mi)
162 Display *display = MI_DISPLAY(mi);
163 Window window = MI_WINDOW(mi);
168 if (fadeplots == NULL)
170 fp = &fadeplots[MI_SCREEN(mi)];
171 if (fp->stab == NULL)
174 MI_IS_DRAWN(mi) = True;
175 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
176 XDrawPoints(display, window, gc, fp->pts, fp->maxpts, CoordModeOrigin);
178 if (MI_NPIXELS(mi) > 2) {
179 XSetForeground(display, gc, MI_PIXEL(mi, fp->pix));
180 if (++fp->pix >= MI_NPIXELS(mi))
183 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
186 for (j = 0; j < fp->nbstep; j++) {
187 for (i = 0; i < fp->maxpts / fp->nbstep; i++) {
189 fp->stab[(fp->st.x + fp->speed.x * j + i * fp->step.x) % fp->angles] *
190 fp->factor.x + fp->width / 2 - fp->min;
192 fp->stab[(fp->st.y + fp->speed.y * j + i * fp->step.y) % fp->angles] *
193 fp->factor.y + fp->height / 2 - fp->min;
197 XDrawPoints(display, window, gc, fp->pts, temp, CoordModeOrigin);
198 fp->st.x = (fp->st.x + fp->speed.x) % fp->angles;
199 fp->st.y = (fp->st.y + fp->speed.y) % fp->angles;
201 if ((fp->temps % (fp->angles / 2)) == 0) {
202 fp->temps = fp->temps % fp->angles * 5;
203 if ((fp->temps % (fp->angles)) == 0)
204 fp->speed.y = (fp->speed.y + 1) % 30 + 1;
205 if ((fp->temps % (fp->angles * 2)) == 0)
206 fp->speed.x = (fp->speed.x) % 20;
207 if ((fp->temps % (fp->angles * 3)) == 0)
208 fp->step.y = (fp->step.y + 1) % 2 + 1;
215 reshape_fadeplot(ModeInfo * mi, int width, int height)
217 fadeplotstruct *fp = &fadeplots[MI_SCREEN(mi)];
220 fp->min = MAX(MIN(fp->width, fp->height) / 2, 1);
221 fp->factor.x = MAX(fp->width / (2 * fp->min), 1);
222 fp->factor.y = MAX(fp->height / (2 * fp->min), 1);
226 refresh_fadeplot (ModeInfo * mi)
231 release_fadeplot (ModeInfo * mi)
233 if (fadeplots != NULL) {
236 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
237 free_fadeplot(&fadeplots[screen]);
238 (void) free((void *) fadeplots);
239 fadeplots = (fadeplotstruct *) NULL;
243 XSCREENSAVER_MODULE ("FadePlot", fadeplot)
245 #endif /* MODE_fadeplot */