From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / boing.c
index 2f86b42da0269c2575d759d346e74e14e590e3bd..22d512bf808ae1219c323751bfe0967f0d0d3655 100644 (file)
@@ -1,4 +1,4 @@
-/* boing, Copyright (c) 2005 Jamie Zawinski <jwz@jwz.org>
+/* boing, Copyright (c) 2005-2014 Jamie Zawinski <jwz@jwz.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
  * does not, obviously.
  */
 
-#include <X11/Intrinsic.h>
+#define DEFAULTS       "*delay:        30000            \n" \
+                       "*showFPS:      False            \n" \
+                       "*wireframe:    False            \n" \
+
+# define refresh_boing 0
+# define release_boing 0
+#undef countof
+#define countof(x) (sizeof((x))/sizeof((*x)))
+
+#include "xlockmore.h"
+#include "gltrackball.h"
+#include <ctype.h>
 
-extern XtAppContext app;
+#ifdef USE_GL /* whole file */
 
-#define PROGCLASS      "Boing"
-#define HACK_INIT      init_boing
-#define HACK_DRAW      draw_boing
-#define HACK_RESHAPE   reshape_boing
-#define HACK_HANDLE_EVENT boing_handle_event
-#define EVENT_MASK      PointerMotionMask
-#define sws_opts       xlockmore_opts
 
 #define DEF_SPIN        "True"
 #define DEF_LIGHTING    "False"
 #define DEF_SMOOTH      "False"
-#define DEF_SCANLINES   "False"
+#define DEF_SCANLINES   "True"
 #define DEF_SPEED       "1.0"
-#define DEF_SIZE        "0.5"
+#define DEF_BALL_SIZE   "0.5"
 #define DEF_ANGLE       "15"
 #define DEF_MERIDIANS   "16"
 #define DEF_PARALLELS   "8"
@@ -51,21 +55,6 @@ extern XtAppContext app;
 #define DEF_SHADOW_COLOR "#303030"
 #define DEF_BACKGROUND   "#8C8C8C"
 
