From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / jigglypuff.c
index 9ed889f8f8c642f8b9cd3380b54d547315bfcaab..4a77c8c562687b8992f5dc816d6c02bcd0ebe9d9 100644 (file)
@@ -39,6 +39,7 @@
 # define DEFAULTS           "*delay: 20000\n" \
                             "*showFPS: False\n" \
                             "*wireframe: False\n" \
+                           "*suppressRotationAnimation: True\n" \
 
 # define refresh_jigglypuff 0
 # define release_jigglypuff 0
@@ -66,6 +67,9 @@
 #define DEF_HOLD            "800"
 #define DEF_SPHERISM        "75"
 #define DEF_DAMPING         "500"
+#define DEF_RANDOM         "True"
+#define DEF_TETRA          "False"
+#define DEF_SPOOKY         "0"
 
 #ifndef max
 #define max(a,b) (((a)>(b))?(a):(b))
@@ -137,9 +141,9 @@ static XrmOptionDescRec opts[] = {
 };
 
 static argtype vars[] = {
-    {&random_parms, "random", "Random", "True", t_Bool},
-    {&do_tetrahedron, "tetra", "Tetra", "False", t_Bool},
-    {&spooky, "spooky", "Spooky", "0", t_Int},
+    {&random_parms, "random", "Random", DEF_RANDOM, t_Bool},
+    {&do_tetrahedron, "tetra", "Tetra", DEF_TETRA, t_Bool},
+    {&spooky, "spooky", "Spooky", DEF_SPOOKY, t_Int},
     {&color, "color", "Color", DEF_COLOR, t_String},
     {&shininess, "shininess", "Shininess", DEF_SHININESS, t_Int},
     {&complexity, "complexity", "Complexity", DEF_COMPLEXITY, t_Int},
@@ -283,10 +287,12 @@ static inline void vector_scale(vector v, coord s)
     v[2] *= s;
 }
 
+/*
 static inline coord dot(vector v1, vector v2)
 {
     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
 }
+*/
 
 static inline void cross(vector v1, vector v2, vector v)
 {
@@ -431,11 +437,10 @@ static vertex *vertex_split(hedge *h, vector v)
     hedge *h2, *hn1, *hn2;
     vertex *vtxn;
     edge *en;
-    face *f1, *f2;
+    face *f1;
 
     f1 = h->f;
     h2 = partner(h);
-    f2 = h2->f;
     
     vtxn = vertex_new(f1->s, v);
     hn1 = hedge_new(h, vtxn);
@@ -670,9 +675,10 @@ static inline void vertex_render(vertex *vtx, jigglystruct *js)
  * see what the cost is of calling glBegin/glEnd for each
  * triangle.
  */
-static inline void face_render(face *f, jigglystruct *js)
+static inline int face_render(face *f, jigglystruct *js)
 {
     hedge *h1, *h2, *hend;
+    int polys = 0;
 
     h1 = f->start;
     hend = h1->prev;
@@ -688,12 +694,15 @@ static inline void face_render(face *f, jigglystruct *js)
        vertex_render(hend->vtx, js);
        h1 = h2;
        h2 = h1->next;
+        polys++;
     }
     glEnd();
+    return polys;
 }
 
-static void jigglypuff_render(jigglystruct *js) 
+static int jigglypuff_render(jigglystruct *js) 
 {
+    int polys = 0;
     face *f = js->shape->faces;
     vertex *vtx = js->shape->vertices;
 
@@ -702,9 +711,10 @@ static void jigglypuff_render(jigglystruct *js)
        vtx = vtx->next;
     }
     while(f) {
-       face_render(f, js);
+       polys += face_render(f, js);
        f=f->next;
     }
+    return polys;
 }
 
 /* This is the jiggling code */
@@ -777,7 +787,6 @@ static void setup_opengl(ModeInfo *mi, jigglystruct *js)
     const GLfloat scolor[4]= {0.9f, 0.9f, 0.9f, 0.5f};
 
     glDrawBuffer(GL_BACK);
-    glClearColor(0, 0, 0, 0);
     glShadeModel(GL_SMOOTH);
     glEnable(GL_DEPTH_TEST);
 
@@ -832,10 +841,12 @@ static int parse_color(jigglystruct *js)
        js->color_style = COLOR_STYLE_FLOWERBOX;
        return 1;
     }
+# ifndef HAVE_JWZGLES  /* SPHERE_MAP unimplemented */
     else if(!strcmp(color, "chrome")) {
        js->color_style = COLOR_STYLE_CHROME;
        return 1;
     }
