From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / topblock.c
index f794b197db2d285c63e0111ccce12e4b8d40c0f6..ddf1eb1cd113c2cb6a5cfcaf7ba2af36c1aa72cc 100644 (file)
@@ -1,5 +1,4 @@
-/* topblock, Copyright (c) 2006-2009
- *  rednuht <topblock.xscreensaver@jumpstation.co.uk>
+/* topblock, Copyright (c) 2006-2012 rednuht <topblock.xscreensaver@jumpstation.co.uk>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -30,6 +29,7 @@ History
 #include <math.h>
 
 # define refresh_topBlock 0
+# define release_topBlock 0
 
 #define DEFAULTS       "*delay:        10000       \n" \
                        "*count:        30           \n" \
@@ -42,14 +42,13 @@ History
 #include "xlockmore.h"
 #include "topblock.h"
 #include "sphere.h"
+#include "tube.h"
 #include "gltrackball.h"
 #include <ctype.h>
 
 #ifdef USE_GL /* whole file */
 
-#ifdef HAVE_COCOA
-# include <OpenGL/glu.h>
-#else
+#ifndef HAVE_JWXYZ
 # include <GL/glu.h>
 #endif
 
@@ -57,6 +56,10 @@ typedef struct
 {
   GLXContext *glx_context;
   trackball_state *trackball;
+  GLfloat rotateSpeed;
+  GLfloat dropSpeed;
+  int maxFalling;
+  int resolution;
   Bool button_down_p;
   int numFallingBlocks;
   GLfloat highest,highestFalling;
@@ -74,22 +77,22 @@ typedef struct
 } topBlockSTATE;
 
 /* parameter vars */
