aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-08-02 21:39:15 +0200
committerDavid Robillard <d@drobilla.net>2019-08-02 23:28:15 +0200
commitb628c72193e068277ab4fbf0e6db9a572c43be46 (patch)
tree2e4d5aeafe5a2a9742496ebd6d4e494467eb5c77 /test
parent2359dafa529216a3b3a327edb9e4b5daf92985f7 (diff)
Test: Factor out FPS printer
Diffstat (limited to 'test')
-rw-r--r--test/pugl_cairo_test.c16
-rw-r--r--test/pugl_test.c14
-rw-r--r--test/test_utils.h29
3 files changed, 35 insertions, 24 deletions
diff --git a/test/pugl_cairo_test.c b/test/pugl_cairo_test.c
index 4219728..d3b157e 100644
--- a/test/pugl_cairo_test.c
+++ b/test/pugl_cairo_test.c
@@ -18,6 +18,8 @@
@file pugl_cairo_test.c A simple Pugl test that creates a top-level window.
*/
+#include "test_utils.h"
+
#include "pugl/pugl.h"
#include "pugl/pugl_cairo_backend.h"
@@ -216,10 +218,8 @@ main(int argc, char** argv)
puglShowWindow(view);
- float lastReportTime = (float)puglGetTime(view);
+ PuglFpsPrinter fpsPrinter = { puglGetTime(view) };
while (!quit) {
- const float thisTime = (float)puglGetTime(view);
-
if (continuous) {
puglPostRedisplay(view);
} else {
@@ -228,14 +228,8 @@ main(int argc, char** argv)
puglProcessEvents(view);
- if (continuous && thisTime > lastReportTime + 5) {
- const double fps = framesDrawn / (thisTime - lastReportTime);
- fprintf(stderr,
- "%u frames in %.0f seconds = %.3f FPS\n",
- framesDrawn, thisTime - lastReportTime, fps);
-
- lastReportTime = thisTime;
- framesDrawn = 0;
+ if (continuous) {
+ puglPrintFps(view, &fpsPrinter, &framesDrawn);
}
}
diff --git a/test/pugl_test.c b/test/pugl_test.c
index f276d8a..12ce529 100644
--- a/test/pugl_test.c
+++ b/test/pugl_test.c
@@ -196,8 +196,8 @@ main(int argc, char** argv)
puglShowWindow(view);
- float lastReportTime = (float)puglGetTime(view);
- bool requestedAttention = false;
+ PuglFpsPrinter fpsPrinter = { puglGetTime(view) };
+ bool requestedAttention = false;
while (!quit) {
const float thisTime = (float)puglGetTime(view);
@@ -214,14 +214,8 @@ main(int argc, char** argv)
requestedAttention = true;
}
- if (continuous && thisTime > lastReportTime + 5) {
- const double fps = framesDrawn / (thisTime - lastReportTime);
- fprintf(stderr,
- "%u frames in %.0f seconds = %.3f FPS\n",
- framesDrawn, thisTime - lastReportTime, fps);
-
- lastReportTime = thisTime;
- framesDrawn = 0;
+ if (continuous) {
+ puglPrintFps(view, &fpsPrinter, &framesDrawn);
}
}
diff --git a/test/test_utils.h b/test/test_utils.h
index fb04f4b..9738d96 100644
--- a/test/test_utils.h
+++ b/test/test_utils.h
@@ -20,6 +20,10 @@
#include <stdint.h>
#include <stdio.h>
+typedef struct {
+ double lastReportTime;
+} PuglFpsPrinter;
+
static const float cubeVertices[] = {
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
@@ -71,7 +75,7 @@ static const float cubeVertices[] = {
};
/** Calculate a projection matrix for a given perspective. */
-static void
+static inline void
perspective(float* m, float fov, float aspect, float zNear, float zFar)
{
const float h = tanf(fov);
@@ -86,7 +90,7 @@ perspective(float* m, float fov, float aspect, float zNear, float zFar)
m[12] = 0; m[13] = 0; m[14] = qn; m[15] = 0;
}
-static int
+static inline int
printModifiers(const uint32_t mods)
{
return fprintf(stderr, "Modifiers:%s%s%s%s\n",
@@ -96,7 +100,7 @@ printModifiers(const uint32_t mods)
(mods & PUGL_MOD_SUPER) ? " Super" : "");
}
-static int
+static inline int
printEvent(const PuglEvent* event, const char* prefix)
{
switch (event->type) {
@@ -142,3 +146,22 @@ printEvent(const PuglEvent* event, const char* prefix)
return 0;
}
+
+static inline void
+puglPrintFps(PuglView* view,
+ PuglFpsPrinter* printer,
+ unsigned* const framesDrawn)
+{
+ const double thisTime = puglGetTime(view);
+ if (thisTime > printer->lastReportTime + 5) {
+ const double fps = *framesDrawn / (thisTime - printer->lastReportTime);
+ fprintf(stderr,
+ "%u frames in %.0f seconds = %.3f FPS\n",
+ *framesDrawn,
+ thisTime - printer->lastReportTime,
+ fps);
+
+ printer->lastReportTime = thisTime;
+ *framesDrawn = 0;
+ }
+}