]> git.cworth.org Git - glfps/blob - glfps.c
Add support for glXGetProcAddress as well
[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 #include <string.h>
12
13 /* How many frames between reports. */
14 #define REPORT_FREQ 60
15
16 static void
17 on_each_frame (void)
18 {
19         static int count = 0;
20         static struct timeval tv_last;
21         struct timeval tv_now;
22
23         if ((count % REPORT_FREQ) == 0) {
24                 gettimeofday (&tv_now, NULL);
25                 if (count == 0) {
26                         printf ("glfps: Initializing FPS timer\n");
27                 } else {
28                         double elapsed = ((tv_now.tv_sec - tv_last.tv_sec) +
29                                           (tv_now.tv_usec - tv_last.tv_usec) / 1e6);
30                         printf ("FPS: %.3f\n", ((double) REPORT_FREQ) / elapsed);
31                 }
32                 tv_last = tv_now;
33         }
34
35         count++;
36 }
37
38 void
39 glXSwapBuffers (Display *dpy, GLXDrawable drawable)
40 {
41         static typeof(&glXSwapBuffers) real_glXSwapBuffers = NULL;
42
43         if (real_glXSwapBuffers == NULL)
44                 real_glXSwapBuffers = dlsym (RTLD_NEXT, "glXSwapBuffers");
45
46         on_each_frame ();
47
48         real_glXSwapBuffers (dpy, drawable);
49 }
50
51 void
52 (*glXGetProcAddressARB (const GLubyte *func))(void)
53 {
54         static typeof(&glXGetProcAddressARB) real_glXGetProcAddressARB = NULL;
55
56         if (strcmp((char *) func, "glXSwapBuffers") == 0)
57                 return (void*) glXSwapBuffers;
58
59         if (real_glXGetProcAddressARB == NULL)
60                 real_glXGetProcAddressARB = dlsym (RTLD_NEXT, "glXGetProcAddressARB");
61
62         return real_glXGetProcAddressARB (func);
63 }
64
65 void
66 (*glXGetProcAddress (const GLubyte *func))(void)
67 {
68         static typeof(&glXGetProcAddress) real_glXGetProcAddress = NULL;
69
70         if (strcmp((char *) func, "glXSwapBuffers") == 0)
71                 return (void*) glXSwapBuffers;
72
73         if (real_glXGetProcAddress == NULL)
74                 real_glXGetProcAddress = dlsym (RTLD_NEXT, "glXGetProcAddress");
75
76         return real_glXGetProcAddress (func);
77 }