]> git.draconx.ca Git - rrace.git/blob - src/x11.c
x11: Improve redraw of empty spaces.
[rrace.git] / src / x11.c
1 /*
2  * X11 GUI for slide puzzle game
3  * Copyright © 2022 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <assert.h>
24
25 #include <X11/Intrinsic.h>
26 #include <X11/StringDefs.h>
27 #include <X11/Xatom.h>
28 #include <X11/Shell.h>
29 #include "motif.h"
30
31 /* Size of the traditional icon pixmap (multiple of 3) */
32 #define ICON_SIZE 48
33
34 /* Define to 1 to add highlights and delays for visually debugging redraws */
35 #if X11_RENDER_DEBUG
36 #  include <unistd.h>
37 #endif
38
39 enum {
40         NUM_TILE_COLOURS = COLOUR_MAX*(TILE_MAX-1),
41         COLOUR_UI_GAMEBG = NUM_TILE_COLOURS,
42         COLOUR_UI_MAX
43 };
44 typedef Pixel colour_tab[COLOUR_UI_MAX];
45
46 #define COLOURRES(name, index, def) { \
47         (name), (NULL), XtRPixel, sizeof (String), \
48         offsetof(struct { colour_tab t; }, t[index]), \
49         XtRString, (def) }
50
51 static XtResource colour_resources[] = {
52         COLOURRES("colour0", 0, COLOUR0_PRIMARY),
53         COLOURRES("colour1", 1, COLOUR1_PRIMARY),
54         COLOURRES("colour2", 2, COLOUR2_PRIMARY),
55         COLOURRES("colour3", 3, COLOUR3_PRIMARY),
56         COLOURRES("colour4", 4, COLOUR4_PRIMARY),
57         COLOURRES("colour5", 5, COLOUR5_PRIMARY),
58
59         COLOURRES("colourDark0",  6, COLOUR0_DARK),
60         COLOURRES("colourDark1",  7, COLOUR1_DARK),
61         COLOURRES("colourDark2",  8, COLOUR2_DARK),
62         COLOURRES("colourDark3",  9, COLOUR3_DARK),
63         COLOURRES("colourDark4", 10, COLOUR4_DARK),
64         COLOURRES("colourDark5", 11, COLOUR5_DARK),
65
66         COLOURRES("colourLight0", 12, COLOUR0_LIGHT),
67         COLOURRES("colourLight1", 13, COLOUR1_LIGHT),
68         COLOURRES("colourLight2", 14, COLOUR2_LIGHT),
69         COLOURRES("colourLight3", 15, COLOUR3_LIGHT),
70         COLOURRES("colourLight4", 16, COLOUR4_LIGHT),
71         COLOURRES("colourLight5", 17, COLOUR5_LIGHT),
72
73         COLOURRES("gameBackground", COLOUR_UI_GAMEBG, "#181818")
74 };
75
76 static void init_colours(struct app_state *state, Widget shell)
77 {
78         unsigned i;
79
80         colour_tab colours;
81         XtGetApplicationResources(shell, &colours, colour_resources,
82                                   XtNumber(colour_resources), NULL, 0);
83
84         for (i = 0; i < NUM_TILE_COLOURS; i++) {
85                 unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1);
86
87                 state->tile_colour[tile][shade] = colours[i];
88         }
89
90         XtVaSetValues(state->game, XtNbackground, colours[COLOUR_UI_GAMEBG],
91                                    (char *)NULL);
92 }
93
94 void x11_initialize(struct app_state *state, Widget shell)
95 {
96         Display *display = XtDisplay(shell);
97         Screen *screen = XtScreen(shell);
98         Window root = RootWindowOfScreen(screen);
99         XGCValues gcv;
100
101         init_colours(state, shell);
102
103         gcv.line_width = 1;
104         state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
105
106         state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
107                                            DefaultDepthOfScreen(screen));
108 }
109
110 static void draw_tile(struct app_state *state, Display *display, Drawable d,
111                       int tile, int gx, int gy, Dimension tw, Dimension th)
112 {
113         int tx = gx * tw, ty = gy * th;
114
115         XSegment topshadow[] = {
116                 { tx,      ty,      tx,      ty+th   },
117                 { tx+1,    ty,      tx+tw,   ty      },
118
119                 { tx+1,    ty+1,    tx+1,    ty+th-1 },
120                 { tx+2,    ty+1,    tx+tw-1, ty+1    }
121         };
122
123         XSegment bottomshadow[] = {
124                 { tx+1,    ty+th-1, tx+tw,   ty+th-1 },
125                 { tx+tw-1, ty+th-1, tx+tw-1, ty+1    },
126
127                 { tx+2,    ty+th-2, tx+tw-1, ty+th-2 },
128                 { tx+tw-2, ty+th-2, tx+tw-2, ty+2    }
129         };
130
131         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
132         XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
133
134         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
135         XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
136
137         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_PRIMARY]);
138         XFillRectangle(display, d, state->tile_gc, tx+2, ty+2, tw-4, th-4);
139 }
140
141 /*
142  * Clear a contiguous rectangle of tiles from top-left (x0, y0) to
143  * bottom-right (x1, y1).
144  */
145 static void
146 clear_tiles(struct app_state *state, Display *display, Drawable d,
147             int x0, int y0, int x1, int y1, Dimension w, Dimension h)
148 {
149         XRectangle r = { x0*w, y0*h, (x1+1)*w, (y1+1)*h };
150
151 #if X11_RENDER_DEBUG
152         XSetForeground(display, state->tile_gc, 0xff0000);
153         XFillRectangles(display, d, state->tile_gc, &r, 1);
154         XFlush(display);
155         usleep(70000);
156 #endif
157
158         XClearArea(display, d, r.x, r.y, r.width, r.height, 0);
159 }
160
161 /*
162  * Efficiently clear all the border tiles in the game area.  The mask indicates
163  * which tiles need clearing, but for the border clear it is safe to wipe an
164  * entire row or column of the border using a single XClearArea.
165  *
166  * The idea is to pick whichever row or column has the most tiles to clear,
167  * clear them, and then repeat until none are left (repeats at most 4 times).
168  */
169 static void clear_border(struct app_state *state, Display *display, Drawable d,
170                          Dimension w, Dimension h, uint_fast32_t mask)
171 {
172         uint_fast32_t best_mask = 0;
173         int best_count = -1;
174         int i, best = -1;
175
176         if (!(mask &= ~GOAL_MASK & 0x1ffffff))
177                 return;
178
179         for (i = 0; i < 4; i++) {
180                 uint_fast32_t this_mask, tmp;
181                 int this_count = 0;
182
183                 if (i & 2)
184                         this_mask = board_column(4*(i & 1));
185                 else
186                         this_mask = board_row(4*(i & 1));
187
188                 /* Count set bits */
189                 for (tmp = mask & this_mask; tmp; this_count++)
190                         tmp &= tmp - 1;
191
192                 if (this_count > best_count) {
193                         best_count = this_count;
194                         best_mask = this_mask;
195                         best = i;
196                 }
197         }
198
199         switch (best) {
200         case 0: clear_tiles(state, display, d, 0, 0, 4, 0, w, h); break;
201         case 1: clear_tiles(state, display, d, 0, 4, 4, 4, w, h); break;
202         case 2: clear_tiles(state, display, d, 0, 0, 0, 4, w, h); break;
203         case 3: clear_tiles(state, display, d, 4, 0, 4, 4, w, h); break;
204         }
205
206         clear_border(state, display, d, w, h, mask & ~best_mask);
207 }
208
209 static int
210 redraw_tile(struct app_state *state, Display *display, Drawable d,
211             uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2,
212             int x, int y, Dimension w, Dimension h)
213 {
214         uint_fast32_t pos = board_position(x, y);
215         unsigned char tile = 0;
216
217 #if X11_RENDER_DEBUG
218         if (d == XtWindow(state->game) || d == XtWindow(state->goal)) {
219                 XRectangle r = { x*w, y*h, w, h };
220                 XSetForeground(display, state->tile_gc, 0xff0000);
221                 XFillRectangles(display, d, state->tile_gc, &r, 1);
222                 XFlush(display);
223                 usleep(70000);
224         }
225 #endif
226
227         if (bit0 & pos) tile |= 1;
228         if (bit1 & pos) tile |= 2;
229         if (bit2 & pos) tile |= 4;
230         assert(tile < TILE_MAX);
231
232         if (tile == TILE_EMPTY) {
233                 XClearArea(display, d, x*w, y*h, w, h, 0);
234         } else {
235                 draw_tile(state, display, d, tile, x, y, w, h);
236         }
237
238         return tile;
239 }
240
241 static int
242 redraw_goal_tile(struct app_state *state, Display *display, Drawable d,
243                  int x, int y, Dimension w, Dimension h)
244 {
245         uint_least16_t *gp = state->board.goal;
246
247         return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h);
248 }
249
250 void x11_redraw_goal(struct app_state *state, uint_fast32_t mask)
251 {
252         Display *display = XtDisplay(state->goal);
253         Window goal = XtWindow(state->goal);
254         Dimension w, h;
255         int i;
256
257         XtVaGetValues(state->goal, XtNwidth, &w, XtNheight, &h, (char *)NULL);
258         w /= 3; h /= 3;
259
260         for (i = 0; i < 9; i++) {
261                 int x = i%3, y = i/3;
262
263                 if (mask & 1) {
264                         redraw_goal_tile(state, display, goal, x, y, w, h);
265                 }
266
267                 /*
268                  * Goal bitmaps have a gap of 2 tiles between each row.
269                  * This funny shift will accomodate that.
270                  */
271                 mask >>= 1|x;
272         }
273 }
274
275 /* Render the goal area as the window's icon */
276 void x11_redraw_icon(struct app_state *state, Widget shell)
277 {
278         Display *display = XtDisplay(shell);
279         Dimension tilesz = ICON_SIZE/3;
280         int i, j, tile;
281
282         XColor colours[(TILE_MAX-1)*COLOUR_MAX];
283         unsigned long *icons[ICON_MAX];
284         void *wm_icon = NULL;
285
286         if (state->use_ewmh_icons && (wm_icon = ewmh_icon_alloc(icons))) {
287                 Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
288
289                 for (i = 0; i < TILE_MAX-1; i++) {
290                         for (j = 0; j < COLOUR_MAX; j++) {
291                                 XColor *c = &colours[i*COLOUR_MAX+j];
292                                 c->pixel = state->tile_colour[i][j];
293                         }
294                 }
295
296                 XQueryColors(display, cmap, colours, i*j);
297         }
298
299         for (i = 0; i < 9; i++) {
300                 tile = redraw_goal_tile(state, display, state->icon_pixmap,
301                                         i%3, i/3, tilesz, tilesz);
302
303                 if (wm_icon) {
304                         XColor *c = &colours[(tile-1)*COLOUR_MAX];
305                         ewmh_tile16(icons[ICON_16x16], c, i%3, i/3);
306                         ewmh_tile24(icons[ICON_24x24], c, i%3, i/3);
307                         ewmh_tile32(icons[ICON_32x32], c, i%3, i/3);
308                         ewmh_tile48(icons[ICON_48x48], c, i%3, i/3);
309                 }
310         }
311
312         /*
313          * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
314          * notice the changed icon.
315          */
316         XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
317         XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
318
319         if (wm_icon) {
320                 Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
321
322                 XChangeProperty(display, XtWindow(shell), net_wm_icon,
323                                 XA_CARDINAL, 32, PropModeReplace,
324                                 wm_icon, EWMH_ICON_NELEM);
325
326                 free(wm_icon);
327         }
328 }
329
330 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
331 {
332         Display *display = XtDisplay(state->goal);
333         Window game = XtWindow(state->game);
334         uint_least32_t buf[3], *gp = state->board.game;
335         Dimension w, h;
336         int i;
337
338         XtVaGetValues(state->game, XtNwidth, &w, XtNheight, &h, (char *)NULL);
339         w /= 5; h /= 5;
340
341         if (state->view_goal_on_game) {
342                 for (i = 0; i < 3; i++) {
343                         buf[i] = state->board.goal[i];
344                         buf[i] = (gp[i] & ~GOAL_MASK) | (buf[i] << GOAL_SHIFT);
345                 }
346                 gp = buf;
347         }
348
349         /* Optimize the game end case where the outer frame is cleared */
350         if (((gp[0] | gp[1] | gp[2]) & ~GOAL_MASK) == 0) {
351                 clear_border(state, display, game, w, h, mask);
352                 mask &= GOAL_MASK;
353         }
354
355         for (i = 0; i < 25; i++) {
356                 if (mask & 1) {
357                         redraw_tile(state, display, game,
358                                     gp[0], gp[1], gp[2],
359                                     i%5, i/5, w, h);
360                 }
361                 mask >>= 1;
362         }
363 }