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/>.
22 #include <Xm/XmStrDefs.h>
25 /* TODO user-selectable colours */
26 static const char * const colours[][3] = {
27 /*primary bottom top */
28 "#8d2e28", "#6a1b17", "#a14842", /* red */
29 "#b46e28", "#924a16", "#c7904f", /* orange */
30 "#d8b740", "#c59f39", "#e2c65d", /* yellow */
31 "#286428", "#194719", "#4e874e", /* green */
32 "#003471", "#001f4f", "#00528b", /* blue */
33 "#dcdcdc", "#c0c0c0", "#eaeaea", /* white */
36 static void init_colours(struct app_state *state, Screen *screen)
38 Display *display = DisplayOfScreen(screen);
39 Colormap cmap = DefaultColormapOfScreen(screen);
43 for (j = 0; j < COLOUR_MAX; j++) {
44 for (i = 0; i < TILE_MAX-1; i++) {
45 XAllocNamedColor(display, cmap, colours[i][j],
47 state->tile_colour[i][j] = colour.pixel;
52 void x11_initialize(struct app_state *state, Screen *screen)
54 Display *display = DisplayOfScreen(screen);
55 Window root = RootWindowOfScreen(screen);
56 Colormap cmap = DefaultColormapOfScreen(screen);
59 init_colours(state, screen);
62 state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
65 static void draw_tile(struct app_state *state, Display *display, Drawable d,
66 int tile, int gx, int gy, Dimension tw, Dimension th)
68 int tx = gx * tw, ty = gy * th;
70 XSegment topshadow[] = {
71 { tx, ty, tx, ty+th },
72 { tx+1, ty, tx+tw, ty },
74 { tx+1, ty+1, tx+1, ty+th-1 },
75 { tx+2, ty+1, tx+tw-1, ty+1 }
78 XSegment bottomshadow[] = {
79 { tx+1, ty+th-1, tx+tw, ty+th-1 },
80 { tx+tw-1, ty+th-1, tx+tw-1, ty+1 },
82 { tx+2, ty+th-2, tx+tw-1, ty+th-2 },
83 { tx+tw-2, ty+th-2, tx+tw-2, ty+2 }
86 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
87 XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
89 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
90 XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
92 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_PRIMARY]);
93 XFillRectangle(display, d, state->tile_gc, tx+2, ty+2, tw-4, th-4);
97 redraw_tile(struct app_state *state, Display *display, Drawable d,
98 uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2,
99 int x, int y, Dimension w, Dimension h)
101 uint_fast32_t pos = board_position(x, y);
102 unsigned char tile = 0;
104 if (bit0 & pos) tile |= 1;
105 if (bit1 & pos) tile |= 2;
106 if (bit2 & pos) tile |= 4;
107 assert(tile < TILE_MAX);
109 if (tile == TILE_EMPTY) {
110 XClearArea(display, d, x*w, y*h, w, h, 0);
112 draw_tile(state, display, d, tile, x, y, w, h);
116 void x11_redraw_goal(struct app_state *state)
118 Display *display = XtDisplay(state->goal);
119 Window goal = XtWindow(state->goal);
123 XtVaGetValues(state->goal, XmNwidth, &w, XmNheight, &h, (char *)NULL);
126 for (i = 0; i < 9; i++) {
127 uint_least16_t *gp = state->board.goal;
129 redraw_tile(state, display, goal,
135 void x11_redraw_game(struct app_state *state)
137 Display *display = XtDisplay(state->goal);
138 Window game = XtWindow(state->game);
142 XtVaGetValues(state->game, XmNwidth, &w, XmNheight, &h, (char *)NULL);
145 for (i = 0; i < 25; i++) {
146 uint_least32_t *gp = state->board.game+1;
148 redraw_tile(state, display, game,