http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.01.tar.gz
[xscreensaver] / hacks / vines.c
index 4c562f9d344811170efe55db40f374382269cfb3..f4151cd4c254604b70125f3d02cebde5ab581fd8 100644 (file)
@@ -1,46 +1,77 @@
-/* -*- Mode: C; tab-width: 4 -*-
- * vines --- another geometric pattern generator.
- */
+/* -*- Mode: C; tab-width: 4 -*- */
+/* vines --- vine fractals */
+
 #if !defined( lint ) && !defined( SABER )
-static const char sccsid[] = "@(#)vines.c      4.02 97/04/01 xlockmore";
+static const char sccsid[] = "@(#)vines.c      5.00 2000/11/01 xlockmore";
+
 #endif
 
-/* xlockmore mode written by Tracy Camp
- * campt@hurrah.com 1997
- * released to the public domain
+/*-
+ * Copyright (c) 1997 by Tracy Camp campt@hurrah.com
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose and without fee is hereby granted,
+ * provided that the above copyright notice appear in all copies and that
+ * both that copyright notice and this permission notice appear in
+ * supporting documentation.
  *
+ * This file is provided AS IS with no warranties of any kind.  The author
+ * shall have no liability with respect to the infringement of copyrights,
+ * trade secrets or any patents by this file or any part thereof.  In no
+ * event will the author be liable for any lost revenue or profits or
+ * other special, indirect and consequential damages.
+ *
+ * If you make a modification I would of course appreciate a copy.
+ *
+ * Revision History:
+ * 01-Nov-2000: Allocation checks
+ * 11-Jul-1997: David Hansen <dhansen@metapath.com>
+ *              Changed names to vines and modified draw loop
+ *              to honor batchcount so vines can be grown or plotted.
+ * 10-May-1997: Compatible with xscreensaver
+ * 21-Mar-1997: David Hansen <dhansen@metapath.com>
+ *              Updated mode to draw complete patterns on every
+ *              iteration instead of growing the vine.  Also made
+ *              adjustments to randomization and changed variable
+ *              names to make logic easier to follow.
+ */
+
+/*-
  * This was modifed from a 'screen saver' that a friend and I
  * wrote on our TI-8x calculators in high school physics one day
  * Basically another geometric pattern generator, this ones claim
  * to fame is a pseudo-fractal looking vine like pattern that creates
  * nifty whorls and loops.
- *
- * Revision History:
- * 10-May-97: jwz@jwz.org: turned into a standalone program.
- * 21-Mar-97:  David Hansen <dhansen@metapath.com>
- *             Updated mode to draw complete patterns on every
- *             iteration instead of growing the vine.  Also made
- *             adjustments to randomization and changed variable
- *             names to make logic easier to follow.
  */
 
 #ifdef STANDALONE
