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