9 Commits

Author SHA1 Message Date
0272fd726d Add option to set random seed for deterministic behavior
Bump version to 1.6
2022-01-15 14:40:21 -05:00
476aa4bce6 Remove useless quotation marks 2021-11-29 19:40:54 -05:00
632746d933 Ignore ninja artifacts 2021-11-29 19:38:59 -05:00
5c0eddb2db MacOS compatibility 2021-11-29 19:18:58 -05:00
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
e05724ea53 Add color options, bump minor version 2021-05-09 18:20:46 -04:00
13 changed files with 242 additions and 33 deletions

4
.gitignore vendored
View File

@@ -8,6 +8,7 @@ cmake_install.cmake
config.h
install_manifest.txt
mazemaker
*.dylib
*.tar.gz
*.png
*.pc
@@ -15,3 +16,6 @@ mazemaker
*.so
*.so.*
logs
.ninja_deps
.ninja_log
build.ninja

View File

@@ -1,13 +1,18 @@
cmake_minimum_required (VERSION 3.0)
cmake_policy(VERSION 3.0)
project (mazemaker VERSION 1.2)
project (mazemaker VERSION 1.6)
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)
set(CMAKE_MACOSX_RPATH 1)
set(CMAKE_INSTALL_RPATH "${LIB_INSTALL_DIR}")
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/mazemaker.pc.cmake ${CMAKE_CURRENT_BINARY_DIR}/mazemaker.pc)
link_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/lib)
add_subdirectory(lib)
add_subdirectory(utils)

View File

@@ -22,9 +22,20 @@ typedef enum {
EDGE_LEFT
} mazeedge_dir_t;
typedef struct mazeoptions mazeoptions_t;
void mazemaker_generate_maze(int width, int height, mazegrid_t* result);
void mazemaker_generate_maze_opt(int width, int height, mazegrid_t* result, mazeoptions_t const*);
void mazemaker_free_maze(mazegrid_t* maze);
int mazemaker_maze_to_png(mazegrid_t const* maze, char const* filename);
int mazemaker_maze_to_png_opt(mazegrid_t const* maze, char const* filename, mazeoptions_t const*);
int mazemaker_maze_to_png_mem(mazegrid_t const* maze, size_t* len, uint8_t** buf);
int mazemaker_maze_to_png_mem_opt(mazegrid_t const* maze, size_t* len, uint8_t** buf, mazeoptions_t const*);
mazeoptions_t* mazemaker_options_new();
void mazemaker_options_free(mazeoptions_t*);
int mazemaker_options_set_wall_color(mazeoptions_t*, char const* color_desc);
int mazemaker_options_set_background_color(mazeoptions_t*, char const* color_desc);
void mazemaker_options_set_seed(mazeoptions_t*, unsigned int seed);
#endif // !def(_MAZEMAKER_H)

View File

@@ -1,7 +1,10 @@
project(mazemaker_lib)
cmake_minimum_required (VERSION 2.8.12)
include(CheckSymbolExists)
set (mazemaker_SOVERSION_CURRENT 0)
set (mazemaker_SOVERSION_REVISION 1)
set (mazemaker_SOVERSION_REVISION 2)
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}")
@@ -11,6 +14,12 @@ set (mazemaker_SOVERSION ${mazemaker_SOVERSION_MAJOR}.${mazemaker_SOVERSION_MINO
find_package(PkgConfig REQUIRED)
pkg_search_module(PNG REQUIRED libpng)
check_symbol_exists(arc4random_uniform "stdlib.h" HAVE_ARC4RANDOM)
check_symbol_exists(srand_deterministic "stdlib.h" HAVE_SRAND_DETERMINISTIC)
configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/config.h.in"
"${CMAKE_CURRENT_SOURCE_DIR}/config.h" )
file(GLOB SOURCES *.c *.h)
add_library(mazemaker_shared SHARED ${SOURCES})
@@ -27,9 +36,7 @@ set_target_properties(mazemaker_static PROPERTIES
)
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})
target_link_libraries(mazemaker_shared PUBLIC ${PNG_LIBRARIES};m)
target_link_directories(mazemaker_shared PUBLIC ${PNG_LIBRARY_DIRS})
target_include_directories(mazemaker_shared PUBLIC ${PNG_INCLUDE_DIRS};../include)
target_include_directories(mazemaker_static PUBLIC ${PNG_INCLUDE_DIRS};../include)

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

