5b7155223c4c5ab8da5d213402ed74ed3d9c2694
[xscreensaver] / hacks / fadeplot.c
1 /* -*- Mode: C; tab-width: 4 -*-
2  * fadeplot.c --- some easy plotting stuff, by Bas van Gaalen, Holland, PD
3  */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)fadeplot.c  4.04 97/07/26 xlockmore";
6 #endif
7
8 /* Converted for xlock by Charles Vidal
9  * See xlock.c for copying information.
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
24 /*-
25  1) Not random enough, i.e. always same starting position.
26  2) Needs to be less flashy
27  */
28
29 #ifdef STANDALONE
30 # define PROGCLASS                      "Fadeplot"
31 # define HACK_INIT                      init_fadeplot
32 # define HACK_DRAW                      draw_fadeplot
33 # define fadeplot_opts          xlockmore_opts
34 # define DEFAULTS                       "*count:                10      \n"                     \
35                                                         "*delay:                30000   \n"                     \
36                                                         "*cycles:               1500    \n"                     \
37                                                         "*ncolors:              64      \n"
38 # define BRIGHT_COLORS
39 # define UNIFORM_COLORS
40 # include "xlockmore.h"                         /* from the xscreensaver distribution */
41 #else /* STANDALONE */
42 # include "xlock.h"                                     /* from the xlockmore distribution */
43 #endif /* STANDALONE */
44
45 ModeSpecOpt fadeplot_opts = {
46   0, NULL, 0, NULL, NULL };
47
48 #define MINSTEPS 1
49 #define ANGLES 1000
50
51 typedef struct {
52         XPoint      speed, step, factor, st;
53         int         temps, maxpts, nbstep;
54         int         min;
55         int         width, height;
56         int         pix;
57         int         stab[ANGLES];
58         XPoint     *pts;
59 } fadeplotstruct;
60
61 static fadeplotstruct *fadeplots = NULL;
62
63 static void
64 initSintab(ModeInfo * mi)
65 {
66         fadeplotstruct *fp = &fadeplots[MI_SCREEN(mi)];
67         int         i;
68         float       x;
69
70         for (i = 0; i < ANGLES; i++) {
71                 x = SINF(i * 2 * M_PI / ANGLES);
72                 fp->stab[i] = (int) (x * ABS(x) * fp->min) + fp->min;
73         }
74 }
75
76 void
77 init_fadeplot(ModeInfo * mi)
78 {
79         fadeplotstruct *fp;
80
81         if (fadeplots == NULL) {
82                 if ((fadeplots = (fadeplotstruct *) calloc(MI_NUM_SCREENS(mi),
83                                            sizeof (fadeplotstruct))) == NULL)
84                         return;
85         }
86         fp = &fadeplots[MI_SCREEN(mi)];
87
88         fp->width = MI_WIN_WIDTH(mi);
89         fp->height = MI_WIN_HEIGHT(mi);
90   fp->min = MAX(MIN(fp->width, fp->height) / 2, 1);
91
92         fp->speed.x = 8;
93         fp->speed.y = 10;
94         fp->step.x = 1;
95         fp->step.y = 1;
96         fp->temps = 0;
97         fp->factor.x = MAX(fp->width / (2 * fp->min), 1);
98         fp->factor.y = MAX(fp->height / (2 * fp->min), 1);
99
100         fp->nbstep = MI_BATCHCOUNT(mi);
101         if (fp->nbstep < -MINSTEPS) {
102                 fp->nbstep = NRAND(-fp->nbstep - MINSTEPS + 1) + MINSTEPS;
103         } else if (fp->nbstep < MINSTEPS)
104                 fp->nbstep = MINSTEPS;
105
106   fp->maxpts = MI_CYCLES(mi);
107   if (fp->maxpts < 1)
108     fp->maxpts = 1;
109
110         if (fp->pts == NULL)
111                 fp->pts = (XPoint *) calloc(fp->maxpts, sizeof (XPoint));
112         if (MI_NPIXELS(mi) > 2)
113                 fp->pix = NRAND(MI_NPIXELS(mi));
114
115         initSintab(mi);
116
117         XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
118 }
119
120 void
121 draw_fadeplot(ModeInfo * mi)
122 {
123         fadeplotstruct *fp = &fadeplots[MI_SCREEN(mi)];
124         Display    *display = MI_DISPLAY(mi);
125         Window      window = MI_WINDOW(mi);
126         GC          gc = MI_GC(mi);
127         int         i, j;
128         long        temp;
129
130         XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
131         XDrawPoints(display, window, gc, fp->pts, fp->maxpts, CoordModeOrigin);
132
133         if (MI_NPIXELS(mi) > 2) {
134                 XSetForeground(display, gc, MI_PIXEL(mi, fp->pix));
135                 if (++fp->pix >= MI_NPIXELS(mi))
136                         fp->pix = 0;
137         } else
138                 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
139
140                 for (temp = fp->nbstep - 1; temp >= 0; temp--) {
141                         j = temp;
142                         for (i = 0; i < fp->maxpts / fp->nbstep; i++) {
143                                 fp->pts[temp * i + i].x =
144                                         fp->stab[(fp->st.x + fp->speed.x * j + i * fp->step.x) % ANGLES] *
145                                         fp->factor.x + fp->width / 2 - fp->min;
146                                 fp->pts[temp * i + i].y =
147                                         fp->stab[(fp->st.y + fp->speed.y * j + i * fp->step.y) % ANGLES] *
148                                         fp->factor.y + fp->height / 2 - fp->min;
149                         }
150                 }
151         XDrawPoints(display, window, gc, fp->pts, fp->maxpts, CoordModeOrigin);
152         XFlush(display);
153         fp->st.x = (fp->st.x + fp->speed.x) % ANGLES;
154         fp->st.y = (fp->st.y + fp->speed.y) % ANGLES;
155         fp->temps++;
156         if ((fp->temps % (ANGLES / 2)) == 0) {
157                 fp->temps = fp->temps % ANGLES * 5;
158                 if ((fp->temps % (ANGLES)) == 0)
159                         fp->speed.y = (fp->speed.y++) % 30 + 1;
160                 if ((fp->temps % (ANGLES * 2)) == 0)
161                         fp->speed.x = (fp->speed.x) % 20;
162                 if ((fp->temps % (ANGLES * 3)) == 0)
163                         fp->step.y = (fp->step.y++) % 2 + 1;
164                 XClearWindow(display, window);
165         }
166 }
167 void
168 refresh_fadeplot(ModeInfo * mi)
169 {
170
171 }
172
173 void
174 release_fadeplot(ModeInfo * mi)
175 {
176         /* Do nothing, it will refresh by itself */
177 }