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))
34 static const char *progname = "rrace";
35 static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
44 static struct app_state {
47 WINDOW *gamewin[WINDOW_MAX], *goalwin[WINDOW_MAX];
51 static void print_version(void)
53 version_print_head("rrace-curses", stdout);
54 puts("License GPLv3+: GNU GPL version 3 or any later version");
55 puts("This is free software: you are free to change and redistribute it.");
56 puts("There is NO WARRANTY, to the extent permitted by law.");
59 static void print_usage(FILE *f)
61 fprintf(f, "Usage: %s [options]\n", progname);
63 fprintf(f, "Try %s --help for more information.\n", progname);
66 static void print_help(void)
68 struct lopt_help help = {0};
69 const struct option *opt;
75 for (opt = lopts; opt->name; opt++) {
76 if (!lopt_get_help(opt, &help))
78 help_print_option(opt, help.arg, help.desc, 20);
82 printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
85 static void draw_tile(WINDOW **win, unsigned colour, unsigned selected,
86 unsigned x, unsigned y, unsigned start_column)
88 WINDOW *border = win[WINDOW_TILEBORDER], *fill = win[WINDOW_TILEFILL];
89 int w, h, attr, ch, bc = selected ? '#' : 0;
91 assert(colour < TILE_MAX);
92 attr = COLOR_PAIR(colour);
94 case TILE_RED: ch = 'X'; break;
95 case TILE_ORANGE: ch = '|'; break;
96 case TILE_GREEN: ch = '+'; break;
97 case TILE_YELLOW: ch = '~'; attr |= A_BOLD; break;
98 case TILE_BLUE: ch = 'o'; attr |= A_BOLD; break;
99 case TILE_WHITE: ch = '.'; attr |= A_BOLD; break;
101 case TILE_EMPTY: attr = A_BOLD|COLOR_PAIR(TILE_MAX);
104 getmaxyx(border, h, w);
107 if (mvwin(border, 2+h*y, start_column+w*x) == ERR)
110 if (colour != TILE_EMPTY) {
111 mvderwin(fill, 1, 1);
112 wbkgdset(fill, A_REVERSE|attr|ch);
119 wattrset(border, attr);
120 wborder(border, bc, bc, bc, bc, bc, bc, bc, bc);
123 wnoutrefresh(border);
127 redraw_tile(WINDOW **win, unsigned x, unsigned y, unsigned start_column,
128 uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2,
131 uint_fast32_t pos = board_position(x, y);
132 unsigned char tile = 0;
134 if (bit0 & pos) tile |= 1;
135 if (bit1 & pos) tile |= 2;
136 if (bit2 & pos) tile |= 4;
137 assert(tile < TILE_MAX);
139 draw_tile(win, tile, selected, x, y, start_column);
143 static void redraw_area_border(WINDOW **win, unsigned x, unsigned sz)
145 int w, h, tr = 0, rs = 0, br = 0, bs = 0, bl = 0;
146 WINDOW *area = win[WINDOW_AREA];
148 getmaxyx(stdscr, h, w);
151 bl = ACS_ULCORNER, br = ACS_URCORNER;
152 } else if (h <= 3*sz+2) {
153 bl = br = ACS_VLINE, bs = ' ';
157 tr = ACS_ULCORNER, br = ACS_LLCORNER;
158 } else if (w <= 6*sz+x+2) {
159 tr = ACS_HLINE, rs = ' ';
160 br = br ? ' ' : ACS_HLINE;
163 wborder(area, 0, rs, 0, bs, 0, tr, bl, br);
167 static void curs_redraw_game(struct app_state *state, uint_fast32_t mask)
169 uint_least32_t *gp = state->board.game;
173 redraw_area_border(state->gamewin, 2, 5);
175 for (i = 0; i < 25; i++) {
177 redraw_tile(state->gamewin, i%5, i/5,
178 4, gp[0], gp[1], gp[2],
185 static void curs_redraw_goal(struct app_state *state, uint_fast32_t mask)
187 uint_least16_t *gp = state->board.goal;
190 if (!state->goalwin[WINDOW_AREA])
193 getbegyx(state->goalwin[WINDOW_AREA], y, x);
195 redraw_area_border(state->goalwin, x, 3);
197 for (i = 0; i < 9; i++) {
199 redraw_tile(state->goalwin, i%3, i/3,
200 x+2, gp[0], gp[1], gp[2], 0);
206 static WINDOW *realloc_area(WINDOW **orig, int h, int w, int y, int x)
211 #if HAVE_CURSES_WRESIZE
212 if (wresize(win, h, w) != ERR) {
221 return *orig = subwin(stdscr, h, w, y, x);
225 static void realloc_tiles(WINDOW **win, int h)
227 WINDOW *border = win[WINDOW_TILEBORDER], *fill = win[WINDOW_TILEFILL];
230 if (fill && border) {
233 #if HAVE_CURSES_WRESIZE
234 if (wresize(fill, h-2, w-2) != ERR
235 && wresize(border, h, w) != ERR)
241 getmaxyx(border, old_h, old_w);
251 win[WINDOW_TILEBORDER] = border = newwin(h, w, 0, 0);
252 win[WINDOW_TILEFILL] = derwin(border, h-2, w-2, 1, 1);
255 static void setup_mainwin(struct app_state *state)
257 int w, h, gamesz, goalsz, scr_w, scr_h, split;
259 getmaxyx(stdscr, scr_h, scr_w);
261 /* First try to fit the game tiles based on window height. */
262 gamesz = MAX(3, (scr_h - 4) / 5);
264 /* Adjust downward until we can fit smallest possible goal area. */
265 for (; split = 5+10*gamesz, gamesz > 3; gamesz--) {
266 if (split + 20 < scr_w)
270 /* Pick a goal size that will fit in the remaining area */
271 goalsz = MAX(3, (scr_w - split - 4) / 6);
272 if (goalsz >= gamesz)
273 goalsz = MAX(3, gamesz - 1);
275 realloc_tiles(state->gamewin, gamesz);
276 realloc_tiles(state->goalwin, goalsz);
278 /* Frame for game area */
279 w = MIN(scr_w-2, 3+10*gamesz);
280 h = MIN(scr_h-1, 2+5*gamesz);
281 realloc_area(&state->gamewin[WINDOW_AREA], h, w, 1, 2);
283 /* Frame for goal area */
284 w = MIN(scr_w-split, 3+6*goalsz);
285 h = MIN(scr_h-1, 2+3*goalsz);
286 realloc_area(&state->goalwin[WINDOW_AREA], h, w, 1, split);
289 static void app_initialize(int argc, char **argv)
291 int enable_mouse = 1;
297 while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
317 game_reset(&state.board);
318 state.cursor = 5*state.board.y + state.board.x;
322 if (curs_set(0) != ERR)
323 leaveok(stdscr, TRUE);
326 keypad(stdscr, TRUE);
328 #if HAVE_CURSES_MOUSE_SET
329 mouse_set(BUTTON1_PRESSED);
330 #elif HAVE_CURSES_MOUSEMASK
331 mousemask(BUTTON1_PRESSED, NULL);
333 #if HAVE_CURSES_MOUSEINTERVAL
340 init_pair(TILE_RED, COLOR_RED, COLOR_BLACK);
341 init_pair(TILE_ORANGE, COLOR_YELLOW, COLOR_BLACK);
342 init_pair(TILE_YELLOW, COLOR_YELLOW, COLOR_BLACK);
343 init_pair(TILE_GREEN, COLOR_GREEN, COLOR_BLACK);
344 init_pair(TILE_BLUE, COLOR_BLUE, COLOR_BLACK);
345 init_pair(TILE_WHITE, COLOR_WHITE, COLOR_BLACK);
346 init_pair(TILE_MAX, COLOR_BLACK, COLOR_BLACK);
348 setup_mainwin(&state);
352 static uint_fast32_t do_move(struct app_state *state, int x, int y)
356 if ((mask = game_do_move(&state->board, x, y)) != 0) {
357 curs_redraw_game(state, mask);
364 #if HAVE_CURSES_MOUSE_SUPPORT
365 static void do_mouse(struct app_state *state)
367 unsigned long bstate;
370 #if HAVE_CURSES_GETMOUSE_NCURSES
373 if (getmouse(&mev) == ERR)
376 x = mev.x, y = mev.y;
378 #elif HAVE_CURSES_REQUEST_MOUSE_POS
384 if (BUTTON_CHANGED(1)) {
385 switch (BUTTON_STATUS(1)) {
386 case BUTTON_RELEASED: bstate |= BUTTON1_RELEASED;
387 case BUTTON_PRESSED: bstate |= BUTTON1_PRESSED;
391 if (bstate == BUTTON1_PRESSED) {
392 uint_fast32_t cursor_mask, move_mask;
395 /* Determine size of the game area */
396 getmaxyx(state->gamewin[WINDOW_TILEBORDER], h, w);
399 if (x < 4 || (x -= 4)/5 >= w) return;
400 if (y < 2 || (y -= 2)/5 >= h) return;
402 /* Turn off the keyboard cursor when using the mouse */
403 cursor_mask = state->cursor < 0 ? -1 : 1ul << state->cursor;
406 move_mask = do_move(state, x/w, y/h);
407 if ((cursor_mask & move_mask) == 0) {
408 curs_redraw_game(state, cursor_mask);
415 static void do_move_cursor(struct app_state *state, int c)
417 uint_fast32_t mask = 1ul << state->cursor;
419 if (state->cursor < 0) {
420 /* Reset keyboard cursor to the empty position */
421 state->cursor = 5*state->board.y + state->board.x;
426 if ((state->cursor -= 5) < 0)
430 if ((state->cursor += 5) >= 25)
434 if ((state->cursor -= 1) % 5 == 4 || state->cursor < 0)
438 if ((state->cursor += 1) % 5 == 0)
443 curs_redraw_game(state, mask | 1ul << state->cursor);
447 static void do_keystroke(struct app_state *state, int c)
450 case KEY_DOWN: case KEY_UP: case KEY_LEFT: case KEY_RIGHT:
451 do_move_cursor(state, c);
454 if (state->cursor >= 0)
455 do_move(state, state->cursor%5, state->cursor/5);
460 int main(int argc, char **argv)
462 setlocale(LC_ALL, "");
463 app_initialize(argc, argv);
465 curs_redraw_game(&state, -1);
466 curs_redraw_goal(&state, -1);
475 setup_mainwin(&state);
478 curs_redraw_game(&state, -1);
479 curs_redraw_goal(&state, -1);
483 #if HAVE_CURSES_MOUSE_SUPPORT
489 do_keystroke(&state, c);