@@ -0,0 +1,2 @@
#cmakedefine HAVE_ARC4RANDOM @HAVE_ARC4RANDOM@
#cmakedefine HAVE_SRAND_DETERMINISTIC @HAVE_SRAND_DETERMINISTIC@

View File

@@ -1,5 +1,7 @@
#include <stdlib.h>
#include "grid.h"
#include "options.h"
#include "config.h"
#define MAX_EDGE_WEIGHT 100
#define TOP_LEFT_CORNER "\xe2\x94\x8f"
@@ -18,6 +20,25 @@
#define HORIZONTAL_HALF_LEFT "\xe2\x95\xb8"
#define HORIZONTAL_HALF_RIGHT "\xe2\x95\xba"
static edgeweight_t random_edgeweight(mazeoptions_t const* options) {
if (!options->seed_set) {
#ifdef HAVE_ARC4RANDOM
return arc4random_uniform(MAX_EDGE_WEIGHT);
#else // HAVE_ARC4RANDOM
return random()%MAX_EDGE_WEIGHT;
#endif // HAVE_ARC4RANDOM
} else {
// use deterministic random number generator
return rand()%MAX_EDGE_WEIGHT;
}
}
#ifdef HAVE_SRAND_DETERMINISTIC
#define SRAND(x) (srand_deterministic(x))
#else
#define SRAND(x) (srand(x))
#endif // defined(HAVE_SRAND_DETERMINISTIC)
mazegrid_t mazegrid_new(size_t width, size_t height) {
mazeedges_t** grid = calloc(height, sizeof(mazeedges_t*));
for (size_t i = 0; i < height; i++) {
@@ -63,11 +84,16 @@ edgeweight_t mazegrid_get_edge(mazegrid_t const* g, size_t x, size_t y, mazeedge
else return g->grid[y][x].up;
}
void mazegrid_randomize(mazegrid_t* g) {
void mazegrid_randomize(mazegrid_t* g, mazeoptions_t const* options) {
// initialize random system
if (options->seed_set) {
SRAND(options->seed);
}
for (size_t i = 0; i < g->height; i++) {
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 (j < g->width - 1) g->grid[i][j].right = (edgeweight_t)(random()%MAX_EDGE_WEIGHT);
if (i < g->height - 1) g->grid[i][j].up = random_edgeweight(options);
if (j < g->width - 1) g->grid[i][j].right = random_edgeweight(options);
}
}
}

View File

@@ -10,7 +10,7 @@ void mazegrid_free(mazegrid_t* g);
int mazegrid_set_edge(mazegrid_t* g, size_t x, size_t y, mazeedge_dir_t dir, edgeweight_t wt);
edgeweight_t mazegrid_get_edge(mazegrid_t const* g, size_t, size_t y, mazeedge_dir_t dir);
void mazegrid_randomize(mazegrid_t* g);
void mazegrid_randomize(mazegrid_t* g, mazeoptions_t const* options);
void mazegrid_uniform(mazegrid_t* g);
void mazegrid_print(mazegrid_t const* g, FILE * f);

View File

@@ -6,6 +6,7 @@
#include <string.h>
#include <mazemaker.h>
#include "grid.h"
#include "options.h"
#define LINE_THICKNESS 3
#define BLOCK_SIZE 32
@@ -43,7 +44,7 @@ static img_data_t* gridToImageData(mazegrid_t const* g) {
result->data = malloc(result->w * result->h);
//fprintf(stderr, "Box dimensions: %d x %d\n", result->w, result->h);
// white background
// set background
memset(result->data, 0, result->w * result->h);
// draw basic outline
@@ -71,16 +72,20 @@ static img_data_t* gridToImageData(mazegrid_t const* g) {
return result;
}
static void writeImageData(img_data_t const* img, png_structp png_ptr) {
static void writeImageData(img_data_t const* img, png_structp png_ptr, mazeoptions_t const* options) {
for (int i = 0; i < img->h; i++) {
png_byte row[3 * img->w];
for (int j = 0; j < img->w; j++) {
if (img->data[i * img->w + j]) {
// black
row[3 * j] = row[3 * j + 1] = row[3 * j + 2] = 0;
// wall color
row[3 * j] = options->wall_color[0];
row[3 * j + 1] = options->wall_color[1];
row[3 * j + 2] = options->wall_color[2];
} else {
// white
row[3 * j] = row[3 * j + 1] = row[3 * j + 2] = 255;
// background
row[3 * j] = options->background_color[0];
row[3 * j + 1] = options->background_color[1];
row[3 * j + 2] = options->background_color[2];
}
}
png_write_row(png_ptr, row);
@@ -92,7 +97,7 @@ static void freeImageData(img_data_t* img) {
free(img);
}
int mazemaker_maze_to_png(mazegrid_t const* g, char const* filename) {
int mazemaker_maze_to_png_opt(mazegrid_t const* g, char const* filename, mazeoptions_t const* options) {
int code = 1;
img_data_t* img = gridToImageData(g);
FILE *f = NULL;
@@ -139,7 +144,7 @@ int mazemaker_maze_to_png(mazegrid_t const* g, char const* filename) {
png_write_info(png_ptr, info_ptr);
writeImageData(img, png_ptr);
writeImageData(img, png_ptr, options);
png_write_end(png_ptr, NULL);
@@ -156,6 +161,13 @@ exit:
return code;
}
int mazemaker_maze_to_png(mazegrid_t const* g, char const* filename) {
mazeoptions_t* options = mazemaker_options_new(); // use default options
int result = mazemaker_maze_to_png_opt(g, filename, options);
mazemaker_options_free(options);
return result;
}
static void append_png_data(png_structp png_ptr, png_bytep data, png_size_t length) {
struct mem_encode* p = (struct mem_encode*)png_get_io_ptr(png_ptr);
size_t nsize = p->size + length;
@@ -173,7 +185,7 @@ static void png_no_flush(png_structp png_ptr) {
/* noop */
}
int mazemaker_maze_to_png_mem(mazegrid_t const* g, size_t* len, uint8_t** buf) {
int mazemaker_maze_to_png_mem_opt(mazegrid_t const* g, size_t* len, uint8_t** buf, mazeoptions_t const* options) {
int code = 1;
img_data_t* img = gridToImageData(g);
png_structp png_ptr = NULL;
@@ -209,7 +221,7 @@ int mazemaker_maze_to_png_mem(mazegrid_t const* g, size_t* len, uint8_t** buf) {
png_write_info(png_ptr, info_ptr);
writeImageData(img, png_ptr);
writeImageData(img, png_ptr, options);
png_write_end(png_ptr, NULL);
@@ -228,3 +240,10 @@ exit:
if (img != NULL) freeImageData(img);
return code;
}
int mazemaker_maze_to_png_mem(mazegrid_t const* g, size_t* len, uint8_t** buf) {
mazeoptions_t* options = mazemaker_options_new(); // use default options
int result = mazemaker_maze_to_png_mem_opt(g, len, buf, options);
mazemaker_options_free(options);
return result;
}

View File

@@ -1,13 +1,21 @@
#include <mazemaker.h>
#include <stdlib.h>
#include "grid.h"
#include "prim.h"
void mazemaker_generate_maze(int width, int height, mazegrid_t* result) {
void mazemaker_generate_maze_opt(int width, int height, mazegrid_t* result, mazeoptions_t const* options) {
mazegrid_t g = mazegrid_new(width, height);
mazegrid_randomize(&g);
mazegrid_randomize(&g, options);
prim(&g, result);
mazegrid_free(&g);
}
void mazemaker_generate_maze(int width, int height, mazegrid_t* result) {
mazeoptions_t* options = mazemaker_options_new(); // use defaults
mazemaker_generate_maze_opt(width, height, result, options);
mazemaker_options_free(options);
}
void mazemaker_free_maze(mazegrid_t* maze) {
mazegrid_free(maze);
}

86
lib/options.c Normal file
View File

@@ -0,0 +1,86 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "options.h"
mazeoptions_t* mazemaker_options_new() {
mazeoptions_t* result = malloc(sizeof(mazeoptions_t));
// set defaults
memset(result->wall_color, 0, 3);
memset(result->background_color, 0xff, 3);
result->seed = 0;
result->seed_set = false;
return result;
}
void mazemaker_options_free(mazeoptions_t* 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) {
if (color_desc[0] != '#') return interpretColorName(color_desc, dst);
size_t l;
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
char x2[3] = { 0, 0, 0 };
if (l == 4) {
x2[0] = x2[1] = color_desc[1];
sscanf(x2, "%hhx", &dst[0]);
x2[0] = x2[1] = color_desc[2];
sscanf(x2, "%hhx", &dst[1]);
x2[0] = x2[1] = color_desc[3];
sscanf(x2, "%hhx", &dst[2]);
} else {
strncpy(x2, color_desc + 1, 2);
sscanf(x2, "%hhx", &dst[0]);
strncpy(x2, color_desc + 3, 2);
sscanf(x2, "%hhx", &dst[1]);
strncpy(x2, color_desc + 5, 2);
sscanf(x2, "%hhx", &dst[2]);
}
return 0;
}
int mazemaker_options_set_wall_color(mazeoptions_t* options, char const* color_desc) {
return stringToColor(color_desc, options->wall_color);
}
int mazemaker_options_set_background_color(mazeoptions_t* options, char const* color_desc) {
return stringToColor(color_desc, options->background_color);
}
void mazemaker_options_set_seed(mazeoptions_t* options, rand_seed_t seed) {
options->seed = seed;
options->seed_set = true;
}

17
lib/options.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef _OPTIONS_H
#define _OPTIONS_H 1
#include <stdint.h>
#include <stdbool.h>
typedef uint8_t rgb_color_t[3];
typedef unsigned int rand_seed_t;
typedef struct mazeoptions {
rgb_color_t wall_color;
rgb_color_t background_color;
rand_seed_t seed;
bool seed_set;
} mazeoptions_t;
#endif // !def(_OPTIONS_H)

View File

@@ -1,11 +1,10 @@
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})
include_directories("../include" ${POPT_INCLUDE_DIRS})
add_executable(mazemaker mazemaker.c)
target_link_directories(mazemaker PUBLIC ${POPT_LIBRARY_DIRS})
target_link_libraries(mazemaker mazemaker_shared ${POPT_LIBRARIES})
install (TARGETS mazemaker DESTINATION bin)

View File

@@ -6,12 +6,19 @@
int main(int argc, char* argv[]) {
char c;
int width = 0, height = 0;
char const *filename = NULL;
unsigned int seed = 0;
char const *filename = NULL, *fg_color = NULL, *bg_color = NULL, *seed_str = 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" },
{ "seed", 's', POPT_ARG_STRING, &seed_str, 0,
"Random seed", "SEED" },
POPT_AUTOHELP
{ NULL, 0, 0, NULL, 0 }
};
@@ -36,8 +43,26 @@ int main(int argc, char* argv[]) {
return 1;
}
mazegrid_t maze;
mazemaker_generate_maze(width, height, &maze);
mazemaker_maze_to_png(&maze, filename);
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 (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);
mazemaker_free_maze(&maze);
mazemaker_options_free(options);
poptFreeContext(ctx);
}