Tutorial: Generate six mazes side by side, one step per frame, and watch each algorithm work.
Tutorial: Generate six mazes side by side, one step per frame, and watch each algorithm work.This tutorial introduces the maze API: the representation in maze.h, which holds cells and the passages between them, and the builders in maze_builder.h, which carve those passages. The six built-in algorithms run at once on identical grids, so their habits show up next to each other.
Cells are plain indices. This example keeps its per-maze state in a small table rather than inside the maze, which is the intended way to work: anything you want to remember about a cell is your own array, indexed by the cell.
The six builders are stepped independently and finish at their own pace: the binary tree is done in one pass over the cells, while Aldous-Broder wanders for a long time before it is through.
Most of what you want to draw is already in the maze — a cell with no passage has not been reached yet. What the maze cannot tell you is where the algorithm is standing and what it is still undecided about, which is what bj_maze_marks are for.
#define WINDOW_W 800
#define WINDOW_H 600
#define MAZE_CELL_PER_SIDE 15
#define MAZE_PIXEL_PER_CELL 5
#define MAZE_BMP_SIZE (MAZE_CELL_PER_SIDE * MAZE_PIXEL_PER_CELL + (MAZE_CELL_PER_SIDE + 1))
#define MAZE_MARGIN 10
#define LABEL_H 32
#define LABEL_FONT_H 18
#define ANIMATION_HZ 30
static struct {
const char* name;
struct bj_maze_marks* marks;
};
const struct bj_maze_marks* marks
) {
};
: rock ? wall_c
: unseen_c);
}
}
int x2 = x1 + w;
int y2 = y1 + w;
}
}
}
}
}
}
}
void* user_data
) {
(void)w; (void)dirty; (void)user_data;
};
label_color, "SPACE: generate again ESC: quit");
for(
size_t m = 0 ; m <
n_mazes ; ++m) {
.x =
mazes[m].blit_dest.x,
.y = (int16_t)(
mazes[m].blit_dest.y +
mazes[m].blit_dest.h),
.w =
mazes[m].blit_dest.w,
};
label_color,
mazes[m].name);
}
}
for (
int c = 1; c <= (int)
n_mazes; c++) {
if (cs > s) { s = cs; cols = c; }
}
int rows = ((int)
n_mazes + cols - 1) / cols, row = n / cols, col = n % cols;
int iir = row == rows - 1 ? (int)
n_mazes - row * cols : cols;
return out;
}
int width,
int height,
void* user_data
) {
(void)
w; (void)user_data;
for(
size_t m = 0 ; m <
n_mazes ; ++m) {
}
}
for(
size_t m = 0 ; m <
n_mazes ; ++m) {
}
}
void* user_data
) {
}
}
static void*
setup(
struct bj_app* app,
void* init_data) {
(void)init_data;
return 0;
}
for(
size_t m = 0 ; m <
n_mazes ; ++m) {
mazes[m].marks, 0, 0, 0);
}
return 0;
}
static void step(
struct bj_app* app,
struct bj_tick_info tick,
void* user_data) {
(void)tick;
(void)user_data;
for(
size_t m = 0 ; m <
n_mazes ; ++m) {
}
}
}
}
static void teardown(
struct bj_app* app,
void* user_data) {
(void)user_data;
for(
size_t m = 0 ; m <
n_mazes ; ++m) {
}
}
int main(
int argc,
char* argv[]) {
(void)argc; (void)argv;
}
Application lifecycle: callback-driven setup, step, and teardown.
static void step(struct bj_app *app, struct bj_tick_info tick, void *user_data)
static void teardown(struct bj_app *app, void *user_data)
static void * setup(struct bj_app *app, void *init_data)
Header file for Bitmap type.
static void on_draw(struct bj_window *w, struct bj_render_target *target, const struct bj_rect *dirty, void *user_data)
Header file for Bitmap drawing functions.
Sytem event management API.
int bj_run_app(bj_app_setup_fn setup, bj_app_step_fn step, bj_app_fixed_step_fn fixed_step, bj_app_teardown_fn teardown, void *init_data)
Drive the application lifecycle.
void bj_set_frame_rate(struct bj_app *app, int hz)
Cap the run loop to at most hz iterations per second.
void bj_quit_app(struct bj_app *app, int exit_code)
Signal the given application to exit on the next iteration.
Timing snapshot handed to the step and fixed-step callbacks.
void bj_clear_bitmap(struct bj_bitmap *bitmap)
Fills the entire bitmap with the clear colour.
bj_bool bj_blit_bitmap_stretched(const struct bj_bitmap *src, const struct bj_rect *src_area, struct bj_bitmap *dst, const struct bj_rect *dst_area, enum bj_blit_op op)
Stretched bitmap blitting (nearest neighbor).
void bj_set_bitmap_clear_color(struct bj_bitmap *bitmap, uint32_t color)
Set the colour bj_clear_bitmap fills with.
uint32_t bj_pack_bitmap_rgb(struct bj_bitmap *bitmap, uint8_t red, uint8_t green, uint8_t blue)
Pack an (R, G, B) triple into the bitmap's native pixel format.
struct bj_bitmap * bj_create_bitmap(size_t width, size_t height, enum bj_pixel_mode mode, size_t stride)
Creates a new struct bj_bitmap with the specified width and height.
void bj_draw_text(struct bj_bitmap *dst, unsigned height, struct bj_rect area, bj_text_halign halign, bj_text_valign valign, uint32_t fg_native, const char *text)
Prints text in a foreground colour over a transparent background.
void bj_destroy_bitmap(struct bj_bitmap *bitmap)
Deletes a struct bj_bitmap object and releases associated memory.
@ BJ_BLIT_OP_COPY
Copy source to destination (fast path when formats match)
@ BJ_TEXT_HALIGN_CENTER
Center text horizontally within the area.
@ BJ_TEXT_VALIGN_CENTER
Center text vertically within the area.
struct bj_render_target bj_render_target
uint32_t bj_bool
Boolean type used throughout the Banjo API.
struct bj_bitmap bj_bitmap
struct bj_maze_builder bj_maze_builder
struct bj_window bj_window
void bj_draw_filled_rectangle(struct bj_bitmap *bitmap, const struct bj_rect *area, uint32_t pixel)
Draws a filled rectangle in the given bitmap.
void bj_draw_line(struct bj_bitmap *bitmap, int x0, int y0, int x1, int y1, uint32_t pixel)
Draws a line of pixels in the given bitmap.
enum bj_event_action action
Action (press/release/repeat)
enum bj_key key
Key identifier.
void bj_dispatch_events(void)
Poll and dispatch all pending events.
void bj_close_on_escape(struct bj_window *window, const struct bj_key_event *event, void *user_data)
Handle the ESC key to close a window.
bj_key_callback_fn bj_set_key_callback(bj_key_callback_fn callback, void *user_data)
Set the global callback for keyboard key events.
@ BJ_KEY_SPACE
Spacebar key.
@ BJ_EVENT_ACTION_PRESS
The key or button was pressed.
Represent a keyboard key event.
uint16_t w
Width in pixels.
uint16_t h
Height in pixels.
int16_t y
Y coordinate of the top-left corner (pixels, can be negative).
int16_t x
X coordinate of the top-left corner (pixels, can be negative).
Axis-aligned rectangle: a top-left corner plus a width and height.
void bj_maze_close_all(struct bj_maze *maze)
Close every passage, leaving the maze as it came out of its constructor.
struct bj_maze_algorithm bj_maze_algorithm_wilson(void)
Wilson's algorithm.
void bj_destroy_maze_builder(struct bj_maze_builder *builder)
Destroy a builder and release the algorithm state it accumulated.
bj_bool bj_step_maze_builder(struct bj_maze_builder *builder)
Perform one step of generation.
struct bj_maze_algorithm bj_maze_algorithm_backtracker(void)
The backtracker, known in the literature as the recursive backtracker.
bj_bool bj_reset_maze_builder(struct bj_maze_builder *builder, struct bj_error **error)
Start the algorithm over.
bj_bool bj_maze_cell_is_open(const struct bj_maze *maze, uint32_t cell, enum bj_maze_dir dir)
Tell whether cell has a passage through slot dir.
struct bj_maze_algorithm bj_maze_algorithm_sidewinder(void)
The sidewinder algorithm.
struct bj_maze_algorithm bj_maze_algorithm_hunt_and_kill(void)
The hunt-and-kill algorithm.
struct bj_maze_marks * bj_create_maze_marks(const struct bj_maze *maze, struct bj_error **error)
Allocate marks for every cell of maze, all clear.
uint8_t bj_maze_cell_marks(const struct bj_maze_marks *marks, uint32_t cell)
Read what is recorded about a cell.
struct bj_maze_algorithm bj_maze_algorithm_btree(void)
The binary tree algorithm.
struct bj_maze * bj_create_maze_ortho2d(size_t width, size_t height, struct bj_error **error)
Allocate a new orthogonal 2D maze, with every wall still standing.
void bj_destroy_maze(struct bj_maze *maze)
Deletes a struct bj_maze object and releases associated memory.
uint32_t bj_maze_ortho2d_cell(const struct bj_maze *maze, size_t row, size_t col)
Find the cell at a position of an orthogonal 2D maze.
struct bj_maze_builder * bj_create_maze_builder(struct bj_maze *maze, struct bj_maze_algorithm algorithm, struct bj_maze_marks *marks, bj_random_u32_fn next, void *rng_state, struct bj_error **error)
Create a builder that carves maze with algorithm.
void bj_destroy_maze_marks(struct bj_maze_marks *marks)
Release marks.
struct bj_maze_algorithm bj_maze_algorithm_aldous_broder(void)
The Aldous-Broder algorithm.
@ BJ_MAZE_MARK_PATH
On the route currently under consideration.
@ BJ_MAZE_MARK_VISITED
The algorithm is done with this cell.
@ BJ_MAZE_MARK_HEAD
Where the algorithm is working now.
@ BJ_MAZE_DIR_NORTH
Towards the top of the grid.
@ BJ_MAZE_DIR_EAST
Towards the right of the grid.
@ BJ_MAZE_DIR_SOUTH
Towards the bottom of the grid.
@ BJ_MAZE_DIR_WEST
Towards the left of the grid.
A maze generation algorithm.
@ BJ_PIXEL_MODE_XRGB8888
32bpp RGB
void bj_srand(unsigned int seed)
Seed the standard PRNG.
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
uint64_t bj_get_time(void)
Get the current system time in seconds since the Unix epoch.
static void bj_invalidate_window(struct bj_window *window)
Mark the whole window as needing a repaint.
bj_bool bj_window_should_close(struct bj_window *window)
Report whether a window's close flag is set.
struct bj_window * bj_create_window(const char *title, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t flags, struct bj_error **error)
Create a window with the given attributes.
void bj_set_window_draw_callback(struct bj_window *window, bj_window_draw_fn fn, void *user_data)
Register the redraw callback for window.
struct bj_bitmap * bj_render_target_bitmap(struct bj_render_target *target)
Reach the software framebuffer behind a render target.
void bj_set_window_resize_callback(struct bj_window *window, bj_window_resize_fn fn, void *user_data)
Register the resize callback for window.
void bj_destroy_window(struct bj_window *window)
Destroy a window and release its resources.
@ BJ_WINDOW_FLAG_RESIZABLE
User may resize the window.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
static void on_resize(struct bj_window *w, int width, int height, void *user_data)
static void regenerate(void)
static struct bj_rect grid_rect_place(int n, int win_w, int win_h)
static void draw_maze(bj_bitmap *target, const bj_maze *maze, const struct bj_maze_marks *marks)
#define MAZE_PIXEL_PER_CELL
#define MAZE_CELL_PER_SIDE
static struct @056016103232074337222244202315226141072276235164 mazes[]
static void on_key(struct bj_window *w, const struct bj_key_event *event, void *user_data)
static const size_t n_mazes
Header file for Maze representation.
Header file for Maze generation.
Header file for general pixel manipulation facilities.
Pseudo-random number generation API.
Header file for system interactions.
Header file for time manipulation utilities.
Header file for bj_window type.