From http://www.jwz.org/xscreensaver/xscreensaver-5.35.tar.gz
[xscreensaver] / hacks / glx / rubik.c
index 525c6e8fe012fb3ce045e11a8f0976626196e055..0cc99019ebc19ab8dc4970a6f2ee9e127117f28e 100644 (file)
@@ -105,19 +105,22 @@ static const char sccsid[] = "@(#)rubik.c 5.01 2001/03/01 xlockmore";
 
 #ifdef STANDALONE
 # define MODE_rubik
-# define DEFAULTS      "*delay: 40000 \n"              \
+# define DEFAULTS      "*delay: 20000 \n"              \
                                        "*count: -30 \n"                \
                                        "*showFPS: False \n"    \
-                                       "*cycles: 5 \n"                 \
-                                       "*size:  -6 \n"
+                                       "*cycles: 20 \n"                \
+                                       "*size:  -6 \n"         \
+                                       "*suppressRotationAnimation: True\n" \
+
 # define refresh_rubik 0
-# define rubik_handle_event 0
 # include "xlockmore.h"                                /* from the xscreensaver distribution */
 #else /* !STANDALONE */
 # include "xlock.h"                                    /* from the xlockmore distribution */
 # include "vis.h"
 #endif /* !STANDALONE */
 
+#include "gltrackball.h"
+
 #ifdef MODE_rubik
 
 #define DEF_SIZEX     "0"
@@ -372,6 +375,8 @@ typedef struct {
        GLfloat     rotatestep;
        GLfloat     PX, PY, VX, VY;
        GLXContext *glx_context;
+    Bool button_down_p;
+    trackball_state *trackball;
 } rubikstruct;
 
 static const float front_shininess[] = {60.0};
@@ -468,6 +473,7 @@ faceSizes(rubikstruct * rp, int face, int * sizeOfRow, int * sizeOfColumn)
                        *sizeOfRow = MAXSIZEX;
                        *sizeOfColumn = MAXSIZEY;
                        break;
+                default: abort();
        }
 }
 
@@ -1621,7 +1627,7 @@ moveRubik(rubikstruct * rp, int face, int direction, int position)
 }
 
 #ifdef DEBUG
-void
+static void
 printCube(rubikstruct * rp)
 {
        int         face, position, sizeOfRow, sizeOfColumn;
@@ -1789,10 +1795,10 @@ shuffle(ModeInfo * mi)
                                return False;
                rp->moves[i] = move;
        }
-       rp->VX = 0.05;
+       rp->VX = 0.005;
        if (NRAND(100) < 50)
                rp->VX *= -1;
-       rp->VY = 0.05;
+       rp->VY = 0.005;
        if (NRAND(100) < 50)
                rp->VY *= -1;
        rp->movement.face = NO_FACE;
@@ -1813,14 +1819,31 @@ reshape_rubik(ModeInfo * mi, int width, int height)
        glLoadIdentity();
        glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
        glMatrixMode(GL_MODELVIEW);
+}
 
+ENTRYPOINT Bool
+rubik_handle_event (ModeInfo *mi, XEvent *event)
+{
+  rubikstruct *rp = &rubik[MI_SCREEN(mi)];
+
+  if (gltrackball_event_handler (event, rp->trackball,
+                                 MI_WIDTH (mi), MI_HEIGHT (mi),
+                                 &rp->button_down_p))
+    return True;
+  else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
+    {
+      rp->done = 1;
+      return True;
+    }
+
+  return False;
 }
 
+
 static Bool
 pinit(ModeInfo * mi)
 {
        glClearDepth(1.0);
-       glClearColor(0.0, 0.0, 0.0, 1.0);
        glColor3f(1.0, 1.0, 1.0);
 
        glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
@@ -1898,6 +1921,8 @@ init_rubik(ModeInfo * mi)
        rp->PX = ((float) LRAND() / (float) MAXRAND) * 2.0 - 1.0;
        rp->PY = ((float) LRAND() / (float) MAXRAND) * 2.0 - 1.0;
 
+    rp->trackball = gltrackball_init (True);
+
        if ((rp->glx_context = init_GL(mi)) != NULL) {
 
                reshape_rubik(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
@@ -1966,16 +1991,16 @@ draw_rubik(ModeInfo * mi)
                bounced = True;
        }
        if (bounced) {
-               rp->VX += ((float) LRAND() / (float) MAXRAND) * 0.02 - 0.01;
-               rp->VX += ((float) LRAND() / (float) MAXRAND) * 0.02 - 0.01;
-               if (rp->VX > 0.06)
-                       rp->VX = 0.06;
-               if (rp->VY > 0.06)
-                       rp->VY = 0.06;
-               if (rp->VX < -0.06)
-                       rp->VX = -0.06;
-               if (rp->VY < -0.06)
-                       rp->VY = -0.06;
+               rp->VX += ((float) LRAND() / (float) MAXRAND) * 0.002 - 0.001;
+               rp->VY += ((float) LRAND() / (float) MAXRAND) * 0.002 - 0.001;
+               if (rp->VX > 0.006)
+                       rp->VX = 0.006;
+               if (rp->VY > 0.006)
+                       rp->VY = 0.006;
+               if (rp->VX < -0.006)
+                       rp->VX = -0.006;
+               if (rp->VY < -0.006)
+                       rp->VY = -0.006;
        }
        if (!MI_IS_ICONIC(mi)) {
                glTranslatef(rp->PX, rp->PY, 0);
@@ -1984,6 +2009,20 @@ draw_rubik(ModeInfo * mi)
                glScalef(Scale4Iconic * rp->WindH / rp->WindW, Scale4Iconic, Scale4Iconic);
        }
 
+# ifdef HAVE_MOBILE    /* Keep it the same relative size when rotated. */
+  {
+    GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
+    int o = (int) current_device_rotation();
+    if (o != 0 && o != 180 && o != -180) {
+      glScalef (1/h, h, 1); /* #### not quite right */
+      h = 1.8;
+      glScalef (h, h, h);
+    }
+  }
+# endif
+
+    gltrackball_rotate (rp->trackball);
+
        glRotatef(rp->step * 100, 1, 0, 0);
        glRotatef(rp->step * 95, 0, 1, 0);
        glRotatef(rp->step * 90, 0, 0, 1);
@@ -2084,7 +2123,7 @@ draw_rubik(ModeInfo * mi)
 
        glFlush();
 
-       rp->step += 0.05;
+       rp->step += 0.002;
 }
 
 #ifndef STANDALONE