ftp://ftp.linux.ncsu.edu/mirror/ftp.redhat.com/pub/redhat/linux/enterprise/4/en/os...
[xscreensaver] / hacks / glx / flipscreen3d.c
index 2134fbcfae256ebaba876c623352c86aa27aa864..4cee56ee262011142accee066014dd53add1e138 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * screenflip - takes snapshots of the screen and flips it around
+ * flipscreen3d - takes snapshots of the screen and flips it around
  *
  * version 1.0 - Oct 24, 2001
  *
 
 
 #ifdef STANDALONE
-# define PROGCLASS                                      "Screenflip"
+# define PROGCLASS                                      "FlipScreen3D"
 # define HACK_INIT                                      init_screenflip
 # define HACK_DRAW                                      draw_screenflip
 # define HACK_RESHAPE                           reshape_screenflip
+# define HACK_HANDLE_EVENT                             screenflip_handle_event
+# define EVENT_MASK                                    PointerMotionMask
 # define screenflip_opts                                     xlockmore_opts
 /* insert defaults here */
 
@@ -61,13 +63,13 @@ GLfloat qx = -6 , qy = 6;
 
 
 static XrmOptionDescRec opts[] = {
-  {"+rotate", ".screenflip.rotate", XrmoptionNoArg, (caddr_t) "false" },
-  {"-rotate", ".screenflip.rotate", XrmoptionNoArg, (caddr_t) "true" },
+  {"+rotate", ".screenflip.rotate", XrmoptionNoArg, "false" },
+  {"-rotate", ".screenflip.rotate", XrmoptionNoArg, "true" },
 };
 
 
 static argtype vars[] = {
-  {(caddr_t *) &rotate, "rotate", "Rotate", "True", t_Bool},
+  {&rotate, "rotate", "Rotate", "True", t_Bool},
 };
 
 
@@ -97,6 +99,7 @@ static Screenflip *screenflip = NULL;
 #include <stdio.h>
 #include <stdlib.h>
 #include "grab-ximage.h"
+#include "gltrackball.h"
 
 #ifndef M_PI
 #define M_PI 3.14159265
@@ -107,6 +110,40 @@ static GLfloat viewer[] = {0.0, 0.0, 15.0};
 int regrab = 0;
 int fadetime = 0; /* fade before regrab */
 
+static trackball_state *trackball;
+static Bool button_down_p = False;
+
+
+Bool
+screenflip_handle_event (ModeInfo *mi, XEvent *event)
+{
+  if (event->xany.type == ButtonPress &&
+      event->xbutton.button & Button1)
+    {
+      button_down_p = True;
+      gltrackball_start (trackball,
+                         event->xbutton.x, event->xbutton.y,
+                         MI_WIDTH (mi), MI_HEIGHT (mi));
+      return True;
+    }
+  else if (event->xany.type == ButtonRelease &&
+           event->xbutton.button & Button1)
+    {
+      button_down_p = False;
+      return True;
+    }
+  else if (event->xany.type == MotionNotify &&
+           button_down_p)
+    {
+      gltrackball_track (trackball,
+                         event->xmotion.x, event->xmotion.y,
+                         MI_WIDTH (mi), MI_HEIGHT (mi));
+      return True;
+    }
+
+  return False;
+}
+
 
 /* draw the texture mapped quad (actually two back to back)*/
 void showscreen(int frozen, int wire)
@@ -143,19 +180,23 @@ void showscreen(int frozen, int wire)
   if (!frozen) {
      w *= sin (stretch_val_x) + 1;
      x *= sin (stretch_val_x) + 1;
+     if (!button_down_p) {
      if (!fadetime) stretch_val_x += stretch_val_dx;
      if (stretch_val_x > 2*M_PI && !(random() % 5))
        stretch_val_dx = (float)(random() % 100) / 5000;
      else
        stretch_val_x -= 2*M_PI;
+     }
 
-     if (!fadetime) stretch_val_y += stretch_val_dy;
+     if (!button_down_p && !fadetime) stretch_val_y += stretch_val_dy;
      h *= sin (stretch_val_y) / 2 + 1;
      y *= sin (stretch_val_y) / 2 + 1;
+     if (!button_down_p) {
      if (stretch_val_y > 2*M_PI && !(random() % 5))
        stretch_val_dy = (float)(random() % 100) / 5000;
      else
        stretch_val_y -= 2*M_PI;
+     }
   }
 
   glColor4f(r, g, b, a);
@@ -299,7 +340,7 @@ void display(int wire)
     frozen = 0;
     glTranslatef(5 * sin(theta), 5 * sin(rho), 10 * cos(gamma) - 10);
 /* randomly change the speed */
-    if (!(random() % 300)) {
+    if (!button_down_p && !(random() % 300)) {
       if (random() % 2)
         drho = 1/60 - (float)(random() % 100)/3000;
       if (random() % 2)
@@ -307,9 +348,10 @@ void display(int wire)
       if (random() % 2)
         dgamma = 1/60 - (float)(random() % 100)/3000;
     }
+    gltrackball_rotate (trackball);
     if (rotate) glRotatef(rot, rx, ry, rz);
 /* update variables with each frame */
-    if (!fadetime) {
+    if(!button_down_p && !fadetime) {
       theta += dtheta;
       rho += drho;
       gamma += dgamma;
@@ -327,7 +369,7 @@ void display(int wire)
     rot = 0;
     frozen = 1;
   }
-  if (!fadetime && (rot >= 360 || rot <= -360) && !(random() % 7)) { /* rotate  change */
+  if (!button_down_p && !fadetime && (rot >= 360 || rot <= -360) && !(random() % 7)) { /* rotate  change */
     rx = (GLfloat)(random() % 100) / 100;
     ry = (GLfloat)(random() % 100) / 100;
     rz = (GLfloat)(random() % 100) / 100;
@@ -363,7 +405,7 @@ void getSnapshot (ModeInfo *modeinfo)
   if (MI_IS_WIREFRAME(modeinfo))
     return;
 
- ximage = screen_to_ximage (modeinfo->xgwa.screen, modeinfo->window);
+ ximage = screen_to_ximage (modeinfo->xgwa.screen, modeinfo->window, NULL);
 
   qw = QW; qh = QH;
   tw = modeinfo->xgwa.width;
@@ -389,9 +431,15 @@ void getSnapshot (ModeInfo *modeinfo)
  status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
                             ximage->width, ximage->height,
                             GL_RGBA, GL_UNSIGNED_BYTE, ximage->data);
+
+ if (!status && glGetError())
+   /* Some implementations of gluBuild2DMipmaps(), but set a GL error anyway.
+      We could just call check_gl_error(), but that would exit. */
+   status = -1;
+
  if (status)
    {
-     const char *s = gluErrorString (status);
+     const char *s = (char *) gluErrorString (status);
      fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
               progname, ximage->width, ximage->height,
               (s ? s : "(unknown)"));
@@ -420,6 +468,8 @@ void init_screenflip(ModeInfo *mi)
  c = &screenflip[screen];
  c->window = MI_WINDOW(mi);
 
+ trackball = gltrackball_init ();
+
  if ((c->glx_context = init_GL(mi)) != NULL) {
       reshape_screenflip(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
  } else {