Factor out some more useful functions
authorAntonio Ospite <ospite@studenti.unina.it>
Sun, 14 Jul 2013 10:53:36 +0000 (12:53 +0200)
committerAntonio Ospite <ospite@studenti.unina.it>
Sun, 14 Jul 2013 10:53:36 +0000 (12:53 +0200)
fps-meter.c

index c66cb69..0de937e 100644 (file)
@@ -18,6 +18,7 @@
  */
 
 #include <stdio.h>
+#include <string.h>
 #include <time.h>
 
 #ifdef DEBUG
                }                                                \
        } while(0)
 
-int main(void)
-{
+struct fps_meter_stats {
        struct timespec time_start;
        struct timespec time_end;
        struct timespec elapsed;
 
-       struct timespec tmp;
+       unsigned int frames;
+       double nsecs;
+};
 
-       unsigned int frames = 0;
-       double nsecs = 0;
-       float fps;
+static void fps_meter_init(struct fps_meter_stats *stats)
+{
+       memset(stats, 0, sizeof(*stats));
 
-       while (1) {
-               clock_gettime(CLOCK_MONOTONIC, &time_start);
-               dbg("Start time: s: %ld, ns: %ld", time_start.tv_sec, time_start.tv_nsec);
+       clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
+       dbg("Init time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
+}
+
+static void fps_meter_update(struct fps_meter_stats *stats)
+{
+       dbg("Start time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
+
+       clock_gettime(CLOCK_MONOTONIC, &stats->time_end);
+       dbg("End time: s: %ld, ns: %ld", stats->time_end.tv_sec, stats->time_end.tv_nsec);
+
+       timespecsub(&stats->time_end, &stats->time_start, &stats->elapsed);
+       dbg("Elapsed s: %ld ns: %ld", stats->elapsed.tv_sec, stats->elapsed.tv_nsec);
 
+       stats->frames++;
+       stats->nsecs += (stats->elapsed.tv_sec * NSEC_PER_SEC + stats->elapsed.tv_nsec);
+       if (stats->nsecs >= NSEC_PER_SEC) {
+               /* 
+                * if we were garanteed that each frame took less than
+                * a second, then just printing 'frames' would be enough here,
+                * but if we want to cover the case when a frame may take more
+                * than a second, some calculations have to be done.
+                */
+               float fps = stats->frames / (stats->nsecs / NSEC_PER_SEC);
+               printf("(frames: %d, nsecs: %f) FPS: %.2f\n", stats->frames, stats->nsecs, fps);
+               stats->nsecs = 0;
+               stats->frames = 0;
+       }
+
+       /* update the stats for the next iteration */
+       clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
+}
+
+int main(void)
+{
+       struct fps_meter_stats stats;
+       struct timespec tmp;
+
+       fps_meter_init(&stats);
+       while (1) {
                /* simulate some workload of about 60fps */
                dbg("Doing some work, which takes about %d ns", NSEC_PER_SEC / 60);
                tmp.tv_sec = 0;
                tmp.tv_nsec = NSEC_PER_SEC / 60;
                clock_nanosleep(CLOCK_MONOTONIC, 0, &tmp, NULL);
 
-               clock_gettime(CLOCK_MONOTONIC, &time_end);
-               dbg("End time: s: %ld, ns: %ld", time_end.tv_sec, time_end.tv_nsec);
-               timespecsub(&time_end, &time_start, &elapsed);
-               dbg("Elapsed s: %ld ns: %ld", elapsed.tv_sec, elapsed.tv_nsec);
-
-               frames++;
-               nsecs += (elapsed.tv_sec * NSEC_PER_SEC + elapsed.tv_nsec);
-               if (nsecs >= NSEC_PER_SEC) {
-                       /* 
-                        * if each frame takes less than a second, then just
-                        * printing 'frames' is enough, but if we want to
-                        * cover the case when a frame may take more than
-                        * a second, some calculations have to be done.
-                        */
-                       fps = frames / (nsecs / NSEC_PER_SEC);
-                       printf("(frames: %d, nsecs: %f) FPS: %.2f\n", frames, nsecs, fps);
-                       nsecs = 0;
-                       frames = 0;
-               }
+               fps_meter_update(&stats);
        }
 
        return 0;