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/>.
32 #define TIMER_UPDATE_MS 33
34 #define PROGNAME "rrace"
35 static const char *progname = PROGNAME;
36 static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
38 static char * const default_resources[] = {
40 "*game.height: 371", // 365 + 2*shadowThickness
43 "*game.XmFrame.shadowThickness: 3",
44 "*game.XmFrame.shadowType: shadow_in",
45 "*goalArea.leftOffset: 1",
47 "*gameNew.accelerator: Ctrl<Key>N",
48 "*gameNew.acceleratorText: Ctrl+N",
49 "*gameExit.accelerator: Ctrl<Key>Q",
50 "*gameExit.acceleratorText: Ctrl+Q",
55 static void print_version(void)
57 printf("%s %s\n", PROGNAME, PACKAGE_VERSION);
58 printf("Copyright (C) 2022 Nick Bowler\n");
59 puts("License GPLv3+: GNU GPL version 3 or any later version");
60 puts("This is free software: you are free to change and redistribute it.");
61 puts("There is NO WARRANTY, to the extent permitted by law.");
64 static void print_usage(FILE *f)
66 fprintf(f, "Usage: %s [options]\n", progname);
68 fprintf(f, "Try %s --help for more information.\n", progname);
71 static void print_help(void)
73 struct lopt_help help = {0};
74 const struct option *opt;
80 for (opt = lopts; opt->name; opt++) {
81 if (!lopt_get_help(opt, &help))
83 help_print_option(opt, help.arg, help.desc, 20);
87 printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
90 static Widget early_setup(XtAppContext *app, int argc, char **argv)
95 /* Check for --help/--version early (before X connection) */
97 while ((opt = getopt_long_only(argc, argv, "", lopts, NULL)) != -1) {
108 shell = XtOpenApplication(app, PACKAGE_TARNAME, NULL, 0,
109 &argc, argv, (String *)default_resources,
110 sessionShellWidgetClass, NULL, 0);
113 while ((opt = getopt_long_only(argc, argv, "", lopts, NULL)) != -1) {
122 fprintf(stderr, "%s: excess command-line arguments.\n",
131 static void timer_tick(void *data, XtIntervalId *id)
133 struct app_state *state = data;
136 if (state->board.x > 4) {
138 state->timer_tick = 0;
142 app = XtWidgetToApplicationContext(state->timer);
143 ui_timer_update(state, game_elapsed(&state->board));
144 state->timer_tick = XtAppAddTimeOut(app, TIMER_UPDATE_MS,
148 static void do_input_move(struct app_state *state, int x, int y)
152 if ((mask = game_do_move(&state->board, x, y)) != 0) {
153 if (game_check_goal(&state->board) == 0) {
154 int_fast32_t ms = game_finish(&state->board);
157 ui_timer_update(state, ms);
159 sec = ms / 1000, ms %= 1000;
160 min = sec / 60, sec %= 60;
161 printf("You won! Time was %u:%.2u.%.3u\n",
162 min, sec, (unsigned)ms);
167 x11_redraw_game(state, mask);
171 static void set_view_goal(struct app_state *state, int view_goal)
173 state->view_goal_on_game = view_goal;
174 x11_redraw_game(state, game_check_goal(&state->board));
177 static void game_input(Widget w, void *data, void *cb_data)
179 XmDrawingAreaCallbackStruct *cbs = cb_data;
180 XButtonEvent *b = &cbs->event->xbutton;
181 struct app_state *state = data;
182 Dimension width, height;
184 switch (cbs->event->type) {
188 if (b->state & Button3Mask)
191 XtVaGetValues(w, XmNwidth, &width,
195 do_input_move(state, b->x / (width / 5),
196 b->y / (height / 5));
199 set_view_goal(state, 1);
206 set_view_goal(state, 0);
212 static struct app_state state;
214 static void proc_exit(Widget w, XEvent *e, String *argv, Cardinal *argc)
216 XtAppSetExitFlag(XtWidgetToApplicationContext(w));
219 static void proc_new_game(Widget w, XEvent *e, String *argv, Cardinal *argc)
223 for (shell = w; !XtIsWMShell(shell);)
224 shell = XtParent(shell);
226 game_reset(&state.board);
228 x11_redraw_goal(&state, -1);
229 x11_redraw_game(&state, -1);
230 x11_redraw_icon(&state, shell);
232 if (!state.timer_tick) {
233 XtAppContext app = XtWidgetToApplicationContext(w);
234 state.timer_tick = XtAppAddTimeOut(app, TIMER_UPDATE_MS,
238 game_begin(&state.board);
241 static const XtActionsRec menu_actions[] = {
242 { "gameNew", proc_new_game },
243 { "gameExit", proc_exit }
246 static XtAppContext app_initialize(int argc, char **argv)
255 shell = early_setup(&app, argc, argv);
256 XtAppAddActions(app, (void *)menu_actions, XtNumber(menu_actions));
257 ui_initialize(&state, shell);
258 x11_initialize(&state, shell);
260 XtAddCallback(state.game, XmNinputCallback, game_input, &state);
262 /* Begin with the game in winning state */
263 game_reset(&state.board);
264 for (i = 0; i < 3; i++)
265 state.board.game[i] = state.board.goal[i] << GOAL_SHIFT;
266 game_finish(&state.board);
268 state.use_ewmh_icons = ewmh_probe_wm_icon(shell);
269 XtRealizeWidget(shell);
271 x11_redraw_icon(&state, shell);
276 int main(int argc, char **argv)
278 setlocale(LC_ALL, "");
279 XtAppMainLoop(app_initialize(argc, argv));