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