Skip to content

Commit

Permalink
Working binary tree testing
Browse files Browse the repository at this point in the history
  • Loading branch information
omeyenburg committed Jan 26, 2024
1 parent ac8c869 commit 4992d31
Show file tree
Hide file tree
Showing 25 changed files with 255 additions and 99 deletions.
2 changes: 1 addition & 1 deletion build/lib/mazeforge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
166 changes: 166 additions & 0 deletions build/lib/mazeforge/core/loader.py
Original file line number Diff line number Diff line change
@@ -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
"""
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
13 changes: 13 additions & 0 deletions build/lib/mazeforge/maze.py
Original file line number Diff line number Diff line change
@@ -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)
76 changes: 9 additions & 67 deletions compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=(
Expand Down Expand Up @@ -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
20 changes: 10 additions & 10 deletions src/mazeforge.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 15 additions & 6 deletions src/mazeforge/core/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Binary file modified src/mazeforge/core/platform/linux/aarch64/libmaze.so
Binary file not shown.
Binary file modified src/mazeforge/core/platform/linux/arm/libmaze.so
Binary file not shown.
Binary file modified src/mazeforge/core/platform/linux/i686/libmaze.so
Binary file not shown.
Binary file modified src/mazeforge/core/platform/linux/x86_64/libmaze.so
Binary file not shown.
Binary file modified src/mazeforge/core/platform/macos/arm/libmaze.dylib
Binary file not shown.
Binary file modified src/mazeforge/core/platform/macos/x86_64/libmaze.dylib
Binary file not shown.
Binary file modified src/mazeforge/core/platform/windows/i686/libmaze.dll
Binary file not shown.
Binary file modified src/mazeforge/core/platform/windows/x86_64/libmaze.dll
Binary file not shown.
Loading

0 comments on commit 4992d31

Please sign in to comment.