-Bool override;
-Bool rotate;
-Bool follow;
-Bool drawCarpet;
-Bool drawBlob;
-Bool drawNipples;
-GLfloat rotateSpeed;
-GLfloat camX;
-GLfloat camY;
-GLfloat camZ;
-GLfloat dropSpeed;
-int maxFalling;
-int maxColors;
-int size;
-int spawn;
-int resolution;
+static Bool override;
+static Bool rotate;
+static Bool follow;
+static Bool drawCarpet;
+static Bool drawBlob;
+static Bool drawNipples;
+static GLfloat rotateSpeed;
+static GLfloat camX;
+static GLfloat camY;
+static GLfloat camZ;
+static GLfloat dropSpeed;
+static int maxFalling;
+static int maxColors;
+static int size;
+static int spawn;
+static int resolution;
 
 static XrmOptionDescRec opts[] = {
   { "-size",        ".size",        XrmoptionSepArg, 0 },
@@ -149,7 +152,7 @@ static argtype vars[] = {
 
 static topBlockSTATE *tbs = NULL;
 
-ModeSpecOpt topBlock_opts = {countof(opts), opts, countof(vars), vars, NULL};
+static ModeSpecOpt topBlock_opts = {countof(opts), opts, countof(vars), vars, NULL};
 
 /* Window management, etc */
 ENTRYPOINT void
@@ -166,8 +169,8 @@ reshape_topBlock (ModeInfo *mi, int width, int height)
 }
 
 /* clean up on exit, not required ... */
-ENTRYPOINT void
-release_topBlock(ModeInfo *mi)
+static void
+free_topBlock(ModeInfo *mi)
 {
   topBlockSTATE *tb = &tbs[MI_SCREEN(mi)];
        NODE *llCurrent, *llOld;
@@ -186,11 +189,7 @@ init_topBlock (ModeInfo *mi)
   topBlockSTATE *tb;
   int wire = MI_IS_WIREFRAME(mi);
 
-  if (!tbs) {
-    tbs = (topBlockSTATE *)
-      calloc (MI_NUM_SCREENS(mi), sizeof (topBlockSTATE));
-    if (!tbs) abort();
-  }
+  MI_INIT (mi, tbs, free_topBlock);
 
   tb = &tbs[MI_SCREEN(mi)];
 
@@ -198,7 +197,7 @@ init_topBlock (ModeInfo *mi)
 
   reshape_topBlock (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
 
-       if (wire) { drawNipples=False; }
+/*     if (wire) { drawNipples=False; }*/
   tb->numFallingBlocks=0;
 
        if (size>10) { size = 10; }
@@ -206,34 +205,33 @@ init_topBlock (ModeInfo *mi)
        tb->carpetWidth = 8 * size;
        tb->carpetLength = tb->carpetWidth;
   
-  maxFalling*=size;
+  tb->maxFalling = maxFalling;
+  tb->maxFalling*=size;
 
        if (spawn<4) { spawn=4; }
        if (spawn>1000) { spawn=1000; }
 
-       if (rotateSpeed<1) {rotateSpeed=1; }
-       if (rotateSpeed>1000) {rotateSpeed=1000;}
-  rotateSpeed /= 100;
+       tb->rotateSpeed = rotateSpeed;
+       if (tb->rotateSpeed<1) {tb->rotateSpeed=1; }
+       if (tb->rotateSpeed>1000) {tb->rotateSpeed=1000;}
+  tb->rotateSpeed /= 100;
 
-       if (resolution<4) {resolution=4;}
-       if (resolution>20) {resolution=20;}
-  resolution*=2;
+       tb->resolution = resolution;
+       if (tb->resolution<4) {tb->resolution=4;}
+       if (tb->resolution>20) {tb->resolution=20;}
+  tb->resolution*=2;
 
        if (maxColors<1) {maxColors=1;}
        if (maxColors>8) {maxColors=8;}
 
-       if (dropSpeed<1) {dropSpeed=1;}
-       if (dropSpeed>9) {dropSpeed=9;} /* 10+ produces blocks that can pass through each other */
+       tb->dropSpeed = dropSpeed;
+       if (tb->dropSpeed<1) {tb->dropSpeed=1;}
+       if (tb->dropSpeed>9) {tb->dropSpeed=9;} /* 10+ produces blocks that can pass through each other */
   
-       dropSpeed = 80/dropSpeed;
-       dropSpeed = (blockHeight/dropSpeed); 
+       tb->dropSpeed = 80/tb->dropSpeed;
+       tb->dropSpeed = (blockHeight/tb->dropSpeed);
 
   reshape_topBlock (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
-  if (0==1) {
-      glClearColor(1.0f, 1.0f, 1.0f, 0.5f);
-  } else {
-      glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
-  }
   glClearDepth(1.0f);
   if (!wire) {
     GLfloat pos[4] = {10.0, 10.0, 1.0, 0.0};
@@ -286,7 +284,7 @@ init_topBlock (ModeInfo *mi)
     tb->eyeY=20;
     tb->eyeZ=0;
   }
-  tb->trackball = gltrackball_init ();
+  tb->trackball = gltrackball_init (False);
 }
 
 /* provides the per frame entertainment */
@@ -310,11 +308,13 @@ draw_topBlock (ModeInfo *mi)
 
        generateNewBlock(mi);
 
-       if (rotate && (!tb->button_down_p)) { tb->rotation += rotateSpeed; } 
+       if (rotate && (!tb->button_down_p)) { tb->rotation += tb->rotateSpeed; }
        if (tb->rotation>=360) { tb->rotation=tb->rotation-360; } 
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);             /* clear current */
        glLoadIdentity();       /* resets directions, do it every time ! */
+        glRotatef(current_device_rotation(), 0, 0, 1);
+
        if (!follow) {
                if (tb->highest>tb->eyeLine) { tb->eyeLine+=((tb->highest-tb->eyeLine)/100);    } /* creates a smooth camera transition */
                gluLookAt(camX, camY+tb->eyeLine, camZ, tb->eyeX, tb->eyeY+tb->eyeLine, tb->eyeZ, 0.0, 1.0, 0.0);               /* setup viewer, xyz cam, xyz looking at and where is up normaly 0,1,0 */
@@ -323,13 +323,24 @@ draw_topBlock (ModeInfo *mi)
                glRotatef(90, 0.0, 0.0, 1.0);     /* z axis */
                followBlock(mi);
        }
-       glRotatef(-90, 1.0, 0.0, 0.0);          
-  gltrackball_rotate (tb->trackball);
-       glRotatef(90, 1.0, 0.0, 0.0);           
+
+        /* Rotate the scene around a point that's a little higher up. */
+        glTranslatef (0, 0, -5);
+        gltrackball_rotate (tb->trackball);
+        glTranslatef (0, 0, 5);
 
        /* rotate the world */
        glRotatef(tb->rotation, 0.0, 0.0, 1.0);         
 
+# 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, 1/h, 1/h);
+        }
+# endif
+
        llCurrent = tb->blockNodeRoot;
        if (drawCarpet) {
                /* center carpet */
@@ -403,7 +414,7 @@ draw_topBlock (ModeInfo *mi)
            spcN2y = 0;
                        if (llCurrent->height>tb->highestFalling) {tb->highestFalling=llCurrent->height;}
                        /* all blocks fall at the same rate to avoid mid air collisions */
-                       llCurrent->height-=dropSpeed;
+                       llCurrent->height-=tb->dropSpeed;
                        if (llCurrent->height<=0) {
                                llCurrent->falling=0;
                                if (tb->highest==0) { 
@@ -492,7 +503,7 @@ draw_topBlock (ModeInfo *mi)
   if (mi->fps_p) do_fps (mi);
   glFinish();
 
-       if (tb->highest>(5*maxFalling)) { drawCarpet=False; }
+       if (tb->highest>(5*tb->maxFalling)) { drawCarpet=False; }
   glXSwapBuffers(dpy, window);
 }
 
@@ -578,7 +589,7 @@ static void generateNewBlock(ModeInfo *mi)
                   llTail = llCurrent;
                   tb->blockNodeRoot = llCurrent; 
                } else {
-                       if (tb->numFallingBlocks>=maxFalling) {
+                       if (tb->numFallingBlocks>=tb->maxFalling) {
                                /* recycle */
                                llCurrent=llTail->next;
                                tb->blockNodeRoot=llCurrent->next;
@@ -616,7 +627,7 @@ static void generateNewBlock(ModeInfo *mi)
                llCurrent->y=(startOffy-(tb->carpetLength/2)) + getLocation(random() % ((tb->carpetLength/2)+endOffy) );
                llCurrent->color=(random() % maxColors);
                llCurrent->height=getHeight(tb->plusheight+tb->highest); 
-               if (tb->numFallingBlocks>=maxFalling) {
+               if (tb->numFallingBlocks>=tb->maxFalling) {
                        tb->numFallingBlocks--;
                        tb->numFallingBlocks--;
                } 
@@ -634,7 +645,6 @@ static void buildCarpet(ModeInfo *mi)
        GLfloat color[4];
        int wire = MI_IS_WIREFRAME(mi);
        topBlockSTATE *tb = &tbs[MI_SCREEN(mi)];
-       GLUquadricObj *quadratic;
                color[0] = 0.0f;        
                color[1] = 1.0f;        
                color[2] = 0.0f;        
@@ -656,8 +666,7 @@ static void buildCarpet(ModeInfo *mi)
                glVertex3f(x,y,0.0);
                glVertex3f(0.0,y,0.0);
                 tb->carpet_polys++;
-           if (wire) { glEnd(); } 
-               else {
+           if (!wire) {
                /* add edge pieces */
                /* side 1 */
                glNormal3f( 0, -1, 0 );
@@ -691,20 +700,18 @@ static void buildCarpet(ModeInfo *mi)
        glEnd();
        /* nipples */
        if (drawNipples) {
-               quadratic=gluNewQuadric();                      /* Create A Pointer To The Quadric Object */
-               gluQuadricNormals(quadratic, GLU_SMOOTH);       /* Create Smooth Normals  */
-               gluQuadricTexture(quadratic, GL_TRUE);          /* Create Texture Coords  */
                glTranslatef(0.5f,0.5f,-.25);                   /* move to the cylinder center */
                for (c=0;c<x;c++) {
                        glPushMatrix(); /* save state */
                        for (i=0;i<y;i++) {
-                               gluCylinder(quadratic, cylSize, cylSize, 0.25f, resolution, resolution);        /* quad, radius(bottom, radius(top), height, subdivisions (around Z), subdevisions (along Z) */
-                                tb->carpet_polys += resolution*resolution;
-                               glRotatef(180, 0.0f, 1.0f, 0.0f); /* they are upside down */
-                               gluDisk(quadratic, 0.0f, cylSize, resolution, resolution );      /* inner size (cd hole), outer size (radius), subdivisions radial, subdivisions circular  */
-                                tb->carpet_polys += resolution*resolution;
-                               glRotatef(180, 0.0f, 1.0f, 0.0f); /* recover */
-                               glTranslatef(0.0f,1.0f,0.0f);                   /* move to the next cylinder center (backward) */
+                          tb->carpet_polys += tube(0, 0, -0.1,
+                                                   0, 0, 0.26,
+                                                   cylSize, 0,
+                                                   tb->resolution, True, True,
+                                                   wire);
+                          glRotatef(180, 0.0f, 1.0f, 0.0f); /* they are upside down */
+                          glRotatef(180, 0.0f, 1.0f, 0.0f); /* recover */
+                          glTranslatef(0.0f,1.0f,0.0f);                        /* move to the next cylinder center (backward) */
                        }
                        glPopMatrix();  /* save state */
                        glTranslatef(1.0f,0.0f,0.0f);                   /* reset   */
@@ -735,7 +742,6 @@ static void buildBlock(ModeInfo *mi)
        int i,c;
   int wire = MI_IS_WIREFRAME(mi);
   topBlockSTATE *tb = &tbs[MI_SCREEN(mi)];
-       GLUquadricObj *quadratic;
        tb->block=glGenLists(1);        /* only one */
        glNewList(tb->block,GL_COMPILE);
         tb->block_polys=0;
@@ -750,17 +756,16 @@ static void buildBlock(ModeInfo *mi)
        if (drawNipples) {
                /* nipples */
                /* draw 8 cylinders each with a disk cap */
-               quadratic=gluNewQuadric();                      /* Create A Pointer To The Quadric Object  */
-               gluQuadricNormals(quadratic, GLU_SMOOTH);       /* Create Smooth Normals  */
                glRotatef(90, 0.0f, 1.0f, 0.0f);                /* 'aim' the pointer ready for the cylinder */
                glTranslatef(0.5f,0.5f,0.99f);                  /* move to the cylinder center */
                for (c=0;c<2;c++) {
                        for (i=0;i<4;i++) {
-                               gluCylinder(quadratic, cylSize, cylSize, 0.25f, resolution, resolution);        /* quad, radius(bottom, radius(top), height, subdivisions (around Z), subdevisions (along Z) */
-                                tb->block_polys += resolution*resolution;
+                          tb->block_polys += tube(0, 0, 0,
+                                                  0, 0, 0.25,
+                                                  cylSize, 0,
+                                                  tb->resolution, True, True,
+                                                  wire);
                                glTranslatef(0.0f,0.0f,0.25f);                  /* move to the cylinder cap  */
-                               gluDisk(quadratic, 0.0f, cylSize, resolution, resolution );      /* inner size (cd hole), outer size (radius), subdivisions radial, subdivisions circular  */
-                                tb->block_polys += resolution*resolution;
                                glTranslatef(0.0f,0.0f,-0.25f);                 /* move back from the cylinder cap  */
                                if (c==0) {     
                                        glTranslatef(0.0f,-1.0f,0.0f);                  /* move to the next cylinder center (forward) */
@@ -773,10 +778,13 @@ static void buildBlock(ModeInfo *mi)
                /* udders */
                /* 3 cylinders on the underside */
                glTranslatef(1.5f,-2.5f,-1.5f);         /* move to the center, under the top of the brick        */
+                if (! wire)
                for (c=0;c<3;c++) {
-                       gluCylinder(quadratic, uddSize, uddSize, 1.5f, resolution, resolution); /* quad, radius(bottom, radius(top), height, subdivisions (around Z), subdevisions (along Z) */
-                        tb->block_polys += resolution*resolution;
-                       glTranslatef(0.0f,-1.0f,0.0f);          /* move to the center */        
+                  tb->block_polys += tube(0, 0, 0.1,
+                                          0, 0, 1.4,
+                                          uddSize, 0,
+                                          tb->resolution, True, True, wire);
+                  glTranslatef(0.0f,-1.0f,0.0f);               /* move to the center */        
                }
        }
        glPopMatrix();  /* restore state */
@@ -795,11 +803,11 @@ static void buildBlobBlock(ModeInfo *mi)
        glNewList(tb->block,GL_COMPILE);
        glPushMatrix();
   glScalef(1.4,1.4,1.4);
-  unit_sphere (resolution/2,resolution, wire);
+  unit_sphere (tb->resolution/2,tb->resolution, wire);
   glPopMatrix();
   glTranslatef(0.0f,-2.0f,0.0f);
   glScalef(1.4,1.4,1.4);
-  unit_sphere (resolution/2,resolution, wire);
+  unit_sphere (tb->resolution/2,tb->resolution, wire);
        glEndList();    
 }
 
@@ -809,7 +817,12 @@ ENTRYPOINT Bool
 topBlock_handle_event (ModeInfo *mi, XEvent *event)
 {
   topBlockSTATE *tb = &tbs[MI_SCREEN(mi)];
-       if (event->xany.type == KeyPress)    {
+
+  if (gltrackball_event_handler (event, tb->trackball,
+                                 MI_WIDTH (mi), MI_HEIGHT (mi),
+                                 &tb->button_down_p))
+    return True;
+  else if (event->xany.type == KeyPress) {
     KeySym keysym;
     char c = 0;
     XLookupString (&event->xkey, &c, 1, &keysym, 0);
@@ -854,40 +867,8 @@ topBlock_handle_event (ModeInfo *mi, XEvent *event)
                        return True;
                }
        }
-  if (event->xany.type == ButtonPress &&
-      event->xbutton.button == Button1)
-    {
-      tb->button_down_p = True;
-      gltrackball_start (tb->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)
-    {
-      tb->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 (tb->trackball, event->xbutton.button, 10,
-                              !!event->xbutton.state);
-      return True;
-    }
-  else if (event->xany.type == MotionNotify &&
-           tb->button_down_p)
-    {
-      gltrackball_track (tb->trackball,
-                         event->xmotion.x, event->xmotion.y,
-                         MI_WIDTH (mi), MI_HEIGHT (mi));
-      return True;
-    }
-       return False;
+
+  return False;
 }
 
 /* this is tha main change for v5 compatability and acompanying ENTRYPOINTS */