Reorganize code into library for reuse
This commit is contained in:
11
utils/CMakeLists.txt
Normal file
11
utils/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_search_module(POPT REQUIRED popt)
|
||||
|
||||
add_executable(mazemaker)
|
||||
target_sources(mazemaker PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/mazemaker.c)
|
||||
target_link_libraries(mazemaker PRIVATE mazemaker_shared PUBLIC ${POPT_LIBRARIES})
|
||||
target_include_directories(mazemaker PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include PUBLIC ${POPT_INCLUDE_DIRS})
|
||||
target_compile_options(mazemaker PUBLIC ${POPT_CFLAGS_OTHER})
|
||||
target_link_options(mazemaker PUBLIC -L${POPT_LIBDIR})
|
||||
|
||||
install (TARGETS mazemaker DESTINATION bin)
|
||||
43
utils/mazemaker.c
Normal file
43
utils/mazemaker.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <popt.h>
|
||||
#include <mazemaker.h>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
char c;
|
||||
int width = 0, height = 0;
|
||||
char const *filename = 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" },
|
||||
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;
|
||||
mazemaker_generate_maze(width, height, &maze);
|
||||
mazemaker_maze_to_png(&maze, filename);
|
||||
mazemaker_free_maze(&maze);
|
||||
poptFreeContext(ctx);
|
||||
}
|
||||
Reference in New Issue
Block a user