4 Commits
v1.3 ... v1.5

Author SHA1 Message Date
776def6948 Use arc4random if available 2021-11-23 21:47:15 -05:00
00db3de911 Add dir for popt library 2021-10-05 23:30:46 -04:00
49b62ee7bd MacOS compatibility 2021-10-05 23:30:32 -04:00
ddf2bc5b42 Color names, sort out cmake wackiness 2021-05-09 22:26:40 -04:00
8 changed files with 70 additions and 18 deletions

1
.gitignore vendored
View File

@@ -8,6 +8,7 @@ cmake_install.cmake
config.h config.h
install_manifest.txt install_manifest.txt
mazemaker mazemaker
*.dylib
*.tar.gz *.tar.gz
*.png *.png
*.pc *.pc

View File

@@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.0) cmake_minimum_required (VERSION 3.0)
cmake_policy(VERSION 3.0) cmake_policy(VERSION 3.0)
project (mazemaker VERSION 1.3) project (mazemaker VERSION 1.5)
SET(EXEC_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} CACHE PATH "Installation prefix for executables and object code libraries" FORCE) 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(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include CACHE PATH "Installation prefix for C header files" FORCE)

View File

@@ -1,5 +1,8 @@
project(mazemaker_lib) project(mazemaker_lib)
cmake_minimum_required (VERSION 2.8.12) cmake_minimum_required (VERSION 2.8.12)
include(CheckSymbolExists)
set (mazemaker_SOVERSION_CURRENT 0) set (mazemaker_SOVERSION_CURRENT 0)
set (mazemaker_SOVERSION_REVISION 2) set (mazemaker_SOVERSION_REVISION 2)
set (mazemaker_SOVERSION_AGE 0) set (mazemaker_SOVERSION_AGE 0)
@@ -11,6 +14,11 @@ set (mazemaker_SOVERSION ${mazemaker_SOVERSION_MAJOR}.${mazemaker_SOVERSION_MINO
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
pkg_search_module(PNG REQUIRED libpng) pkg_search_module(PNG REQUIRED libpng)
check_symbol_exists(arc4random_uniform "stdlib.h" HAVE_ARC4RANDOM)
configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/config.h.in"
"${CMAKE_CURRENT_SOURCE_DIR}/config.h" )
file(GLOB SOURCES *.c *.h) file(GLOB SOURCES *.c *.h)
add_library(mazemaker_shared SHARED ${SOURCES}) add_library(mazemaker_shared SHARED ${SOURCES})
@@ -27,9 +35,7 @@ set_target_properties(mazemaker_static PROPERTIES
) )
install (TARGETS mazemaker_shared mazemaker_static DESTINATION ${LIB_INSTALL_DIR}) install (TARGETS mazemaker_shared mazemaker_static DESTINATION ${LIB_INSTALL_DIR})
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/../include/mazemaker.h DESTINATION ${INCLUDE_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_link_libraries(mazemaker_shared PUBLIC ${PNG_LIBRARIES};m)
target_include_directories(mazemaker_shared PUBLIC ${PNG_INCLUDE_DIRS} ../include) target_link_directories(mazemaker_shared PUBLIC ${PNG_LIBRARY_DIRS})
target_include_directories(mazemaker_static PUBLIC ${PNG_INCLUDE_DIRS} ../include) target_include_directories(mazemaker_shared PUBLIC ${PNG_INCLUDE_DIRS};../include)
target_compile_options(mazemaker_shared PUBLIC ${PNG_CFLAGS_OTHER}) target_include_directories(mazemaker_static PUBLIC ${PNG_INCLUDE_DIRS};../include)
target_compile_options(mazemaker_static PUBLIC ${PNG_CFLAGS_OTHER})
target_link_options(mazemaker_shared PUBLIC -L${PNG_LIBDIR})

1
lib/config.h.in Normal file
View File

@@ -0,0 +1 @@
#cmakedefine HAVE_ARC4RANDOM "@HAVE_ARC4RANDOM@"

View File

@@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include "grid.h" #include "grid.h"
#include "config.h"
#define MAX_EDGE_WEIGHT 100 #define MAX_EDGE_WEIGHT 100
#define TOP_LEFT_CORNER "\xe2\x94\x8f" #define TOP_LEFT_CORNER "\xe2\x94\x8f"
@@ -18,6 +19,14 @@
#define HORIZONTAL_HALF_LEFT "\xe2\x95\xb8" #define HORIZONTAL_HALF_LEFT "\xe2\x95\xb8"
#define HORIZONTAL_HALF_RIGHT "\xe2\x95\xba" #define HORIZONTAL_HALF_RIGHT "\xe2\x95\xba"
static edgeweight_t random_edgeweight() {
#ifdef HAVE_ARC4RANDOM
return arc4random_uniform(MAX_EDGE_WEIGHT);
#else // HAVE_ARC4RANDOM
return random()%MAX_EDGE_WEIGHT;
#endif // HAVE_ARC4RANDOM
}
mazegrid_t mazegrid_new(size_t width, size_t height) { mazegrid_t mazegrid_new(size_t width, size_t height) {
mazeedges_t** grid = calloc(height, sizeof(mazeedges_t*)); mazeedges_t** grid = calloc(height, sizeof(mazeedges_t*));
for (size_t i = 0; i < height; i++) { for (size_t i = 0; i < height; i++) {
@@ -66,8 +75,8 @@ edgeweight_t mazegrid_get_edge(mazegrid_t const* g, size_t x, size_t y, mazeedge
void mazegrid_randomize(mazegrid_t* g) { void mazegrid_randomize(mazegrid_t* g) {
for (size_t i = 0; i < g->height; i++) { for (size_t i = 0; i < g->height; i++) {
for (size_t j = 0; j < g->width; j++) { for (size_t j = 0; j < g->width; j++) {
if (i < g->height - 1) g->grid[i][j].up = (edgeweight_t)(random()%MAX_EDGE_WEIGHT); if (i < g->height - 1) g->grid[i][j].up = random_edgeweight();
if (j < g->width - 1) g->grid[i][j].right = (edgeweight_t)(random()%MAX_EDGE_WEIGHT); if (j < g->width - 1) g->grid[i][j].right = random_edgeweight();
} }
} }
} }

View File

@@ -18,15 +18,40 @@ void mazemaker_options_free(mazeoptions_t* options) {
free(options); free(options);
} }
static int interpretColorName(char const* color_desc, rgb_color_t dst) {
if (0 == strcasecmp(color_desc, "black")) { dst[0] = dst[1] = dst[2] = 0; }
else if (0 == strcasecmp(color_desc, "white")) { dst[0] = dst[1] = dst[2] = 0xff; }
else if (0 == strcasecmp(color_desc, "red")) { dst[0] = 0xff; dst[1] = dst[2] = 0; }
else if (0 == strcasecmp(color_desc, "blue")) { dst[0] = dst[1] = 0; dst[2] = 0xff; }
else if (0 == strcasecmp(color_desc, "green")) { dst[0] = dst[2] = 0; dst[1] = 0xff; }
else if (0 == strcasecmp(color_desc, "yellow")) { dst[2] = 0; dst[0] = dst[1] = 0xff; }
else if ((0 == strcasecmp(color_desc, "magenta")) ||
(0 == strcasecmp(color_desc, "purple"))) { dst[1] = 0; dst[0] = dst[2] = 0xff; }
else if (0 == strcasecmp(color_desc, "cyan")) { dst[0] = 0; dst[1] = dst[2] = 0xff; }
else if (0 == strcasecmp(color_desc, "orange")) { dst[0] = 0xff; dst[1] = 0x7f; dst[2] = 0; }
else if (0 == strcasecmp(color_desc, "brown")) { dst[0] = 0x7f; dst[1] = 0x3f; dst[2] = 0; }
else if ((0 == strcasecmp(color_desc, "gray")) ||
(0 == strcasecmp(color_desc, "grey"))) { dst[0] = dst[1] = dst[2] = 0x7f; }
else if (0 == strcasecmp(color_desc, "darkblue")) { dst[0] = dst[1] = 0; dst[2] = 0x7f; }
else if (0 == strcasecmp(color_desc, "darkgreen")) { dst[0] = dst[2] = 0; dst[1] = 0x7f; }
else if (0 == strcasecmp(color_desc, "darkred")) { dst[0] = 0x7f; dst[1] = dst[2] = 0; }
else if (0 == strcasecmp(color_desc, "pink")) { dst[0] = 0xff; dst[1] = 0xa0; dst[2] = 0xbf; }
else if ((0 == strcasecmp(color_desc, "darkmagenta")) ||
(0 == strcasecmp(color_desc, "darkpurple"))) { dst[1] = 0; dst[0] = dst[2] = 0x7f; }
else if (0 == strcasecmp(color_desc, "darkcyan")) { dst[0] = 0; dst[1] = dst[2] = 0x7f; }
else return -1;
return 0;
}
static int stringToColor(char const* color_desc, rgb_color_t dst) { static int stringToColor(char const* color_desc, rgb_color_t dst) {
if (color_desc[0] != '#') return -1; // bad format if (color_desc[0] != '#') return interpretColorName(color_desc, dst);
size_t l; size_t l;
for (l = 1; color_desc[l] != '\0'; l++) if (!isxdigit(color_desc[l])) return -1; // bad format for (l = 1; color_desc[l] != '\0'; l++) if (!isxdigit(color_desc[l])) return -1; // bad format
if ((l != 4) && (l != 7)) return -1; // bad format if ((l != 4) && (l != 7)) return -1; // bad format
char x2[3] = { 0, 0, 0}; char x2[3] = { 0, 0, 0 };
if (l == 4) { if (l == 4) {
x2[0] = x2[1] = color_desc[1]; x2[0] = x2[1] = color_desc[1];
sscanf(x2, "%hhx", &dst[0]); sscanf(x2, "%hhx", &dst[0]);

View File

@@ -1,10 +1,10 @@
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
pkg_search_module(POPT REQUIRED popt) pkg_search_module(POPT REQUIRED popt)
add_executable(mazemaker) include_directories("../include" ${POPT_INCLUDE_DIRS})
target_sources(mazemaker PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/mazemaker.c)
target_link_libraries(mazemaker PUBLIC mazemaker_shared ${POPT_LIBRARIES}) add_executable(mazemaker mazemaker.c)
target_include_directories(mazemaker PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include PUBLIC ${POPT_INCLUDE_DIRS}) target_link_directories(mazemaker PUBLIC ${POPT_LIBRARY_DIRS})
target_compile_options(mazemaker PUBLIC ${POPT_CFLAGS_OTHER}) target_link_libraries(mazemaker mazemaker_shared ${POPT_LIBRARIES})
install (TARGETS mazemaker DESTINATION bin) install (TARGETS mazemaker DESTINATION bin)

View File

@@ -41,8 +41,18 @@ int main(int argc, char* argv[]) {
} }
mazegrid_t maze; mazegrid_t maze;
mazeoptions_t* options = mazemaker_options_new(); mazeoptions_t* options = mazemaker_options_new();
if (fg_color != NULL) mazemaker_options_set_wall_color(options, fg_color); if (fg_color != NULL) {
if (bg_color != NULL) mazemaker_options_set_background_color(options, bg_color); 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);
}
}
mazemaker_generate_maze(width, height, &maze); mazemaker_generate_maze(width, height, &maze);
mazemaker_maze_to_png_opt(&maze, filename, options); mazemaker_maze_to_png_opt(&maze, filename, options);
mazemaker_free_maze(&maze); mazemaker_free_maze(&maze);