From aa4d8efdd271697e64dac8d5ea5a1d4695e814f7 Mon Sep 17 00:00:00 2001 From: David Baer Date: Sat, 8 May 2021 15:58:59 -0400 Subject: [PATCH] Reorganize code into library for reuse --- .gitignore | 4 +++ CMakeLists.txt | 43 ++++++++------------------------- config.h.in | 3 --- include/mazemaker.h | 29 ++++++++++++++++++++++ lib/CMakeLists.txt | 35 +++++++++++++++++++++++++++ {src => lib}/grid.c | 0 {src => lib}/grid.h | 20 +-------------- {src => lib}/image.c | 5 ++-- lib/maze.c | 13 ++++++++++ lib/path.h | 18 ++++++++++++++ {src => lib}/pq.c | 0 {src => lib}/pq.h | 0 {src => lib}/prim.c | 0 {src => lib}/prim.h | 0 {src => lib}/set.c | 0 {src => lib}/set.h | 0 mazemaker.pc.cmake | 11 +++++++++ src/image.h | 8 ------ utils/CMakeLists.txt | 11 +++++++++ src/main.c => utils/mazemaker.c | 15 ++++-------- 20 files changed, 140 insertions(+), 75 deletions(-) delete mode 100644 config.h.in create mode 100644 include/mazemaker.h create mode 100644 lib/CMakeLists.txt rename {src => lib}/grid.c (100%) rename {src => lib}/grid.h (63%) rename {src => lib}/image.c (97%) create mode 100644 lib/maze.c create mode 100644 lib/path.h rename {src => lib}/pq.c (100%) rename {src => lib}/pq.h (100%) rename {src => lib}/prim.c (100%) rename {src => lib}/prim.h (100%) rename {src => lib}/set.c (100%) rename {src => lib}/set.h (100%) create mode 100644 mazemaker.pc.cmake delete mode 100644 src/image.h create mode 100644 utils/CMakeLists.txt rename src/main.c => utils/mazemaker.c (81%) diff --git a/.gitignore b/.gitignore index 80841ee..c0c1746 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,8 @@ install_manifest.txt mazemaker *.tar.gz *.png +*.pc +*.a +*.so +*.so.* logs diff --git a/CMakeLists.txt b/CMakeLists.txt index ced0c71..b6f3d5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,37 +1,14 @@ -cmake_minimum_required (VERSION 2.8.12) -project (mazemaker) -set(CMAKE_C_FLAGS "-Wall -O2 -g") -set (VERSION_MAJOR 1) -set (VERSION_MINOR 0) +cmake_minimum_required (VERSION 3.0) +cmake_policy(VERSION 3.0) +project (mazemaker VERSION 1.0) -configure_file ( - "${PROJECT_SOURCE_DIR}/config.h.in" - "${PROJECT_BINARY_DIR}/config.h" - ) +SET(EXEC_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} CACHE PATH "Installation prefix for executables and object code libraries" FORCE) +SET(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include CACHE PATH "Installation prefix for C header files" FORCE) +SET(LIB_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib CACHE PATH "Installation prefix for object code libraries" FORCE) +CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/mazemaker.pc.cmake ${CMAKE_CURRENT_BINARY_DIR}/mazemaker.pc) -find_package(PkgConfig REQUIRED) -pkg_search_module(POPT REQUIRED popt) -pkg_search_module(PNG REQUIRED libpng) - -include_directories("${PROJECT_BINARY_DIR}") - -file(GLOB_RECURSE SOURCES src/*.c src/*.h) - -add_executable (mazemaker ${SOURCES}) -install (TARGETS mazemaker DESTINATION bin) - -target_link_libraries(mazemaker PUBLIC ${POPT_LIBRARIES} ${PNG_LIBRARIES} m) -target_include_directories(mazemaker PUBLIC ${POPT_INCLUDE_DIRS} ${PNG_INCLUDE_DIRS}) -target_compile_options(mazemaker PUBLIC ${POPT_CFLAGS_OTHER} ${PNG_CFLAGS_OTHER}) -target_link_options(mazemaker PUBLIC -L${POPT_LIBDIR} -L${PNG_LIBDIR}) - -set( - CPACK_SOURCE_PACKAGE_FILE_NAME - "mazemaker-${VERSION_MAJOR}.${VERSION_MINOR}" - CACHE INTERNAL "tarball basename" - ) -set(CPACK_SOURCE_GENERATOR "TGZ") -set(CPACK_SOURCE_IGNORE_FILES "*.png;/logs/;/.git/;.gitignore;.tar.gz$;/Makefile;/CMakeCache.txt;/CMakeFiles/;cmake_install.cmake;install_manifest.txt;/_CPack;/mazemaker$;/config.h$;.cmake$;${CPACK_SOURCE_IGNORE_FILES}") -include(CPack) +add_subdirectory(lib) +add_subdirectory(utils) +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/mazemaker.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) diff --git a/config.h.in b/config.h.in deleted file mode 100644 index 458c83d..0000000 --- a/config.h.in +++ /dev/null @@ -1,3 +0,0 @@ -// the configured options and settings for Tutorial -#define VERSION_MAJOR @VERSION_MAJOR@ -#define VERSION_MINOR @VERSION_MINOR@ diff --git a/include/mazemaker.h b/include/mazemaker.h new file mode 100644 index 0000000..05a61a6 --- /dev/null +++ b/include/mazemaker.h @@ -0,0 +1,29 @@ +#ifndef _MAZEMAKER_H +#define _MAZEMAKER_H 1 + +#include +#include + +typedef uint8_t edgeweight_t; + +typedef struct { + edgeweight_t up, right; +} mazeedges_t; + +typedef struct { + size_t width, height; + mazeedges_t** grid; +} mazegrid_t; + +typedef enum { + EDGE_UP, + EDGE_RIGHT, + EDGE_DOWN, + EDGE_LEFT +} mazeedge_dir_t; + +void mazemaker_generate_maze(int width, int height, mazegrid_t* result); +void mazemaker_free_maze(mazegrid_t* maze); +int mazemaker_maze_to_png(mazegrid_t const* maze, char const* filename); + +#endif // !def(_MAZEMAKER_H) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt new file mode 100644 index 0000000..03cd733 --- /dev/null +++ b/lib/CMakeLists.txt @@ -0,0 +1,35 @@ +project(mazemaker_lib) +cmake_minimum_required (VERSION 2.8.12) +set (mazemaker_SOVERSION_CURRENT 0) +set (mazemaker_SOVERSION_REVISION 1) +set (mazemaker_SOVERSION_AGE 0) +math (EXPR mazemaker_SOVERSION_MAJOR "${mazemaker_SOVERSION_CURRENT} - ${mazemaker_SOVERSION_AGE}") +math (EXPR mazemaker_SOVERSION_MINOR "${mazemaker_SOVERSION_AGE} + ${mazemaker_SOVERSION_REVISION}") +set (mazemaker_VERSION ${mazemaker_SOVERSION_MAJOR}.${mazemaker_SOVERSION_MINOR}) +set (mazemaker_SOVERSION ${mazemaker_SOVERSION_MAJOR}.${mazemaker_SOVERSION_MINOR}) + +find_package(PkgConfig REQUIRED) +pkg_search_module(PNG REQUIRED libpng) + +file(GLOB SOURCES *.c *.h) + +add_library(mazemaker_shared SHARED ${SOURCES}) +add_library(mazemaker_static STATIC ${SOURCES}) +set_target_properties(mazemaker_shared PROPERTIES + LIBRARY_OUTPUT_NAME mazemaker + VERSION ${mazemaker_VERSION} + SOVERSION ${mazemaker_SOVERSION} +) +set_target_properties(mazemaker_static PROPERTIES + OUTPUT_NAME mazemaker + VERSION ${mazemaker_VERSION} + SOVERSION ${mazemaker_SOVERSION} +) +install (TARGETS mazemaker_shared mazemaker_static DESTINATION ${LIB_INSTALL_DIR}) +install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/../include/mazemaker.h DESTINATION ${INCLUDE_INSTALL_DIR}) +target_link_libraries(mazemaker_shared PUBLIC ${PNG_LIBRARIES} m) +target_include_directories(mazemaker_shared PUBLIC ${PNG_INCLUDE_DIRS} ../include) +target_include_directories(mazemaker_static PUBLIC ${PNG_INCLUDE_DIRS} ../include) +target_compile_options(mazemaker_shared PUBLIC ${PNG_CFLAGS_OTHER}) +target_compile_options(mazemaker_static PUBLIC ${PNG_CFLAGS_OTHER}) +target_link_options(mazemaker_shared PUBLIC -L${PNG_LIBDIR}) diff --git a/src/grid.c b/lib/grid.c similarity index 100% rename from src/grid.c rename to lib/grid.c diff --git a/src/grid.h b/lib/grid.h similarity index 63% rename from src/grid.h rename to lib/grid.h index 71fbf73..e45093d 100644 --- a/src/grid.h +++ b/lib/grid.h @@ -1,26 +1,8 @@ #ifndef _GRID_H #define _GRID_H 1 +#include #include -#include - -typedef uint8_t edgeweight_t; - -typedef struct { - edgeweight_t up, right; -} mazeedges_t; - -typedef struct { - size_t width, height; - mazeedges_t** grid; -} mazegrid_t; - -typedef enum { - EDGE_UP, - EDGE_RIGHT, - EDGE_DOWN, - EDGE_LEFT -} mazeedge_dir_t; mazegrid_t mazegrid_new(size_t width, size_t height); void mazegrid_free(mazegrid_t* g); diff --git a/src/image.c b/lib/image.c similarity index 97% rename from src/image.c rename to lib/image.c index b6eac9e..17a7b6b 100644 --- a/src/image.c +++ b/lib/image.c @@ -4,7 +4,8 @@ #include #include #include -#include "image.h" +#include +#include "grid.h" #define LINE_THICKNESS 3 #define BLOCK_SIZE 32 @@ -86,7 +87,7 @@ static void freeImageData(img_data_t* img) { free(img); } -int writePNG(mazegrid_t const* g, char const* filename) { +int mazemaker_maze_to_png(mazegrid_t const* g, char const* filename) { int code = 1; img_data_t* img = gridToImageData(g); FILE *f = NULL; diff --git a/lib/maze.c b/lib/maze.c new file mode 100644 index 0000000..3851c80 --- /dev/null +++ b/lib/maze.c @@ -0,0 +1,13 @@ +#include +#include "grid.h" +#include "prim.h" + +void mazemaker_generate_maze(int width, int height, mazegrid_t* result) { + mazegrid_t g = mazegrid_new(width, height); + mazegrid_randomize(&g); + prim(&g, result); + mazegrid_free(&g); +} +void mazemaker_free_maze(mazegrid_t* maze) { + mazegrid_free(maze); +} diff --git a/lib/path.h b/lib/path.h new file mode 100644 index 0000000..b7bc19c --- /dev/null +++ b/lib/path.h @@ -0,0 +1,18 @@ +#ifndef _PATH_H +#define _PATH_H 1 + +#include "grid.h" + +typedef struct { + unsigned row, col; +} block_t; + +typedef struct { + size_t length; + block_t *nodes; +} path_t; + +int shortest_path(mazegrid_t const* maze, path_t* result); +void free_path(path_t* p); + +#endif // !def(_PATH_H) diff --git a/src/pq.c b/lib/pq.c similarity index 100% rename from src/pq.c rename to lib/pq.c diff --git a/src/pq.h b/lib/pq.h similarity index 100% rename from src/pq.h rename to lib/pq.h diff --git a/src/prim.c b/lib/prim.c similarity index 100% rename from src/prim.c rename to lib/prim.c diff --git a/src/prim.h b/lib/prim.h similarity index 100% rename from src/prim.h rename to lib/prim.h diff --git a/src/set.c b/lib/set.c similarity index 100% rename from src/set.c rename to lib/set.c diff --git a/src/set.h b/lib/set.h similarity index 100% rename from src/set.h rename to lib/set.h diff --git a/mazemaker.pc.cmake b/mazemaker.pc.cmake new file mode 100644 index 0000000..e910e42 --- /dev/null +++ b/mazemaker.pc.cmake @@ -0,0 +1,11 @@ +prefix=${CMAKE_INSTALL_PREFIX} +exec_prefix=${EXEC_INSTALL_PREFIX} +libdir=${LIB_INSTALL_DIR} +includedir=${INCLUDE_INSTALL_DIR} + +Name: ${PROJECT_NAME} +Description: Mazemaker library +Version: ${PROJECT_VERSION} +Requires: libpng +Libs: -L${LIB_INSTALL_DIR} -lmazemaker +Cflags: -I${INCLUDE_INSTALL_DIR} diff --git a/src/image.h b/src/image.h deleted file mode 100644 index 89916ce..0000000 --- a/src/image.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef _IMAGE_H -#define _IMAGE_H 1 - -#include "grid.h" - -int writePNG(mazegrid_t const* g, char const* filename); - -#endif // !def(_IMAGE_H) diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt new file mode 100644 index 0000000..941a85a --- /dev/null +++ b/utils/CMakeLists.txt @@ -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) diff --git a/src/main.c b/utils/mazemaker.c similarity index 81% rename from src/main.c rename to utils/mazemaker.c index e987652..b52efde 100644 --- a/src/main.c +++ b/utils/mazemaker.c @@ -1,9 +1,7 @@ #include #include #include -#include "grid.h" -#include "image.h" -#include "prim.h" +#include int main(int argc, char* argv[]) { char c; @@ -37,12 +35,9 @@ int main(int argc, char* argv[]) { fprintf(stderr, "An output filename is required.\n"); return 1; } - mazegrid_t g = mazegrid_new(width, height), maze; - mazegrid_randomize(&g); - //mazegrid_print(&g, stdout); - prim(&g, &maze); - writePNG(&maze, filename); - mazegrid_free(&g); - mazegrid_free(&maze); + mazegrid_t maze; + mazemaker_generate_maze(width, height, &maze); + mazemaker_maze_to_png(&maze, filename); + mazemaker_free_maze(&maze); poptFreeContext(ctx); }