#include #include #include #include int main(int argc, char* argv[]) { char c; int width = 0, height = 0; unsigned int seed = 0; char const *filename = NULL, *fg_color = NULL, *bg_color = NULL, *path_color = NULL, *seed_str = NULL, *solution_fn = NULL; struct poptOption options_table[] = { { "width", 'w', POPT_ARG_INT, &width, 0, "Width of the maze", "BLOCKS" }, { "height", 'h', POPT_ARG_INT, &height, 0, "Height of the maze", "BLOCKS" }, { "foreground", 'f', POPT_ARG_STRING, &fg_color, 0, "Foreground (wall) color", "#rrggbb" }, { "background", 'b', POPT_ARG_STRING, &bg_color, 0, "Background color", "#rrggbb" }, { "path", 'p', POPT_ARG_STRING, &path_color, 0, "Path (solution) color", "#rrggbb" }, { "seed", 's', POPT_ARG_STRING, &seed_str, 0, "Random seed", "SEED" }, { "solution", 'u', POPT_ARG_STRING, &solution_fn, 0, "Output solution", "FILENAME" }, POPT_AUTOHELP { NULL, 0, 0, NULL, 0 } }; poptContext ctx = poptGetContext(NULL, argc, (const char**) argv, options_table, 0); poptSetOtherOptionHelp(ctx, "OUTPUT"); if (argc < 2) { poptPrintUsage(ctx, stderr, 0); exit(1); } while ((c = poptGetNextOpt(ctx)) >= 0) /* noop */ ; if (c < -1) { fprintf(stderr, "%s: %s\n", poptBadOption(ctx, POPT_BADOPTION_NOALIAS), poptStrerror(c)); return 1; } filename = poptGetArg(ctx); if ((width == 0) || (height == 0)) { fprintf(stderr, "Positive values for width (-w) and height (-h) are required.\n"); return 1; } if (filename == NULL) { fprintf(stderr, "An output filename is required.\n"); return 1; } mazegrid_t maze; mazeoptions_t* options = mazemaker_options_new(); if (fg_color != NULL) { if (0 > mazemaker_options_set_wall_color(options, fg_color)) { fprintf(stderr, "Unknown color: \"%s\"\n", fg_color); exit(1); } } if (bg_color != NULL) { if (0 > mazemaker_options_set_background_color(options, bg_color)) { fprintf(stderr, "Unknown color: \"%s\"\n", bg_color); exit(1); } } if (path_color != NULL) { if (0 > mazemaker_options_set_path_color(options, path_color)) { fprintf(stderr, "Unknown color: \"%s\"\n", path_color); exit(1); } } if (seed_str != NULL) { seed = (unsigned int)atol(seed_str); mazemaker_options_set_seed(options, seed); } mazemaker_generate_maze_opt(width, height, &maze, options); mazemaker_maze_to_png_opt(&maze, filename, options); if (solution_fn != NULL) { maze_path_t solution; if (mazemaker_solve(&maze, &solution) == 0) { fprintf(stderr, "Error: no maze solution!\n"); } else { mazemaker_path_to_png_opt(&solution, solution_fn, options); mazemaker_path_free(&solution); } } mazemaker_free_maze(&maze); mazemaker_options_free(options); poptFreeContext(ctx); }