diff --git a/src/main.c b/src/main.c index 7011f0a..251ded6 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include #include +#include "buddhabrot.h" #include "image.h" #include "mandelbrot.h" #include "options.h" @@ -14,7 +15,15 @@ int main(int argc, char *argv[]) { if (!image) err(EXIT_FAILURE, "could not allocate image"); - mandelbrot(image, opt.max_iter); + switch (opt.render) { + case BUDDHABROT: + buddhabrot(image, opt.max_iter); + break; + case MANDELBROT: + mandelbrot(image, opt.max_iter); + break; + } + print_ppm(image, opt.output); fclose(opt.output); diff --git a/src/options.c b/src/options.c index 4967cc2..da6b55d 100644 --- a/src/options.c +++ b/src/options.c @@ -2,20 +2,29 @@ #include #include +#include #include +#define ARR_SIZE(Arr) (sizeof(Arr) / sizeof(*Arr)) + +static const char *methods[] = { + [MANDELBROT] = "mandelbrot", + [BUDDHABROT] = "buddhabrot", +}; + struct options parse_options(int *argc, char **argv[]) { struct options opts = { .output = stdout, .w = 960, .h = 540, .max_iter = 100, + .render = BUDDHABROT, }; opterr = 0; // Deactivate error message from `getopt` int opt; - while ((opt = getopt(*argc, *argv, "h:m:o:w:")) != -1) { + while ((opt = getopt(*argc, *argv, "h:m:o:r:w:")) != -1) { switch (opt) { case 'h': if (!sscanf(optarg, "%zu", &opts.h)) @@ -29,13 +38,26 @@ struct options parse_options(int *argc, char **argv[]) { if (!freopen(optarg, "w", opts.output)) err(EXIT_FAILURE, "could not open output file"); break; + case 'r': { + size_t i; + for (i = 0; i < ARR_SIZE(methods); ++i) + if (!strcmp(methods[i], optarg)) { + opts.render = i; + break; + } + if (i == ARR_SIZE(methods)) + errx(EXIT_FAILURE, "could not parse render"); + break; + } case 'w': if (!sscanf(optarg, "%zu", &opts.w)) errx(EXIT_FAILURE, "could not parse width"); break; default: fprintf(stderr, - "Usage: %s [-o FILE] [-m MAX_ITER] [-h HEIGHT] [-w WIDTH]\n", + "Usage: %s " + "[-o FILE] [-i MAX_ITER] [-h HEIGHT] [-w WIDTH] " + "[-r ]\n", (*argv)[0]); exit(EXIT_FAILURE); } diff --git a/src/options.h b/src/options.h index 0f2c8f0..3193967 100644 --- a/src/options.h +++ b/src/options.h @@ -9,6 +9,10 @@ struct options { size_t w; size_t h; size_t max_iter; + enum { + BUDDHABROT, + MANDELBROT, + } render; }; struct options parse_options(int *argc, char **argv[]);