]> git.cworth.org Git - glfps/blob - glfps.c
Switch to Glaze to implement glfps
[glfps] / glfps.c
1 #define _GNU_SOURCE /* For RTLD_NEXT */
2 #include <dlfcn.h>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include <X11/Xlib.h>
8 #include <GL/gl.h>
9 #include <GL/glx.h>
10
11 #include <sys/time.h>
12 #include <string.h>
13
14 #include <glaze.h>
15
16 /* How many frames between reports. */
17 #define REPORT_FREQ 60
18
19 static void
20 on_each_frame (void)
21 {
22         static int count = 0;
23         static struct timeval tv_last;
24         struct timeval tv_now;
25
26         if ((count % REPORT_FREQ) == 0) {
27                 gettimeofday (&tv_now, NULL);
28                 if (count == 0) {
29                         printf ("glfps: Initializing FPS timer\n");
30                 } else {
31                         double elapsed = ((tv_now.tv_sec - tv_last.tv_sec) +
32                                           (tv_now.tv_usec - tv_last.tv_usec) / 1e6);
33                         printf ("FPS: %.3f\n", ((double) REPORT_FREQ) / elapsed);
34                 }
35                 tv_last = tv_now;
36         }
37
38         count++;
39 }
40
41 void
42 glXSwapBuffers (Display *dpy, GLXDrawable drawable)
43 {
44         on_each_frame ();
45
46         GLAZE_DEFER (glXSwapBuffers, dpy, drawable);
47 }