2 * X11 GUI for slide puzzle game
3 * Copyright © 2022 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 #include <X11/Intrinsic.h>
24 #include <Xm/XmStrDefs.h>
27 /* TODO user-selectable colours */
28 static const char * const colours[][3] = {
29 /*primary bottom top */
30 "#8d2e28", "#6a1b17", "#a14842", /* red */
31 "#b46e28", "#924a16", "#c7904f", /* orange */
32 "#d8b740", "#c59f39", "#e2c65d", /* yellow */
33 "#286428", "#194719", "#4e874e", /* green */
34 "#003471", "#001f4f", "#00528b", /* blue */
35 "#dcdcdc", "#c0c0c0", "#eaeaea", /* white */
38 static void init_colours(struct app_state *state, Screen *screen)
40 Display *display = DisplayOfScreen(screen);
41 Colormap cmap = DefaultColormapOfScreen(screen);
45 for (j = 0; j < COLOUR_MAX; j++) {
46 for (i = 0; i < TILE_MAX-1; i++) {
47 XAllocNamedColor(display, cmap, colours[i][j],
49 state->tile_colour[i][j] = colour.pixel;
54 void x11_initialize(struct app_state *state, Screen *screen)
56 Display *display = DisplayOfScreen(screen);
57 Window root = RootWindowOfScreen(screen);
60 init_colours(state, screen);
63 state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
66 static void draw_tile(struct app_state *state, Display *display, Drawable d,
67 int tile, int gx, int gy, Dimension tw, Dimension th)
69 int tx = gx * tw, ty = gy * th;
71 XSegment topshadow[] = {
72 { tx, ty, tx, ty+th },
73 { tx+1, ty, tx+tw, ty },
75 { tx+1, ty+1, tx+1, ty+th-1 },
76 { tx+2, ty+1, tx+tw-1, ty+1 }
79 XSegment bottomshadow[] = {
80 { tx+1, ty+th-1, tx+tw, ty+th-1 },
81 { tx+tw-1, ty+th-1, tx+tw-1, ty+1 },
83 { tx+2, ty+th-2, tx+tw-1, ty+th-2 },
84 { tx+tw-2, ty+th-2, tx+tw-2, ty+2 }
87 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
88 XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
90 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
91 XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
93 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_PRIMARY]);
94 XFillRectangle(display, d, state->tile_gc, tx+2, ty+2, tw-4, th-4);
98 redraw_tile(struct app_state *state, Display *display, Drawable d,
99 uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2,
100 int x, int y, Dimension w, Dimension h)
102 uint_fast32_t pos = board_position(x, y);
103 unsigned char tile = 0;
105 if (bit0 & pos) tile |= 1;
106 if (bit1 & pos) tile |= 2;
107 if (bit2 & pos) tile |= 4;
108 assert(tile < TILE_MAX);
110 if (tile == TILE_EMPTY) {
111 XClearArea(display, d, x*w, y*h, w, h, 0);
113 draw_tile(state, display, d, tile, x, y, w, h);
117 void x11_redraw_goal(struct app_state *state)
119 Display *display = XtDisplay(state->goal);
120 Window goal = XtWindow(state->goal);
124 XtVaGetValues(state->goal, XmNwidth, &w, XmNheight, &h, (char *)NULL);
127 for (i = 0; i < 9; i++) {
128 uint_least16_t *gp = state->board.goal;
130 redraw_tile(state, display, goal,
136 void x11_redraw_game(struct app_state *state)
138 Display *display = XtDisplay(state->goal);
139 Window game = XtWindow(state->game);
143 XtVaGetValues(state->game, XmNwidth, &w, XmNheight, &h, (char *)NULL);
146 for (i = 0; i < 25; i++) {
147 uint_least32_t *gp = state->board.game;
149 redraw_tile(state, display, game,