Skip to content

Commit

Permalink
Export multiple generator algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
ccouzens committed Apr 6, 2024
1 parent 23510c4 commit 51db858
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
22 changes: 21 additions & 1 deletion computer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,31 @@ impl rand::RngCore for JSRand {
}

#[no_mangle]
pub extern "C" fn new_maze(width: usize, height: usize) -> &'static mut Maze {
pub extern "C" fn new_maze_wilsons(width: usize, height: usize) -> &'static mut Maze {
let maze = maze::generators::generate_maze_with_wilsons_algorithm(&mut JSRand(), width, height);
Box::leak(Box::new(maze))
}

#[no_mangle]
pub extern "C" fn new_maze_aldous_broder(width: usize, height: usize) -> &'static mut Maze {
let maze = maze::generators::generate_maze_with_aldous_broder(&mut JSRand(), width, height);
Box::leak(Box::new(maze))
}

#[no_mangle]
pub extern "C" fn new_maze_binary_tree(width: usize, height: usize) -> &'static mut Maze {
let maze =
maze::generators::generate_maze_with_binary_tree_algorithm(&mut JSRand(), width, height);
Box::leak(Box::new(maze))
}

#[no_mangle]
pub extern "C" fn new_maze_sidewinder(width: usize, height: usize) -> &'static mut Maze {
let maze =
maze::generators::generate_maze_with_sidewinder_algorithm(&mut JSRand(), width, height);
Box::leak(Box::new(maze))
}

#[no_mangle]
pub extern "C" fn maze_width(maze: &Maze) -> usize {
maze.width()
Expand Down
5 changes: 4 additions & 1 deletion src/computer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ type BitmapRenderer = number & { readonly [tag]: "BITMAP_RENDERER" };

export interface Computer {
memory: WebAssembly.Memory;
new_maze: (width: number, height: number) => Maze;
new_maze_wilsons: (width: number, height: number) => Maze;
new_maze_aldous_broder: (width: number, height: number) => Maze;
new_maze_binary_tree: (width: number, height: number) => Maze;
new_maze_sidewinder: (width: number, height: number) => Maze;
free_maze: (maze: Maze) => void;
string_ptr: (str: RustString) => number;
string_length: (str: RustString) => number;
Expand Down
2 changes: 1 addition & 1 deletion src/game/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export async function initialiseApp(window: WindowType) {
navigate(app, app.window.location.hash, false);
}

const maze = computer.new_maze(app.width, app.height);
const maze = computer.new_maze_wilsons(app.width, app.height);
try {
app.wallsSvgPath.setAttribute(
"d",
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function putMazeOnPage() {
renderers.push([name, renderer]);
}
async function rerender() {
const maze = measureSync(["new maze"], () => computer.new_maze(30, 30));
const maze = measureSync(["new maze"], () => computer.new_maze_wilsons(30, 30));
try {
const bitmap = await measureAsync(["image bitmap"], () =>
imageBitmap(computer, maze),
Expand Down

0 comments on commit 51db858

Please sign in to comment.