1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
diff --git a/apps/plugins/pictureflow.c b/apps/plugins/pictureflow.c
index bd51a88..a644bed 100644
--- a/apps/plugins/pictureflow.c
+++ b/apps/plugins/pictureflow.c
@@ -251,6 +251,8 @@ static int itilt;
 static PFreal offsetX;
 static PFreal offsetY;
 static int number_of_slides;
+static long st_tick;
+static int fps = 0, ticks = 0, frames = 0;

 static struct slide_cache cache[SLIDE_CACHE_SIZE];
 static int  slide_cache_in_use;
@@ -1262,6 +1264,16 @@ static inline pix_t fade_color(pix_t c, unsigned int a)
 }
 #endif

+static inline void time_start(void)
+{
+    st_tick = *(rb->current_tick);
+}
+
+static inline void time_end(void)
+{
+    ticks += *(rb->current_tick) - st_tick;
+}
+
 /**
  * Render a single slide
  * Where xc is the slide's horizontal offset from center, xs is the horizontal
@@ -1287,12 +1299,18 @@ static inline pix_t fade_color(pix_t c, unsigned int a)
  */
 void render_slide(struct slide_data *slide, const int alpha)
 {
+    time_start();
     struct bitmap *bmp = surface(slide->slide_index);
-    if (!bmp) {
+    if (!bmp)
+    {
+        time_end();
         return;
     }
     if (slide->angle > 255 || slide->angle < -255)
+    {
+        time_end();
         return;
+    }
     pix_t *src = (pix_t *)bmp->data;

     const int sw = bmp->width;
@@ -1318,6 +1336,7 @@ void render_slide(struct slide_data *slide, const int alpha)
         >> PFREAL_SHIFT;
     xp = DISPLAY_LEFT_R + xi * PFREAL_ONE;
     if (xi >= w) {
+        time_end();
         return;
     }
     xsnum = CAM_DIST * (slide->cx - xp) - fmuln(xp, zo, PFREAL_SHIFT - 2, 0);
@@ -1408,8 +1427,8 @@ void render_slide(struct slide_data *slide, const int alpha)

     }
     /* let the music play... */
+    time_end();
     rb->yield();
-
     return;
 }

@@ -1488,6 +1507,7 @@ static inline bool is_empty_rect(struct rect *r)
 */
 void render_all_slides(void)
 {
+    frames++;
     MYLCD(set_background)(G_BRIGHT(0));
     /* TODO: Optimizes this by e.g. invalidating rects */
     MYLCD(clear_display)();
@@ -1534,6 +1554,13 @@ void render_all_slides(void)
         }
     }
     render_slide(&center_slide, 256);
+    if (frames && ticks)
+    {
+        int newfps = (256 * frames * HZ + (ticks >> 1)) / ticks;
+        fps = (fps * 3 + newfps) >> 2;
+        frames = 0;
+        ticks = 0;
+    }
 }


@@ -2124,11 +2151,6 @@ int main(void)
     char fpstxt[10];
     int button;

-    int frames = 0;
-    long last_update = *rb->current_tick;
-    long current_update;
-    long update_interval = 100;
-    int fps = 0;
     int fpstxt_y;

     bool instant_update;
@@ -2139,8 +2161,6 @@ int main(void)
 #endif
     rb->lcd_set_drawmode(DRMODE_FG);
     while (true) {
-        current_update = *rb->current_tick;
-        frames++;

         /* Initial rendering */
         instant_update = false;
@@ -2170,13 +2190,6 @@ int main(void)
                 break;
         }

-        /* Calculate FPS */
-        if (current_update - last_update > update_interval) {
-            fps = frames * HZ / (current_update - last_update);
-            last_update = current_update;
-            frames = 0;
-        }
-        /* Draw FPS */
         if (show_fps)
         {
 #ifdef USEGSLIB
@@ -2184,7 +2197,7 @@ int main(void)
 #else
             MYLCD(set_foreground)(G_PIX(255,0,0));
 #endif
-            rb->snprintf(fpstxt, sizeof(fpstxt), "FPS: %d", fps);
+            rb->snprintf(fpstxt, sizeof(fpstxt), "FPS: %d", (fps+128) >> 8);
             if (show_album_name == album_name_top)
                 fpstxt_y = LCD_HEIGHT -
                            rb->screens[SCREEN_MAIN]->getcharheight();