From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / apollonian.c
index 0c1888acd4c834d5d84ae6f3a16a3ceec040f1ef..6986e73eb3a14ad272032223dc9f1f26dbcf4ba8 100644 (file)
@@ -65,19 +65,22 @@ static const char sccsid[] = "@(#)apollonian.c      5.02 2001/07/01 xlockmore";
  */
 
 #ifdef STANDALONE
-#define MODE_apollonian
-#define PROGCLASS "Apollonian"
-#define HACK_INIT init_apollonian
-#define HACK_DRAW draw_apollonian
-#define apollonian_opts xlockmore_opts
-#define DEFAULTS "*delay: 1000000 \n" \
- "*count: 64 \n" \
- "*cycles: 20 \n" \
- "*ncolors: 64 \n"
-#include "xlockmore.h"         /* in xscreensaver distribution */
-#include "erase.h"
+# define MODE_apollonian
+# define DEFAULTS      "*delay:   1000000 \n" \
+                                       "*count:   64      \n" \
+                                       "*cycles:  20      \n" \
+                                       "*ncolors: 64      \n" \
+                                       "*font:    fixed" "\n" \
+                                       "*fpsTop: true     \n" \
+                                       "*fpsSolid: true   \n" \
+                                       "*ignoreRotation: True" \
+
+# define refresh_apollonian 0
+# define release_apollonian 0
+# include "xlockmore.h"                /* in xscreensaver distribution */
+# include "erase.h"
 #else /* STANDALONE */
-#include "xlock.h"             /* in xlockmore distribution */
+# include "xlock.h"            /* in xlockmore distribution */
 #endif /* STANDALONE */
 
 #ifdef MODE_apollonian
@@ -90,23 +93,23 @@ static Bool label;
 
 static XrmOptionDescRec opts[] =
 {
-       {(char *) "-altgeom", (char *) ".apollonian.altgeom", XrmoptionNoArg, (caddr_t) "on"},
-       {(char *) "+altgeom", (char *) ".apollonian.altgeom", XrmoptionNoArg, (caddr_t) "off"},
-       {(char *) "-label", (char *) ".apollonian.label", XrmoptionNoArg, (caddr_t) "on"},
-       {(char *) "+label", (char *) ".apollonian.label", XrmoptionNoArg, (caddr_t) "off"},
+       {"-altgeom", ".apollonian.altgeom", XrmoptionNoArg, "on"},
+       {"+altgeom", ".apollonian.altgeom", XrmoptionNoArg, "off"},
+       {"-label", ".apollonian.label", XrmoptionNoArg, "on"},
+       {"+label", ".apollonian.label", XrmoptionNoArg, "off"},
 };
 static argtype vars[] =
 {
-       {(caddr_t *) & altgeom, (char *) "altgeom", (char *) "AltGeom", (char *) DEF_ALTGEOM, t_Bool},
-       {(caddr_t *) & label, (char *) "label", (char *) "Label", (char *) DEF_LABEL, t_Bool},
+       {&altgeom, "altgeom", "AltGeom", DEF_ALTGEOM, t_Bool},
+       {&label,   "label",   "Label",   DEF_LABEL,   t_Bool},
 };
 static OptionStruct desc[] =
 {
-        {(char *) "-/+altgeom", (char *) "turn on/off alternate geometries (off euclidean space, on includes spherical and hyperbolic)"},
-        {(char *) "-/+label", (char *) "turn on/off alternate space and number labeling"},
+        {"-/+altgeom", "turn on/off alternate geometries (off euclidean space, on includes spherical and hyperbolic)"},
+        {"-/+label", "turn on/off alternate space and number labeling"},
 };
 
-ModeSpecOpt apollonian_opts =
+ENTRYPOINT ModeSpecOpt apollonian_opts =
 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
 
 #ifdef DOFONT
@@ -115,7 +118,7 @@ extern XFontStruct *getFont(Display * display);
 
 #ifdef USE_MODULES
 ModStruct   apollonian_description =