-#define DEFAULTS       "*delay:        30000            \n" \
-                       "*showFPS:      False            \n" \
-                       "*wireframe:    False            \n" \
-
-#undef countof
-#define countof(x) (sizeof((x))/sizeof((*x)))
-
-#include "xlockmore.h"
-#include "gltrackball.h"
-#include <ctype.h>
-
-#ifdef USE_GL /* whole file */
-
-#include <GL/glu.h>
-
 typedef struct { GLfloat x, y, z; } XYZ;
 
 typedef struct {
@@ -73,6 +62,8 @@ typedef struct {
   trackball_state *trackball;
   Bool button_down_p;
 
+  GLfloat speed;
+
   GLuint ball_list;
   double ball_x,   ball_y,   ball_z,   ball_th;
   double ball_dx,  ball_dy,  ball_dz,  ball_dth;
@@ -120,6 +111,8 @@ static XrmOptionDescRec opts[] = {
   { "-ball-color2",".ballColor2",XrmoptionSepArg, 0 },
   { "-grid-color", ".gridColor", XrmoptionSepArg, 0 },
   { "-shadow-color",".shadowColor",XrmoptionSepArg, 0 },
+  { "-background",  ".boingBackground",XrmoptionSepArg, 0 },
+  { "-bg",          ".boingBackground",XrmoptionSepArg, 0 },
 };
 
 static argtype vars[] = {
@@ -129,7 +122,7 @@ static argtype vars[] = {
   {&scanlines_p,"scanlines","Scanlines",  DEF_SCANLINES, t_Bool},
   {&speed,     "speed",     "Speed",      DEF_SPEED,     t_Float},
   {&angle,     "angle",     "Angle",      DEF_ANGLE,     t_Int},
-  {&ball_size, "ballSize",  "BallSize",   DEF_SIZE,      t_Float},
+  {&ball_size, "ballSize",  "BallSize",   DEF_BALL_SIZE, t_Float},
   {&meridians, "meridians", "meridians",  DEF_MERIDIANS, t_Int},
   {&parallels, "parallels", "parallels",  DEF_PARALLELS, t_Int},
   {&tiles,     "tiles",     "Tiles",      DEF_TILES,     t_Int},
@@ -142,7 +135,7 @@ static argtype vars[] = {
   {&bg_str,        "boingBackground", "Background", DEF_BACKGROUND, t_String},
 };
 
-ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
+ENTRYPOINT ModeSpecOpt boing_opts = {countof(opts), opts, countof(vars), vars, NULL};
 
 static void
 parse_color (ModeInfo *mi, const char *name, const char *s, GLfloat *a)
@@ -205,14 +198,14 @@ draw_box (ModeInfo *mi)
   /* boing_configuration *bp = &bps[MI_SCREEN(mi)]; */
   glPushMatrix();
   glTranslatef (0, 0, -0.5);
-  glFrontFace (GL_CCW);
+/*  glFrontFace (GL_CCW);*/
   draw_grid (mi);
   glPopMatrix();
 
   glPushMatrix();
   glRotatef (90, 1, 0, 0);
   glTranslatef (0, 0, 0.5);
-  glFrontFace (GL_CW);
+/*  glFrontFace (GL_CW);*/
   draw_grid (mi);
   glPopMatrix();
 }
@@ -387,7 +380,7 @@ draw_scanlines (ModeInfo *mi)
       int lh, ls;
       int y;
       glLoadIdentity();
-      gluOrtho2D (0, w, 0, h);
+      glOrtho (0, w, 0, h, -1, 1);
 
       if      (h > 500) lh = 4, ls = 4;
       else if (h > 300) lh = 2, ls = 1;
@@ -438,10 +431,10 @@ tick_physics (ModeInfo *mi)
   bp->ball_x  += bp->ball_dx;
   if      (bp->ball_x < min) bp->ball_x = min, bp->ball_dx = -bp->ball_dx,
     bp->ball_dth = -bp->ball_dth,
-    bp->ball_dx += (frand(speed/2) - speed);
+    bp->ball_dx += (frand(bp->speed/2) - bp->speed);
   else if (bp->ball_x > max) bp->ball_x = max, bp->ball_dx = -bp->ball_dx,
     bp->ball_dth = -bp->ball_dth,
-    bp->ball_dx += (frand(speed/2) - speed);
+    bp->ball_dx += (frand(bp->speed/2) - bp->speed);
 
   bp->ball_dy += bp->ball_ddy;
   bp->ball_y  += bp->ball_dy;
@@ -458,7 +451,7 @@ tick_physics (ModeInfo *mi)
 
 /* Window management, etc
  */
-void
+ENTRYPOINT void
 reshape_boing (ModeInfo *mi, int width, int height)
 {
   GLfloat h = (GLfloat) height / (GLfloat) width;
@@ -470,6 +463,13 @@ reshape_boing (ModeInfo *mi, int width, int height)
 
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
+
+  if (height > width)
+    {
+      GLfloat s = width / (GLfloat) height;
+      glScalef (s, s, s);
+    }
+
   gluPerspective (8.0, 1/h, 1.0, 10.0);
 
   glMatrixMode(GL_MODELVIEW);
@@ -482,63 +482,27 @@ reshape_boing (ModeInfo *mi, int width, int height)
 }
 
 
-Bool
+ENTRYPOINT Bool
 boing_handle_event (ModeInfo *mi, XEvent *event)
 {
   boing_configuration *bp = &bps[MI_SCREEN(mi)];
 
-  if (event->xany.type == ButtonPress &&
-      event->xbutton.button == Button1)
-    {
-      bp->button_down_p = True;
-      gltrackball_start (bp->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)
-    {
-      bp->button_down_p = False;
-      return True;
-    }
-  else if (event->xany.type == ButtonPress &&
-           (event->xbutton.button == Button4 ||
-            event->xbutton.button == Button5))
-    {
-      gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
-                              !!event->xbutton.state);
-      return True;
-    }
-  else if (event->xany.type == MotionNotify &&
-           bp->button_down_p)
-    {
-      gltrackball_track (bp->trackball,
-                         event->xmotion.x, event->xmotion.y,
-                         MI_WIDTH (mi), MI_HEIGHT (mi));
-      return True;
-    }
+  if (gltrackball_event_handler (event, bp->trackball,
+                                 MI_WIDTH (mi), MI_HEIGHT (mi),
+                                 &bp->button_down_p))
+    return True;
 
   return False;
 }
 
 
-void 
+ENTRYPOINT void 
 init_boing (ModeInfo *mi)
 {
   boing_configuration *bp;
   int wire = MI_IS_WIREFRAME(mi);
 
-  if (!bps) {
-    bps = (boing_configuration *)
-      calloc (MI_NUM_SCREENS(mi), sizeof (boing_configuration));
-    if (!bps) {
-      fprintf(stderr, "%s: out of memory\n", progname);
-      exit(1);
-    }
-
-    bp = &bps[MI_SCREEN(mi)];
-  }
+  MI_INIT (mi, bps, NULL);
 
   bp = &bps[MI_SCREEN(mi)];
 
@@ -599,22 +563,22 @@ init_boing (ModeInfo *mi)
 
   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
-  speed = speed / 800.0;
+  bp->speed = speed / 800.0;
 
-  bp->ball_dth = (spin ? -speed * 7 * 360 : 0);
+  bp->ball_dth = (spin ? -bp->speed * 7 * 360 : 0);
 
   bp->ball_x   = 0.5 - ((ball_size/2) + frand(1-ball_size));
   bp->ball_y   = 0.2;
-  bp->ball_dx  = speed * 6 + frand(speed);
-  bp->ball_ddy = -speed;
+  bp->ball_dx  = bp->speed * 6 + frand(bp->speed);
+  bp->ball_ddy = -bp->speed;
 
-  bp->ball_dz  = speed * 6 + frand(speed);
+  bp->ball_dz  = bp->speed * 6 + frand(bp->speed);
 
-  bp->trackball = gltrackball_init ();
+  bp->trackball = gltrackball_init (False);
 }
 
 
-void
+ENTRYPOINT void
 draw_boing (ModeInfo *mi)
 {
   boing_configuration *bp = &bps[MI_SCREEN(mi)];
@@ -624,13 +588,13 @@ draw_boing (ModeInfo *mi)
   if (!bp->glx_context)
     return;
 
+  glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
+
   mi->polygon_count = 0;
 
   glShadeModel(GL_SMOOTH);
 
   glEnable(GL_NORMALIZE);
-  glEnable(GL_CULL_FACE);
-  glEnable (GL_DEPTH_TEST);
 
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
@@ -638,12 +602,38 @@ draw_boing (ModeInfo *mi)
     tick_physics (mi);
 
   glPushMatrix ();
+
+  {
+    double rot = current_device_rotation();
+    glRotatef(rot, 0, 0, 1);
+/*
+    if ((rot >  45 && rot <  135) ||
+        (rot < -45 && rot > -135))
+      {
+        GLfloat s = MI_WIDTH(mi) / (GLfloat) MI_HEIGHT(mi);
+        glScalef (1/s, s, 1);
+      }
+*/
+  }
+
   gltrackball_rotate (bp->trackball);
 
   glLightfv (GL_LIGHT0, GL_POSITION, bp->lightpos);
 
+  glDisable (GL_CULL_FACE);
+  glDisable (GL_DEPTH_TEST);
+
+  glEnable (GL_LINE_SMOOTH);
+  glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
+  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+  glEnable (GL_BLEND);
+
   draw_box (mi);
   draw_shadow (mi);
+
+  glEnable (GL_CULL_FACE);
+  glEnable (GL_DEPTH_TEST);
+
   draw_ball (mi);
   if (scanlines_p)
     draw_scanlines (mi);
@@ -656,4 +646,6 @@ draw_boing (ModeInfo *mi)
   glXSwapBuffers(dpy, window);
 }
 
+XSCREENSAVER_MODULE ("Boing", boing)
+
 #endif /* USE_GL */