5 Commits
v1.0 ... v1.3

Author SHA1 Message Date
e05724ea53 Add color options, bump minor version 2021-05-09 18:20:46 -04:00
d5475ba2de Bump minor version 2021-05-08 17:53:25 -04:00
8f88b7ee30 Add memory buffer output option 2021-05-08 17:52:32 -04:00
70f164daee Bump version, add dist archive rule 2021-05-08 16:03:37 -04:00
aa4d8efdd2 Reorganize code into library for reuse 2021-05-08 15:58:59 -04:00
22 changed files with 335 additions and 81 deletions

4
.gitignore vendored
View File

@@ -10,4 +10,8 @@ install_manifest.txt
mazemaker
*.tar.gz
*.png
*.pc
*.a
*.so
*.so.*
logs

View File

@@ -1,37 +1,21 @@
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.3)
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)
link_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/lib)
add_subdirectory(lib)
add_subdirectory(utils)
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/mazemaker.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
ADD_CUSTOM_TARGET(dist
rm -rf "mazemaker-${PROJECT_VERSION}"
COMMAND git archive --prefix="mazemaker-${PROJECT_VERSION}/" v${PROJECT_VERSION} | gzip -9 > mazemaker-${PROJECT_VERSION}.tar.gz
)
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)

View File

@@ -1,3 +0,0 @@
// the configured options and settings for Tutorial
#define VERSION_MAJOR @VERSION_MAJOR@
#define VERSION_MINOR @VERSION_MINOR@

39
include/mazemaker.h Normal file
View File

@@ -0,0 +1,39 @@
#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;
typedef struct mazeoptions mazeoptions_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);
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);
#endif // !def(_MAZEMAKER_H)

35
lib/CMakeLists.txt Normal file
View File

@@ -0,0 +1,35 @@
project(mazemaker_lib)
cmake_minimum_required (VERSION 2.8.12)
set (mazemaker_SOVERSION_CURRENT 0)
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}")
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})

View File

@@ -1,26 +1,8 @@
#ifndef _GRID_H
#define _GRID_H 1
#include <mazemaker.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);
void mazegrid_free(mazegrid_t* g);

View File

@@ -4,7 +4,9 @@
#include <stdlib.h>
#include <png.h>
#include <string.h>
#include "image.h"
#include <mazemaker.h>
#include "grid.h"
#include "options.h"
#define LINE_THICKNESS 3
#define BLOCK_SIZE 32
@@ -15,6 +17,11 @@ typedef struct {
int w, h;
} img_data_t;
struct mem_encode {
uint8_t *buffer;
size_t size;
};
static void setPixel(img_data_t* img, int x, int y, char v) {
//fprintf(stderr, "setPixel(img, %d, %d, %d)\n", x, y, v);
assert((x < img->w) && (y < img->h));
@@ -37,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
@@ -65,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);
@@ -86,7 +97,7 @@ static void freeImageData(img_data_t* img) {
free(img);
}
int writePNG(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;
@@ -133,7 +144,7 @@ int writePNG(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);
@@ -149,3 +160,90 @@ exit:
if (img != NULL) freeImageData(img);
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;
if (p->buffer)
p->buffer = realloc(p->buffer, nsize);
else
p->buffer = malloc(nsize);
if (!p->buffer)
png_error(png_ptr, "Write error");
memcpy(p->buffer + p->size, data, length);
p->size += length;
}
static void png_no_flush(png_structp png_ptr) {
/* noop */
}
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;
png_infop info_ptr = NULL;
struct mem_encode menc = { .buffer = NULL, .size = 0 };
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fprintf(stderr, "Could not allocate PNG write struct.\n");
code = 0;
goto exit;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fprintf(stderr, "Could not allocate PNG info struct.\n");
code = 0;
goto exit;
}
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "Error during PNG creation.\n");
code = 0;
goto exit;
}
png_set_write_fn(png_ptr, &menc, append_png_data, png_no_flush);
// Write header (8 bit color depth)
png_set_IHDR(png_ptr, info_ptr, img->w, img->h,
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
writeImageData(img, png_ptr, options);
png_write_end(png_ptr, NULL);
*len = menc.size;
*buf = menc.buffer;
exit:
if (!code && menc.buffer) {
free(menc.buffer);
}
if (info_ptr != NULL) {
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
png_destroy_info_struct(png_ptr, &info_ptr);
}
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
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;
}

13
lib/maze.c Normal file
View 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);
}

55
lib/options.c Normal file
View File

@@ -0,0 +1,55 @@
#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);
return result;
}
void mazemaker_options_free(mazeoptions_t* options) {
free(options);
}
static int stringToColor(char const* color_desc, rgb_color_t dst) {
if (color_desc[0] != '#') return -1; // bad format
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);
}

13
lib/options.h Normal file
View File

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

18
lib/path.h Normal file
View 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)

View File

View File

11
mazemaker.pc.cmake Normal file
View 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}

View File

@@ -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)

10
utils/CMakeLists.txt Normal file
View File

@@ -0,0 +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 PUBLIC mazemaker_shared ${POPT_LIBRARIES})
target_include_directories(mazemaker PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include PUBLIC ${POPT_INCLUDE_DIRS})
target_compile_options(mazemaker PUBLIC ${POPT_CFLAGS_OTHER})
install (TARGETS mazemaker DESTINATION bin)

View File

@@ -1,19 +1,21 @@
#include <stdio.h>
#include <stdlib.h>
#include <popt.h>
#include "grid.h"
#include "image.h"
#include "prim.h"
#include <mazemaker.h>
int main(int argc, char* argv[]) {
char c;
int width = 0, height = 0;
char const *filename = NULL;
char const *filename = NULL, *fg_color = NULL, *bg_color = 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" },
POPT_AUTOHELP
{ NULL, 0, 0, NULL, 0 }
};
@@ -37,12 +39,13 @@ 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;
mazeoptions_t* options = mazemaker_options_new();
if (fg_color != NULL) mazemaker_options_set_wall_color(options, fg_color);
if (bg_color != NULL) mazemaker_options_set_background_color(options, bg_color);
mazemaker_generate_maze(width, height, &maze);
mazemaker_maze_to_png_opt(&maze, filename, options);
mazemaker_free_maze(&maze);
mazemaker_options_free(options);
poptFreeContext(ctx);
}