http://www.tienza.es/crux/src/www.jwz.org/xscreensaver/xscreensaver-5.05.tar.gz
[xscreensaver] / hacks / glx / menger.c
index fea32236889c4b4f98c4855be2201a13445de081..50b8d022548ca46715977954d17a20510646fba0 100644 (file)
@@ -1,4 +1,4 @@
-/* menger, Copyright (c) 2001, 2002 Jamie Zawinski <jwz@jwz.org>
+/* menger, Copyright (c) 2001-2006 Jamie Zawinski <jwz@jwz.org>
  *         Copyright (c) 2002 Aurelien Jacobs <aurel@gnuage.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  *  just draw them as spots on the surface!  It would look the same.
  */
 
-#include <X11/Intrinsic.h>
-
-extern XtAppContext app;
-
-#define PROGCLASS      "Menger"
-#define HACK_INIT      init_sponge
-#define HACK_DRAW      draw_sponge
-#define HACK_RESHAPE   reshape_sponge
-#define HACK_HANDLE_EVENT sponge_handle_event
-#define EVENT_MASK     PointerMotionMask
-#define sws_opts       xlockmore_opts
-
-#define DEF_SPIN        "True"
-#define DEF_WANDER      "True"
-#define DEF_SPEED       "150"
-#define DEF_MAX_DEPTH   "3"
-#define DEF_OPTIMIZE    "True"
-
 #define DEFAULTS       "*delay:         30000          \n" \
                        "*showFPS:       False          \n" \
                        "*wireframe:     False          \n" \
-                       "*maxDepth:    " DEF_MAX_DEPTH "\n" \
-                       "*speed:"        DEF_SPEED     "\n" \
-                       "*optimize:"     DEF_OPTIMIZE  "\n" \
-                       "*spin:        " DEF_SPIN      "\n" \
-                       "*wander:      " DEF_WANDER    "\n" \
 
 
+# define refresh_sponge 0
+# define release_sponge 0
 #undef countof
 #define countof(x) (sizeof((x))/sizeof((*x)))
 
@@ -89,8 +68,10 @@ extern XtAppContext app;
 
 #ifdef USE_GL /* whole file */
 
