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];
55 static void print_version(void)
57 version_print_head("rrace-curses", stdout);
58 puts("License GPLv3+: GNU GPL version 3 or any later version");
59 puts("This is free software: you are free to change and redistribute it.");
60 puts("There is NO WARRANTY, to the extent permitted by law.");
63 static void print_usage(FILE *f)
65 fprintf(f, "Usage: %s [options]\n", progname);
67 fprintf(f, "Try %s --help for more information.\n", progname);
70 static void print_help(void)
72 struct lopt_help help = {0};
73 const struct option *opt;
79 for (opt = lopts; opt->name; opt++) {
80 if (!lopt_get_help(opt, &help))
82 help_print_option(opt, help.arg, help.desc, 20);
86 printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
89 static void draw_tile(WINDOW **win, unsigned colour, unsigned selected,
90 unsigned x, unsigned y, unsigned start_column)
92 WINDOW *border = win[WINDOW_TILEBORDER], *fill = win[WINDOW_TILEFILL];
93 int w, h, attr, ch, bc = selected ? '#' : 0;
95 assert(colour < TILE_MAX);
96 attr = COLOR_PAIR(colour);
98 case TILE_RED: ch = 'X'; break;
99 case TILE_ORANGE: ch = '|'; break;
100 case TILE_GREEN: ch = '+'; break;
101 case TILE_YELLOW: ch = '~'; attr |= A_BOLD; break;
102 case TILE_BLUE: ch = 'o'; attr |= A_BOLD; break;
103 case TILE_WHITE: ch = '.'; attr |= A_BOLD; break;
105 case TILE_EMPTY: attr = A_BOLD|COLOR_PAIR(TILE_MAX);
108 getmaxyx(border, h, w);
111 if (mvwin(border, 1+GAME_YPOS+h*y, start_column+w*x) == ERR)
114 if (colour != TILE_EMPTY) {
115 mvderwin(fill, 1, 1);
116 wbkgdset(fill, A_REVERSE|attr|ch);
123 wattrset(border, attr);
124 wborder(border, bc, bc, bc, bc, bc, bc, bc, bc);
127 wnoutrefresh(border);
131 redraw_tile(WINDOW **win, unsigned x, unsigned y, unsigned start_column,
132 uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2,
135 uint_fast32_t pos = board_position(x, y);
136 unsigned char tile = 0;
138 if (bit0 & pos) tile |= 1;
139 if (bit1 & pos) tile |= 2;
140 if (bit2 & pos) tile |= 4;
141 assert(tile < TILE_MAX);
143 draw_tile(win, tile, selected, x, y, start_column);
147 static void redraw_area_border(WINDOW **win, unsigned x, unsigned sz)
149 int w, h, tr = 0, rs = 0, br = 0, bs = 0, bl = 0;
150 WINDOW *area = win[WINDOW_AREA];
152 getmaxyx(stdscr, h, w);
154 if (h <= GAME_YPOS+1) {
155 bl = ACS_ULCORNER, br = ACS_URCORNER;
156 } else if (h <= 3*sz+GAME_YPOS+1) {
157 bl = br = ACS_VLINE, bs = ' ';
161 tr = ACS_ULCORNER, br = ACS_LLCORNER;
162 } else if (w <= 6*sz+x+2) {
163 tr = ACS_HLINE, rs = ' ';
164 br = br ? ' ' : ACS_HLINE;
167 wborder(area, 0, rs, 0, bs, 0, tr, bl, br);
171 static void curs_redraw_game(struct app_state *state, uint_fast32_t mask)
173 uint_least32_t *gp = state->board.game;
177 redraw_area_border(state->gamewin, 2, 5);
179 for (i = 0; i < 25; i++) {
181 redraw_tile(state->gamewin, i%5, i/5,
182 4, gp[0], gp[1], gp[2],
189 static void curs_redraw_goal(struct app_state *state, uint_fast32_t mask)
191 uint_least16_t *gp = state->board.goal;
194 if (!state->goalwin[WINDOW_AREA])
197 getbegyx(state->goalwin[WINDOW_AREA], y, x);
199 redraw_area_border(state->goalwin, x, 3);
201 for (i = 0; i < 9; i++) {
203 redraw_tile(state->goalwin, i%3, i/3,
204 x+2, gp[0], gp[1], gp[2], 0);
210 static WINDOW *realloc_area(WINDOW **orig, int h, int w, int y, int x)
215 #if HAVE_CURSES_WRESIZE
216 if (wresize(win, h, w) != ERR) {
225 return *orig = subwin(stdscr, h, w, y, x);
229 static void realloc_tiles(WINDOW **win, int h)
231 WINDOW *border = win[WINDOW_TILEBORDER], *fill = win[WINDOW_TILEFILL];
234 if (fill && border) {
237 #if HAVE_CURSES_WRESIZE
238 if (wresize(fill, h-2, w-2) != ERR
239 && wresize(border, h, w) != ERR)
245 getmaxyx(border, old_h, old_w);
255 win[WINDOW_TILEBORDER] = border = newwin(h, w, 0, 0);
256 win[WINDOW_TILEFILL] = derwin(border, h-2, w-2, 1, 1);
259 static void setup_mainwin(struct app_state *state)
261 int w, h, gamesz, goalsz, scr_w, scr_h, split;
263 getmaxyx(stdscr, scr_h, scr_w);
265 /* First try to fit the game tiles based on window height. */
266 gamesz = MAX(3, (scr_h - 4) / 5);
268 /* Adjust downward until we can fit smallest possible goal area. */
269 for (; split = 5+10*gamesz, gamesz > 3; gamesz--) {
270 if (split + 20 < scr_w)
274 /* Pick a goal size that will fit in the remaining area */
275 goalsz = MAX(3, (scr_w - split - 4) / 6);
276 if (goalsz >= gamesz)
277 goalsz = MAX(3, gamesz - 1);
279 realloc_tiles(state->gamewin, gamesz);
280 realloc_tiles(state->goalwin, goalsz);
282 /* Frame for game area */
283 w = MIN(scr_w-2, 3+10*gamesz);
284 h = MIN(scr_h-GAME_YPOS, 2+5*gamesz);
285 realloc_area(&state->gamewin[WINDOW_AREA], h, w, GAME_YPOS, 2);
287 /* Frame for goal area */
288 w = MIN(scr_w-split, 3+6*goalsz);
289 h = MIN(scr_h-GAME_YPOS, 2+3*goalsz);
290 realloc_area(&state->goalwin[WINDOW_AREA], h, w, GAME_YPOS, split);
293 static void app_initialize(int argc, char **argv)
295 int enable_mouse = 1;
301 while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
321 game_reset(&state.board);
322 state.cursor = 5*state.board.y + state.board.x;
329 keypad(stdscr, TRUE);
331 #if HAVE_CURSES_MOUSE_SET
332 mouse_set(BUTTON1_PRESSED);
333 #elif HAVE_CURSES_MOUSEMASK
334 mousemask(BUTTON1_PRESSED, NULL);
336 #if HAVE_CURSES_MOUSEINTERVAL
343 init_pair(TILE_RED, COLOR_RED, COLOR_BLACK);
344 init_pair(TILE_ORANGE, COLOR_YELLOW, COLOR_BLACK);
345 init_pair(TILE_YELLOW, COLOR_YELLOW, COLOR_BLACK);
346 init_pair(TILE_GREEN, COLOR_GREEN, COLOR_BLACK);
347 init_pair(TILE_BLUE, COLOR_BLUE, COLOR_BLACK);
348 init_pair(TILE_WHITE, COLOR_WHITE, COLOR_BLACK);
349 init_pair(TILE_MAX, COLOR_BLACK, COLOR_BLACK);
351 setup_mainwin(&state);
355 static uint_fast32_t do_move(struct app_state *state, int x, int y)
359 if ((mask = game_do_move(&state->board, x, y)) != 0) {
360 curs_redraw_game(state, mask);
367 #if HAVE_CURSES_MOUSE_SUPPORT
368 static void do_mouse(struct app_state *state)
370 unsigned long bstate;
373 #if HAVE_CURSES_GETMOUSE_NCURSES
376 if (getmouse(&mev) == ERR)
379 x = mev.x, y = mev.y;
381 #elif HAVE_CURSES_REQUEST_MOUSE_POS
387 if (BUTTON_CHANGED(1)) {
388 switch (BUTTON_STATUS(1)) {
389 case BUTTON_RELEASED: bstate |= BUTTON1_RELEASED;
390 case BUTTON_PRESSED: bstate |= BUTTON1_PRESSED;
394 if (bstate == BUTTON1_PRESSED) {
395 uint_fast32_t cursor_mask, move_mask;
398 /* Determine size of the game area */
399 getmaxyx(state->gamewin[WINDOW_TILEBORDER], h, w);
402 if (x < 4 || (x -= 4)/5 >= w) return;
403 if (y <= GAME_YPOS || (y -= GAME_YPOS+1)/5 >= h) return;
405 /* Turn off the keyboard cursor when using the mouse */
406 cursor_mask = state->cursor < 0 ? -1 : 1ul << state->cursor;
409 move_mask = do_move(state, x/w, y/h);
410 if ((cursor_mask & move_mask) == 0) {
411 curs_redraw_game(state, cursor_mask);
418 static void do_move_cursor(struct app_state *state, int c)
420 uint_fast32_t mask = 1ul << state->cursor;
422 if (state->cursor < 0) {
423 /* Reset keyboard cursor to the empty position */
424 state->cursor = 5*state->board.y + state->board.x;
429 if ((state->cursor -= 5) < 0)
433 if ((state->cursor += 5) >= 25)
437 if ((state->cursor -= 1) % 5 == 4 || state->cursor < 0)
441 if ((state->cursor += 1) % 5 == 0)
446 curs_redraw_game(state, mask | 1ul << state->cursor);
450 static void do_keystroke(struct app_state *state, int c)
453 case KEY_DOWN: case KEY_UP: case KEY_LEFT: case KEY_RIGHT:
454 do_move_cursor(state, c);
457 if (state->cursor >= 0)
458 do_move(state, state->cursor%5, state->cursor/5);
463 int main(int argc, char **argv)
465 setlocale(LC_ALL, "");
466 app_initialize(argc, argv);
468 curs_redraw_game(&state, -1);
469 curs_redraw_goal(&state, -1);
478 setup_mainwin(&state);
481 curs_redraw_game(&state, -1);
482 curs_redraw_goal(&state, -1);
486 #if HAVE_CURSES_MOUSE_SUPPORT
492 do_keystroke(&state, c);