+# endif
     else if(!strcmp(color, "cycle")) {
        js->color_style = COLOR_STYLE_CYCLE;
        js->jiggly_color[0] = ((float)random()) / REAL_RAND_MAX * 0.7 + 0.3;
@@ -864,8 +875,14 @@ static int parse_color(jigglystruct *js)
 
 static void randomize_parameters(jigglystruct *js) {
     do_tetrahedron = random() & 1;
+# ifndef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
     js->do_wireframe = !(random() & 3);
+# endif
     js->color_style = random() % 5;
+# ifdef HAVE_JWZGLES  /* #### SPHERE_MAP unimplemented */
+    while (js->color_style == COLOR_STYLE_CHROME)
+      js->color_style = random() % 5;;
+# endif
     if(js->color_style == COLOR_STYLE_NORMAL
        || js->color_style == COLOR_STYLE_CYCLE) {
        js->jiggly_color[0] = ((float)random()) / REAL_RAND_MAX * 0.5 + 0.5;
@@ -913,33 +930,11 @@ ENTRYPOINT Bool jigglypuff_handle_event(ModeInfo *mi, XEvent *event)
 {
     jigglystruct *js = &jss[MI_SCREEN(mi)];
     
-    if(event->xany.type == ButtonPress &&
-       event->xbutton.button == Button1) {
-       js->button_down = 1;
-       gltrackball_start(js->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) {
-       js->button_down = 0;
-       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 (js->trackball, event->xbutton.button, 10,
-                              !!event->xbutton.state);
-      return True;
-    }
-    else if(event->xany.type == MotionNotify && js->button_down) {
-       gltrackball_track(js->trackball, event->xmotion.x, event->xmotion.y,
-                         MI_WIDTH(mi), MI_HEIGHT(mi));
-       return True;
-    }
+    if (gltrackball_event_handler (event, js->trackball,
+                                   MI_WIDTH (mi), MI_HEIGHT (mi),
+                                   &js->button_down))
+    return True;
+
     return False;
 }
 
@@ -966,6 +961,16 @@ ENTRYPOINT void draw_jigglypuff(ModeInfo *mi)
     glLoadIdentity();
     glTranslatef(0,0,-10);
 
+
+# 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
+
     glRotatef(js->angle, sin(js->axis), cos(js->axis), -sin(js->axis));
     glTranslatef(0, 0, 5);
     if(!(js->button_down)) {
@@ -976,6 +981,7 @@ ENTRYPOINT void draw_jigglypuff(ModeInfo *mi)
            js->axis -= 2*M_PI;
        }
     }
+
     gltrackball_rotate(js->trackball);
 
     if(js->color_style == COLOR_STYLE_CYCLE) {
@@ -991,7 +997,7 @@ ENTRYPOINT void draw_jigglypuff(ModeInfo *mi)
        glColor4fv(js->jiggly_color);
     }
     
-    jigglypuff_render(js);
+    mi->polygon_count = jigglypuff_render(js);
     if(MI_IS_FPS(mi))
        do_fps(mi);
     glFinish();
@@ -1004,18 +1010,14 @@ ENTRYPOINT void init_jigglypuff(ModeInfo *mi)
     jigglystruct *js;
     int subdivs;
 
-    if(!jss) {
-       jss = (jigglystruct*)
-           calloc(MI_NUM_SCREENS(mi), sizeof(jigglystruct));
-       if(!jss) {
-           fprintf(stderr, "%s: No..memory...must...abort..\n", progname);
-           exit(1);
-       }
-    }
+    MI_INIT(mi, jss, NULL);
 
     js = &jss[MI_SCREEN(mi)];
 
     js->do_wireframe = MI_IS_WIREFRAME(mi);
+# ifdef HAVE_JWZGLES
+    js->do_wireframe = 0; /* GL_LINE unimplemented */
+# endif
 
     js->shininess = shininess;
 
@@ -1053,7 +1055,7 @@ ENTRYPOINT void init_jigglypuff(ModeInfo *mi)
     else {
        MI_CLEARWINDOW(mi);
     }
-    js->trackball = gltrackball_init();
+    js->trackball = gltrackball_init(True);
 /*    _DEBUG("distance : %f\nhold : %f\nspherify : %f\ndamping : %f\ndfact : %f\n",
           js->stable_distance, js->hold_strength, js->spherify_strength,
           js->damping_velocity, js->damping_factor);
@@ -1061,7 +1063,7 @@ ENTRYPOINT void init_jigglypuff(ModeInfo *mi)
           js->do_wireframe, js->spooky, js->color_style, js->shininess);*/
 }
 
-XSCREENSAVER_MODULE ("Jigglypuff", jigglypuff)
+XSCREENSAVER_MODULE ("JigglyPuff", jigglypuff)
 
 #endif /* USE_GL */