Draw visited cells in a different colour

This commit is contained in:
Camden Dixie O'Brien 2024-11-04 00:01:32 +00:00
parent cc43e870fe
commit f4aa1bc01c

23
main.c
View File

@ -51,9 +51,12 @@ static Display *dpy;
static Window window;
static GC ctx;
static cell_t maze[GRID_SIZE][GRID_SIZE];
static int bg_col, wall_col, visited_col;
static void draw_walls(void)
{
XSetForeground(dpy, ctx, wall_col);
XFillRectangle(
dpy, window, ctx, PX(MARGIN), PX(MARGIN), PX(GRID_SIZE + 2),
PX(WALL_THICKNESS));
@ -80,7 +83,11 @@ static void draw_maze(void)
for (int x = 0; x < GRID_SIZE; ++x) {
for (int y = 0; y < GRID_SIZE; ++y) {
if (maze[x][y].is_path)
if (!maze[x][y].is_path)
XSetForeground(dpy, ctx, wall_col);
else if (maze[x][y].visited)
XSetForeground(dpy, ctx, visited_col);
else
continue;
const int left = margin_px + PX(x);
const int top = margin_px + PX(y);
@ -149,15 +156,21 @@ int main(void)
assert(dpy);
// Create window and configure graphics context
const int black = BlackPixel(dpy, DefaultScreen(dpy));
const int white = WhitePixel(dpy, DefaultScreen(dpy));
wall_col = BlackPixel(dpy, DefaultScreen(dpy));
bg_col = WhitePixel(dpy, DefaultScreen(dpy));
window = XCreateSimpleWindow(
dpy, DefaultRootWindow(dpy), 0, 0, PX(WINDOW_SIZE), PX(WINDOW_SIZE),
0, white, white);
0, bg_col, bg_col);
Atom del = XInternAtom(dpy, "WM_DELETE_WINDOW", false);
XSetWMProtocols(dpy, window, &del, 1);
ctx = DefaultGC(dpy, DefaultScreen(dpy));
XSetForeground(dpy, ctx, black);
// Create colormap and allocate colour for visited cells
Colormap cm = XCreateColormap(
dpy, window, DefaultVisual(dpy, DefaultScreen(dpy)), AllocNone);
XColor xcol = { .red = 55555, .green = 10000, .blue = 10000 };
XAllocColor(dpy, cm, &xcol);
visited_col = xcol.pixel;
// Map window
XSelectInput(dpy, window, StructureNotifyMask);