diff options
author | David Robillard <d@drobilla.net> | 2019-08-04 22:00:04 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2019-09-03 08:34:39 +0200 |
commit | 4c6ac6b1c3e9f7ac04b5f6ba0b30eb8eacfcce9c (patch) | |
tree | 42f39dfb66004b2153ab36a77fcfb13508d9f5dc | |
parent | 36f9c9dd7bc2f3d986b90477238c24e121936a25 (diff) | |
download | pugl-4c6ac6b1c3e9f7ac04b5f6ba0b30eb8eacfcce9c.tar.gz pugl-4c6ac6b1c3e9f7ac04b5f6ba0b30eb8eacfcce9c.tar.bz2 pugl-4c6ac6b1c3e9f7ac04b5f6ba0b30eb8eacfcce9c.zip |
Factor out parsing test options
-rw-r--r-- | test/pugl_cairo_test.c | 35 | ||||
-rw-r--r-- | test/pugl_test.c | 49 | ||||
-rw-r--r-- | test/test_utils.h | 58 |
3 files changed, 82 insertions, 60 deletions
diff --git a/test/pugl_cairo_test.c b/test/pugl_cairo_test.c index 278a74d..3a36e53 100644 --- a/test/pugl_cairo_test.c +++ b/test/pugl_cairo_test.c @@ -31,8 +31,8 @@ #include <string.h> static PuglWorld* world = NULL; +PuglTestOptions opts = {0}; -static bool continuous = false; static int quit = 0; static bool entered = false; static bool mouseDown = false; @@ -133,7 +133,7 @@ onDisplay(PuglView* view) // Draw button for (Button* b = buttons; b->label; ++b) { - buttonDraw(cr, b, continuous ? puglGetTime(world) : 0.0); + buttonDraw(cr, b, opts.continuous ? puglGetTime(world) : 0.0); } ++framesDrawn; @@ -184,25 +184,10 @@ onEvent(PuglView* view, const PuglEvent* event) int main(int argc, char** argv) { - bool ignoreKeyRepeat = false; - bool resizable = false; - for (int i = 1; i < argc; ++i) { - if (!strcmp(argv[i], "-c")) { - continuous = true; - } else if (!strcmp(argv[i], "-h")) { - printf("USAGE: %s [OPTIONS]...\n\n" - " -c Continuously animate and draw\n" - " -h Display this help\n" - " -i Ignore key repeat\n" - " -r Resizable window\n", argv[0]); - return 0; - } else if (!strcmp(argv[i], "-i")) { - ignoreKeyRepeat = true; - } else if (!strcmp(argv[i], "-r")) { - resizable = true; - } else { - fprintf(stderr, "Unknown option: %s\n", argv[i]); - } + opts = puglParseTestOptions(&argc, &argv); + if (opts.help) { + puglPrintTestUsage("pugl_test", ""); + return 1; } world = puglNewWorld(); @@ -212,10 +197,10 @@ main(int argc, char** argv) PuglView* view = puglNewView(world); puglSetFrame(view, frame); puglSetMinSize(view, 256, 256); - puglSetViewHint(view, PUGL_RESIZABLE, resizable); + puglSetViewHint(view, PUGL_RESIZABLE, opts.resizable); puglSetBackend(view, puglCairoBackend()); - puglSetViewHint(view, PUGL_IGNORE_KEY_REPEAT, ignoreKeyRepeat); + puglSetViewHint(view, PUGL_IGNORE_KEY_REPEAT, opts.ignoreKeyRepeat); puglSetEventFunc(view, onEvent); if (puglCreateWindow(view, "Pugl Test")) { @@ -226,7 +211,7 @@ main(int argc, char** argv) PuglFpsPrinter fpsPrinter = { puglGetTime(world) }; while (!quit) { - if (continuous) { + if (opts.continuous) { puglPostRedisplay(view); } else { puglPollEvents(world, -1); @@ -234,7 +219,7 @@ main(int argc, char** argv) puglDispatchEvents(world); - if (continuous) { + if (opts.continuous) { puglPrintFps(world, &fpsPrinter, &framesDrawn); } } diff --git a/test/pugl_test.c b/test/pugl_test.c index 6c4e3e8..740ebcd 100644 --- a/test/pugl_test.c +++ b/test/pugl_test.c @@ -281,35 +281,14 @@ main(int argc, char** argv) PuglTestApp app = {0}; app.dist = 10; - int samples = 0; - int doubleBuffer = PUGL_FALSE; - bool ignoreKeyRepeat = false; - bool resizable = false; - for (int i = 1; i < argc; ++i) { - if (!strcmp(argv[i], "-a")) { - samples = 4; - } else if (!strcmp(argv[i], "-c")) { - app.continuous = true; - } else if (!strcmp(argv[i], "-d")) { - doubleBuffer = PUGL_TRUE; - } else if (!strcmp(argv[i], "-h")) { - printf("USAGE: %s [OPTIONS]...\n\n" - " -a Enable anti-aliasing\n" - " -c Continuously animate and draw\n" - " -d Enable double-buffering\n" - " -h Display this help\n" - " -i Ignore key repeat\n" - " -r Resizable window\n", argv[0]); - return 0; - } else if (!strcmp(argv[i], "-i")) { - ignoreKeyRepeat = true; - } else if (!strcmp(argv[i], "-r")) { - resizable = true; - } else { - fprintf(stderr, "Unknown option: %s\n", argv[i]); - } + const PuglTestOptions opts = puglParseTestOptions(&argc, &argv); + if (opts.help) { + puglPrintTestUsage("pugl_test", ""); + return 1; } + app.continuous = opts.continuous; + app.world = puglNewWorld(); app.parent = puglNewView(app.world); app.child = puglNewView(app.world); @@ -322,11 +301,11 @@ main(int argc, char** argv) puglSetAspectRatio(app.parent, 1, 1, 16, 9); puglSetBackend(app.parent, puglGlBackend()); - puglSetViewHint(app.parent, PUGL_RESIZABLE, resizable); - puglSetViewHint(app.parent, PUGL_SAMPLES, samples); - puglSetViewHint(app.parent, PUGL_DOUBLE_BUFFER, doubleBuffer); - puglSetViewHint(app.parent, PUGL_SWAP_INTERVAL, doubleBuffer); - puglSetViewHint(app.parent, PUGL_IGNORE_KEY_REPEAT, ignoreKeyRepeat); + puglSetViewHint(app.parent, PUGL_RESIZABLE, opts.resizable); + puglSetViewHint(app.parent, PUGL_SAMPLES, opts.samples); + puglSetViewHint(app.parent, PUGL_DOUBLE_BUFFER, opts.doubleBuffer); + puglSetViewHint(app.parent, PUGL_SWAP_INTERVAL, opts.doubleBuffer); + puglSetViewHint(app.parent, PUGL_IGNORE_KEY_REPEAT, opts.ignoreKeyRepeat); puglSetHandle(app.parent, &app); puglSetEventFunc(app.parent, onParentEvent); @@ -340,11 +319,11 @@ main(int argc, char** argv) puglSetFrame(app.child, getChildFrame(parentFrame)); puglSetParentWindow(app.child, puglGetNativeWindow(app.parent)); - puglSetViewHint(app.child, PUGL_SAMPLES, samples); - puglSetViewHint(app.child, PUGL_DOUBLE_BUFFER, doubleBuffer); + puglSetViewHint(app.child, PUGL_SAMPLES, opts.samples); + puglSetViewHint(app.child, PUGL_DOUBLE_BUFFER, opts.doubleBuffer); puglSetViewHint(app.child, PUGL_SWAP_INTERVAL, 0); puglSetBackend(app.child, puglGlBackend()); - puglSetViewHint(app.child, PUGL_IGNORE_KEY_REPEAT, ignoreKeyRepeat); + puglSetViewHint(app.child, PUGL_IGNORE_KEY_REPEAT, opts.ignoreKeyRepeat); puglSetHandle(app.child, &app); puglSetEventFunc(app.child, onEvent); diff --git a/test/test_utils.h b/test/test_utils.h index bf058ac..f4f6c31 100644 --- a/test/test_utils.h +++ b/test/test_utils.h @@ -19,11 +19,21 @@ #include <math.h> #include <stdint.h> #include <stdio.h> +#include <string.h> typedef struct { double lastReportTime; } PuglFpsPrinter; +typedef struct { + int samples; + int doubleBuffer; + bool continuous; + bool help; + bool ignoreKeyRepeat; + bool resizable; +} PuglTestOptions; + static const float cubeStripVertices[] = { -1.0f, 1.0f, 1.0f, // Front top left 1.0f, 1.0f, 1.0f, // Front top right @@ -143,6 +153,54 @@ printEvent(const PuglEvent* event, const char* prefix) } static inline void +puglPrintTestUsage(const char* prog, const char* posHelp) +{ + printf("Usage: %s [OPTION]... %s\n\n" + " -a Enable anti-aliasing\n" + " -c Continuously animate and draw\n" + " -d Enable double-buffering\n" + " -h Display this help\n" + " -i Ignore key repeat\n" + " -r Resizable window\n", + prog, posHelp); +} + +static inline PuglTestOptions +puglParseTestOptions(int* pargc, char*** pargv) +{ + PuglTestOptions opts = { 0, 0, false, false, false, false }; + + char** const argv = *pargv; + int i = 1; + for (; i < *pargc; ++i) { + if (!strcmp(argv[i], "-a")) { + opts.samples = 4; + } else if (!strcmp(argv[i], "-c")) { + opts.continuous = true; + } else if (!strcmp(argv[i], "-d")) { + opts.doubleBuffer = PUGL_TRUE; + } else if (!strcmp(argv[i], "-h")) { + opts.help = true; + return opts; + } else if (!strcmp(argv[i], "-i")) { + opts.ignoreKeyRepeat = true; + } else if (!strcmp(argv[i], "-r")) { + opts.resizable = true; + } else if (argv[i][0] != '-') { + break; + } else { + opts.help = true; + fprintf(stderr, "error: Unknown option: %s\n", argv[i]); + } + } + + *pargc -= i; + *pargv += i; + + return opts; +} + +static inline void puglPrintFps(const PuglWorld* world, PuglFpsPrinter* printer, unsigned* const framesDrawn) |