From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / glforestfire.c
index c93c532d907724a83f68fb4b02180ea7a43c2525..e81d51167f497be514660ceef31cf34bc24556f5 100644 (file)
@@ -466,8 +466,9 @@ static void setpartrain(firestruct * fs, rain * r, float dt)
 }
 
 /* draw a tree */
-static void drawtree(float x, float y, float z)
+static int drawtree(float x, float y, float z)
 {
+    int polys = 0;
     glBegin(GL_QUADS);
     glTexCoord2f(0.0,0.0);
     glVertex3f(x-1.5,y+0.0,z);
@@ -480,6 +481,7 @@ static void drawtree(float x, float y, float z)
 
     glTexCoord2f(0.0,1.0);
     glVertex3f(x-1.5,y+3.0,z);
+    polys++;
 
 
     glTexCoord2f(0.0,0.0);
@@ -493,9 +495,11 @@ static void drawtree(float x, float y, float z)
 
     glTexCoord2f(0.0,1.0);
     glVertex3f(x,y+3.0,z-1.5);
+    polys++;
 
     glEnd();
 
+    return polys;
 }
 
 /* calculate observer position : modified only if trackmouse is used */
@@ -617,13 +621,15 @@ static Bool inittree(ModeInfo * mi)
                return False;
     }
     /* initialise positions */
-    for(i=0;i<fs->num_trees;i++)
-       do {
-           fs->treepos[i].x =vrnd()*TREEOUTR*2.0-TREEOUTR;
-           fs->treepos[i].y =0.0;
-           fs->treepos[i].z =vrnd()*TREEOUTR*2.0-TREEOUTR;
-           dist=sqrt(fs->treepos[i].x *fs->treepos[i].x +fs->treepos[i].z *fs->treepos[i].z );
-        } while((dist<TREEINR) || (dist>TREEOUTR));
+    for(i=0;i<fs->num_trees;i++) {
+      do {
+        fs->treepos[i].x =vrnd()*TREEOUTR*2.0-TREEOUTR;
+        fs->treepos[i].y =0.0;
+        fs->treepos[i].z =vrnd()*TREEOUTR*2.0-TREEOUTR;
+        dist = sqrt(fs->treepos[i].x * fs->treepos[i].x +
+                    fs->treepos[i].z * fs->treepos[i].z);
+      } while((dist<TREEINR) || (dist>TREEOUTR));
+    }
        return True;
 }
 
@@ -667,6 +673,8 @@ static void DrawFire(ModeInfo * mi)
     firestruct *fs = &fire[MI_SCREEN(mi)];
     Bool wire = MI_IS_WIREFRAME(mi);
 
+    mi->polygon_count = 0;
+
     if (do_wander && !fs->button_down_p)
     {
        GLfloat x, y, z;
@@ -728,6 +736,7 @@ static void DrawFire(ModeInfo * mi)
     glVertex3fv(q[2]);
     glTexCoord2fv(qt[3]);
     glVertex3fv(q[3]);
+    mi->polygon_count++;
     glEnd();
 
     glAlphaFunc(GL_GEQUAL, 0.9);
@@ -739,7 +748,7 @@ static void DrawFire(ModeInfo * mi)
        glBindTexture(GL_TEXTURE_2D,fs->treeid);
 #endif /* HAVE_GLBINDTEXTURE */
        for(j=0;j<fs->num_trees;j++)
-           drawtree(fs->treepos[j].x ,fs->treepos[j].y ,fs->treepos[j].z );
+      mi->polygon_count += drawtree(fs->treepos[j].x ,fs->treepos[j].y ,fs->treepos[j].z );
        glDisable(GL_ALPHA_TEST);
     }
     glDisable(GL_TEXTURE_2D);
@@ -757,6 +766,7 @@ static void DrawFire(ModeInfo * mi)
 
            glColor4f(black[0], black[1], black[2], fs->p[j].c[2][3]);
            glVertex3f(fs->p[j].p[2][0], 0.1, fs->p[j].p[2][2]);
+        mi->polygon_count++;
        }
        glEnd();
     }
@@ -772,6 +782,7 @@ static void DrawFire(ModeInfo * mi)
 
        glColor4fv(fs->p[j].c[2]);
        glVertex3fv(fs->p[j].p[2]);
+    mi->polygon_count++;
 
        setpart(fs, &fs->p[j]);
     }
@@ -790,6 +801,7 @@ static void DrawFire(ModeInfo * mi)
            glColor4f(0.3f,0.7f,1.0f,1.0f);
            glVertex3fv(fs->r[j].pos);
            setpartrain(fs, &fs->r[j],timeused);
