maze-thing/main.c

164 lines
3.7 KiB
C

/*
* Copyright (c) Camden Dixie O'Brien
* SPDX-License-Identifier: AGPL-3.0-only
*/
#define _POSIX_C_SOURCE 199309L
#include <X11/Xlib.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#define MAZE_SIZE 24
#define GRID_SIZE (2 * MAZE_SIZE - 1)
#define MARGIN 2
#define WALL_THICKNESS 1
#define WINDOW_SIZE (GRID_SIZE + 2 * (WALL_THICKNESS + MARGIN))
#define PX(x) ((x) << 3)
#define GOAL (GRID_SIZE - 1)
typedef enum { LEFT, RIGHT, UP, DOWN } dir_t;
typedef struct {
int x, y;
} vec2_t;
static const struct timespec pause = { .tv_nsec = 5000000 };
static const vec2_t steps[] = {
[LEFT] = { .x = -2, .y = 0 },
[RIGHT] = { .x = 2, .y = 0 },
[UP] = { .x = 0, .y = -2 },
[DOWN] = { .x = 0, .y = 2 },
};
static Display *dpy;
static Window window;
static GC ctx;
static bool maze[GRID_SIZE][GRID_SIZE];
static void draw_walls(void)
{
XFillRectangle(
dpy, window, ctx, PX(MARGIN), PX(MARGIN), PX(GRID_SIZE + 2),
PX(WALL_THICKNESS));
XFillRectangle(
dpy, window, ctx, PX(MARGIN),
PX(MARGIN + WALL_THICKNESS + GRID_SIZE), PX(GRID_SIZE + 2),
PX(WALL_THICKNESS));
XFillRectangle(
dpy, window, ctx, PX(MARGIN), PX(MARGIN + WALL_THICKNESS),
PX(WALL_THICKNESS), PX(GRID_SIZE));
XFillRectangle(
dpy, window, ctx, PX(MARGIN + WALL_THICKNESS + GRID_SIZE),
PX(MARGIN + WALL_THICKNESS), PX(WALL_THICKNESS), PX(GRID_SIZE - 1));
XFlush(dpy);
}
static void draw_maze(void)
{
const int margin_px = PX(MARGIN + WALL_THICKNESS);
XClearArea(
dpy, window, margin_px, margin_px, PX(GRID_SIZE), PX(GRID_SIZE),
false);
for (int x = 0; x < GRID_SIZE; ++x) {
for (int y = 0; y < GRID_SIZE; ++y) {
if (maze[x][y])
continue;
const int left = margin_px + PX(x);
const int top = margin_px + PX(y);
XFillRectangle(dpy, window, ctx, left, top, PX(1), PX(1));
}
}
XFlush(dpy);
}
static void generate_maze(vec2_t p, vec2_t g)
{
maze[p.x][p.y] = true;
draw_maze();
nanosleep(&pause, NULL);
dir_t visit[] = { LEFT, RIGHT, UP, DOWN };
for (int i = 3; i > 0; --i) {
const int r = rand() % (i + 1);
const dir_t tmp = visit[r];
visit[r] = visit[i];
visit[i] = tmp;
}
vec2_t n;
for (int i = 0; i < 4; ++i) {
n.x = p.x + steps[visit[i]].x;
n.y = p.y + steps[visit[i]].y;
const bool x_in_bounds = n.x >= 0 && n.x < GRID_SIZE;
const bool y_in_bounds = n.y >= 0 && n.y < GRID_SIZE;
if (x_in_bounds && y_in_bounds && !maze[n.x][n.y]) {
const int xi = (p.x + n.x) / 2;
const int yi = (p.y + n.y) / 2;
maze[xi][yi] = true;
generate_maze(n, g);
}
}
}
int main(void)
{
// Seed random number generation from time
struct timeval tv;
gettimeofday(&tv, NULL);
srand(tv.tv_usec);
XEvent evt;
dpy = XOpenDisplay(NULL);
assert(dpy);
// Create window and configure graphics context
const int black = BlackPixel(dpy, DefaultScreen(dpy));
const int white = WhitePixel(dpy, DefaultScreen(dpy));
window = XCreateSimpleWindow(
dpy, DefaultRootWindow(dpy), 0, 0, PX(WINDOW_SIZE), PX(WINDOW_SIZE),
0, white, white);
Atom del = XInternAtom(dpy, "WM_DELETE_WINDOW", false);
XSetWMProtocols(dpy, window, &del, 1);
ctx = DefaultGC(dpy, DefaultScreen(dpy));
XSetForeground(dpy, ctx, black);
// Map window
XSelectInput(dpy, window, StructureNotifyMask);
XMapWindow(dpy, window);
do
XNextEvent(dpy, &evt);
while (MapNotify != evt.type);
// Generate and draw maze
memset(&maze, 0, sizeof(maze));
draw_walls();
const vec2_t start = { GOAL, GOAL }, end = { 0, 0 };
generate_maze(start, end);
// Wait for window exit
bool is_del = false;
do {
XNextEvent(dpy, &evt);
if (ClientMessage == evt.type)
is_del = (unsigned long)evt.xclient.data.l[0] == del;
} while (!is_del);
XCloseDisplay(dpy);
return EXIT_SUCCESS;
}