diff --git a/build/lib/mazeforge/__init__.py b/build/lib/mazeforge/__init__.py index b117424..6ea655a 100644 --- a/build/lib/mazeforge/__init__.py +++ b/build/lib/mazeforge/__init__.py @@ -16,7 +16,7 @@ """ from .generator import generate from .base import Maze -from .lib import mazeforge_util +from .core.loader import mazeforge_util __version__ = "0.1.1" diff --git a/build/lib/mazeforge/core/loader.py b/build/lib/mazeforge/core/loader.py new file mode 100644 index 0000000..ac9698d --- /dev/null +++ b/build/lib/mazeforge/core/loader.py @@ -0,0 +1,166 @@ +# -*- coding: utf-8 -*- +import platform +import ctypes +import os + + +SHARED_OBJECT_FOLDER = "platform" +SHARED_OBJECT_NAME = "libmaze" + + +def load_library(): + system = platform.system() + machine = platform.machine() + + library_path = None + cwd = os.path.dirname(os.path.abspath(__file__)) + + if system == "Linux": + if "x86_64" in machine: + library_path = SHARED_OBJECT_FOLDER + "/linux/x86_64/" + SHARED_OBJECT_NAME + ".so" + elif "i686" in machine: + library_path = SHARED_OBJECT_FOLDER + "/linux/i686/" + SHARED_OBJECT_NAME + ".so" + elif "arm" in machine: + library_path = SHARED_OBJECT_FOLDER + "/linux/arm/" + SHARED_OBJECT_NAME + ".so" + elif "aarch64" in machine: + library_path = SHARED_OBJECT_FOLDER + "/linux/aarch64/" + SHARED_OBJECT_NAME + ".so" + elif system == "Darwin": + if "x86_64" in machine: + library_path = SHARED_OBJECT_FOLDER + "/macos/x86_64/" + SHARED_OBJECT_NAME + ".dylib" + else: + library_path = SHARED_OBJECT_FOLDER + "/macos/arm/" + SHARED_OBJECT_NAME + ".dylib" + elif system == "Windows": + if "AMD64" in machine or "x86_64" in machine: + library_path = SHARED_OBJECT_FOLDER + "/windows/x86_64/" + SHARED_OBJECT_NAME + ".dll" + else: + library_path = SHARED_OBJECT_FOLDER + "/windows/i686/" + SHARED_OBJECT_NAME + ".dll" + + if library_path: + try: + return ctypes.CDLL(os.path.join(cwd, library_path)) + except Exception as e: + raise RuntimeError("Error loading library at {}: {}".format(library_path, e)) + else: + raise RuntimeError("Unsupported platform: {} {}".format(system, machine)) + + +def configure_library(lib): + lib.c_print.argtypes = [] + lib.c_print.restype = None + + +mazeforge_util = load_library() +mazeforge_util.c_print.argtypes = [] +mazeforge_util.c_print.restype = None + +""" +0: + + +1: + │ + + +2: + +── + +3: + │ +──┘ +""" + +# print_maze +mazeforge_util.print_maze.argtypes = [ + ctypes.POINTER(ctypes.c_uint8), + ctypes.c_int, + ctypes.c_int +] +mazeforge_util.print_maze.restype = None +print_maze = lambda array, width, height: mazeforge_util.print_maze( + (ctypes.c_uint8 * (width * height))(*array), + width, + height +) + +# generate_maze +mazeforge_util.generate_maze.argtypes = [ + ctypes.POINTER(ctypes.c_uint8), + ctypes.c_int, + ctypes.c_int +] +mazeforge_util.generate_maze.restype = None +generate_maze = lambda array, width, height: mazeforge_util.generate_maze( + (ctypes.c_uint8 * (width * height))(*array), + width, + height +) + + +def array(*args): + return (ctypes.c_uint8 * len(args))(*args) + + +""" +mazeforge_util.print_maze.argtypes = [ctypes.POINTER(ctypes.POINTER(ctypes.c_int)), ctypes.c_int, ctypes.c_int] +mazeforge_util.print_maze.restype = None + +rows = 3 +cols = 4 + +# Create a 2D NumPy array +myArray = np.arange(rows * cols, dtype=np.int32).reshape((rows, cols)) + +# Convert the NumPy array to a pointer to pointer +array_pointer = (ctypes.POINTER(ctypes.c_int) * rows)() +for i in range(rows): + array_pointer[i] = myArray[i].ctypes.data_as(ctypes.POINTER(ctypes.c_int)) + +# Call the C function +mazeforge_util.print_maze(array_pointer, rows, cols) +""" + + +""" +# Function prototypes +graphics.c_init.argtypes = [ctypes.c_int, ctypes.c_int] +graphics.c_init.restype = ctypes.c_int + +graphics.c_update.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)] +graphics.c_update.restype = ctypes.POINTER(ctypes.c_uint8) + +graphics.c_quit.argtypes = [] +graphics.c_quit.restype = None + +graphics.c_info_max_tex_size = [] +graphics.c_info_max_tex_size = ctypes.c_int + +graphics.c_load_shader.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p), ctypes.POINTER(ctypes.c_char_p), ctypes.c_int] +graphics.c_load_shader.restype = ctypes.c_int +graphics.load_shader = lambda vertex, fragment, **variables: graphics.c_load_shader( + vertex.encode("utf-8"), fragment.encode("utf-8"), + (ctypes.c_char_p * len(variables))(*map(lambda v: v.encode("utf-8"), variables.keys())), + (ctypes.c_char_p * len(variables))(*map(lambda v: v.encode("utf-8"), variables.values())), + len(variables)) + +graphics.c_update_shader_value.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_void_p] +graphics.c_update_shader_value.restype = None +def p_update_shader_value(shader, index, value): + if isinstance(value, int): + c_value = ctypes.c_int(value) + elif isinstance(value, float): + c_value = ctypes.c_float(value) + elif isinstance(value, (list, tuple)): + if isinstance(value[0], int): + c_value = (ctypes.c_int * len(value))(*value) + elif isinstance(value[0], float): + c_value = (ctypes.c_float * len(value))(*value) + else: + raise ValueError("Invalid value %r" % value) + graphics.c_update_shader_value(shader, index, ctypes.byref(c_value)) +graphics.update_shader_value = p_update_shader_value + +graphics.activate_shader.argtypes = [ctypes.c_int] +graphics.activate_shader.restype = None + +""" \ No newline at end of file diff --git a/build/lib/mazeforge/core/platform/linux/aarch64/libmaze.so b/build/lib/mazeforge/core/platform/linux/aarch64/libmaze.so new file mode 100755 index 0000000..645a2f7 Binary files /dev/null and b/build/lib/mazeforge/core/platform/linux/aarch64/libmaze.so differ diff --git a/build/lib/mazeforge/core/platform/linux/arm/libmaze.so b/build/lib/mazeforge/core/platform/linux/arm/libmaze.so new file mode 100755 index 0000000..a0bc7ae Binary files /dev/null and b/build/lib/mazeforge/core/platform/linux/arm/libmaze.so differ diff --git a/build/lib/mazeforge/core/platform/linux/i686/libmaze.so b/build/lib/mazeforge/core/platform/linux/i686/libmaze.so new file mode 100755 index 0000000..c150cb6 Binary files /dev/null and b/build/lib/mazeforge/core/platform/linux/i686/libmaze.so differ diff --git a/build/lib/mazeforge/core/platform/linux/x86_64/libmaze.so b/build/lib/mazeforge/core/platform/linux/x86_64/libmaze.so new file mode 100755 index 0000000..8358924 Binary files /dev/null and b/build/lib/mazeforge/core/platform/linux/x86_64/libmaze.so differ diff --git a/build/lib/mazeforge/core/platform/macos/arm/libmaze.dylib b/build/lib/mazeforge/core/platform/macos/arm/libmaze.dylib new file mode 100755 index 0000000..d465012 Binary files /dev/null and b/build/lib/mazeforge/core/platform/macos/arm/libmaze.dylib differ diff --git a/build/lib/mazeforge/core/platform/macos/x86_64/libmaze.dylib b/build/lib/mazeforge/core/platform/macos/x86_64/libmaze.dylib new file mode 100755 index 0000000..ec02742 Binary files /dev/null and b/build/lib/mazeforge/core/platform/macos/x86_64/libmaze.dylib differ diff --git a/build/lib/mazeforge/core/platform/windows/i686/libmaze.dll b/build/lib/mazeforge/core/platform/windows/i686/libmaze.dll new file mode 100755 index 0000000..79900ee Binary files /dev/null and b/build/lib/mazeforge/core/platform/windows/i686/libmaze.dll differ diff --git a/build/lib/mazeforge/core/platform/windows/x86_64/libmaze.dll b/build/lib/mazeforge/core/platform/windows/x86_64/libmaze.dll new file mode 100755 index 0000000..9de9b7b Binary files /dev/null and b/build/lib/mazeforge/core/platform/windows/x86_64/libmaze.dll differ diff --git a/build/lib/mazeforge/maze.py b/build/lib/mazeforge/maze.py new file mode 100644 index 0000000..06a1444 --- /dev/null +++ b/build/lib/mazeforge/maze.py @@ -0,0 +1,13 @@ +# -*- coding: utf-8 -*- +from mazeforge.core.loader import generate_maze, print_maze + + +class Maze: + def __init__(self, width, height): + self.array = [-1] * width * height + + def print(self): + print_maze(self.array, self.width, self.height) + + def generate(self): + generate_maze(self.array, self.width, self.height) \ No newline at end of file diff --git a/compile.sh b/compile.sh index 101dc92..448e7a3 100755 --- a/compile.sh +++ b/compile.sh @@ -10,26 +10,17 @@ DIR_BASE="src/mazeforge/core" DIR_OUT="platform" DIR_SOURCE="scripts" LIB_NAME="maze" -COMPILER_WARNINGS="-w" # Default: -Wall - -GCC_LINUX_x86_64="/usr/local/bin/x86_64-unknown-linux-gnu-gcc" -GCC_LINUX_i686="/usr/local/bin/x86_64-unknown-linux-gnu-gcc" -GCC_LINUX_ARM="/usr/local/bin/x86_64-unknown-linux-gnu-gcc" -GCC_LINUX_AArch64="/usr/local/bin/x86_64-unknown-linux-gnu-gcc" -GCC_MACOS_x86_64="/usr/local/bin/x86_64-unknown-linux-gnu-gcc" -GCC_MACOS_ARM="/usr/local/bin/x86_64-unknown-linux-gnu-gcc" -GCC_WINDOWS_x86_64="/usr/local/bin/x86_64-unknown-linux-gnu-gcc" -GCC_WINDOWS_i686="/usr/local/bin/x86_64-unknown-linux-gnu-gcc" +COMPILER_WARNINGS="-w" # warnings: "-Wall" | no warnings: "-w" COMPILERS=( - ${GCC_LINUX_x86_64} - ${GCC_LINUX_i686} - ${GCC_LINUX_ARM} - ${GCC_LINUX_AArch64} - ${GCC_MACOS_x86_64} - ${GCC_MACOS_ARM} - ${GCC_WINDOWS_x86_64} - ${GCC_WINDOWS_i686} + "/usr/local/bin/x86_64-unknown-linux-gnu-gcc" + "/usr/local/bin/i686-unknown-linux-gnu-gcc" + "/usr/local/bin/arm-unknown-linux-gnueabi-gcc" + "/usr/local/bin/aarch64-unknown-linux-gnu-gcc" + "/usr/local/bin/x86_64-apple-darwin19-gcc-12" + "/usr/local/bin/arm-none-eabi-gcc" + "/usr/local/bin/x86_64-w64-mingw32-gcc" + "/usr/local/bin/i686-w64-mingw32-gcc" ) OUTPUT_DIRECTORIES=( @@ -68,80 +59,31 @@ done rm ${DIR_SOURCE}/generator.o ${DIR_SOURCE}/bst.o echo Done - exit 0 - - -# Create output directories -mkdir -p ${DIR_OUT}/linux/x86_64 -mkdir -p ${DIR_OUT}/linux/i686 -mkdir -p ${DIR_OUT}/linux/arm -mkdir -p ${DIR_OUT}/linux/aarch64 -mkdir -p ${DIR_OUT}/macos/x86_64 -mkdir -p ${DIR_OUT}/macos/arm -mkdir -p ${DIR_OUT}/windows/x86_64 -mkdir -p ${DIR_OUT}/windows/i686 - # Linux: # x86, 64-bit -echo Compiling for linux-x86_64 #/usr/local/bin/x86_64-unknown-linux-gnu-gcc main.c -shared -fPIC -o ${DIR_OUT}/linux/x86_64/lib${LIB_NAME}.so -compiler=${COMPILERS[0]} -${COMPILERS[0]} $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/generator.c -o ${DIR_SOURCE}/generator.o -${COMPILERS[0]} $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/bst.c -o ${DIR_SOURCE}/bst.o -${COMPILERS[0]} -shared -o ${DIR_OUT}/macos/x86_64/lib${LIB_NAME}.dylib ${DIR_SOURCE}/generator.o ${DIR_SOURCE}/bst.o # x86, 32-bit -echo Compiling for linux-i686 #/usr/local/bin/i686-unknown-linux-gnu-gcc main.c -shared -fPIC -o ${DIR_OUT}/linux/i686/lib${LIB_NAME}.so -$GCC_LINUX_i686 $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/generator.c -o ${DIR_SOURCE}/generator.o -$GCC_LINUX_i686 $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/bst.c -o ${DIR_SOURCE}/bst.o -$GCC_LINUX_i686 -shared -o ${DIR_OUT}/macos/x86_64/lib${LIB_NAME}.dylib ${DIR_SOURCE}/generator.o ${DIR_SOURCE}/bst.o # ARM -echo Compiling for linux-arm #/usr/local/bin/arm-unknown-linux-gnueabi-gcc main.c -shared -fPIC -o ${DIR_OUT}/linux/arm/lib${LIB_NAME}.so -$GCC_LINUX_ARM $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/bst.c -o ${DIR_SOURCE}/bst.o -$GCC_LINUX_ARM $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/generator.c -o ${DIR_SOURCE}/generator.o -$GCC_LINUX_ARM -shared -o ${DIR_OUT}/macos/x86_64/lib${LIB_NAME}.dylib ${DIR_SOURCE}/generator.o ${DIR_SOURCE}/bst.o # AArch64 -echo Compiling for linux-aarch64 #/usr/local/bin/aarch64-unknown-linux-gnu-gcc main.c -shared -fPIC -o ${DIR_OUT}/linux/aarch64/lib${LIB_NAME}.so -$GCC_LINUX_AArch64 $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/generator.c -o ${DIR_SOURCE}/generator.o -$GCC_LINUX_AArch64 $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/bst.c -o ${DIR_SOURCE}/bst.o -$GCC_LINUX_AArch64 -shared -o ${DIR_OUT}/macos/x86_64/lib${LIB_NAME}.dylib ${DIR_SOURCE}/generator.o ${DIR_SOURCE}/bst.o # macOS: # x86_64 -echo Compiling for macos-x86_64 #/usr/local/bin/x86_64-apple-darwin19-gcc-12 main.c -shared -fPIC -o ${DIR_OUT}/macos/x86_64/lib${LIB_NAME}.dylib -$GCC_MACOS_x86_64 $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/generator.c -o ${DIR_SOURCE}/generator.o -$GCC_MACOS_x86_64 $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/bst.c -o ${DIR_SOURCE}/bst.o -$GCC_MACOS_x86_64 -shared -o ${DIR_OUT}/macos/x86_64/lib${LIB_NAME}.dylib ${DIR_SOURCE}/generator.o ${DIR_SOURCE}/bst.o # ARM -echo Compiling for macos-arm #/usr/local/bin/arm-none-eabi-gcc main.c -shared -fPIC -o ${DIR_OUT}/macos/arm/lib${LIB_NAME}.dylib -$GCC_MACOS_ARM $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/generator.c -o ${DIR_SOURCE}/generator.o -$GCC_MACOS_ARM $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/bst.c -o ${DIR_SOURCE}/bst.o -$GCC_MACOS_ARM -shared -o ${DIR_OUT}/macos/x86_64/lib${LIB_NAME}.dylib ${DIR_SOURCE}/generator.o ${DIR_SOURCE}/bst.o # Windows: # x86_64-pc-windows-gnu (GNU toolchain, 64-bit) -echo Compiling for windows-x86_64 #/usr/local/bin/x86_64-w64-mingw32-gcc main.c -shared -fPIC -o ${DIR_OUT}/windows/x86_64/${LIB_NAME}.dll -$GCC_WINDOWS_x86_64 $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/generator.c -o ${DIR_SOURCE}/generator.o -$GCC_WINDOWS_x86_64 $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/bst.c -o ${DIR_SOURCE}/bst.o -$GCC_WINDOWS_x86_64 -shared -o ${DIR_OUT}/macos/x86_64/lib${LIB_NAME}.dylib ${DIR_SOURCE}/generator.o ${DIR_SOURCE}/bst.o # i686-pc-windows-gnu (GNU toolchain, 32-bit) -echo Compiling for windows-i686 #/usr/local/bin/i686-w64-mingw32-gcc main.c -shared -fPIC -o ${DIR_OUT}/windows/i686/${LIB_NAME}.dll -$GCC_WINDOWS_i686 $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/generator.c -o ${DIR_SOURCE}/generator.o -$GCC_WINDOWS_i686 $COMPILER_WARNINGS -fPIC -c ${DIR_SOURCE}/bst.c -o ${DIR_SOURCE}/bst.o -$GCC_WINDOWS_i686 -shared -o ${DIR_OUT}/macos/x86_64/lib${LIB_NAME}.dylib ${DIR_SOURCE}/generator.o ${DIR_SOURCE}/bst.o - -rm ${DIR_SOURCE}/generator.o ${DIR_SOURCE}/bst.o -echo Done \ No newline at end of file diff --git a/src/mazeforge.egg-info/SOURCES.txt b/src/mazeforge.egg-info/SOURCES.txt index 9441f57..7dda6fa 100644 --- a/src/mazeforge.egg-info/SOURCES.txt +++ b/src/mazeforge.egg-info/SOURCES.txt @@ -5,19 +5,19 @@ pyproject.toml src/mazeforge/__init__.py src/mazeforge/base.py src/mazeforge/generator.py -src/mazeforge/lib.py -src/mazeforge/test.py +src/mazeforge/maze.py src/mazeforge.egg-info/PKG-INFO src/mazeforge.egg-info/SOURCES.txt src/mazeforge.egg-info/dependency_links.txt src/mazeforge.egg-info/requires.txt src/mazeforge.egg-info/top_level.txt -src/mazeforge/lib/linux/aarch64/libmazeforgeutil.so -src/mazeforge/lib/linux/arm/libmazeforgeutil.so -src/mazeforge/lib/linux/i686/libmazeforgeutil.so -src/mazeforge/lib/linux/x86_64/libmazeforgeutil.so -src/mazeforge/lib/macos/arm/libmazeforgeutil.dylib -src/mazeforge/lib/macos/x86_64/libmazeforgeutil.dylib -src/mazeforge/lib/windows/i686/libmazeforgeutil.dll -src/mazeforge/lib/windows/x86_64/libmazeforgeutil.dll +src/mazeforge/core/loader.py +src/mazeforge/core/platform/linux/aarch64/libmaze.so +src/mazeforge/core/platform/linux/arm/libmaze.so +src/mazeforge/core/platform/linux/i686/libmaze.so +src/mazeforge/core/platform/linux/x86_64/libmaze.so +src/mazeforge/core/platform/macos/arm/libmaze.dylib +src/mazeforge/core/platform/macos/x86_64/libmaze.dylib +src/mazeforge/core/platform/windows/i686/libmaze.dll +src/mazeforge/core/platform/windows/x86_64/libmaze.dll tests/test_generator.py \ No newline at end of file diff --git a/src/mazeforge/core/loader.py b/src/mazeforge/core/loader.py index ee6ccc6..ac9698d 100644 --- a/src/mazeforge/core/loader.py +++ b/src/mazeforge/core/loader.py @@ -72,25 +72,34 @@ def configure_library(lib): # print_maze mazeforge_util.print_maze.argtypes = [ - ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_uint8), ctypes.c_int, ctypes.c_int ] mazeforge_util.print_maze.restype = None print_maze = lambda array, width, height: mazeforge_util.print_maze( - (ctypes.c_int * width * height)(*array), + (ctypes.c_uint8 * (width * height))(*array), width, height ) # generate_maze -mazeforge_util.print_maze.argtypes = [ - ctypes.POINTER(ctypes.c_int), +mazeforge_util.generate_maze.argtypes = [ + ctypes.POINTER(ctypes.c_uint8), ctypes.c_int, ctypes.c_int ] -mazeforge_util.print_maze.restype = None -print_maze = lambda array, width, height: mazeforge_util.print_maze((ctypes.c_int * 4)(*array), width, height) +mazeforge_util.generate_maze.restype = None +generate_maze = lambda array, width, height: mazeforge_util.generate_maze( + (ctypes.c_uint8 * (width * height))(*array), + width, + height +) + + +def array(*args): + return (ctypes.c_uint8 * len(args))(*args) + """ mazeforge_util.print_maze.argtypes = [ctypes.POINTER(ctypes.POINTER(ctypes.c_int)), ctypes.c_int, ctypes.c_int] diff --git a/src/mazeforge/core/platform/linux/aarch64/libmaze.so b/src/mazeforge/core/platform/linux/aarch64/libmaze.so index e65e450..645a2f7 100755 Binary files a/src/mazeforge/core/platform/linux/aarch64/libmaze.so and b/src/mazeforge/core/platform/linux/aarch64/libmaze.so differ diff --git a/src/mazeforge/core/platform/linux/arm/libmaze.so b/src/mazeforge/core/platform/linux/arm/libmaze.so index e65e450..a0bc7ae 100755 Binary files a/src/mazeforge/core/platform/linux/arm/libmaze.so and b/src/mazeforge/core/platform/linux/arm/libmaze.so differ diff --git a/src/mazeforge/core/platform/linux/i686/libmaze.so b/src/mazeforge/core/platform/linux/i686/libmaze.so index e65e450..c150cb6 100755 Binary files a/src/mazeforge/core/platform/linux/i686/libmaze.so and b/src/mazeforge/core/platform/linux/i686/libmaze.so differ diff --git a/src/mazeforge/core/platform/linux/x86_64/libmaze.so b/src/mazeforge/core/platform/linux/x86_64/libmaze.so index e65e450..8358924 100755 Binary files a/src/mazeforge/core/platform/linux/x86_64/libmaze.so and b/src/mazeforge/core/platform/linux/x86_64/libmaze.so differ diff --git a/src/mazeforge/core/platform/macos/arm/libmaze.dylib b/src/mazeforge/core/platform/macos/arm/libmaze.dylib index e65e450..d465012 100755 Binary files a/src/mazeforge/core/platform/macos/arm/libmaze.dylib and b/src/mazeforge/core/platform/macos/arm/libmaze.dylib differ diff --git a/src/mazeforge/core/platform/macos/x86_64/libmaze.dylib b/src/mazeforge/core/platform/macos/x86_64/libmaze.dylib index e65e450..ec02742 100755 Binary files a/src/mazeforge/core/platform/macos/x86_64/libmaze.dylib and b/src/mazeforge/core/platform/macos/x86_64/libmaze.dylib differ diff --git a/src/mazeforge/core/platform/windows/i686/libmaze.dll b/src/mazeforge/core/platform/windows/i686/libmaze.dll index e65e450..79900ee 100755 Binary files a/src/mazeforge/core/platform/windows/i686/libmaze.dll and b/src/mazeforge/core/platform/windows/i686/libmaze.dll differ diff --git a/src/mazeforge/core/platform/windows/x86_64/libmaze.dll b/src/mazeforge/core/platform/windows/x86_64/libmaze.dll index e65e450..9de9b7b 100755 Binary files a/src/mazeforge/core/platform/windows/x86_64/libmaze.dll and b/src/mazeforge/core/platform/windows/x86_64/libmaze.dll differ diff --git a/src/mazeforge/core/scripts/bst.c b/src/mazeforge/core/scripts/bst.c index c4be770..e93bfa5 100755 --- a/src/mazeforge/core/scripts/bst.c +++ b/src/mazeforge/core/scripts/bst.c @@ -7,19 +7,17 @@ Node* create_node(int value) { Node* node = (Node*)malloc(sizeof(Node)); node->value = value; - node->exists = NULL; + node->exists = true; node->lower = node->higher = NULL; return node; } // Public -// Creates and returns an empty binary search tree +// Create and returns an empty binary search tree BinaryTree* bst_create() { BinaryTree* tree = (BinaryTree*)malloc(sizeof(BinaryTree)); - if (tree == NULL) { - exit(EXIT_FAILURE); - } + if (tree == NULL) exit(EXIT_FAILURE); tree->root = NULL; return tree; } @@ -46,7 +44,7 @@ void recursive_insert(Node* node, int value) { } // Public -// Adds value to tree or returns true if value is already in tree +// Add value to tree or returns true if value is already in tree void bst_insert(BinaryTree* tree, int value) { if (tree->root == NULL) { Node* node = create_node(value); @@ -59,7 +57,6 @@ void bst_insert(BinaryTree* tree, int value) { void recursive_remove(Node* node, Node* parent, int value) { if (node->value == value) { - if (parent != NULL && node->lower == NULL && node->higher == NULL) { if (node->value == parent->lower->value) { parent->lower = NULL; @@ -80,6 +77,7 @@ void recursive_remove(Node* node, Node* parent, int value) { } // Public +// Remove a value from a binary search tree void bst_remove(BinaryTree* tree, int value) { if (tree->root == NULL) return; recursive_remove(tree->root, NULL, value); @@ -107,10 +105,8 @@ bool bst_contains(BinaryTree* tree, int value) { void recursive_print(const Node* node) { if (node == NULL) return; - printf("lower\n"); recursive_print(node->lower); if (node->exists == true) printf("%d\n", node->value); - printf("higher\n"); recursive_print(node->higher); } diff --git a/src/mazeforge/core/scripts/bst.h b/src/mazeforge/core/scripts/bst.h index 05dea66..e8be2e6 100755 --- a/src/mazeforge/core/scripts/bst.h +++ b/src/mazeforge/core/scripts/bst.h @@ -20,5 +20,6 @@ void bst_insert(BinaryTree* tree, int value); void bst_remove(BinaryTree* tree, int value); bool bst_contains(BinaryTree* tree, int value); void bst_print(BinaryTree* tree); +void bst_delete(BinaryTree* tree); #endif // BST_H diff --git a/src/mazeforge/core/scripts/generator.c b/src/mazeforge/core/scripts/generator.c index 0373f33..620ce2f 100644 --- a/src/mazeforge/core/scripts/generator.c +++ b/src/mazeforge/core/scripts/generator.c @@ -28,27 +28,54 @@ void print_maze(int8_t *array, int width, int height) { } -void add_adjacent_cells(int x, int y, int* adjacent_cells, int adjacent_cells_count, int8_t* array, int width, int height) { - if (x > 0) { - if (array[x - 1 + y * width] == -1) { +void add_adjacent_cells(int x, int y, BinaryTree* adjacent_cells, int8_t* array, int width, int height) { + int coord = x + y * width; + if (x > 0) { + if (array[coord - 1] == -1) { + bst_insert(adjacent_cells, coord - 1); + } + } + if (x < width - 1) { + if (array[coord + 1] == -1) { + bst_insert(adjacent_cells, coord + 1); + } + } + if (y > 0) { + if (array[coord - width] == -1) { + bst_insert(adjacent_cells, coord - width); + } + } + if (y < height - 1) { + if (array[coord + width] == -1) { + bst_insert(adjacent_cells, coord + width); } } } void generate_maze(int8_t *array, int width, int height) { + for (int x = 0; x < width; x++) + for (int y = 0; y < height; y++) + array[x + y * width] = -1; // calloc(number of elements, size per element) -> allocates space + fills with 0 // malloc(number of elements * size per element) -> allocates space (maybe prefered) BinaryTree* adjacent_cells = bst_create(); - int adjacent_cells_count = 0; - + bst_insert(adjacent_cells, 92); + bst_insert(adjacent_cells, 48); + bst_insert(adjacent_cells, 21); + bst_insert(adjacent_cells, 23); + bst_insert(adjacent_cells, 4); + bst_insert(adjacent_cells, 2); + bst_insert(adjacent_cells, 14); + bst_insert(adjacent_cells, 48); + bst_print(adjacent_cells); // Create starting point int center[2] = {width / 2, height / 2}; array[center[0] + center[1] * width]; - + add_adjacent_cells(center[0], center[1], adjacent_cells, array, width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { @@ -57,4 +84,6 @@ void generate_maze(int8_t *array, int width, int height) { } printf("\n"); } + + bst_delete(adjacent_cells); } \ No newline at end of file