Store display etc in static variables
This commit is contained in:
parent
8c70d6a70f
commit
7a2a589c45
49
main.c
49
main.c
@ -43,29 +43,35 @@ static const vec2_t steps[] = {
|
|||||||
[DOWN] = { .x = 0, .y = 2 },
|
[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(
|
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));
|
PX(WALL_THICKNESS));
|
||||||
XFillRectangle(
|
XFillRectangle(
|
||||||
dpy, w, gc, PX(MARGIN), PX(MARGIN + WALL_THICKNESS + GRID_SIZE),
|
dpy, window, ctx, PX(MARGIN),
|
||||||
PX(GRID_SIZE + 2), PX(WALL_THICKNESS));
|
PX(MARGIN + WALL_THICKNESS + GRID_SIZE), PX(GRID_SIZE + 2),
|
||||||
|
PX(WALL_THICKNESS));
|
||||||
XFillRectangle(
|
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));
|
PX(WALL_THICKNESS), PX(GRID_SIZE));
|
||||||
XFillRectangle(
|
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));
|
PX(MARGIN + WALL_THICKNESS), PX(WALL_THICKNESS), PX(GRID_SIZE - 1));
|
||||||
|
|
||||||
XFlush(dpy);
|
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);
|
const int margin_px = PX(MARGIN + WALL_THICKNESS);
|
||||||
XClearArea(
|
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 x = 0; x < GRID_SIZE; ++x) {
|
||||||
for (int y = 0; y < GRID_SIZE; ++y) {
|
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;
|
continue;
|
||||||
const int left = margin_px + PX(x);
|
const int left = margin_px + PX(x);
|
||||||
const int top = margin_px + PX(y);
|
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);
|
XFlush(dpy);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void generate_maze(maze_t *m, vec2_t p, vec2_t g)
|
||||||
generate_maze(Display *dpy, Window w, GC gc, maze_t *m, vec2_t p, vec2_t g)
|
|
||||||
{
|
{
|
||||||
m->cells[p.x][p.y] = true;
|
m->cells[p.x][p.y] = true;
|
||||||
|
|
||||||
draw_maze(dpy, w, gc, m);
|
draw_maze(m);
|
||||||
nanosleep(&pause, NULL);
|
nanosleep(&pause, NULL);
|
||||||
|
|
||||||
if (p.x == g.x && p.y == g.y)
|
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;
|
const int yi = (p.y + n.y) / 2;
|
||||||
m->cells[xi][yi] = true;
|
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);
|
srand(tv.tv_usec);
|
||||||
|
|
||||||
XEvent evt;
|
XEvent evt;
|
||||||
Display *dpy = XOpenDisplay(NULL);
|
dpy = XOpenDisplay(NULL);
|
||||||
assert(dpy);
|
assert(dpy);
|
||||||
|
|
||||||
// Create window and configure graphics context
|
// Create window and configure graphics context
|
||||||
const int black = BlackPixel(dpy, DefaultScreen(dpy));
|
const int black = BlackPixel(dpy, DefaultScreen(dpy));
|
||||||
const int white = WhitePixel(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),
|
dpy, DefaultRootWindow(dpy), 0, 0, PX(WINDOW_SIZE), PX(WINDOW_SIZE),
|
||||||
0, white, white);
|
0, white, white);
|
||||||
Atom del = XInternAtom(dpy, "WM_DELETE_WINDOW", false);
|
Atom del = XInternAtom(dpy, "WM_DELETE_WINDOW", false);
|
||||||
XSetWMProtocols(dpy, w, &del, 1);
|
XSetWMProtocols(dpy, window, &del, 1);
|
||||||
GC gc = DefaultGC(dpy, DefaultScreen(dpy));
|
ctx = DefaultGC(dpy, DefaultScreen(dpy));
|
||||||
XSetForeground(dpy, gc, black);
|
XSetForeground(dpy, ctx, black);
|
||||||
|
|
||||||
// Map window
|
// Map window
|
||||||
XSelectInput(dpy, w, StructureNotifyMask);
|
XSelectInput(dpy, window, StructureNotifyMask);
|
||||||
XMapWindow(dpy, w);
|
XMapWindow(dpy, window);
|
||||||
do
|
do
|
||||||
XNextEvent(dpy, &evt);
|
XNextEvent(dpy, &evt);
|
||||||
while (MapNotify != evt.type);
|
while (MapNotify != evt.type);
|
||||||
@ -148,9 +153,9 @@ int main(void)
|
|||||||
// Generate and draw maze
|
// Generate and draw maze
|
||||||
maze_t m;
|
maze_t m;
|
||||||
memset(&m, 0, sizeof(maze_t));
|
memset(&m, 0, sizeof(maze_t));
|
||||||
draw_walls(dpy, w, gc);
|
draw_walls();
|
||||||
const vec2_t start = { GOAL, GOAL }, end = { 0, 0 };
|
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
|
// Wait for window exit
|
||||||
bool is_del = false;
|
bool is_del = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user