-# define PROGCLASS                                     "Vines"
-# define HACK_INIT                                     init_vines
-# define HACK_DRAW                                     draw_vines
-# define vines_opts                                    xlockmore_opts
-# define DEFAULTS              "*delay:        200000 \n"                      \
-                                               "*ncolors:      64     \n"                      \
-                                               "*eraseSpeed:   400 \n"                 \
-                                               "*eraseMode:    -1 \n"
-# include "xlockmore.h"                                /* from the xscreensaver distribution */
-# include "erase.h"
-#else  /* !STANDALONE */
-# include "xlock.h"                                    /* from the xlockmore distribution */
-#endif /* !STANDALONE */
-
-ModeSpecOpt vines_opts = {
-  0, NULL, 0, NULL, NULL };
+#define MODE_vines
+#define PROGCLASS "Vines"
+#define HACK_INIT init_vines
+#define HACK_DRAW draw_vines
+#define vines_opts xlockmore_opts
+#define DEFAULTS "*delay: 200000 \n" \
+ "*count: 0 \n" \
+ "*ncolors: 64 \n"
+#include "xlockmore.h"         /* in xscreensaver distribution */
+#include "erase.h"
+#else /* STANDALONE */
+#include "xlock.h"             /* in xlockmore distribution */
+#endif /* STANDALONE */
+
+#ifdef MODE_vines
+
+ModeSpecOpt vines_opts =
+{0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
+
+#ifdef USE_MODULES
+ModStruct   vines_description =
+{"vines", "init_vines", "draw_vines", "release_vines",
+ "refresh_vines", "init_vines", (char *) NULL, &vines_opts,
+ 200000, 0, 1, 1, 64, 1.0, "",
+ "Shows fractals", 0, NULL};
+
+#endif
 
 typedef struct {
        int         a;
@@ -48,6 +79,7 @@ typedef struct {
        int         y1;
        int         x2;
        int         y2;
+       int         i;
        int         length;
        int         iterations;
        int         constant;
@@ -56,87 +88,102 @@ typedef struct {
        int         centery;
 } vinestruct;
 
-static vinestruct *vines = NULL;
+static vinestruct *vines = (vinestruct *) NULL;
 
 void
 refresh_vines(ModeInfo * mi)
 {
-}
+       MI_CLEARWINDOW(mi);
+}                              /* refresh_vines */
 
 void
 init_vines(ModeInfo * mi)
 {
-       Display    *display = MI_DISPLAY(mi);
        vinestruct *fp;
 
        if (vines == NULL) {
-               if ((vines = (vinestruct *) calloc(MI_NUM_SCREENS(mi), sizeof (vinestruct))) == NULL) {
+               if ((vines = (vinestruct *) calloc(MI_NUM_SCREENS(mi),
+                                            sizeof (vinestruct))) == NULL) {
                        return;
                }
        }
        fp = &vines[MI_SCREEN(mi)];
 
+       fp->i = 0;
+       fp->length = 0;
        fp->iterations = 30 + NRAND(100);
 
-       XClearWindow(display, MI_WINDOW(mi));
-}
+       MI_CLEARWINDOW(mi);
+}                              /* init_vines */
 
 void
 draw_vines(ModeInfo * mi)
 {
-       vinestruct *fp = &vines[MI_SCREEN(mi)];
        Display    *display = MI_DISPLAY(mi);
        GC          gc = MI_GC(mi);
-       int         i;
+       int         count;
+       vinestruct *fp;
+
+       if (vines == NULL)
+               return;
+       fp = &vines[MI_SCREEN(mi)];
 
-       if (--(fp->iterations) == 0) {
+       /* MI_IS_DRAWN(mi) = True; */
+       if (fp->i >= fp->length) {
+               if (--(fp->iterations) == 0) {
 #ifdef STANDALONE
          erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
 #endif /* STANDALONE */
-         init_vines(mi);
+                       init_vines(mi);
+               }
+               fp->centerx = NRAND(MI_WIDTH(mi));
+               fp->centery = NRAND(MI_HEIGHT(mi));
+
+               fp->ang = 60 + NRAND(720);
+               fp->length = 100 + NRAND(3000);
+               fp->constant = fp->length * (10 + NRAND(10));
+
+               fp->i = 0;
+               fp->a = 0;
+               fp->x1 = 0;
+               fp->y1 = 0;
+               fp->x2 = 1;
+               fp->y2 = 0;
+
+               if (MI_NPIXELS(mi) > 2)
+                       XSetForeground(display, gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
+               else
+                       XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
        }
+       count = fp->i + MI_COUNT(mi);
+       if ((count <= fp->i) || (count > fp->length))
+               count = fp->length;
 
-       fp->centerx = NRAND(MI_WIN_WIDTH(mi));
-       fp->centery = NRAND(MI_WIN_HEIGHT(mi));
-
-       fp->ang = 60 + NRAND(720);
-       fp->length = 100 + NRAND(3000);
-       fp->constant = fp->length * (10 + NRAND(10));
-
-       fp->a = 0;
-       fp->x1 = 0;
-       fp->y1 = 0;
-       fp->x2 = 1;
-       fp->y2 = 0;
-
-       if (MI_NPIXELS(mi) > 2)
-               XSetForeground(display, gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
-       else
-               XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
-
-
-       for (i = 0; i < fp->length; i++) {
+       while (fp->i < count) {
                XDrawLine(display, MI_WINDOW(mi), gc,
                          fp->centerx + (fp->x1 / fp->constant),
                          fp->centery - (fp->y1 / fp->constant),
                          fp->centerx + (fp->x2 / fp->constant),
                          fp->centery - (fp->y2 / fp->constant));
 
-               fp->a += (fp->ang * i);
+               fp->a += (fp->ang * fp->i);
 
                fp->x1 = fp->x2;
                fp->y1 = fp->y2;
 
-               fp->x2 += (int) (i * ((cos(fp->a) * 360) / (2 * M_PI)));
-               fp->y2 += (int) (i * ((sin(fp->a) * 360) / (2 * M_PI)));
+               fp->x2 += (int) (fp->i * (cos((double) fp->a) * 360.0) / (2.0 * M_PI));
+               fp->y2 += (int) (fp->i * (sin((double) fp->a) * 360.0) / (2.0 * M_PI));
+               fp->i++;
        }
-}
+}                              /* draw_vines */
 
 void
 release_vines(ModeInfo * mi)
 {
        if (vines != NULL) {
                (void) free((void *) vines);
-               vines = NULL;
+               vines = (vinestruct *) NULL;
        }
-}
+}                              /* release_vines */
+
+#endif /* MODE_vines */