2 * Curses UI 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/>.
28 #include "cursesopt.h"
31 #define MIN(a, b) ((a) < (b) ? (a) : (b))
32 #define MAX(a, b) ((a) > (b) ? (a) : (b))
35 GAME_YPOS = 1 // top row of game and goal areas.
38 static const char *progname = "rrace";
39 static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
48 static struct app_state {
51 WINDOW *gamewin[WINDOW_MAX], *goalwin[WINDOW_MAX];
56 static void print_version(void)
58 version_print_head("rrace-curses", stdout);
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 void draw_tile(WINDOW **win, unsigned colour, unsigned selected,
91 unsigned x, unsigned y, unsigned start_column)
93 WINDOW *border = win[WINDOW_TILEBORDER], *fill = win[WINDOW_TILEFILL];
94 int w, h, attr, ch, bc = selected ? '#' : 0;
96 assert(colour < TILE_MAX);
97 attr = COLOR_PAIR(colour);
99 case TILE_RED: ch = 'X'; break;
100 case TILE_ORANGE: ch = '|'; break;
101 case TILE_GREEN: ch = '+'; break;
102 case TILE_YELLOW: ch = '~'; attr |= A_BOLD; break;
103 case TILE_BLUE: ch = 'o'; attr |= A_BOLD; break;
104 case TILE_WHITE: ch = '.'; attr |= A_BOLD; break;
106 case TILE_EMPTY: attr = A_BOLD|COLOR_PAIR(TILE_MAX);
109 getmaxyx(border, h, w);
112 if (mvwin(border, 1+GAME_YPOS+h*y, start_column+w*x) == ERR)
115 if (colour != TILE_EMPTY) {
116 mvderwin(fill, 1, 1);
117 wbkgdset(fill, A_REVERSE|attr|ch);
124 wattrset(border, attr);
125 wborder(border, bc, bc, bc, bc, bc, bc, bc, bc);
128 wnoutrefresh(border);
132 redraw_tile(WINDOW **win, unsigned x, unsigned y, unsigned start_column,
133 uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2,
136 uint_fast32_t pos = board_position(x, y);
137 unsigned char tile = 0;
139 if (bit0 & pos) tile |= 1;
140 if (bit1 & pos) tile |= 2;
141 if (bit2 & pos) tile |= 4;
142 assert(tile < TILE_MAX);
144 draw_tile(win, tile, selected, x, y, start_column);
148 static void redraw_area_border(WINDOW **win, unsigned x, unsigned sz)
150 int w, h, tr = 0, rs = 0, br = 0, bs = 0, bl = 0;
151 WINDOW *area = win[WINDOW_AREA];
153 getmaxyx(stdscr, h, w);
155 if (h <= GAME_YPOS+1) {
156 bl = ACS_ULCORNER, br = ACS_URCORNER;
157 } else if (h <= 3*sz+GAME_YPOS+1) {
158 bl = br = ACS_VLINE, bs = ' ';
162 tr = ACS_ULCORNER, br = ACS_LLCORNER;
163 } else if (w <= 6*sz+x+2) {
164 tr = ACS_HLINE, rs = ' ';
165 br = br ? ' ' : ACS_HLINE;
168 wborder(area, 0, rs, 0, bs, 0, tr, bl, br);
172 static void curs_redraw_game(struct app_state *state, uint_fast32_t mask)
174 uint_least32_t *gp = state->board.game;
178 redraw_area_border(state->gamewin, 2, 5);
180 for (i = 0; i < 25; i++) {
182 redraw_tile(state->gamewin, i%5, i/5,
183 4, gp[0], gp[1], gp[2],
190 static void curs_redraw_goal(struct app_state *state, uint_fast32_t mask)
192 uint_least16_t *gp = state->board.goal;
195 if (!state->goalwin[WINDOW_AREA])
198 getbegyx(state->goalwin[WINDOW_AREA], y, x);
200 redraw_area_border(state->goalwin, x, 3);
202 for (i = 0; i < 9; i++) {
204 redraw_tile(state->goalwin, i%3, i/3,
205 x+2, gp[0], gp[1], gp[2], 0);
211 static WINDOW *realloc_area(WINDOW **orig, int h, int w, int y, int x)
216 #if HAVE_CURSES_WRESIZE
217 if (wresize(win, h, w) != ERR) {
226 return *orig = subwin(stdscr, h, w, y, x);
230 static void realloc_tiles(WINDOW **win, int h)
232 WINDOW *border = win[WINDOW_TILEBORDER], *fill = win[WINDOW_TILEFILL];
235 if (fill && border) {
238 #if HAVE_CURSES_WRESIZE
239 if (wresize(fill, h-2, w-2) != ERR
240 && wresize(border, h, w) != ERR)
246 getmaxyx(border, old_h, old_w);
256 win[WINDOW_TILEBORDER] = border = newwin(h, w, 0, 0);
257 win[WINDOW_TILEFILL] = derwin(border, h-2, w-2, 1, 1);
260 static void setup_mainwin(struct app_state *state)
262 int w, h, gamesz, goalsz, scr_w, scr_h, split;
264 getmaxyx(stdscr, scr_h, scr_w);
266 /* First try to fit the game tiles based on window height. */
267 gamesz = MAX(3, (scr_h - 4) / 5);
269 /* Adjust downward until we can fit smallest possible goal area. */
270 for (; split = 5+10*gamesz, gamesz > 3; gamesz--) {
271 if (split + 20 < scr_w)
275 /* Pick a goal size that will fit in the remaining area */
276 goalsz = MAX(3, (scr_w - split - 4) / 6);
277 if (goalsz >= gamesz)
278 goalsz = MAX(3, gamesz - 1);
280 realloc_tiles(state->gamewin, gamesz);
281 realloc_tiles(state->goalwin, goalsz);
283 /* Frame for game area */
284 w = MIN(scr_w-2, 3+10*gamesz);
285 h = MIN(scr_h-GAME_YPOS, 2+5*gamesz);
286 realloc_area(&state->gamewin[WINDOW_AREA], h, w, GAME_YPOS, 2);
288 /* Frame for goal area */
289 w = MIN(scr_w-split, 3+6*goalsz);
290 h = MIN(scr_h-GAME_YPOS, 2+3*goalsz);
291 realloc_area(&state->goalwin[WINDOW_AREA], h, w, GAME_YPOS, split);
294 w = MAX(0, scr_w-split-1);
295 realloc_area(&state->timer, 1, w, GAME_YPOS+h, split+1);
298 static void app_initialize(int argc, char **argv)
300 int enable_mouse = 1;
306 while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
326 game_reset(&state.board);
334 keypad(stdscr, TRUE);
337 * While we only care about a subset of these events, for
338 * some reason with ncurses failing to enable all of them
339 * causes timeout to stop working when disabled buttons are
340 * pressed (or released).
342 * It is not known if other curses have this problem; at least
343 * pdcurses does not, but the extra events should be harmless
346 #if HAVE_CURSES_MOUSE_SUPPORT
347 unsigned long mask = BUTTON1_PRESSED | BUTTON1_RELEASED
348 | BUTTON2_PRESSED | BUTTON2_RELEASED
349 | BUTTON3_PRESSED | BUTTON3_RELEASED
350 #ifdef BUTTON4_PRESSED
351 | BUTTON4_PRESSED | BUTTON4_RELEASED
353 #ifdef BUTTON5_PRESSED
354 | BUTTON5_PRESSED | BUTTON5_RELEASED
357 #if HAVE_CURSES_MOUSE_SET
359 #elif HAVE_CURSES_MOUSEMASK
360 mousemask(mask, NULL);
362 #if HAVE_CURSES_MOUSEINTERVAL
365 #endif /* HAVE_CURSES_MOUSE_SUPPORT */
370 init_pair(TILE_RED, COLOR_RED, COLOR_BLACK);
371 init_pair(TILE_ORANGE, COLOR_YELLOW, COLOR_BLACK);
372 init_pair(TILE_YELLOW, COLOR_YELLOW, COLOR_BLACK);
373 init_pair(TILE_GREEN, COLOR_GREEN, COLOR_BLACK);
374 init_pair(TILE_BLUE, COLOR_BLUE, COLOR_BLACK);
375 init_pair(TILE_WHITE, COLOR_WHITE, COLOR_BLACK);
376 init_pair(TILE_MAX, COLOR_BLACK, COLOR_BLACK);
378 setup_mainwin(&state);
382 static uint_fast32_t do_move(struct app_state *state, int x, int y)
386 if ((mask = game_do_move(&state->board, x, y)) != 0) {
387 curs_redraw_game(state, mask);
394 static void do_reset_cursor(struct app_state *state)
396 state->cursor = 5*state->board.y + state->board.x;
399 static void update_timer(struct app_state *state, uint_fast32_t ms)
403 sec = ms / 1000; ms %= 1000;
404 min = sec / 60; sec %= 60;
405 mvwprintw(state->timer, 0, 0, "Time: %u:%.2u.%.3u",
406 min, sec, (unsigned)ms);
407 wclrtoeol(state->timer);
408 wrefresh(state->timer);
411 static void do_new_game(struct app_state *state)
413 game_reset(&state->board);
415 do_reset_cursor(state);
416 curs_redraw_game(state, -1);
417 curs_redraw_goal(state, -1);
418 update_timer(state, 0);
422 game_begin(&state->board);
425 #if HAVE_CURSES_MOUSE_SUPPORT
426 static void do_mouse(struct app_state *state)
428 unsigned long bstate;
431 #if HAVE_CURSES_GETMOUSE_NCURSES
434 if (getmouse(&mev) == ERR)
437 x = mev.x, y = mev.y;
439 #elif HAVE_CURSES_REQUEST_MOUSE_POS
446 #define set_bstate_helper(button) do { if (BUTTON_CHANGED(button)) \
447 switch (BUTTON_STATUS(button)) { \
448 case BUTTON_RELEASED: bstate |= BUTTON ## button ## _RELEASED; break; \
449 case BUTTON_PRESSED: bstate |= BUTTON ## button ## _PRESSED; break; \
453 set_bstate_helper(1);
454 set_bstate_helper(3);
456 if (bstate == BUTTON1_PRESSED) {
457 uint_fast32_t cursor_mask, move_mask;
460 /* Determine size of the game area */
461 getmaxyx(state->gamewin[WINDOW_TILEBORDER], h, w);
464 if (x < 4 || (x -= 4)/5 >= w) return;
465 if (y <= GAME_YPOS || (y -= GAME_YPOS+1)/5 >= h) return;
467 /* Turn off the keyboard cursor when using the mouse */
468 cursor_mask = state->cursor < 0 ? -1 : 1ul << state->cursor;
471 move_mask = do_move(state, x/w, y/h);
472 if ((cursor_mask & move_mask) == 0) {
473 curs_redraw_game(state, cursor_mask);
480 static void do_move_cursor(struct app_state *state, int c)
482 uint_fast32_t mask = 1ul << state->cursor;
484 if (state->cursor < 0) {
485 /* Cursor was hidden; reset it */
486 do_reset_cursor(state);
491 if ((state->cursor -= 5) < 0)
495 if ((state->cursor += 5) >= 25)
499 if ((state->cursor -= 1) % 5 == 4 || state->cursor < 0)
503 if ((state->cursor += 1) % 5 == 0)
508 curs_redraw_game(state, mask | 1ul << state->cursor);
512 static void do_keystroke(struct app_state *state, int c)
515 case KEY_DOWN: case KEY_UP: case KEY_LEFT: case KEY_RIGHT:
516 do_move_cursor(state, c);
519 if (state->cursor >= 0)
520 do_move(state, state->cursor%5, state->cursor/5);
525 int main(int argc, char **argv)
527 setlocale(LC_ALL, "");
528 app_initialize(argc, argv);
538 setup_mainwin(&state);
541 curs_redraw_game(&state, -1);
542 curs_redraw_goal(&state, -1);
546 #if HAVE_CURSES_MOUSE_SUPPORT
552 do_keystroke(&state, c);
556 update_timer(&state, game_elapsed(&state.board));