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 reshape_fadeplot 0
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 */
50 ENTRYPOINT ModeSpecOpt fadeplot_opts =
51 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
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};
65 XPoint speed, step, factor, st;
66 int temps, maxpts, nbstep;
75 static fadeplotstruct *fadeplots = (fadeplotstruct *) NULL;
78 free_fadeplot(fadeplotstruct *fp)
80 if (fp->pts != NULL) {
81 (void) free((void *) fp->pts);
82 fp->pts = (XPoint *) NULL;
84 if (fp->stab != NULL) {
85 (void) free((void *) fp->stab);
86 fp->stab = (int *) NULL;
91 initSintab(ModeInfo * mi)
93 fadeplotstruct *fp = &fadeplots[MI_SCREEN(mi)];
97 fp->angles = NRAND(950) + 250;
98 if ((fp->stab = (int *) malloc(fp->angles * sizeof (int))) == NULL) {
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;
110 init_fadeplot (ModeInfo * mi)
114 if (fadeplots == NULL) {
115 if ((fadeplots = (fadeplotstruct *) calloc(MI_NUM_SCREENS(mi),
116 sizeof (fadeplotstruct))) == NULL)
119 fp = &fadeplots[MI_SCREEN(mi)];
121 fp->width = MI_WIDTH(mi);
122 fp->height = MI_HEIGHT(mi);
123 fp->min = MAX(MIN(fp->width, fp->height) / 2, 1);
130 fp->factor.x = MAX(fp->width / (2 * fp->min), 1);
131 fp->factor.y = MAX(fp->height / (2 * fp->min), 1);
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;
139 fp->maxpts = MI_CYCLES(mi);
143 if (fp->pts == NULL) {
144 if ((fp->pts = (XPoint *) calloc(fp->maxpts, sizeof (XPoint))) ==
150 if (MI_NPIXELS(mi) > 2)
151 fp->pix = NRAND(MI_NPIXELS(mi));
153 if (fp->stab != NULL)
154 (void) free((void *) fp->stab);
161 draw_fadeplot (ModeInfo * mi)
163 Display *display = MI_DISPLAY(mi);
164 Window window = MI_WINDOW(mi);
169 if (fadeplots == NULL)
171 fp = &fadeplots[MI_SCREEN(mi)];
172 if (fp->stab == NULL)
175 MI_IS_DRAWN(mi) = True;
176 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
177 XDrawPoints(display, window, gc, fp->pts, fp->maxpts, CoordModeOrigin);
179 if (MI_NPIXELS(mi) > 2) {
180 XSetForeground(display, gc, MI_PIXEL(mi, fp->pix));
181 if (++fp->pix >= MI_NPIXELS(mi))
184 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
187 for (j = 0; j < fp->nbstep; j++) {
188 for (i = 0; i < fp->maxpts / fp->nbstep; i++) {
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;
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;
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;
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;
216 refresh_fadeplot (ModeInfo * mi)
222 release_fadeplot (ModeInfo * mi)
224 if (fadeplots != NULL) {
227 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
228 free_fadeplot(&fadeplots[screen]);
229 (void) free((void *) fadeplots);
230 fadeplots = (fadeplotstruct *) NULL;
234 XSCREENSAVER_MODULE ("FadePlot", fadeplot)
236 #endif /* MODE_fadeplot */