From http://www.jwz.org/xscreensaver/xscreensaver-5.15.tar.gz
[xscreensaver] / hacks / glx / photopile.c
1 /* photopile, Copyright (c) 2008-2011 Jens Kilian <jjk@acm.org>
2  * Based on carousel, Copyright (c) 2005-2008 Jamie Zawinski <jwz@jwz.org>
3  * Loads a sequence of images and shuffles them into a pile.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation.  No representations are made about the suitability of this
10  * software for any purpose.  It is provided "as is" without express or
11  * implied warranty.
12  */
13
14 #define DEF_FONT "-*-helvetica-bold-r-normal-*-240-*"
15 #define DEFAULTS  "*count:           7         \n" \
16                   "*delay:           10000     \n" \
17                   "*wireframe:       False     \n" \
18                   "*showFPS:         False     \n" \
19                   "*fpsSolid:        True      \n" \
20                   "*useSHM:          True      \n" \
21                   "*font:          " DEF_FONT "\n" \
22                   "*desktopGrabber:  xscreensaver-getimage -no-desktop %s\n" \
23                   "*grabDesktopImages:   False \n" \
24                   "*chooseRandomImages:  True  \n"
25
26 # define refresh_photopile 0
27 # define release_photopile 0
28 # define photopile_handle_event 0
29
30 #undef countof
31 #define countof(x) (sizeof((x))/sizeof((*x)))
32
33 #ifndef HAVE_COCOA
34 # include <X11/Intrinsic.h>     /* for XrmDatabase in -debug mode */
35 #endif
36 #include <math.h>
37
38 #include "xlockmore.h"
39 #include "grab-ximage.h"
40 #include "texfont.h"
41 #include "dropshadow.h"
42
43 #ifdef USE_GL
44
45 # define DEF_SCALE          "0.4"
46 # define DEF_MAX_TILT       "50"
47 # define DEF_SPEED          "1.0"
48 # define DEF_DURATION       "5"
49 # define DEF_MIPMAP         "True"
50 # define DEF_TITLES         "False"
51 # define DEF_POLAROID       "True"
52 # define DEF_CLIP           "True"
53 # define DEF_SHADOWS        "True"
54 # define DEF_DEBUG          "False"
55
56 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
57
58 typedef struct {
59   GLfloat x, y;                 /* position on screen */
60   GLfloat angle;                /* rotation angle */
61
62 } position;
63
64 typedef struct {
65   Bool loaded_p;                /* true if image can be drawn */
66
67   char *title;                  /* the filename of this image */
68   int w, h;                     /* size in pixels of the image */
69   int tw, th;                   /* size in pixels of the texture */
70   XRectangle geom;              /* where in the image the bits are */
71
72   position pos[4];              /* control points for calculating position */
73
74   GLuint texid;                 /* GL texture ID */
75
76 } image;
77
78
79 typedef enum { EARLY, SHUFFLE, NORMAL, LOADING } fade_mode;
80 static int fade_ticks = 60;
81
82 typedef struct {
83   ModeInfo *mi;
84   GLXContext *glx_context;
85
86   image *frames;                /* pointer to array of images */
87   int nframe;                   /* image being (resp. next to be) loaded */
88
89   GLuint shadow;
90   texture_font_data *texfont;
91   int loading_sw, loading_sh;
92
93   time_t last_time, now;
94   int draw_tick;
95   fade_mode mode;
96   int mode_tick;
97
98 } photopile_state;
99
100 static photopile_state *sss = NULL;
101
102
103 /* Command-line arguments
104  */
105 static GLfloat scale;        /* Scale factor for loading images. */
106 static GLfloat max_tilt;     /* Maximum angle from vertical. */
107 static GLfloat speed;        /* Animation speed scale factor. */
108 static int duration;         /* Reload images after this long. */
109 static Bool mipmap_p;        /* Use mipmaps instead of single textures. */
110 static Bool titles_p;        /* Display image titles. */
111 static Bool polaroid_p;      /* Use instant-film look for images. */
112 static Bool clip_p;          /* Clip images instead of scaling for -polaroid. */
113 static Bool shadows_p;       /* Draw drop shadows. */
114 static Bool debug_p;         /* Be loud and do weird things. */
115
116
117 static XrmOptionDescRec opts[] = {
118   {"-scale",        ".scale",         XrmoptionSepArg, 0 },
119   {"-maxTilt",      ".maxTilt",       XrmoptionSepArg, 0 },
120   {"-speed",        ".speed",         XrmoptionSepArg, 0 },
121   {"-duration",     ".duration",      XrmoptionSepArg, 0 },
122   {"-mipmaps",      ".mipmap",        XrmoptionNoArg, "True"  },
123   {"-no-mipmaps",   ".mipmap",        XrmoptionNoArg, "False" },
124   {"-titles",       ".titles",        XrmoptionNoArg, "True"  },
125   {"-no-titles",    ".titles",        XrmoptionNoArg, "False" },
126   {"-polaroid",     ".polaroid",      XrmoptionNoArg, "True"  },
127   {"-no-polaroid",  ".polaroid",      XrmoptionNoArg, "False" },
128   {"-clip",         ".clip",          XrmoptionNoArg, "True"  },
129   {"-no-clip",      ".clip",          XrmoptionNoArg, "False" },
130   {"-shadows",      ".shadows",       XrmoptionNoArg, "True"  },
131   {"-no-shadows",   ".shadows",       XrmoptionNoArg, "False" },
132   {"-debug",        ".debug",         XrmoptionNoArg, "True"  },
133   {"-font",         ".font",          XrmoptionSepArg, 0 },
134 };
135
136 static argtype vars[] = {
137   { &scale,         "scale",        "Scale",        DEF_SCALE,       t_Float},
138   { &max_tilt,      "maxTilt",      "MaxTilt",      DEF_MAX_TILT,    t_Float},
139   { &speed,         "speed",        "Speed",        DEF_SPEED,       t_Float},
140   { &duration,      "duration",     "Duration",     DEF_DURATION,    t_Int},
141   { &mipmap_p,      "mipmap",       "Mipmap",       DEF_MIPMAP,      t_Bool},
142   { &titles_p,      "titles",       "Titles",       DEF_TITLES,      t_Bool},
143   { &polaroid_p,    "polaroid",     "Polaroid",     DEF_POLAROID,    t_Bool},
144   { &clip_p,        "clip",         "Clip",         DEF_CLIP,        t_Bool},
145   { &shadows_p,     "shadows",      "Shadows",      DEF_SHADOWS,     t_Bool},
146   { &debug_p,       "debug",        "Debug",        DEF_DEBUG,       t_Bool},
147 };
148
149 ENTRYPOINT ModeSpecOpt photopile_opts = {countof(opts), opts, countof(vars), vars, NULL};
150
151
152 /* Functions to interpolate between image positions.
153  */
154 static position
155 add_pos(position p, position q)
156 {
157   p.x += q.x;
158   p.y += q.y;
159   p.angle += q.angle;
160   return p;
161 }
162
163 static position
164 scale_pos(GLfloat t, position p)
165 {
166   p.x *= t;
167   p.y *= t;
168   p.angle *= t;
169   return p;
170 }
171
172 static position
173 linear_combination(GLfloat t, position p, position q)
174 {
175   return add_pos(scale_pos(1.0 - t, p), scale_pos(t, q));
176 }
177
178 static position
179 interpolate(GLfloat t, position p[4])
180 {
181   /* de Casteljau's algorithm, 4 control points */
182   position p10 = linear_combination(t, p[0], p[1]);
183   position p11 = linear_combination(t, p[1], p[2]);
184   position p12 = linear_combination(t, p[2], p[3]);
185
186   position p20 = linear_combination(t, p10, p11);
187   position p21 = linear_combination(t, p11, p12);
188
189   return linear_combination(t, p20, p21);
190 }
191
192 static position
193 offset_pos(position p, GLfloat th, GLfloat r)
194 {
195   p.x += cos(th) * r;
196   p.y += sin(th) * r;
197   p.angle = (frand(2.0) - 1.0) * max_tilt;
198   return p;
199 }
200
201 /* Calculate new positions for all images.
202  */
203 static void
204 set_new_positions(photopile_state *ss)
205 {
206   ModeInfo *mi = ss->mi;
207   int i;
208
209   for (i = 0; i < MI_COUNT(mi)+1; ++i)
210     {
211       image *frame = ss->frames + i;
212       GLfloat d = sqrt(frame->w*frame->w + frame->h*frame->h);
213       GLfloat leave = frand(M_PI * 2.0);
214       GLfloat enter = frand(M_PI * 2.0);
215
216       /* start position */
217       frame->pos[0] = frame->pos[3];
218
219       /* end position */
220       frame->pos[3].x = BELLRAND(MI_WIDTH(mi));
221       frame->pos[3].y = BELLRAND(MI_HEIGHT(mi));
222       frame->pos[3].angle = (frand(2.0) - 1.0) * max_tilt;
223
224       /* intermediate points */
225       frame->pos[1] = offset_pos(frame->pos[0], leave, d * (0.5 + frand(1.0)));
226       frame->pos[2] = offset_pos(frame->pos[3], enter, d * (0.5 + frand(1.0)));
227     }
228 }
229
230 /* Callback that tells us that the texture has been loaded.
231  */
232 static void
233 image_loaded_cb (const char *filename, XRectangle *geom,
234                  int image_width, int image_height,
235                  int texture_width, int texture_height,
236                  void *closure)
237 {
238   photopile_state *ss = (photopile_state *) closure;
239   ModeInfo *mi = ss->mi;
240   int wire = MI_IS_WIREFRAME(mi);
241   image *frame = ss->frames + ss->nframe;
242
243   if (wire)
244     {
245       if (random() % 2)
246         {
247           frame->w = (int)(MI_WIDTH(mi)  * scale) - 1;
248           frame->h = (int)(MI_HEIGHT(mi) * scale) - 1;
249         }
250       else
251         {
252           frame->w = (int)(MI_HEIGHT(mi) * scale) - 1;
253           frame->h = (int)(MI_WIDTH(mi)  * scale) - 1;
254         }
255       if (frame->w <= 10) frame->w = 10;
256       if (frame->h <= 10) frame->h = 10;
257       frame->geom.width  = frame->w;
258       frame->geom.height = frame->h;
259       goto DONE;
260     }
261
262   if (image_width == 0 || image_height == 0)
263     exit (1);
264
265   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
266   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
267                    mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
268
269   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
270   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
271
272   frame->w  = image_width;
273   frame->h  = image_height;
274   frame->tw = texture_width;
275   frame->th = texture_height;
276   frame->geom = *geom;
277
278   if (frame->title)
279     free (frame->title);
280   frame->title = (filename ? strdup (filename) : 0);
281
282   /* xscreensaver-getimage returns paths relative to the image directory
283      now, so leave the sub-directory part in.  Unless it's an absolute path.
284   */
285   if (frame->title && frame->title[0] == '/')
286     {
287       /* strip filename to part after last /. */
288       char *s = strrchr (frame->title, '/');
289       if (s) strcpy (frame->title, s+1);
290     }
291
292   if (debug_p)
293     fprintf (stderr, "%s:   loaded %4d x %-4d  %4d x %-4d  \"%s\"\n",
294              progname,
295              frame->geom.width, 
296              frame->geom.height, 
297              frame->tw, frame->th,
298              (frame->title ? frame->title : "(null)"));
299
300  DONE:
301   frame->loaded_p = True;
302 }
303
304
305 /* Load a new file.
306  */
307 static void
308 load_image (ModeInfo *mi)
309 {
310   photopile_state *ss = &sss[MI_SCREEN(mi)];
311   int wire = MI_IS_WIREFRAME(mi);
312   image *frame = ss->frames + ss->nframe;
313
314   if (debug_p && !wire && frame->w != 0)
315     fprintf (stderr, "%s:  dropped %4d x %-4d  %4d x %-4d  \"%s\"\n",
316              progname, 
317              frame->geom.width, 
318              frame->geom.height, 
319              frame->tw, frame->th,
320              (frame->title ? frame->title : "(null)"));
321
322   frame->loaded_p = False;
323
324   if (wire)
325     image_loaded_cb (0, 0, 0, 0, 0, 0, ss);
326   else
327     {
328       int w = MI_WIDTH(mi);
329       int h = MI_HEIGHT(mi);
330       int size = (int)((w > h ? w : h) * scale);
331       if (size <= 10) size = 10;
332       load_texture_async (mi->xgwa.screen, mi->window, *ss->glx_context,
333                           size, size,
334                           mipmap_p, frame->texid, 
335                           image_loaded_cb, ss);
336     }
337 }
338
339
340 static void
341 loading_msg (ModeInfo *mi)
342 {
343   photopile_state *ss = &sss[MI_SCREEN(mi)];
344   int wire = MI_IS_WIREFRAME(mi);
345   const char text[] = "Loading...";
346
347   if (wire) return;
348
349   if (ss->loading_sw == 0)    /* only do this once */
350     ss->loading_sw = texture_string_width (ss->texfont, text, &ss->loading_sh);
351
352   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
353
354   glMatrixMode(GL_PROJECTION);
355   glPushMatrix();
356   glLoadIdentity();
357
358   glMatrixMode(GL_MODELVIEW);
359   glPushMatrix();
360   glLoadIdentity();
361   gluOrtho2D(0, MI_WIDTH(mi), 0, MI_HEIGHT(mi));
362
363   glTranslatef ((MI_WIDTH(mi)  - ss->loading_sw) / 2,
364                 (MI_HEIGHT(mi) - ss->loading_sh) / 2,
365                 0);
366   glColor3f (1, 1, 0);
367   glEnable (GL_TEXTURE_2D);
368   glDisable (GL_DEPTH_TEST);
369   print_texture_string (ss->texfont, text);
370   glEnable (GL_DEPTH_TEST);
371   glPopMatrix();
372
373   glMatrixMode(GL_PROJECTION);
374   glPopMatrix();
375
376   glMatrixMode(GL_MODELVIEW);
377
378   glFinish();
379   glXSwapBuffers (MI_DISPLAY (mi), MI_WINDOW(mi));
380 }
381
382
383 static Bool
384 loading_initial_image (ModeInfo *mi)
385 {
386   photopile_state *ss = &sss[MI_SCREEN(mi)];
387
388   if (ss->frames[ss->nframe].loaded_p)
389     {
390       /* The initial image has been fully loaded, start fading it in. */
391       int i;
392
393       for (i = 0; i < ss->nframe; ++i)
394         {
395           ss->frames[i].pos[3].x = MI_WIDTH(mi) * 0.5;
396           ss->frames[i].pos[3].y = MI_HEIGHT(mi) * 0.5;
397           ss->frames[i].pos[3].angle = 0.0;
398         }
399       set_new_positions(ss);
400
401       ss->mode = SHUFFLE;
402       ss->mode_tick = fade_ticks / speed;
403     }
404   else
405     {
406       loading_msg(mi);
407     }
408
409   return (ss->mode == EARLY);
410 }
411
412
413 ENTRYPOINT void
414 reshape_photopile (ModeInfo *mi, int width, int height)
415 {
416   glViewport (0, 0, (GLint) width, (GLint) height);
417
418   glMatrixMode(GL_PROJECTION);
419   glLoadIdentity();
420
421   glMatrixMode(GL_MODELVIEW);
422   glLoadIdentity();
423   gluOrtho2D(0, MI_WIDTH(mi), 0, MI_HEIGHT(mi));
424
425   glClear(GL_COLOR_BUFFER_BIT);
426 }
427
428
429 /* Kludge to add "-v" to invocation of "xscreensaver-getimage" in -debug mode
430  */
431 static void
432 hack_resources (Display *dpy)
433 {
434 # ifndef HAVE_COCOA
435   char *res = "desktopGrabber";
436   char *val = get_string_resource (dpy, res, "DesktopGrabber");
437   char buf1[255];
438   char buf2[255];
439   XrmValue value;
440   XrmDatabase db = XtDatabase (dpy);
441   sprintf (buf1, "%.100s.%.100s", progname, res);
442   sprintf (buf2, "%.200s -v", val);
443   value.addr = buf2;
444   value.size = strlen(buf2);
445   XrmPutResource (&db, buf1, "String", &value);
446 # endif /* !HAVE_COCOA */
447 }
448
449
450 ENTRYPOINT void
451 init_photopile (ModeInfo *mi)
452 {
453   int screen = MI_SCREEN(mi);
454   photopile_state *ss;
455   int wire = MI_IS_WIREFRAME(mi);
456
457   if (sss == NULL) {
458     if ((sss = (photopile_state *)
459          calloc (MI_NUM_SCREENS(mi), sizeof(photopile_state))) == NULL)
460       return;
461   }
462   ss = &sss[screen];
463   ss->mi = mi;
464
465   if ((ss->glx_context = init_GL(mi)) != NULL) {
466     reshape_photopile (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
467     clear_gl_error(); /* WTF? sometimes "invalid op" from glViewport! */
468   } else {
469     MI_CLEARWINDOW(mi);
470   }
471
472   glDisable (GL_LIGHTING);
473   glEnable (GL_DEPTH_TEST);
474   glDisable (GL_CULL_FACE);
475
476   if (! wire)
477     {
478       glShadeModel (GL_SMOOTH);
479       glEnable (GL_LINE_SMOOTH);
480       glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
481       glEnable (GL_BLEND);
482       glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
483     }
484
485   ss->shadow = init_drop_shadow();
486   ss->texfont = load_texture_font (MI_DISPLAY(mi), "font");
487
488   if (debug_p)
489     hack_resources (MI_DISPLAY (mi));
490
491   ss->frames = (image *)calloc (MI_COUNT(mi) + 1, sizeof(image));
492   ss->nframe = 0;
493   if (!wire)
494     {
495       int i;
496       for (i = 0; i < MI_COUNT(mi) + 1; ++i)
497         {
498           glGenTextures (1, &(ss->frames[i].texid));
499           if (ss->frames[i].texid <= 0) abort();
500         }
501     }
502
503   ss->mode = EARLY;
504   load_image(mi); /* start loading the first image */
505 }
506
507
508 static void
509 draw_image (ModeInfo *mi, int i, GLfloat t, GLfloat s, GLfloat z)
510 {
511   int wire = MI_IS_WIREFRAME(mi);
512   photopile_state *ss = &sss[MI_SCREEN(mi)];
513   image *frame = ss->frames + i;
514
515   position pos = interpolate(t, frame->pos);
516   GLfloat w = frame->geom.width * 0.5;
517   GLfloat h = frame->geom.height * 0.5;
518   GLfloat z1 = z - 0.25 / (MI_COUNT(mi) + 1);
519   GLfloat z2 = z - 0.5  / (MI_COUNT(mi) + 1); 
520   GLfloat w1 = w;
521   GLfloat h1 = h;
522   GLfloat h2 = h;
523
524   if (polaroid_p)
525     {
526       GLfloat minSize = MIN(w, h);
527       GLfloat maxSize = MAX(w, h);
528
529       /* Clip or scale image to fit in the frame.
530        */
531       if (clip_p)
532         {
533           w = h = minSize;
534         }
535       else
536         {
537           GLfloat scale = minSize / maxSize;
538           w *= scale;
539           h *= scale;
540         }
541
542       w1 = minSize * 1.16;      /* enlarge frame border */
543       h1 = minSize * 1.5;
544       h2 = w1;
545       s /= 1.5;                 /* compensate for border size */
546     }
547
548   glPushMatrix();
549
550   /* Position and scale this image.
551    */
552   glTranslatef (pos.x, pos.y, 0);
553   glRotatef (pos.angle, 0, 0, 1);
554   glScalef (s, s, 1);
555
556   /* Draw the drop shadow. */
557   if (shadows_p && !wire)
558     {
559       glColor3f (0, 0, 0);
560       draw_drop_shadow(ss->shadow, -w1, -h1, z2, 2.0 * w1, h1 + h2, 20.0);
561     }
562
563   /* Draw the retro instant-film frame.
564    */
565   if (polaroid_p)
566     {
567       if (! wire)
568         {
569           glColor3f (1, 1, 1);
570           glBegin (GL_QUADS);
571           glVertex3f (-w1, -h1, z2);
572           glVertex3f ( w1, -h1, z2);
573           glVertex3f ( w1,  h2, z2);
574           glVertex3f (-w1,  h2, z2);
575           glEnd();
576         }
577
578       glLineWidth (2.0);
579       glColor3f (0.5, 0.5, 0.5);
580       glBegin (GL_LINE_LOOP);
581       glVertex3f (-w1, -h1, z);
582       glVertex3f ( w1, -h1, z);
583       glVertex3f ( w1,  h2, z);
584       glVertex3f (-w1,  h2, z);
585       glEnd();
586     }
587
588   /* Draw the image quad.
589    */
590   if (! wire)
591     {
592       GLfloat texw = w / frame->tw;
593       GLfloat texh = h / frame->th;
594       GLfloat texx = (frame->geom.x + 0.5 * frame->geom.width)  / frame->tw;
595       GLfloat texy = (frame->geom.y + 0.5 * frame->geom.height) / frame->th;
596
597       glBindTexture (GL_TEXTURE_2D, frame->texid);
598       glEnable (GL_TEXTURE_2D);
599       glColor3f (1, 1, 1);
600       glBegin (GL_QUADS);
601       glTexCoord2f (texx - texw, texy + texh); glVertex3f (-w, -h, z1);
602       glTexCoord2f (texx + texw, texy + texh); glVertex3f ( w, -h, z1);
603       glTexCoord2f (texx + texw, texy - texh); glVertex3f ( w,  h, z1);
604       glTexCoord2f (texx - texw, texy - texh); glVertex3f (-w,  h, z1);
605       glEnd();
606       glDisable (GL_TEXTURE_2D);
607     }
608
609   /* Draw a box around it.
610    */
611   glLineWidth (2.0);
612   glColor3f (0.5, 0.5, 0.5);
613   glBegin (GL_LINE_LOOP);
614   glVertex3f (-w, -h, z);
615   glVertex3f ( w, -h, z);
616   glVertex3f ( w,  h, z);
617   glVertex3f (-w,  h, z);
618   glEnd();
619
620   /* Draw a title under the image.
621    */
622   if (titles_p)
623     {
624       int sw, sh;
625       GLfloat scale = 0.6;
626       const char *title = frame->title ? frame->title : "(untitled)";
627
628       /* #### Highly approximate, but doing real clipping is harder... */
629       int max = 35;
630       if (strlen(title) > max)
631         title += strlen(title) - max;
632
633       sw = texture_string_width (ss->texfont, title, &sh);
634
635       glTranslatef (-sw*scale*0.5, -h - sh*scale, z);
636       glScalef (scale, scale, 1);
637
638       if (wire || !polaroid_p)
639         {
640           glColor3f (1, 1, 1);
641         }
642       else
643         {
644           glColor3f (0, 0, 0);
645         }
646
647       if (!wire)
648         {
649           glEnable (GL_TEXTURE_2D);
650           print_texture_string (ss->texfont, title);
651           glDisable (GL_TEXTURE_2D);
652         }
653       else
654         {
655           glBegin (GL_LINE_LOOP);
656           glVertex3f (0,  0,  0);
657           glVertex3f (sw, 0,  0);
658           glVertex3f (sw, sh, 0);
659           glVertex3f (0,  sh, 0);
660           glEnd();
661         }
662     }
663
664   glPopMatrix();
665 }
666
667
668 ENTRYPOINT void
669 draw_photopile (ModeInfo *mi)
670 {
671   photopile_state *ss = &sss[MI_SCREEN(mi)];
672   int i;
673
674   if (!ss->glx_context)
675     return;
676
677   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(ss->glx_context));
678
679   if (ss->mode == EARLY)
680     if (loading_initial_image (mi))
681       return;
682
683   /* Only check the wall clock every 10 frames */
684   if (ss->now == 0 || ss->draw_tick++ > 10)
685     {
686       ss->now = time((time_t *) 0);
687       if (ss->last_time == 0) ss->last_time = ss->now;
688       ss->draw_tick = 0;
689     }
690
691   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
692   {
693     GLfloat t;
694
695     /* Handle state transitions. */
696     switch (ss->mode)
697       {
698       case SHUFFLE:
699         if (--ss->mode_tick <= 0)
700           {
701             ss->nframe = (ss->nframe+1) % (MI_COUNT(mi)+1);
702
703             ss->mode = NORMAL;
704             ss->last_time = time((time_t *) 0);
705           }
706         break;
707       case NORMAL:
708         if (ss->now - ss->last_time > duration)
709           {
710             ss->mode = LOADING;
711             load_image(mi);
712           }
713         break;
714       case LOADING:
715         if (ss->frames[ss->nframe].loaded_p)
716           {
717             set_new_positions(ss);
718             ss->mode = SHUFFLE;
719             ss->mode_tick = fade_ticks / speed;
720           }
721         break;
722       default:
723         abort();
724       }
725
726     t = 1.0 - ss->mode_tick / (fade_ticks / speed);
727     t = 0.5 * (1.0 - cos(M_PI * t));
728
729     /* Draw the images. */
730     for (i = 0; i < MI_COUNT(mi) + (ss->mode == SHUFFLE); ++i)
731       {
732         int j = (ss->nframe + i + 1) % (MI_COUNT(mi) + 1);
733
734         if (ss->frames[j].loaded_p)
735           {
736             GLfloat s = 1.0;
737             GLfloat z = (GLfloat)i / (MI_COUNT(mi) + 1);
738
739             switch (ss->mode)
740               {
741               case SHUFFLE:
742                 if (i == MI_COUNT(mi))
743                   {
744                     s *= t;
745                   }
746                 else if (i == 0)
747                   {
748                     s *= 1.0 - t;
749                   }
750                 break;
751               case NORMAL:
752               case LOADING:
753                 t = 1.0;
754                 break;
755               default:
756                 abort();
757               }
758
759             draw_image(mi, j, t, s, z);
760           }
761       }
762   }
763
764   if (mi->fps_p) do_fps (mi);
765   glFinish();
766   glXSwapBuffers (MI_DISPLAY (mi), MI_WINDOW(mi));
767 }
768
769 XSCREENSAVER_MODULE ("Photopile", photopile)
770
771 #endif /* USE_GL */