-{"apollonian", "init_apollonian", "draw_apollonian", "release_apollonian",
+{"apollonian", "init_apollonian", "draw_apollonian", (char *) NULL,
  "init_apollonian", "init_apollonian", (char *) NULL, &apollonian_opts,
  1000000, 64, 20, 1, 64, 1.0, "",
  "Shows Apollonian Circles", 0, NULL};
@@ -136,7 +139,7 @@ enum space {
   euclidean = 0, spherical, hyperbolic
 };
 
-const char * space_string[] = {
+static const char * space_string[] = {
   "euclidean",
   "spherical",
   "hyperbolic"
@@ -159,7 +162,7 @@ the label in the standard picture) then the "spherical label" is
 (e^2+x^2+y^2-1)/(2*e) (an integer!)  and the "hyperbolic label", is 
 calulated by h + s = e.
 */
-circle examples[][4] = {
+static circle examples[][4] = {
 { /* double semi-bounded */
        { 0, 0, 0,   0,  1},
        { 0, 0, 0,   0, -1},
@@ -296,6 +299,9 @@ typedef struct {
 #endif
        int         time;
        int         game;
+#ifdef STANDALONE
+  eraser_state *eraser;
+#endif
 } apollonianstruct;
 
 static apollonianstruct *apollonians = (apollonianstruct *) NULL;
@@ -501,7 +507,7 @@ static void
 p(ModeInfo *mi, circle c)
 {
        apollonianstruct *cp = &apollonians[MI_SCREEN(mi)];
-       char string[10];
+       char string[15];
        double g, e;
        int g_width;
 
@@ -547,7 +553,7 @@ p(ModeInfo *mi, circle c)
                  XDrawString(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
                        ((int) (cp->size * c.x / (2.0 * c.e) + cp->offset.x)),
                        ((int) (cp->size * c.y / (2.0 * c.e) + MI_HEIGHT(mi) -
-                       FONT_HEIGHT / 2)), space_string[cp->geometry],
+                       FONT_HEIGHT / 2)), (char *) space_string[cp->geometry],
                        strlen(space_string[cp->geometry]));
                }
                return;
@@ -608,29 +614,34 @@ p(ModeInfo *mi, circle c)
 
 #define BIG 7
 static void
-f(ModeInfo *mi, circle c1, circle c2, circle c3, circle c4)
+f(ModeInfo *mi, circle c1, circle c2, circle c3, circle c4, int depth)
 {
        apollonianstruct *cp = &apollonians[MI_SCREEN(mi)];
        int e = (int) ((cp->c1.e >= 0.0) ? 1.0 : -cp->c1.e);
         circle c;
 
+       if (depth > mi->recursion_depth) mi->recursion_depth = depth;
+
         c.e = 2*(c1.e+c2.e+c3.e) - c4.e;
         c.s = 2*(c1.s+c2.s+c3.s) - c4.s;
         c.h = 2*(c1.h+c2.h+c3.h) - c4.h;
         c.x = 2*(c1.x+c2.x+c3.x) - c4.x;
         c.y = 2*(c1.y+c2.y+c3.y) - c4.y;
-        if (c.e > cp->size * e || c.x / c.e > BIG || c.y / c.e > BIG ||
+        if (c.e == 0 ||
+            c.e > cp->size * e || c.x / c.e > BIG || c.y / c.e > BIG ||
             c.x / c.e < -BIG || c.y / c.e < -BIG)
                 return;
         p(mi, c);
-        f(mi, c2, c3, c, c1);
-        f(mi, c1, c3, c, c2);
-        f(mi, c1, c2, c, c3);
+        f(mi, c2, c3, c, c1, depth+1);
+        f(mi, c1, c3, c, c2, depth+1);
+        f(mi, c1, c2, c, c3, depth+1);
 }
 
-static void
-free_apollonian(Display *display, apollonianstruct *cp)
+ENTRYPOINT void
+free_apollonian (ModeInfo * mi)
 {
+       apollonianstruct *cp = &apollonians[MI_SCREEN(mi)];
+
        if (cp->quad != NULL) {
                (void) free((void *) cp->quad);
                cp->quad = (apollonian_quadruple *) NULL;
@@ -648,7 +659,7 @@ free_apollonian(Display *display, apollonianstruct *cp)
 }
 
 #ifndef DEBUG
-void
+static void
 randomize_c(int randomize, circle * c)
 {
   if (randomize / 2) {
@@ -665,17 +676,13 @@ randomize_c(int randomize, circle * c)
 }
 #endif
 
-void
-init_apollonian(ModeInfo * mi)
+ENTRYPOINT void
+init_apollonian (ModeInfo * mi)
 {
        apollonianstruct *cp;
        int i;
 
-       if (apollonians == NULL) {
-               if ((apollonians = (apollonianstruct *) calloc(MI_NUM_SCREENS(mi),
-                                            sizeof (apollonianstruct))) == NULL)
-                       return;
-       }
+       MI_INIT (mi, apollonians, free_apollonian);
        cp = &apollonians[MI_SCREEN(mi)];
 
        cp->size = MAX(MIN(MI_WIDTH(mi), MI_HEIGHT(mi)) - 1, 1);
@@ -720,7 +727,9 @@ init_apollonian(ModeInfo * mi)
                        cquad(&(cp->c1), &(cp->c2), &(cp->c3), &(cp->c4));
        }
        cp->time = 0;
+#ifndef STANDALONE
        MI_CLEARWINDOW(mi);
+#endif
        if (cp->game != 0) {
                double q123;
 
@@ -760,10 +769,12 @@ init_apollonian(ModeInfo * mi)
        randomize_c(i, &(cp->c3));
        randomize_c(i, &(cp->c4));
 #endif 
+
+    mi->recursion_depth = -1;
 }
 
-void
-draw_apollonian(ModeInfo * mi)
+ENTRYPOINT void
+draw_apollonian (ModeInfo * mi)
 {
        apollonianstruct *cp;
 
@@ -771,6 +782,14 @@ draw_apollonian(ModeInfo * mi)
                return;
        cp = &apollonians[MI_SCREEN(mi)];
 
+#ifdef STANDALONE
+    if (cp->eraser) {
+      cp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), cp->eraser);
+      if (!cp->eraser)
+        init_apollonian(mi);
+      return;
+    }
+#endif
 
        MI_IS_DRAWN(mi) = True;
 
@@ -783,38 +802,46 @@ draw_apollonian(ModeInfo * mi)
                        p(mi, cp->c4);
                        break;
                case 1:
-                       f(mi, cp->c1, cp->c2, cp->c3, cp->c4);
+                       f(mi, cp->c1, cp->c2, cp->c3, cp->c4, 0);
                        break;
                case 2:
-                       f(mi, cp->c1, cp->c2, cp->c4, cp->c3);
+                       f(mi, cp->c1, cp->c2, cp->c4, cp->c3, 0);
                        break;
                case 3:
-                       f(mi, cp->c1, cp->c3, cp->c4, cp->c2);
+                       f(mi, cp->c1, cp->c3, cp->c4, cp->c2, 0);
                        break;
                case 4:
-                       f(mi, cp->c2, cp->c3, cp->c4, cp->c1);
+                       f(mi, cp->c2, cp->c3, cp->c4, cp->c1, 0);
                }
        }
        if (++cp->time > MI_CYCLES(mi))
       {
 #ifdef STANDALONE
-        erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
-#endif /* STANDALONE */
+        cp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), cp->eraser);
+#else /* !STANDALONE */
                init_apollonian(mi);
+#endif /* !STANDALONE */
       }
 }
 
-void
-release_apollonian(ModeInfo * mi)
+ENTRYPOINT void
+reshape_apollonian(ModeInfo * mi, int width, int height)
 {
-       if (apollonians != NULL) {
-               int         screen;
+  XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
+  init_apollonian (mi);
+}
 
-               for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
-                       free_apollonian(MI_DISPLAY(mi), &apollonians[screen]);
-               (void) free((void *) apollonians);
-               apollonians = (apollonianstruct *) NULL;
-       }
+ENTRYPOINT Bool
+apollonian_handle_event (ModeInfo *mi, XEvent *event)
+{
+  if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
+    {
+      reshape_apollonian (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
+      return True;
+    }
+  return False;
 }
 
+XSCREENSAVER_MODULE ("Apollonian", apollonian)
+
 #endif /* MODE_apollonian */