Reorganize code into library for reuse
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -10,4 +10,8 @@ install_manifest.txt
|
|||||||
mazemaker
|
mazemaker
|
||||||
*.tar.gz
|
*.tar.gz
|
||||||
*.png
|
*.png
|
||||||
|
*.pc
|
||||||
|
*.a
|
||||||
|
*.so
|
||||||
|
*.so.*
|
||||||
logs
|
logs
|
||||||
|
|||||||
@@ -1,37 +1,14 @@
|
|||||||
cmake_minimum_required (VERSION 2.8.12)
|
cmake_minimum_required (VERSION 3.0)
|
||||||
project (mazemaker)
|
cmake_policy(VERSION 3.0)
|
||||||
set(CMAKE_C_FLAGS "-Wall -O2 -g")
|
project (mazemaker VERSION 1.0)
|
||||||
set (VERSION_MAJOR 1)
|
|
||||||
set (VERSION_MINOR 0)
|
|
||||||
|
|
||||||
configure_file (
|
SET(EXEC_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} CACHE PATH "Installation prefix for executables and object code libraries" FORCE)
|
||||||
"${PROJECT_SOURCE_DIR}/config.h.in"
|
SET(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include CACHE PATH "Installation prefix for C header files" FORCE)
|
||||||
"${PROJECT_BINARY_DIR}/config.h"
|
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)
|
add_subdirectory(lib)
|
||||||
pkg_search_module(POPT REQUIRED popt)
|
add_subdirectory(utils)
|
||||||
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)
|
|
||||||
|
|
||||||
|
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/mazemaker.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
// the configured options and settings for Tutorial
|
|
||||||
#define VERSION_MAJOR @VERSION_MAJOR@
|
|
||||||
#define VERSION_MINOR @VERSION_MINOR@
|
|
||||||
29
include/mazemaker.h
Normal file
29
include/mazemaker.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef _MAZEMAKER_H
|
||||||
|
#define _MAZEMAKER_H 1
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
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)
|
||||||
35
lib/CMakeLists.txt
Normal file
35
lib/CMakeLists.txt
Normal file
@@ -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})
|
||||||
@@ -1,26 +1,8 @@
|
|||||||
#ifndef _GRID_H
|
#ifndef _GRID_H
|
||||||
#define _GRID_H 1
|
#define _GRID_H 1
|
||||||
|
|
||||||
|
#include <mazemaker.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
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);
|
mazegrid_t mazegrid_new(size_t width, size_t height);
|
||||||
void mazegrid_free(mazegrid_t* g);
|
void mazegrid_free(mazegrid_t* g);
|
||||||
@@ -4,7 +4,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <png.h>
|
#include <png.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "image.h"
|
#include <mazemaker.h>
|
||||||
|
#include "grid.h"
|
||||||
|
|
||||||
#define LINE_THICKNESS 3
|
#define LINE_THICKNESS 3
|
||||||
#define BLOCK_SIZE 32
|
#define BLOCK_SIZE 32
|
||||||
@@ -86,7 +87,7 @@ static void freeImageData(img_data_t* img) {
|
|||||||
free(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;
|
int code = 1;
|
||||||
img_data_t* img = gridToImageData(g);
|
img_data_t* img = gridToImageData(g);
|
||||||
FILE *f = NULL;
|
FILE *f = NULL;
|
||||||
13
lib/maze.c
Normal file
13
lib/maze.c
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include <mazemaker.h>
|
||||||
|
#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);
|
||||||
|
}
|
||||||
18
lib/path.h
Normal file
18
lib/path.h
Normal file
@@ -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)
|
||||||
11
mazemaker.pc.cmake
Normal file
11
mazemaker.pc.cmake
Normal file
@@ -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}
|
||||||
@@ -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)
|
|
||||||
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)
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <popt.h>
|
#include <popt.h>
|
||||||
#include "grid.h"
|
#include <mazemaker.h>
|
||||||
#include "image.h"
|
|
||||||
#include "prim.h"
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
char c;
|
char c;
|
||||||
@@ -37,12 +35,9 @@ int main(int argc, char* argv[]) {
|
|||||||
fprintf(stderr, "An output filename is required.\n");
|
fprintf(stderr, "An output filename is required.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
mazegrid_t g = mazegrid_new(width, height), maze;
|
mazegrid_t maze;
|
||||||
mazegrid_randomize(&g);
|
mazemaker_generate_maze(width, height, &maze);
|
||||||
//mazegrid_print(&g, stdout);
|
mazemaker_maze_to_png(&maze, filename);
|
||||||
prim(&g, &maze);
|
mazemaker_free_maze(&maze);
|
||||||
writePNG(&maze, filename);
|
|
||||||
mazegrid_free(&g);
|
|
||||||
mazegrid_free(&maze);
|
|
||||||
poptFreeContext(ctx);
|
poptFreeContext(ctx);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user