]> git.draconx.ca Git - rrace.git/blob - src/x11.c
598b11542fd3705772bd603fcece267bb8298a3f
[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 <assert.h>
22 #include <Xm/XmStrDefs.h>
23 #include "motif.h"
24
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 */
34 };
35
36 static void init_colours(struct app_state *state, Screen *screen)
37 {
38         Display *display = DisplayOfScreen(screen);
39         Colormap cmap = DefaultColormapOfScreen(screen);
40         XColor colour, junk;
41         unsigned i, j;
42
43         for (j = 0; j < COLOUR_MAX; j++) {
44                 for (i = 0; i < TILE_MAX-1; i++) {
45                         XAllocNamedColor(display, cmap, colours[i][j],
46                                                   &colour, &junk);
47                         state->tile_colour[i][j] = colour.pixel;
48                 }
49         }
50 }
51
52 void x11_initialize(struct app_state *state, Screen *screen)
53 {
54         Display *display = DisplayOfScreen(screen);
55         Window root = RootWindowOfScreen(screen);
56         Colormap cmap = DefaultColormapOfScreen(screen);
57         XGCValues gcv;
58
59         init_colours(state, screen);
60
61         gcv.line_width = 1;
62         state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
63 }
64
65 static void draw_tile(struct app_state *state, Display *display, Drawable d,
66                       int tile, int gx, int gy, Dimension tw, Dimension th)
67 {
68         int tx = gx * tw, ty = gy * th;
69
70         XSegment topshadow[] = {
71                 { tx,      ty,      tx,      ty+th   },
72                 { tx+1,    ty,      tx+tw,   ty      },
73
74                 { tx+1,    ty+1,    tx+1,    ty+th-1 },
75                 { tx+2,    ty+1,    tx+tw-1, ty+1    }
76         };
77
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    },
81
82                 { tx+2,    ty+th-2, tx+tw-1, ty+th-2 },
83                 { tx+tw-2, ty+th-2, tx+tw-2, ty+2    }
84         };
85
86         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
87         XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
88
89         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
90         XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
91
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);
94 }
95
96 static void
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)
100 {
101         uint_fast32_t pos = board_position(x, y);
102         unsigned char tile = 0;
103
104         if (bit0 & pos) tile |= 1;
105         if (bit1 & pos) tile |= 2;
106         if (bit2 & pos) tile |= 4;
107         assert(tile < TILE_MAX);
108
109         if (tile == TILE_EMPTY) {
110                 XClearArea(display, d, x*w, y*h, w, h, 0);
111         } else {
112                 draw_tile(state, display, d, tile, x, y, w, h);
113         }
114 }
115
116 void x11_redraw_goal(struct app_state *state)
117 {
118         Display *display = XtDisplay(state->goal);
119         Window goal = XtWindow(state->goal);
120         Dimension w, h;
121         int i;
122
123         XtVaGetValues(state->goal, XmNwidth, &w, XmNheight, &h, (char *)NULL);
124         w /= 3; h /= 3;
125
126         for (i = 0; i < 9; i++) {
127                 uint_least16_t *gp = state->board.goal;
128
129                 redraw_tile(state, display, goal,
130                             gp[0], gp[1], gp[2],
131                             i%3, i/3, w, h);
132         }
133 }
134
135 void x11_redraw_game(struct app_state *state)
136 {
137         Display *display = XtDisplay(state->goal);
138         Window game = XtWindow(state->game);
139         Dimension w, h;
140         int i;
141
142         XtVaGetValues(state->game, XmNwidth, &w, XmNheight, &h, (char *)NULL);
143         w /= 5; h /= 5;
144
145         for (i = 0; i < 25; i++) {
146                 uint_least32_t *gp = state->board.game;
147
148                 redraw_tile(state, display, game,
149                             gp[0], gp[1], gp[2],
150                             i%5, i/5, w, h);
151         }
152 }