diff options
author | turret <turret@duck.com> | 2023-06-29 17:00:38 -0500 |
---|---|---|
committer | turret <turret@duck.com> | 2023-06-29 17:00:38 -0500 |
commit | f98f768869eac86701bd7291c966df46fd6df97d (patch) | |
tree | db7f149701609e17c45f92222b963469a4038741 | |
parent | 700a3e787bd0a07171c38fd2b2b051c550ee35cf (diff) | |
download | donut-f98f768869eac86701bd7291c966df46fd6df97d.tar.gz donut-f98f768869eac86701bd7291c966df46fd6df97d.tar.bz2 donut-f98f768869eac86701bd7291c966df46fd6df97d.zip |
donut: add cli arguments
-rw-r--r-- | arg.h | 49 | ||||
-rw-r--r-- | config.def.h | 3 | ||||
-rw-r--r-- | donut.c | 34 |
3 files changed, 80 insertions, 6 deletions
@@ -0,0 +1,49 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][0] == '-'\ + && argv[0][1];\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/config.def.h b/config.def.h index f8a1bb3..968b48c 100644 --- a/config.def.h +++ b/config.def.h @@ -1,3 +1,2 @@ #define FPS_MAX 50 -#define SCALE 2 -#define ROTATION_SPEED_FACTOR 1 + @@ -23,13 +23,15 @@ #include <stdlib.h> #include <stdint.h> #include <string.h> -#define __USE_MISC #include <math.h> #include <X11/Xlib.h> #include <X11/Xutil.h> #include <X11/Xos.h> +char *argv0; +#include "arg.h" + #define BETWEEN(x, min, max) min < x && x< max const float theta_spacing = 0.07; @@ -80,7 +82,32 @@ void drawTorus (float *b, const float *Az, const float *Bz) { free(z); } -int main() { +void usage() { + fprintf(stderr, "usage: %s [-h] [-s scale] [-r speed]\n", argv0); + exit(1); +} + +int main(int argc, char **argv) { + /*#define SCALE 2 + #define ROTATION_SPEED_FACTOR 1*/ + + int SCALE = 2; + int ROTATION_SPEED_FACTOR = 1; + + ARGBEGIN { + case 's': + SCALE = atoi(EARGF(usage())); + break; + case 'r': + ROTATION_SPEED_FACTOR = atoi(EARGF(usage())); + break; + case 'h': + usage(); + break; + default: + break; + } ARGEND + Display *dis = XOpenDisplay((char*)0); int screen = DefaultScreen(dis); unsigned long black = BlackPixel(dis, screen), white = WhitePixel(dis, screen); @@ -137,8 +164,7 @@ int main() { A += (rtime-stime) * (0.04f * ROTATION_SPEED_FACTOR) / (1000/FPS_MAX);// / (1000/FPS_MAX), 0.04f * ROTATION_SPEED_FACTOR); B += (rtime-stime) * (0.02f * ROTATION_SPEED_FACTOR) / (1000/FPS_MAX);// / (1000/FPS_MAX), 0.04f * ROTATION_SPEED_FACTOR); - printf("%.1f %d ", (rtime-stime), 1000/(FPS_MAX)); - printf("fps: %.1f %d \r", 1000/(rtime-stime), FPS_MAX); + printf("fps: %.1f \r", 1000/(rtime-stime)); } } |