Store display etc in static variables

This commit is contained in:
Camden Dixie O'Brien 2024-11-03 22:45:48 +00:00
parent 8c70d6a70f
commit 7a2a589c45

49
main.c
View File

@ -43,29 +43,35 @@ static const vec2_t steps[] = {
[DOWN] = { .x = 0, .y = 2 },
};
static void draw_walls(Display *dpy, Window w, GC gc)
static Display *dpy;
static Window window;
static GC ctx;
static void draw_walls(void)
{
XFillRectangle(
dpy, w, gc, PX(MARGIN), PX(MARGIN), PX(GRID_SIZE + 2),
dpy, window, ctx, PX(MARGIN), PX(MARGIN), PX(GRID_SIZE + 2),
PX(WALL_THICKNESS));
XFillRectangle(
dpy, w, gc, PX(MARGIN), PX(MARGIN + WALL_THICKNESS + GRID_SIZE),
PX(GRID_SIZE + 2), PX(WALL_THICKNESS));
dpy, window, ctx, PX(MARGIN),
PX(MARGIN + WALL_THICKNESS + GRID_SIZE), PX(GRID_SIZE + 2),
PX(WALL_THICKNESS));
XFillRectangle(
dpy, w, gc, PX(MARGIN), PX(MARGIN + WALL_THICKNESS),
dpy, window, ctx, PX(MARGIN), PX(MARGIN + WALL_THICKNESS),
PX(WALL_THICKNESS), PX(GRID_SIZE));
XFillRectangle(
dpy, w, gc, PX(MARGIN + WALL_THICKNESS + GRID_SIZE),
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(Display *dpy, Window w, GC gc, const maze_t *m)
static void draw_maze(const maze_t *m)
{
const int margin_px = PX(MARGIN + WALL_THICKNESS);
XClearArea(
dpy, w, margin_px, margin_px, PX(GRID_SIZE), PX(GRID_SIZE), false);
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) {
@ -73,19 +79,18 @@ static void draw_maze(Display *dpy, Window w, GC gc, const maze_t *m)
continue;
const int left = margin_px + PX(x);
const int top = margin_px + PX(y);
XFillRectangle(dpy, w, gc, left, top, PX(1), PX(1));
XFillRectangle(dpy, window, ctx, left, top, PX(1), PX(1));
}
}
XFlush(dpy);
}
static void
generate_maze(Display *dpy, Window w, GC gc, maze_t *m, vec2_t p, vec2_t g)
static void generate_maze(maze_t *m, vec2_t p, vec2_t g)
{
m->cells[p.x][p.y] = true;
draw_maze(dpy, w, gc, m);
draw_maze(m);
nanosleep(&pause, NULL);
if (p.x == g.x && p.y == g.y)
@ -111,7 +116,7 @@ generate_maze(Display *dpy, Window w, GC gc, maze_t *m, vec2_t p, vec2_t g)
const int yi = (p.y + n.y) / 2;
m->cells[xi][yi] = true;
generate_maze(dpy, w, gc, m, n, g);
generate_maze(m, n, g);
}
}
}
@ -124,23 +129,23 @@ int main(void)
srand(tv.tv_usec);
XEvent evt;
Display *dpy = XOpenDisplay(NULL);
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 w = XCreateSimpleWindow(
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, w, &del, 1);
GC gc = DefaultGC(dpy, DefaultScreen(dpy));
XSetForeground(dpy, gc, black);
XSetWMProtocols(dpy, window, &del, 1);
ctx = DefaultGC(dpy, DefaultScreen(dpy));
XSetForeground(dpy, ctx, black);
// Map window
XSelectInput(dpy, w, StructureNotifyMask);
XMapWindow(dpy, w);
XSelectInput(dpy, window, StructureNotifyMask);
XMapWindow(dpy, window);
do
XNextEvent(dpy, &evt);
while (MapNotify != evt.type);
@ -148,9 +153,9 @@ int main(void)
// Generate and draw maze
maze_t m;
memset(&m, 0, sizeof(maze_t));
draw_walls(dpy, w, gc);
draw_walls();
const vec2_t start = { GOAL, GOAL }, end = { 0, 0 };
generate_maze(dpy, w, gc, &m, start, end);
generate_maze(&m, start, end);
// Wait for window exit
bool is_del = false;