Replaced ad-hoc argument processing with getopt.
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 7a31f98..614cbd9 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -41,7 +41,7 @@
add_executable(boing WIN32 boing.c)
add_executable(gears WIN32 gears.c)
add_executable(heightmap WIN32 heightmap.c ${GETOPT})
- add_executable(particles WIN32 particles.c ${TINYCTHREAD})
+ add_executable(particles WIN32 particles.c ${TINYCTHREAD} ${GETOPT})
add_executable(simple WIN32 simple.c)
add_executable(splitview WIN32 splitview.c)
add_executable(wave WIN32 wave.c)
diff --git a/examples/particles.c b/examples/particles.c
index 66e698a..4230fe7 100644
--- a/examples/particles.c
+++ b/examples/particles.c
@@ -46,6 +46,7 @@
#include <GLFW/glfw3.h>
#include <tinycthread.h>
+#include <getopt.h>
// Define tokens for GL_EXT_separate_specular_color if not already defined
#ifndef GL_EXT_separate_specular_color
@@ -239,6 +240,24 @@
//========================================================================
+// Print usage information
+//========================================================================
+
+static void usage(void)
+{
+ printf("Usage: particles [-hbs]\n");
+ printf("Options:\n");
+ printf(" -b Benchmark (run program for 60 seconds)\n");
+ printf(" -s Run program as single thread (default is to use two threads)\n");
+ printf(" -h Display this help\n");
+ printf("\n");
+ printf("Program runtime controls:\n");
+ printf(" W Toggle wireframe mode\n");
+ printf(" Esc Exit program\n");
+}
+
+
+//========================================================================
// Initialize a new particle
//========================================================================
@@ -940,7 +959,7 @@
int main(int argc, char** argv)
{
- int i, frames, benchmark;
+ int i, ch, frames, benchmark;
double t0, t;
thrd_t physics_thread = 0;
GLFWwindow* window;
@@ -949,38 +968,19 @@
multithreading = 1;
benchmark = 0;
- for (i = 1; i < argc; i++)
+ while ((ch = getopt(argc, argv, "bhs")) != -1)
{
- // Use benchmarking?
- if (strcmp(argv[i], "-b") == 0)
- benchmark = 1;
-
- // Force multithreading off?
- else if (strcmp(argv[i], "-s") == 0)
- multithreading = 0;
-
- // With a Finder launch on Mac OS X we get a bogus -psn_0_46268417
- // kind of argument (actual numbers vary). Ignore it.
- else if (strncmp(argv[i], "-psn_", 5) == 0)
- ;
-
- // Usage
- else
+ switch (ch)
{
- if (strcmp(argv[i], "-?") != 0)
- printf("Unknonwn option %s\n\n", argv[i]);
-
- printf("Usage: %s [options]\n", argv[0]);
- printf("\n");
- printf("Options:\n");
- printf(" -b Benchmark (run program for 60 s)\n");
- printf(" -s Run program as single thread (default is to use two threads)\n");
- printf(" -? Display this text\n");
- printf("\n");
- printf("Program runtime controls:\n");
- printf(" w Toggle wireframe mode\n");
- printf(" ESC Exit program\n");
- exit(EXIT_FAILURE);
+ case 'b':
+ benchmark = 1;
+ break;
+ case 'h':
+ usage();
+ exit(EXIT_SUCCESS);
+ case 's':
+ multithreading = 0;
+ break;
}
}