return shell;
}
+static void do_input_move(struct app_state *state, int x, int y)
+{
+ uint_fast32_t mask;
+
+ if ((mask = game_do_move(&state->board, x, y)) != 0) {
+ if (game_check_goal(&state->board) == 0) {
+ int_fast32_t ms = game_finish(&state->board);
+ unsigned min, sec;
+
+ /* Negative time just means clock jumps and
+ * display headaches. */
+ if (ms < 0)
+ ms = 0;
+
+ sec = ms / 1000, ms %= 1000;
+ min = sec / 60, sec %= 60;
+ printf("You won! Time was %u:%.2u:%.3u\n",
+ min, sec, (unsigned)ms);
+ mask |= ~GOAL_MASK;
+ }
+
+ x11_redraw_game(state, mask);
+ }
+}
+
+static void set_view_goal(struct app_state *state, int view_goal)
+{
+ state->view_goal_on_game = view_goal;
+ x11_redraw_game(state, game_check_goal(&state->board));
+}
+
+static void game_input(Widget w, void *data, void *cb_data)
+{
+ XmDrawingAreaCallbackStruct *cbs = cb_data;
+ XButtonEvent *b = &cbs->event->xbutton;
+ struct app_state *state = data;
+ Dimension width, height;
+
+ switch (cbs->event->type) {
+ case ButtonPress:
+ switch (b->button) {
+ case Button1:
+ if (b->state & Button3Mask)
+ break;
+
+ XtVaGetValues(w, XmNwidth, &width,
+ XmNheight, &height,
+ (char *)NULL);
+
+ do_input_move(state, b->x / (width / 5),
+ b->y / (height / 5));
+ break;
+ case Button3:
+ set_view_goal(state, 1);
+ break;
+ }
+ break;
+ case ButtonRelease:
+ switch (b->button) {
+ case Button3:
+ set_view_goal(state, 0);
+ break;
+ }
+ }
+}
+
static struct app_state state;
static void proc_exit(Widget w, XEvent *e, String *argv, Cardinal *argc)
ui_initialize(&state, shell);
x11_initialize(&state, shell);
+ XtAddCallback(state.game, XmNinputCallback, game_input, &state);
+
/* Begin with the game in winning state */
game_reset(&state.board);
for (i = 0; i < 3; i++)
XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL);
}
-static void game_configure(Widget form)
+static void configure_mainwin(struct app_state *state, Widget form)
{
Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
assert(gamearea && goalarea);
XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
+ state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
XmNtopAttachment, XmATTACH_FORM,
(char *)NULL);
+ state->goal = XtNameToWidget(goalarea, &tree_strtab[goalCanvas]);
XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
XmNleftWidget, gamearea,
XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
x11_redraw_goal(data, -1);
}
-static void do_input_move(struct app_state *state, int x, int y)
-{
- uint_fast32_t mask;
-
- if ((mask = game_do_move(&state->board, x, y)) != 0) {
- if (game_check_goal(&state->board) == 0) {
- int_fast32_t ms = game_finish(&state->board);
- unsigned min, sec;
-
- /* Negative time just means clock jumps and
- * display headaches. */
- if (ms < 0)
- ms = 0;
-
- sec = ms / 1000, ms %= 1000;
- min = sec / 60, sec %= 60;
- printf("You won! Time was %u:%.2u:%.3u\n",
- min, sec, (unsigned)ms);
- mask |= ~GOAL_MASK;
- }
-
- x11_redraw_game(state, mask);
- }
-}
-
-static void set_view_goal(struct app_state *state, int view_goal)
-{
- state->view_goal_on_game = view_goal;
- x11_redraw_game(state, game_check_goal(&state->board));
-}
-
-static void game_input(Widget w, void *data, void *cb_data)
-{
- XmDrawingAreaCallbackStruct *cbs = cb_data;
- XButtonEvent *b = &cbs->event->xbutton;
- struct app_state *state = data;
- Dimension width, height;
-
- switch (cbs->event->type) {
- case ButtonPress:
- switch (b->button) {
- case Button1:
- if (b->state & Button3Mask)
- break;
-
- XtVaGetValues(w, XmNwidth, &width,
- XmNheight, &height,
- (char *)NULL);
-
- do_input_move(state, b->x / (width / 5),
- b->y / (height / 5));
- break;
- case Button3:
- set_view_goal(state, 1);
- break;
- }
- break;
- case ButtonRelease:
- switch (b->button) {
- case Button3:
- set_view_goal(state, 0);
- break;
- }
- }
-}
-
/* Figure out which tiles intersect a rectangle. */
static uint_fast32_t
expose_mask(int rect_x, int rect_y, int rect_w, int rect_h,
help = XtNameToWidget(menubar, "helpMenu");
XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
- game_configure(XtNameToWidget(shell, "*game"));
-
- state->game = XtNameToWidget(shell, "*gameCanvas");
+ configure_mainwin(state, XtNameToWidget(shell, "*game"));
XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
XtAddCallback(state->game, XmNexposeCallback, game_expose, state);
- XtAddCallback(state->game, XmNinputCallback, game_input, state);
-
- state->goal = XtNameToWidget(shell, "*goalCanvas");
XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state);
}