+        mi->polygon_count++;
        }
        glEnd();
        glShadeModel(GL_FLAT);
@@ -843,23 +855,6 @@ static Bool Init(ModeInfo * mi)
                       fs->eject_r, fs->ridtri);
     }
 
-    glShadeModel(GL_FLAT);
-    glEnable(GL_DEPTH_TEST);
-
-    /* makes particles blend with background */
-    if (!MI_IS_WIREFRAME(mi)||(!fs->np))
-    {
-       glEnable(GL_BLEND);
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-    }
-
-    /* fog stuff */
-    glEnable(GL_FOG);
-    glFogi(GL_FOG_MODE, GL_EXP);
-    glFogfv(GL_FOG_COLOR, fogcolor);
-    glFogf(GL_FOG_DENSITY, 0.03);
-    glHint(GL_FOG_HINT, GL_NICEST);
-
     /* initialise particles and trees */
     for (i = 0; i < fs->np; i++) {
        setnewpart(fs, &(fs->p[i]));
@@ -936,14 +931,7 @@ init_fire(ModeInfo * mi)
 {
     firestruct *fs;
 
-    /* allocate the main fire table if needed */
-    if (fire == NULL) {
-       if ((fire = (firestruct *) calloc(MI_NUM_SCREENS(mi),
-                                         sizeof(firestruct))) == NULL)
-           return;
-    }
-
-    /* initialise the per screen fire structure */
+    MI_INIT (mi, fire, 0);
     fs = &fire[MI_SCREEN(mi)];
     fs->np = MI_COUNT(mi);
     fs->fog = do_fog;
@@ -969,7 +957,7 @@ init_fire(ModeInfo * mi)
     else
        fs->num_trees = 0;
 
-    fs->trackball = gltrackball_init ();
+    fs->trackball = gltrackball_init (False);
 
     /* xlock GL stuff */
     if ((fs->glx_context = init_GL(mi)) != NULL) {
@@ -1007,7 +995,28 @@ ENTRYPOINT void draw_fire(ModeInfo * mi)
        return;
 
     glXMakeCurrent(display, window, *(fs->glx_context));
+
+    glShadeModel(GL_FLAT);
+    glEnable(GL_DEPTH_TEST);
+
+    /* makes particles blend with background */
+    if (!MI_IS_WIREFRAME(mi)||(!fs->np))
+    {
+       glEnable(GL_BLEND);
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+    }
+
+    /* fog stuff */
+    glEnable(GL_FOG);
+    glFogi(GL_FOG_MODE, GL_EXP);
+    glFogfv(GL_FOG_COLOR, fogcolor);
+    glFogf(GL_FOG_DENSITY, 0.03);
+    glHint(GL_FOG_HINT, GL_NICEST);
+
+    glPushMatrix();
+    glRotatef(current_device_rotation(), 0, 0, 1);
     DrawFire(mi);
+    glPopMatrix();
 #ifndef STANDALONE
     Reshape(mi); /* xlock mode */
 #else
@@ -1050,41 +1059,10 @@ fire_handle_event (ModeInfo *mi, XEvent *event)
 {
   firestruct *fs = &fire[MI_SCREEN(mi)];
 
-  if (event->xany.type == ButtonPress &&
-      event->xbutton.button == Button1)
-    {
-      fs->button_down_p = True;
-      event->xbutton.x = MI_WIDTH(mi)  - event->xbutton.x; /* kludge! */
-      event->xbutton.y = MI_HEIGHT(mi) - event->xbutton.y;
-      gltrackball_start (fs->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)
-    {
-      fs->button_down_p = False;
-      return True;
-    }
-  else if (event->xany.type == ButtonPress &&
-           (event->xbutton.button == Button4 ||
-            event->xbutton.button == Button5))
-    {
-      gltrackball_mousewheel (fs->trackball, event->xbutton.button, 5,
-                              !!event->xbutton.state);
-      return True;
-    }
-  else if (event->xany.type == MotionNotify &&
-           fs->button_down_p)
-    {
-      event->xmotion.x = MI_WIDTH(mi)  - event->xmotion.x; /* kludge! */
-      event->xmotion.y = MI_HEIGHT(mi) - event->xmotion.y;
-      gltrackball_track (fs->trackball,
-                         event->xmotion.x, event->xmotion.y,
-                         MI_WIDTH (mi), MI_HEIGHT (mi));
-      return True;
-    }
+  if (gltrackball_event_handler (event, fs->trackball,
+                                 MI_WIDTH (mi), MI_HEIGHT (mi),
+                                 &fs->button_down_p))
+    return True;
 
   return False;
 }