]> git.cworth.org Git - glfps/commitdiff
Initial import of libglfps demo for glaze
authorCarl Worth <cworth@cworth.org>
Fri, 20 Sep 2013 22:29:03 +0000 (15:29 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 20 Sep 2013 23:30:40 +0000 (16:30 -0700)
This first implementation is a simple FPS-counter for OpenGL
implemented via a library suitable for use with LD_PRELOAD.

.gitignore [new file with mode: 0644]
Makefile [new file with mode: 0644]
glfps.c [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..916f458
--- /dev/null
@@ -0,0 +1 @@
+libglfps.so
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..3e7e390
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,8 @@
+CC ?= gcc
+CFLAGS ?= -g -Wall -Wextra -Wmissing-declarations
+
+libglfps.so: glfps.c
+       $(CC) $(CFLAGS) -fPIC -shared -Wl,-Bsymbolic -o $@ $<
+
+clean:
+       rm -f libglfps.so
diff --git a/glfps.c b/glfps.c
new file mode 100644 (file)
index 0000000..68d389a
--- /dev/null
+++ b/glfps.c
@@ -0,0 +1,48 @@
+#define _GNU_SOURCE /* For RTLD_NEXT */
+#include <dlfcn.h>
+
+#include <stdio.h>
+
+#include <X11/Xlib.h>
+#include <GL/gl.h>
+#include <GL/glx.h>
+
+#include <sys/time.h>
+
+/* How many frames between reports. */
+#define REPORT_FREQ 60
+
+static void
+on_each_frame (void)
+{
+       static int count = 0;
+       static struct timeval tv_last;
+       struct timeval tv_now;
+
+       if ((count % REPORT_FREQ) == 0) {
+               gettimeofday (&tv_now, NULL);
+               if (count == 0) {
+                       printf ("glfps: Initializing FPS timer\n");
+               } else {
+                       double elapsed = ((tv_now.tv_sec - tv_last.tv_sec) +
+                                         (tv_now.tv_usec - tv_last.tv_usec) / 1e6);
+                       printf ("FPS: %.3f\n", ((double) REPORT_FREQ) / elapsed);
+               }
+               tv_last = tv_now;
+       }
+
+       count++;
+}
+
+void
+glXSwapBuffers (Display *dpy, GLXDrawable drawable)
+{
+       static typeof(&glXSwapBuffers) real_glXSwapBuffers = NULL;
+
+       if (real_glXSwapBuffers == NULL)
+               real_glXSwapBuffers = dlsym (RTLD_NEXT, "glXSwapBuffers");
+
+       on_each_frame ();
+
+       real_glXSwapBuffers (dpy, drawable);
+}