1 /* -*- Mode: C; tab-width: 4 -*-
2 * lisa.c --- animated full-loop lisajous figures
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)lisa.c 4.03 97/05/10 xlockmore";
8 /* Copyright (c) 1997 by Caleb Cullen.
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and that
13 * both that copyright notice and this permission notice appear in
14 * supporting documentation.
16 * This file is provided AS IS with no warranties of any kind. The author
17 * shall have no liability with respect to the infringement of copyrights,
18 * trade secrets or any patents by this file or any part thereof. In no
19 * event will the author be liable for any lost revenue or profits or
20 * other special, indirect and consequential damages.
23 * 10-May-97: Compatible with xscreensaver
25 * The inspiration for this program, Lasp, was written by Adam B. Roach
26 * in 1990, assisted by me, Caleb Cullen. It was written first in C, then
27 * in assembly, and used pre-calculated data tables to graph lisajous
28 * figures on 386 machines and lower. This version bears only superficial
29 * resemblances to the original Lasp.
31 * The `lissie' module's source code was studied as an example of how
32 * to incorporate a new module into xlock. Resemblances to it are
33 * expected, but not intended to be plaigiaristic.
37 # define PROGCLASS "Lisa"
38 # define HACK_INIT init_lisa
39 # define HACK_DRAW draw_lisa
40 # define lisa_opts xlockmore_opts
41 # define DEFAULTS "*count: 1 \n" \
46 # define UNIFORM_COLORS
47 # include "xlockmore.h" /* from the xscreensaver distribution */
48 #else /* !STANDALONE */
49 # include "xlock.h" /* from the xlockmore distribution */
50 #endif /* !STANDALONE */
52 ModeSpecOpt lisa_opts = {
53 0, NULL, 0, NULL, NULL };
57 #define ADDITIVE "True"
58 #define XVMAX 10 /* Maximum velocities */
60 #define LISAMAXFUNCS 2
61 #define NUMSTDFUNCS 10
63 #define lisasetcolor() \
64 if (MI_NPIXELS(mi) > 2) { \
65 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_PIXEL(mi, loop->color)); \
66 if (++(loop->color) >= MI_NPIXELS(mi)) { loop->color=0; } \
67 } else { XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_WHITE_PIXEL(mi)); }
68 #define getRadius(context) \
69 ((context->width > context->height)?context->height:context->width) * 3 / 8
70 #define checkRadius(loop, context) \
71 if ((context->height / 2 > MI_SIZE(mi)) && (context->width / 2 > MI_SIZE(mi))) \
72 loop->radius = MI_SIZE(mi); \
73 if ((loop->radius < 0) || \
74 (loop->radius > loop->center.x) || \
75 (loop->radius > loop->center.y)) loop->radius = getRadius(context)
78 typedef struct lisafunc_struct {
79 double xcoeff[2], ycoeff[2];
84 typedef struct lisa_struct {
85 int radius, color, dx, dy, nsteps, nfuncs, melting;
86 double pistep, phi, theta;
87 XPoint center, *lastpoint;
88 lisafuncs *function[LISAMAXFUNCS];
91 typedef struct lisacontext_struct {
93 int width, height, nlisajous, loopcount;
97 static lisacons *Lisa = NULL;
101 static XrmOptionDescRec lisa_xrm_opts[] =
103 {"-additive", ".lisa.additive", XrmoptionNoArg, (caddr_t) "True"},
104 {"+additive", ".lisa.additive", XrmoptionNoArg, (caddr_t) "False"}
107 static argtype lisa_vars[] =
109 {(caddr_t *) & additive, "additive", "Additive", ADDITIVE, t_Bool}
112 static OptionStruct lisa_vars_desc[] =
114 {"-/+additive", "turn on/off additive functions mode"}
117 ModeSpecOpt lisa_opts =
118 {2, lisa_xrm_opts, 1, lisa_vars, lisa_vars_desc};
119 #endif /* STANDALONE */
122 void refresh_lisa(ModeInfo * mi);
123 void change_lisa(ModeInfo * mi);
126 static lisafuncs Function[NUMSTDFUNCS] =
130 {1.0, 2.0}, 2, 2, 0},
133 {1.0, 1.0}, 2, 2, 1},
136 {1.0, 2.0}, 2, 2, 2},
139 {1.0, 3.0}, 2, 2, 3},
142 {1.0, 2.0}, 2, 2, 4},
145 {1.0, 3.0}, 2, 2, 5},
148 {1.0, 4.0}, 2, 2, 6},
151 {1.0, 5.0}, 2, 2, 7},
154 {2.0, 5.0}, 2, 2, 8},
161 drawlisa(ModeInfo * mi, lisas * loop)
164 XPoint *lp = loop->lastpoint;
165 lisacons *lc = &Lisa[MI_SCREEN(mi)];
166 lisafuncs **lf = loop->function;
167 int phase = lc->loopcount % loop->nsteps;
168 int pctr, fctr, xctr, yctr;
169 double xprod, yprod, xsum, ysum;
171 /* Allocate the np array */
172 np = (XPoint *) calloc(loop->nsteps, sizeof (XPoint));
174 /* Update the center */
175 loop->center.x += loop->dx;
176 loop->center.y += loop->dy;
177 checkRadius(loop, lc);
178 if ((loop->center.x - loop->radius) <= 0) {
179 loop->center.x = loop->radius;
180 loop->dx = NRAND(XVMAX);
181 } else if ((loop->center.x + loop->radius) >= lc->width) {
182 loop->center.x = lc->width - loop->radius;
183 loop->dx = -NRAND(XVMAX);
185 if ((loop->center.y - loop->radius) <= 0) {
186 loop->center.y = loop->radius;
187 loop->dy = NRAND(YVMAX);
188 } else if ((loop->center.y + loop->radius) >= lc->height) {
189 loop->center.y = lc->height - loop->radius;
190 loop->dy = -NRAND(YVMAX);
193 /* Now draw the points, and erase the ones from the last cycle */
195 for (pctr = 0; pctr < loop->nsteps; pctr++) {
197 loop->phi = (double) (pctr - phase) * loop->pistep;
198 loop->theta = (double) (pctr + phase) * loop->pistep;
206 xprod += sin(lf[fctr]->xcoeff[xctr] * loop->theta);
208 yprod += sin(lf[fctr]->ycoeff[yctr] * loop->phi);
212 *(double) (loop->nsteps - loop->melting) \
213 /(double) loop->nsteps;
215 *(double) (loop->nsteps - loop->melting) \
216 /(double) loop->nsteps;
219 *(double) loop->melting \
220 /(double) loop->nsteps;
222 *(double) loop->melting \
223 /(double) loop->nsteps;
231 *(double) loop->radius \
232 /(double) lf[fctr]->nx;
234 *(double) loop->radius \
235 /(double) lf[fctr]->ny;
240 yprod = xprod = (double) loop->radius \
241 *(double) (loop->nsteps - loop->melting) \
242 /(double) (loop->nsteps);
244 yprod = xprod = (double) loop->radius \
245 *(double) (loop->melting) \
246 /(double) (loop->nsteps);
249 xprod = yprod = (double) loop->radius;
252 xprod *= sin(lf[fctr]->xcoeff[xctr] * loop->theta);
254 yprod *= sin(lf[fctr]->ycoeff[yctr] * loop->phi);
259 if ((loop->nfuncs > 1) && (!loop->melting)) {
260 xsum /= (double) loop->nfuncs;
261 ysum /= (double) loop->nfuncs;
263 xsum += (double) loop->center.x;
264 ysum += (double) loop->center.y;
266 np[pctr].x = (int) ceil(xsum);
267 np[pctr].y = (int) ceil(ysum);
270 if (!--loop->melting) {
272 loop->function[0] = loop->function[1];
275 for (pctr = 0; pctr < loop->nsteps; pctr++) {
277 #if defined DRAWLINES
278 /* erase the last cycle's point */
279 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
280 XDrawLine(MI_DISPLAY(mi), MI_WINDOW(mi), \
281 MI_GC(mi), lp[pctr].x, lp[pctr].y, \
282 lp[(pctr + 1) % loop->nsteps].x, \
283 lp[(pctr + 1) % loop->nsteps].y);
285 /* Set the new color */
288 /* plot this cycle's point */
289 XDrawLine(MI_DISPLAY(mi), MI_WINDOW(mi), \
290 MI_GC(mi), np[pctr].x, np[pctr].y, \
291 np[(pctr + 1) % loop->nsteps].x, \
292 np[(pctr + 1) % loop->nsteps].y);
294 /* erase the last cycle's point */
295 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
296 XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi), \
297 MI_GC(mi), lp[pctr].x, lp[pctr].y);
299 /* Set the new color */
302 /* plot this cycle's point */
303 XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi), \
304 MI_GC(mi), np[pctr].x, np[pctr].y);
307 (void) free((void *) lp);
308 loop->lastpoint = np;
312 initlisa(ModeInfo * mi, lisas * loop)
314 lisacons *lc = &Lisa[MI_SCREEN(mi)];
315 lisafuncs **lf = loop->function;
317 int phase, pctr, fctr, xctr, yctr;
318 double xprod, yprod, xsum, ysum;
320 if (MI_NPIXELS(mi) > 2) {
323 loop->color = MI_WIN_WHITE_PIXEL(mi);
324 loop->nsteps = MI_CYCLES(mi);
325 if (loop->nsteps == 0)
327 lc->maxcycles = (MAXCYCLES * loop->nsteps) - 1;
330 loop->pistep = 2.0 * M_PI / (double) loop->nsteps;
331 loop->center.x = lc->width / 2;
332 loop->center.y = lc->height / 2;
333 loop->radius = MI_SIZE(mi);
334 checkRadius(loop, lc);
335 loop->dx = NRAND(XVMAX);
336 loop->dy = NRAND(YVMAX);
339 lf[0] = &Function[lc->loopcount % NUMSTDFUNCS];
340 if ((lp = loop->lastpoint = (XPoint *)
341 calloc(loop->nsteps, sizeof (XPoint))) == NULL)
343 phase = lc->loopcount % loop->nsteps;
345 for (pctr = 0; pctr < loop->nsteps; pctr++) {
346 loop->phi = (double) (pctr - phase) * loop->pistep;
347 loop->theta = (double) (pctr + phase) * loop->pistep;
351 xprod = yprod = (double) loop->radius;
355 xprod *= sin(lf[fctr]->xcoeff[xctr] * loop->theta);
357 yprod *= sin(lf[fctr]->ycoeff[yctr] * loop->phi);
361 if (loop->nfuncs > 1) {
365 xsum += (double) loop->center.x;
366 ysum += (double) loop->center.y;
368 lp[pctr].x = (int) ceil(xsum);
369 lp[pctr].y = (int) ceil(ysum);
371 for (pctr = 0; pctr < loop->nsteps; pctr++) {
374 #if defined DRAWLINES
375 XDrawLine(MI_DISPLAY(mi), MI_WINDOW(mi), \
376 MI_GC(mi), lp[pctr].x, lp[pctr].y, \
377 lp[(pctr + 1) % loop->nsteps].x, \
378 lp[(pctr + 1) % loop->nsteps].y);
380 XDrawPoint(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi), \
381 lp[pctr].x, lp[pctr].y);
387 init_lisa(ModeInfo * mi)
393 if ((Lisa = (lisacons *) calloc(MI_NUM_SCREENS(mi), sizeof (lisacons))) \
397 lc = &Lisa[MI_SCREEN(mi)];
398 lc->width = MI_WIN_WIDTH(mi);
399 lc->height = MI_WIN_HEIGHT(mi);
400 lc->nlisajous = MI_BATCHCOUNT(mi);
403 if (lc->lisajous == NULL) {
404 if ((lc->lisajous = (lisas *) calloc(lc->nlisajous, sizeof (lisas))) \
407 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
408 for (lctr = 0; lctr < lc->nlisajous; lctr++) {
409 initlisa(mi, &lc->lisajous[lctr]);
415 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
419 draw_lisa(ModeInfo * mi)
421 lisacons *lc = &Lisa[MI_SCREEN(mi)];
423 if (++lc->loopcount > lc->maxcycles) {
430 refresh_lisa(ModeInfo * mi)
432 lisacons *lc = &Lisa[MI_SCREEN(mi)];
435 for (lctr = 0; lctr < lc->nlisajous; lctr++) {
436 drawlisa(mi, &lc->lisajous[lctr]);
441 release_lisa(ModeInfo * mi)
447 for (sctr = 0; sctr < MI_NUM_SCREENS(mi); sctr++) {
449 while (lc->lisajous) {
450 for (lctr = 0; lctr < lc->nlisajous; lctr++) {
451 (void) free(lc->lisajous[lctr].lastpoint);
453 (void) free(lc->lisajous);
463 change_lisa(ModeInfo * mi)
465 lisacons *lc = &Lisa[MI_SCREEN(mi)];
470 for (lctr = 0; lctr < lc->nlisajous; lctr++) {
471 loop = &lc->lisajous[lctr];
472 loop->function[1] = &Function[(loop->function[0]->index + 1) %
474 loop->melting = loop->nsteps - 1;