-#include <GL/glu.h>
-
+#define DEF_SPIN        "True"
+#define DEF_WANDER      "True"
+#define DEF_SPEED       "150"
+#define DEF_MAX_DEPTH   "3"
 
 typedef struct {
   GLXContext *glx_context;
@@ -111,6 +92,8 @@ typedef struct {
   int ccolor1;
   int ccolor2;
 
+  int draw_tick;
+
 } sponge_configuration;
 
 static sponge_configuration *sps = NULL;
@@ -118,34 +101,29 @@ static sponge_configuration *sps = NULL;
 static Bool do_spin;
 static Bool do_wander;
 static int speed;
-static Bool do_optimize;
 static int max_depth;
 
 static XrmOptionDescRec opts[] = {
-  { "-spin",   ".spin",   XrmoptionNoArg, "True" },
-  { "+spin",   ".spin",   XrmoptionNoArg, "False" },
-  { "-wander", ".wander", XrmoptionNoArg, "True" },
-  { "+wander", ".wander", XrmoptionNoArg, "False" },
-  { "-speed",  ".speed",  XrmoptionSepArg, 0 },
-  { "-optimize", ".optimize", XrmoptionNoArg, "True" },
-  { "+optimize", ".optimize", XrmoptionNoArg, "False" },
-  {"-depth",   ".maxDepth", XrmoptionSepArg, (caddr_t) 0 },
+  { "-wander", ".wander",   XrmoptionNoArg, "True"  },
+  { "+wander", ".wander",   XrmoptionNoArg, "False" },
+  { "-spin",   ".spin",     XrmoptionSepArg, 0 },
+  { "-speed",  ".speed",    XrmoptionSepArg, 0 },
+  { "-depth",  ".maxDepth", XrmoptionSepArg, 0 },
 };
 
 static argtype vars[] = {
   {&do_spin,     "spin",     "Spin",     DEF_SPIN,      t_Bool},
   {&do_wander,   "wander",   "Wander",   DEF_WANDER,    t_Bool},
   {&speed,       "speed",    "Speed",    DEF_SPEED,     t_Int},
-  {&do_optimize, "optimize", "Optimize", DEF_OPTIMIZE,  t_Bool},
   {&max_depth,   "maxDepth", "MaxDepth", DEF_MAX_DEPTH, t_Int},
 };
 
-ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
+ENTRYPOINT ModeSpecOpt sponge_opts = {countof(opts), opts, countof(vars), vars, NULL};
 
 
 /* Window management, etc
  */
-void
+ENTRYPOINT void
 reshape_sponge (ModeInfo *mi, int width, int height)
 {
   GLfloat h = (GLfloat) height / (GLfloat) width;
@@ -250,17 +228,16 @@ cube (float x0, float x1, float y0, float y1, float z0, float z1,
 }
 
 static int
-menger_recurs (int level, float x0, float x1, float y0, float y1,
-               float z0, float z1, int faces, Bool wireframe, int orig)
+menger_recurs_1 (int level, float x0, float x1, float y0, float y1,
+                 float z0, float z1, int faces, Bool wireframe, 
+                 int orig, int forig)
 {
   float xi, yi, zi;
   int f, x, y, z;
-  static int forig;
   int n = 0;
 
   if (orig)
     {
-      forig = faces;
       if (wireframe)
         n += cube (x0, x1, y0, y1, z0, z1,
                    faces & (X0 | X1 | Y0 | Y1), wireframe);
@@ -314,10 +291,11 @@ menger_recurs (int level, float x0, float x1, float y0, float y1,
                   if (forig & Z1 && z == 0 && (x == 1 || y == 1))
                     f |= Z1;
 
-                  n += menger_recurs (level-1,
-                                      x0+x*xi, x0+(x+1)*xi,
-                                      y0+y*yi, y0+(y+1)*yi,
-                                      z0+z*zi, z0+(z+1)*zi, f, wireframe, 0);
+                  n += menger_recurs_1 (level-1,
+                                        x0+x*xi, x0+(x+1)*xi,
+                                        y0+y*yi, y0+(y+1)*yi,
+                                        z0+z*zi, z0+(z+1)*zi, f, wireframe, 0,
+                                        forig);
                 }
               else if (wireframe && (x != 1 || y != 1 || z != 1))
                 n += cube (x0+x*xi, x0+(x+1)*xi,
@@ -330,6 +308,16 @@ menger_recurs (int level, float x0, float x1, float y0, float y1,
   return n;
 }
 
+static int
+menger_recurs (int level, float x0, float x1, float y0, float y1,
+               float z0, float z1, int faces, Bool wireframe, 
+               int orig)
+{
+  return menger_recurs_1 (level, x0, x1, y0, y1, z0, z1, faces, 
+                          wireframe, orig, faces);
+}
+
+
 static void
 build_sponge (sponge_configuration *sp, Bool wireframe, int level)
 {
@@ -353,13 +341,13 @@ build_sponge (sponge_configuration *sp, Bool wireframe, int level)
 }
 
 
-Bool
+ENTRYPOINT Bool
 sponge_handle_event (ModeInfo *mi, XEvent *event)
 {
   sponge_configuration *sp = &sps[MI_SCREEN(mi)];
 
   if (event->xany.type == ButtonPress &&
-      event->xbutton.button & Button1)
+      event->xbutton.button == Button1)
     {
       sp->button_down_p = True;
       gltrackball_start (sp->trackball,
@@ -368,11 +356,21 @@ sponge_handle_event (ModeInfo *mi, XEvent *event)
       return True;
     }
   else if (event->xany.type == ButtonRelease &&
-           event->xbutton.button & Button1)
+           event->xbutton.button == Button1)
     {
       sp->button_down_p = False;
       return True;
     }
+  else if (event->xany.type == ButtonPress &&
+           (event->xbutton.button == Button4 ||
+            event->xbutton.button == Button5 ||
+            event->xbutton.button == Button6 ||
+            event->xbutton.button == Button7))
+    {
+      gltrackball_mousewheel (sp->trackball, event->xbutton.button, 5,
+                              !!event->xbutton.state);
+      return True;
+    }
   else if (event->xany.type == MotionNotify &&
            sp->button_down_p)
     {
@@ -387,7 +385,7 @@ sponge_handle_event (ModeInfo *mi, XEvent *event)
 
 
 
-void 
+ENTRYPOINT void 
 init_sponge (ModeInfo *mi)
 {
   sponge_configuration *sp;
@@ -412,10 +410,10 @@ init_sponge (ModeInfo *mi)
 
   if (!wire)
     {
-      static GLfloat pos0[4] = {-1.0, -1.0, 1.0, 0.1};
-      static GLfloat pos1[4] = { 1.0, -0.2, 0.2, 0.1};
-      static GLfloat dif0[4] = {1.0, 1.0, 1.0, 1.0};
-      static GLfloat dif1[4] = {1.0, 1.0, 1.0, 1.0};
+      static const GLfloat pos0[4] = {-1.0, -1.0, 1.0, 0.1};
+      static const GLfloat pos1[4] = { 1.0, -0.2, 0.2, 0.1};
+      static const GLfloat dif0[4] = {1.0, 1.0, 1.0, 1.0};
+      static const GLfloat dif1[4] = {1.0, 1.0, 1.0, 1.0};
 
       glLightfv(GL_LIGHT0, GL_POSITION, pos0);
       glLightfv(GL_LIGHT1, GL_POSITION, pos1);
@@ -456,25 +454,27 @@ init_sponge (ModeInfo *mi)
   sp->sponge_list0 = glGenLists (1);
   sp->sponge_list1 = glGenLists (1);
   sp->sponge_list2 = glGenLists (1);
+
+  sp->draw_tick = 9999999;
 }
 
 
-void
+ENTRYPOINT void
 draw_sponge (ModeInfo *mi)
 {
   sponge_configuration *sp = &sps[MI_SCREEN(mi)];
   Display *dpy = MI_DISPLAY(mi);
   Window window = MI_WINDOW(mi);
 
-  static GLfloat color0[4] = {0.0, 0.0, 0.0, 1.0};
-  static GLfloat color1[4] = {0.0, 0.0, 0.0, 1.0};
-  static GLfloat color2[4] = {0.0, 0.0, 0.0, 1.0};
-
-  static int tick = 99999;
+  GLfloat color0[4] = {0.0, 0.0, 0.0, 1.0};
+  GLfloat color1[4] = {0.0, 0.0, 0.0, 1.0};
+  GLfloat color2[4] = {0.0, 0.0, 0.0, 1.0};
 
   if (!sp->glx_context)
     return;
 
+  glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
+
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
   glPushMatrix ();
@@ -516,9 +516,9 @@ draw_sponge (ModeInfo *mi)
   if (sp->ccolor1 >= sp->ncolors) sp->ccolor1 = 0;
   if (sp->ccolor2 >= sp->ncolors) sp->ccolor2 = 0;
 
-  if (tick++ >= speed)
+  if (sp->draw_tick++ >= speed)
     {
-      tick = 0;
+      sp->draw_tick = 0;
       if (sp->current_depth >= max_depth)
         sp->current_depth = -max_depth;
       sp->current_depth++;
@@ -547,4 +547,6 @@ draw_sponge (ModeInfo *mi)
   glXSwapBuffers(dpy, window);
 }
 
+XSCREENSAVER_MODULE_2 ("Menger", menger, sponge)
+
 #endif /* USE_GL */