From 7a2a589c45759fc735fae627b8567377c0636c0f Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sun, 3 Nov 2024 22:45:48 +0000 Subject: [PATCH] Store display etc in static variables --- main.c | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/main.c b/main.c index 3560954..f4e66a1 100644 --- a/main.c +++ b/main.c @@ -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;