]> git.draconx.ca Git - rrace.git/commitdiff
motif: Combine status details into a single flags member.
authorNick Bowler <nbowler@draconx.ca>
Mon, 2 Jan 2023 22:15:27 +0000 (17:15 -0500)
committerNick Bowler <nbowler@draconx.ca>
Sat, 7 Jan 2023 16:43:13 +0000 (11:43 -0500)
The timer_tick, view_goal_on_game and use_ewmh_icons values all are used
to encode just a single bit of information.  We can combine them into
a single member to reduce the overall size of the state structure.

Dropping the timer ID further enables a slight reorganization of the
timer process which seems to reduce the overall code size somewhat.

src/motif.c
src/motif.h
src/version.c
src/version.h
src/x11.c

index fbdc9a750a2e86cb934109de3d3e9aebbb8ec791..66e31df1a56ad03e5b163450456f3b093ce58c3e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * X11 GUI for slide puzzle game
- * Copyright © 2022 Nick Bowler
+ * Copyright © 2022-2023 Nick Bowler
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -143,14 +143,13 @@ static void timer_tick(void *data, XtIntervalId *id)
 
        if (state->board.x > 4) {
                /* Game is over */
-               state->timer_tick = 0;
+               state->flags &= ~FLAG_TIMER_RUNNING;
                return;
        }
 
-       app = XtWidgetToApplicationContext(state->game);
        ui_timer_update(state, game_elapsed(&state->board));
-       state->timer_tick = XtAppAddTimeOut(app, TIMER_UPDATE_MS,
-                                           timer_tick, state);
+       app = XtWidgetToApplicationContext(state->game);
+       XtAppAddTimeOut(app, TIMER_UPDATE_MS, timer_tick, state);
 }
 
 static void do_input_move(struct app_state *state, int x, int y)
@@ -178,7 +177,9 @@ static void do_input_move(struct app_state *state, int x, int y)
 
 static void set_view_goal(struct app_state *state, int view_goal)
 {
-       state->view_goal_on_game = !!view_goal;
+       state->flags &= ~FLAG_VIEW_GOAL_ON_GAME;
+       state->flags |= view_goal ? FLAG_VIEW_GOAL_ON_GAME : 0;
+
        x11_redraw_game(state, game_check_goal(&state->board));
 }
 
@@ -231,18 +232,18 @@ static void proc_exit(Widget w, XEvent *e, String *argv, Cardinal *argc)
 
 static void proc_new_game(Widget w, XEvent *e, String *argv, Cardinal *argc)
 {
+       XtAppContext app;
+
        game_reset(&state.board);
 
        x11_redraw_goal(&state, -1, get_shell(w));
        x11_redraw_game(&state, -1);
 
-       if (!state.timer_tick) {
-               XtAppContext app = XtWidgetToApplicationContext(w);
-               state.timer_tick = XtAppAddTimeOut(app, TIMER_UPDATE_MS,
-                                                  timer_tick, &state);
-       }
-
        game_begin(&state.board);
+       if (!(state.flags & FLAG_TIMER_RUNNING)) {
+               state.flags |= FLAG_TIMER_RUNNING;
+               timer_tick(&state, 0);
+       }
 }
 
 static void proc_about(Widget w, XEvent *e, String *argv, Cardinal *argc)
@@ -312,7 +313,7 @@ static XtAppContext app_initialize(int argc, char **argv)
                state.board.game[i] = state.board.goal[i] << GOAL_SHIFT;
        game_finish(&state.board);
 
-       state.use_ewmh_icons = ewmh_probe_wm_icon(shell);
+       state.flags = ewmh_probe_wm_icon(shell);
        XtRealizeWidget(shell);
 
        x11_redraw_goal(&state, 0, shell);
index b168c4e5708498e67a5af71e14f722f87d947627..922f5c12459437e279cebd74aaf387eff3baf136 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * X11 GUI for slide puzzle game
- * Copyright © 2022 Nick Bowler
+ * Copyright © 2022-2023 Nick Bowler
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "colour.h"
 #include "game.h"
 
+/* If set, _NET_WM_ICON property will be added/updated to toplevel window. */
+#define FLAG_USE_EWMH_ICONS    1u
+
+/* If set, the goal will be displayed over the main play area. */
+#define FLAG_VIEW_GOAL_ON_GAME 2u
+
+/* If set, the Xt timer update process is currently installed. */
+#define FLAG_TIMER_RUNNING     4u
+
 struct app_state {
        struct board board;
 
        Widget game, goal;
        struct xcounter *timer;
 
-       XtIntervalId timer_tick;
        Pixmap icon_pixmap;
        GC tile_gc;
 
        uint_least32_t render_game_mask;
        uint_least16_t render_goal_mask;
        uint_least16_t game_tile_sz, goal_tile_sz;
-
-       /* If true, the goal will be displayed over the main play area. */
-       uint_least8_t view_goal_on_game;
-
-       /* Whether to set _NET_WM_ICON property on WMShell */
-       uint_least8_t use_ewmh_icons;
+       uint_least16_t flags;
 
        uint_least32_t tile_colour[TILE_MAX-1][3];
 };
index 6a5ce9aab0b01c79d7ef37aba3222996684ab55d..de174b554e1f1ca5435c1ddf7428de3cd2b397f1 100644 (file)
@@ -1,3 +1,21 @@
+/*
+ * Version string boilerplate for slide puzzle game.
+ * Copyright © 2022-2023 Nick Bowler
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
 #include <config.h>
 #include <stdio.h>
 #include <stdarg.h>
@@ -19,7 +37,7 @@ const char *init_copysign(char **alloc)
        return "(C)";
 }
 
-#define VERSION_HEAD_FMT "%s (RRace) %s\nCopyright %s 2022 Nick Bowler"
+#define VERSION_HEAD_FMT "%s (RRace) %s\nCopyright %s 2023 Nick Bowler"
 #define VERSION_HEAD_ARGS progname, PACKAGE_VERSION, copysign
 
 void version_print_head(const char *progname, FILE *f)
index 5c3850f748a7d48ccdf1f56c54294ac7c405f048..b8d4f7817dcae7c0f84a559ec28ff8c83d12c9b3 100644 (file)
@@ -1,3 +1,21 @@
+/*
+ * Version string boilerplate for slide puzzle game.
+ * Copyright © 2022-2023 Nick Bowler
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
 #ifndef RRACE_VERSION_H_
 #define RRACE_VERSION_H_
 
index 459694a4d96a122e103f2e8883316744d1940d2c..44bb8670ebec32c805917630acc9b3e32f1158d5 100644 (file)
--- a/src/x11.c
+++ b/src/x11.c
@@ -1,6 +1,6 @@
 /*
  * X11 GUI for slide puzzle game
- * Copyright © 2022 Nick Bowler
+ * Copyright © 2022-2023 Nick Bowler
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -214,7 +214,7 @@ static void set_icon(struct app_state *state, Display *display,
        XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
        XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
 
-       if (state->use_ewmh_icons) {
+       if (state->flags & FLAG_USE_EWMH_ICONS) {
                Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
                Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
                XColor colours[(TILE_MAX-1)*COLOUR_MAX];
@@ -285,7 +285,7 @@ void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
        unsigned sz = state->game_tile_sz;
        int i;
 
-       if (state->view_goal_on_game) {
+       if (state->flags & FLAG_VIEW_GOAL_ON_GAME) {
                for (i = 0; i < 3; i++) {
                        buf[i] = state->board.goal[i];
                        buf[i] = (gp[i] & ~GOAL_MASK) | (buf[i] << GOAL_SHIFT);