From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[xscreensaver] / hacks / glx / flipflop.c
index ace3b88b1a239a76863b5722a0df5deaeb0dda5e..c26458c2dc1cb22283514114fe8cefafb38c62a5 100644 (file)
@@ -43,7 +43,7 @@
                        "*showFPS:   False     \n" \
                        "*wireframe: False     \n"
 
-# define refresh_flipflop 0
+# define release_flipflop 0
 # include "xlockmore.h"
 
 #else
@@ -94,8 +94,8 @@ ENTRYPOINT ModeSpecOpt flipflop_opts = {countof(opts), opts, countof(vars), vars
 
 #ifdef USE_MODULES
 ModStruct   flipflop_description =
-    {"flipflop", "init_flipflop", "draw_flipflop", "release_flipflop",
-     "draw_flipflop", "init_flipflop", NULL, &flipflop_opts,
+    {"flipflop", "init_flipflop", "draw_flipflop", NULL,
+     "draw_flipflop", "init_flipflop", "free_flipflop", &flipflop_opts,
      1000, 1, 2, 1, 4, 1.0, "",
      "Flipflop", 0, NULL};
 
@@ -169,7 +169,7 @@ static void randsheet_move( randsheet *rs, float rot );
 static int randsheet_draw( randsheet *rs );
 static void setup_lights(void);
 static int drawBoard(Flipflopcreen *);
-static int display(Flipflopcreen *c);
+static int display(ModeInfo *mi);
 static int draw_sheet(float *tex);
 
 
@@ -193,44 +193,28 @@ setup_lights(void)
     glEnable(GL_LIGHT0);
 }
 
+static void get_texture(ModeInfo *);
+
+
 ENTRYPOINT Bool
 flipflop_handle_event (ModeInfo *mi, XEvent *event)
 {
     Flipflopcreen *c = &qs[MI_SCREEN(mi)];
 
-    if (event->xany.type == ButtonPress &&
-        event->xbutton.button == Button1)
-        {
-            c->button_down_p = True;
-            gltrackball_start (c->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)
-        {
-            c->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 (c->trackball, event->xbutton.button, 5,
-                                    !event->xbutton.state);
-            return True;
-        }
-    else if (event->xany.type == MotionNotify &&
-             c->button_down_p)
+  if (gltrackball_event_handler (event, c->trackball,
+                                 MI_WIDTH (mi), MI_HEIGHT (mi),
+                                 &c->button_down_p))
+    return True;
+  else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
+    {
+      if (!textured || c->got_texture)
         {
-            gltrackball_track (c->trackball,
-                               event->xmotion.x, event->xmotion.y,
-                               MI_WIDTH (mi), MI_HEIGHT (mi));
-            return True;
+          textured = 1;
+          c->got_texture = False;
+          get_texture (mi);
+          return True;
         }
+    }
 
     return False;
 }
@@ -249,8 +233,9 @@ drawBoard(Flipflopcreen *c)
 
 
 static int
-display(Flipflopcreen *c)
+display(ModeInfo *mi)
 {
+    Flipflopcreen *c = &qs[MI_SCREEN(mi)];
     GLfloat amb[] = { 0.8, 0.8, 0.8, 1.0 };
     int polys = 0;
 
@@ -258,13 +243,14 @@ display(Flipflopcreen *c)
     glClear(clearbits);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
-
     glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.2);
     glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.15/board_avg_size );
     glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.15/board_avg_size );
     glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
 
 
+    glRotatef(current_device_rotation(), 0, 0, 1);
+
     /** setup perspectif */
     glTranslatef(0.0, 0.0, -c->reldist*board_avg_size);
     glRotatef(22.5, 1.0, 0.0, 0.0);  
@@ -276,6 +262,15 @@ display(Flipflopcreen *c)
     if(textured)
       glBindTexture(GL_TEXTURE_2D, c->texid);
 
+# 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
+
     polys = drawBoard(c);
 
     if (!c->button_down_p) {
@@ -288,12 +283,20 @@ display(Flipflopcreen *c)
 ENTRYPOINT void
 reshape_flipflop(ModeInfo *mi, int width, int height)
 {
-    GLfloat h = (GLfloat) height / (GLfloat) width;
-    glViewport(0,0, width, height);
-    glMatrixMode(GL_PROJECTION);
-    glLoadIdentity();
-    gluPerspective(45, 1/h, 1.0, 300.0);
-    glMatrixMode(GL_MODELVIEW);
+  GLfloat h = (GLfloat) height / (GLfloat) width;
+  int y = 0;
+
+  if (width > height * 5) {   /* tiny window: show middle */
+    height = width * 9/16;
+    y = -height/2;
+    h = height / (GLfloat) width;
+  }
+
+  glViewport(0,y, width, height);
+  glMatrixMode(GL_PROJECTION);
+  glLoadIdentity();
+  gluPerspective(45, 1/h, 1.0, 300.0);
+  glMatrixMode(GL_MODELVIEW);
 }
 
 static void
@@ -397,13 +400,11 @@ init_flipflop(ModeInfo *mi)
     screen = MI_SCREEN(mi);
     wire = MI_IS_WIREFRAME(mi);
 
-    if(!qs &&
-       !(qs = (Flipflopcreen *) calloc(MI_NUM_SCREENS(mi), sizeof(Flipflopcreen))))
-        return;
+    MI_INIT(mi, qs);
 
     c = &qs[screen];
     c->window = MI_WINDOW(mi);
-    c->trackball = gltrackball_init ();
+    c->trackball = gltrackball_init (False);
 
     c->flipspeed = 0.03;
     c->reldist = 1;
@@ -419,8 +420,6 @@ init_flipflop(ModeInfo *mi)
     c->sheet = (randsheet*) malloc(sizeof(randsheet)); 
     randsheet_create( c->sheet ); 
 
-    glClearColor(0.0, 0.0, 0.0, 0.0);
-
     clearbits = GL_COLOR_BUFFER_BIT;
 
     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
@@ -459,7 +458,7 @@ draw_flipflop(ModeInfo *mi)
 
     glXMakeCurrent(disp, w, *(c->glx_context));
 
-    mi->polygon_count = display(c);
+    mi->polygon_count = display(mi);
 
     if(mi->fps_p){
         do_fps(mi);
@@ -472,27 +471,13 @@ draw_flipflop(ModeInfo *mi)
 }
 
 ENTRYPOINT void
-release_flipflop(ModeInfo *mi)
+free_flipflop(ModeInfo *mi)
 {
-  if(qs) {
-    int screen;
-
-    for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
-    {
-      Flipflopcreen *c = &qs[MI_SCREEN(mi)];
-      if (c->glx_context)
-        c->glx_context = 0;
-      if (c->sheet) {
-        randsheet_free(c->sheet);
-        free (c->sheet);
-        c->sheet = 0;
-      }
-    }
-    free(qs);
-    qs = 0;
+  Flipflopcreen *c = &qs[MI_SCREEN(mi)];
+  if (c->sheet) {
+    randsheet_free(c->sheet);
+    free (c->sheet);
   }
-
-  FreeAllGL(mi);
 }
 
 /*** ADDED RANDSHEET FUNCTIONS ***/
@@ -729,12 +714,10 @@ randsheet_new_move( randsheet* rs )
 static void
 randsheet_move( randsheet *rs, float rot )
 {
-    int i, j, index;
+    int index;
     float tmp;
     for( index = 0 ; index < numsquares; index++ )
         {
-            i = rs->xpos[ index ];
-            j = rs->ypos[ index ];
             switch( rs->direction[ index ] )
                 {
                 case 0: