Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d5475ba2de | |||
| 8f88b7ee30 | |||
| 70f164daee | |||
| aa4d8efdd2 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -10,4 +10,8 @@ install_manifest.txt
|
||||
mazemaker
|
||||
*.tar.gz
|
||||
*.png
|
||||
*.pc
|
||||
*.a
|
||||
*.so
|
||||
*.so.*
|
||||
logs
|
||||
|
||||
@@ -1,37 +1,19 @@
|
||||
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.2)
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
// the configured options and settings for Tutorial
|
||||
#define VERSION_MAJOR @VERSION_MAJOR@
|
||||
#define VERSION_MINOR @VERSION_MINOR@
|
||||
30
include/mazemaker.h
Normal file
30
include/mazemaker.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#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);
|
||||
int mazemaker_maze_to_png_mem(mazegrid_t const* maze, size_t* len, uint8_t** buf);
|
||||
|
||||
#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
|
||||
#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);
|
||||
@@ -4,7 +4,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <png.h>
|
||||
#include <string.h>
|
||||
#include "image.h"
|
||||
#include <mazemaker.h>
|
||||
#include "grid.h"
|
||||
|
||||
#define LINE_THICKNESS 3
|
||||
#define BLOCK_SIZE 32
|
||||
@@ -15,6 +16,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));
|
||||
@@ -86,7 +92,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;
|
||||
@@ -149,3 +155,76 @@ exit:
|
||||
if (img != NULL) freeImageData(img);
|
||||
return code;
|
||||
}
|
||||
|
||||
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(mazegrid_t const* g, size_t* len, uint8_t** buf) {
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
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 <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;
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user