From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[xscreensaver] / hacks / glx / cubetwist.c
index 8205561e723587596f853ce7e7558a65a2fe6c9c..61af2879219bc564d2379a29d0bd54d4ac83d3c2 100644 (file)
@@ -1,4 +1,4 @@
-/* cubetwist, Copyright (c) 2016 Jamie Zawinski <jwz@jwz.org>
+/* cubetwist, Copyright (c) 2016-2017 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
@@ -14,7 +14,7 @@
                        "*wireframe:    False       \n" \
                        "*suppressRotationAnimation: True\n" \
 
-# define refresh_cube 0
+# define release_cube 0
 #undef countof
 #define countof(x) (sizeof((x))/sizeof((*x)))
 
@@ -130,7 +130,6 @@ draw_cubes (ModeInfo *mi, cube *c)
   int polys = 0;
   int i, j;
 
-  glPushMatrix();
   glColor4fv (c->color);
   glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c->color);
 
@@ -153,6 +152,8 @@ draw_cubes (ModeInfo *mi, cube *c)
 
   if (c->next)
     {
+      /* This leaves rotations on the prevailing matrix stack, but since
+         this is a tail-call, that's fine.  Don't blow the matrix stack. */
       glRotatef (c->rot.x, 1, 0, 0);
       glRotatef (c->rot.y, 0, 1, 0);
       glRotatef (c->rot.z, 0, 0, 1);
@@ -161,8 +162,8 @@ draw_cubes (ModeInfo *mi, cube *c)
       c->next->rot = c->rot;
       polys += draw_cubes (mi, c->next);
     }
-  glPopMatrix();
 
+  check_gl_error("cubetwist");
   return polys;
 }
 
@@ -339,8 +340,15 @@ ENTRYPOINT void
 reshape_cube (ModeInfo *mi, int width, int height)
 {
   GLfloat h = (GLfloat) height / (GLfloat) width;
+  int y = 0;
+
+  if (width > height * 5) {   /* tiny window: show middle */
+    height = width;
+    y = -height/2;
+    h = height / (GLfloat) width;
+  }
 
-  glViewport (0, 0, (GLint) width, (GLint) height);
+  glViewport (0, y, (GLint) width, (GLint) height);
 
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
@@ -421,14 +429,7 @@ init_cube (ModeInfo *mi)
   cube_configuration *bp;
   int wire = MI_IS_WIREFRAME(mi);
 
-  if (!bps) {
-    bps = (cube_configuration *)
-      calloc (MI_NUM_SCREENS(mi), sizeof (cube_configuration));
-    if (!bps) {
-      fprintf(stderr, "%s: out of memory\n", progname);
-      exit(1);
-    }
-  }
+  MI_INIT (mi, bps);
 
   bp = &bps[MI_SCREEN(mi)];
 
@@ -570,7 +571,7 @@ draw_cube (ModeInfo *mi)
 }
 
 ENTRYPOINT void
-release_cube (ModeInfo *mi)
+free_cube (ModeInfo *mi)
 {
   cube_configuration *bp = &bps[MI_SCREEN(mi)];
   while (bp->cubes)