X-Git-Url: https://git.draconx.ca/gitweb/rrace.git/blobdiff_plain/89952d7fa03d1bffb2a4e32dcb7a0693a085eda3..7a8e9a559a8d08937a0fde473fb992f18e74132f:/src/curses.c diff --git a/src/curses.c b/src/curses.c index eecf3a1..8405e23 100644 --- a/src/curses.c +++ b/src/curses.c @@ -25,27 +25,18 @@ #include "help.h" #include "version.h" -#include "cursesopt.h" -#include "game.h" - -#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -static const char *progname = "rrace"; -static const struct option lopts[] = { LOPTS_INITIALIZER, {0} }; +#include "cursesui.h" +#include "cursesopt.h" enum { - WINDOW_TILEBORDER, - WINDOW_TILEFILL, - WINDOW_AREA, - WINDOW_MAX, + GAME_YPOS = 1 // top row of game and goal areas. }; -static struct app_state { - struct board board; +static const char *progname = "rrace"; +static const struct option lopts[] = { LOPTS_INITIALIZER, {0} }; - WINDOW *gamewin[WINDOW_MAX], *goalwin[WINDOW_MAX]; -} state; +static struct app_state state; static void print_version(void) { @@ -81,12 +72,11 @@ static void print_help(void) printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT); } -static void draw_tile(WINDOW **win, unsigned colour, +static void draw_tile(WINDOW **win, unsigned colour, unsigned selected, unsigned x, unsigned y, unsigned start_column) { - WINDOW *border = win[WINDOW_TILEBORDER], *fill = win[WINDOW_TILEFILL]; - int attr, ch; - int w, h; + WINDOW *border = win[PLAYWIN_TILEBORDER], *fill = win[PLAYWIN_TILEFILL]; + int w, h, attr, ch, bc = selected ? '#' : 0; assert(colour < TILE_MAX); attr = COLOR_PAIR(colour); @@ -97,18 +87,17 @@ static void draw_tile(WINDOW **win, unsigned colour, case TILE_YELLOW: ch = '~'; attr |= A_BOLD; break; case TILE_BLUE: ch = 'o'; attr |= A_BOLD; break; case TILE_WHITE: ch = '.'; attr |= A_BOLD; break; + + case TILE_EMPTY: attr = A_BOLD|COLOR_PAIR(RR_COLOUR_SHADOW); } getmaxyx(border, h, w); w = 2*(w+1)/2; - if (mvwin(border, 2+h*y, start_column+w*x) == ERR) + if (mvwin(border, 1+GAME_YPOS+h*y, start_column+w*x) == ERR) return; if (colour != TILE_EMPTY) { - wattrset(border, attr); - box(border, 0, 0); - mvderwin(fill, 1, 1); wbkgdset(fill, A_REVERSE|attr|ch); werase(fill); @@ -116,12 +105,18 @@ static void draw_tile(WINDOW **win, unsigned colour, werase(border); } + if (bc || colour) { + wattrset(border, attr); + wborder(border, bc, bc, bc, bc, bc, bc, bc, bc); + } + wnoutrefresh(border); } static int redraw_tile(WINDOW **win, unsigned x, unsigned y, unsigned start_column, - uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2) + uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2, + unsigned selected) { uint_fast32_t pos = board_position(x, y); unsigned char tile = 0; @@ -131,20 +126,20 @@ redraw_tile(WINDOW **win, unsigned x, unsigned y, unsigned start_column, if (bit2 & pos) tile |= 4; assert(tile < TILE_MAX); - draw_tile(win, tile, x, y, start_column); + draw_tile(win, tile, selected, x, y, start_column); return tile; } static void redraw_area_border(WINDOW **win, unsigned x, unsigned sz) { int w, h, tr = 0, rs = 0, br = 0, bs = 0, bl = 0; - WINDOW *area = win[WINDOW_AREA]; + WINDOW *area = win[PLAYWIN_AREA]; getmaxyx(stdscr, h, w); - if (h <= 2) { + if (h <= GAME_YPOS+1) { bl = ACS_ULCORNER, br = ACS_URCORNER; - } else if (h <= 3*sz+2) { + } else if (h <= 3*sz+GAME_YPOS+1) { bl = br = ACS_VLINE, bs = ' '; } @@ -161,16 +156,25 @@ static void redraw_area_border(WINDOW **win, unsigned x, unsigned sz) static void curs_redraw_game(struct app_state *state, uint_fast32_t mask) { - uint_least32_t *gp = state->board.game; + uint_least32_t buf[3], *gp = state->board.game; int i; if (mask == -1) redraw_area_border(state->gamewin, 2, 5); + if (state->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); + } + gp = buf; + } + for (i = 0; i < 25; i++) { if (mask & 1) { redraw_tile(state->gamewin, i%5, i/5, - 4, gp[0], gp[1], gp[2]); + 4, gp[0], gp[1], gp[2], + i == state->cursor); } mask >>= 1; } @@ -181,33 +185,41 @@ static void curs_redraw_goal(struct app_state *state, uint_fast32_t mask) uint_least16_t *gp = state->board.goal; int i, x, y; - if (!state->goalwin[WINDOW_AREA]) + if (!state->goalwin[PLAYWIN_AREA]) return; - getbegyx(state->goalwin[WINDOW_AREA], y, x); + getbegyx(state->goalwin[PLAYWIN_AREA], y, x); if (mask == -1) redraw_area_border(state->goalwin, x, 3); for (i = 0; i < 9; i++) { if (mask & 1) { redraw_tile(state->goalwin, i%3, i/3, - x+2, gp[0], gp[1], gp[2]); + x+2, gp[0], gp[1], gp[2], 0); } mask >>= 1; } } +/* Thin wrapper around wresize which returns ERR if support is unavailable. */ +static int do_wresize(WINDOW *window, int lines, int columns) +{ +#if HAVE_CURSES_WRESIZE + return wresize(window, lines, columns); +#endif + return ERR; +} + static WINDOW *realloc_area(WINDOW **orig, int h, int w, int y, int x) { WINDOW *win = *orig; if (win) { -#if HAVE_CURSES_WRESIZE - if (wresize(win, h, w) != ERR) { + if (do_wresize(win, h, w) != ERR) { mvwin(win, y, x); return win; } -#endif + delwin(win); } @@ -218,19 +230,15 @@ static WINDOW *realloc_area(WINDOW **orig, int h, int w, int y, int x) static void realloc_tiles(WINDOW **win, int h) { - WINDOW *border = win[WINDOW_TILEBORDER], *fill = win[WINDOW_TILEFILL]; + WINDOW *border = win[PLAYWIN_TILEBORDER], *fill = win[PLAYWIN_TILEFILL]; int w = 2*h - 1; if (fill && border) { int old_w, old_h; -#if HAVE_CURSES_WRESIZE - if (wresize(fill, h-2, w-2) != ERR - && wresize(border, h, w) != ERR) - { + if (do_wresize(fill, h-2, w-2) != ERR + && do_wresize(border, h, w) != ERR) return; - } -#endif getmaxyx(border, old_h, old_w); if (old_h == h) @@ -242,8 +250,8 @@ static void realloc_tiles(WINDOW **win, int h) if (border) delwin(border); - win[WINDOW_TILEBORDER] = border = newwin(h, w, 0, 0); - win[WINDOW_TILEFILL] = derwin(border, h-2, w-2, 1, 1); + win[PLAYWIN_TILEBORDER] = border = newwin(h, w, 0, 0); + win[PLAYWIN_TILEFILL] = derwin(border, h-2, w-2, 1, 1); } static void setup_mainwin(struct app_state *state) @@ -271,13 +279,21 @@ static void setup_mainwin(struct app_state *state) /* Frame for game area */ w = MIN(scr_w-2, 3+10*gamesz); - h = MIN(scr_h-1, 2+5*gamesz); - realloc_area(&state->gamewin[WINDOW_AREA], h, w, 1, 2); + h = MIN(scr_h-GAME_YPOS, 2+5*gamesz); + realloc_area(&state->gamewin[PLAYWIN_AREA], h, w, GAME_YPOS, 2); /* Frame for goal area */ w = MIN(scr_w-split, 3+6*goalsz); - h = MIN(scr_h-1, 2+3*goalsz); - realloc_area(&state->goalwin[WINDOW_AREA], h, w, 1, split); + h = MIN(scr_h-GAME_YPOS, 2+3*goalsz); + realloc_area(&state->goalwin[PLAYWIN_AREA], h, w, GAME_YPOS, split); + + /* Status area */ + w = MAX(0, scr_w-split-1); + realloc_area(&state->timer, 1, w, GAME_YPOS+h, split+1); + + /* Toolbar */ + realloc_area(&state->toolbar, 1, scr_w, scr_h-1, 0); + curs_draw_toolbar(state); } static void app_initialize(int argc, char **argv) @@ -309,23 +325,45 @@ static void app_initialize(int argc, char **argv) } game_reset(&state.board); + state.cursor = -1; initscr(); start_color(); - if (curs_set(0) != ERR) - leaveok(stdscr, TRUE); + curs_set(0); cbreak(); keypad(stdscr, TRUE); if (enable_mouse) { + /* + * While we only care about a subset of these events, for + * some reason with ncurses failing to enable all of them + * causes timeout to stop working when disabled buttons are + * pressed (or released). + * + * It is not known if other curses have this problem; at least + * pdcurses does not, but the extra events should be harmless + * in any case. + */ +#if HAVE_CURSES_MOUSE_SUPPORT + unsigned long mask = BUTTON1_PRESSED | BUTTON1_RELEASED + | BUTTON2_PRESSED | BUTTON2_RELEASED + | BUTTON3_PRESSED | BUTTON3_RELEASED +#ifdef BUTTON4_PRESSED + | BUTTON4_PRESSED | BUTTON4_RELEASED +#endif +#ifdef BUTTON5_PRESSED + | BUTTON5_PRESSED | BUTTON5_RELEASED +#endif + ; #if HAVE_CURSES_MOUSE_SET - mouse_set(BUTTON1_PRESSED); + mouse_set(mask); #elif HAVE_CURSES_MOUSEMASK - mousemask(BUTTON1_PRESSED, NULL); + mousemask(mask, NULL); #endif #if HAVE_CURSES_MOUSEINTERVAL mouseinterval(0); #endif +#endif /* HAVE_CURSES_MOUSE_SUPPORT */ } noecho(); @@ -336,22 +374,144 @@ static void app_initialize(int argc, char **argv) init_pair(TILE_GREEN, COLOR_GREEN, COLOR_BLACK); init_pair(TILE_BLUE, COLOR_BLUE, COLOR_BLACK); init_pair(TILE_WHITE, COLOR_WHITE, COLOR_BLACK); + init_pair(RR_COLOUR_SHADOW, COLOR_BLACK, COLOR_BLACK); + init_pair(RR_COLOUR_TOOLBAR, COLOR_CYAN, COLOR_BLACK); setup_mainwin(&state); refresh(); } -static void do_move(struct app_state *state, int x, int y) +static void update_timer(struct app_state *state, uint_fast32_t ms) +{ + unsigned sec, min; + + state->timer_ms = ms; + sec = ms / 1000; ms %= 1000; + min = sec / 60; sec %= 60; + mvwprintw(state->timer, 0, 0, "Time: %u:%.2u.%.3u", + min, sec, (unsigned)ms); + wclrtoeol(state->timer); + wnoutrefresh(state->timer); +} + +static uint_fast32_t do_move(struct app_state *state, int x, int y) { uint_fast32_t mask; if ((mask = game_do_move(&state->board, x, y)) != 0) { + uint_fast32_t goal = game_check_goal(&state->board); + + if (state->view_goal_on_game) { + state->view_goal_on_game = 0; + mask |= goal; + } + + if (goal == 0) { + /* Solved! */ + update_timer(state, game_finish(&state->board)); + mask |= ~GOAL_MASK; + state->cursor = -1; + timeout(-1); + } + curs_redraw_game(state, mask); - refresh(); + doupdate(); } + + return mask; +} + +static void do_reset_cursor(struct app_state *state) +{ + state->cursor = 5*state->board.y + state->board.x; +} + +void curs_new_game(struct app_state *state) +{ + game_reset(&state->board); + + do_reset_cursor(state); + curs_redraw_game(state, -1); + curs_redraw_goal(state, -1); + update_timer(state, 0); + doupdate(); + + timeout(33); + game_begin(&state->board); } #if HAVE_CURSES_MOUSE_SUPPORT +/* + * Given x, y as screen coordinates, record which (if any) toolbar function + * label is at that position, to be performed later. + * + * If no function is indicated, returns zero. Otherwise, returns non-zero. + */ +static int press_toolbar(struct app_state *state, int x, int y) +{ + return state->toolbar_click = curs_toolbar_mouse_func(state, x, y); +} + +/* + * Given x, y as screen coordinates, perform the toolbar action. + * + * Perform the action previously recorded by press_toolbar, if and only if + * the x, y screen coordinates correspond to the same function. + */ +static void release_toolbar(struct app_state *state, int x, int y) +{ + int func = curs_toolbar_mouse_func(state, x, y); + + if (func && state->toolbar_click == func) { + curs_execute_function(state, func); + } + + state->toolbar_click = 0; +} + +/* + * Given x, y as screen coordinates, determine which (if any) game tile is + * at that position. + * + * If there is no such tile, performs no action and returns 0. + * + * Otherwise, attempts to move that tile and returns non-zero. + */ +static int press_tile(struct app_state *state, int x, int y) +{ + int game_x, game_y, tile_w, tile_h; + uint_fast32_t cursor_mask, move_mask; + + getbegyx(state->gamewin[PLAYWIN_AREA], game_y, game_x); + getmaxyx(state->gamewin[PLAYWIN_TILEBORDER], tile_h, tile_w); + tile_w += tile_w & 1; + + /* special case the left spacer column */ + if (x == game_x+1) + x++; + + if (x < game_x+2 || (x -= game_x+2)/5 >= tile_w) + return 0; + if (y < game_y+1 || (y -= game_y+1)/5 >= tile_h) + return 0; + + /* OK, selected a tile. */ + x /= tile_w; + y /= tile_h; + + /* Disable the keyboard cursor due to mouse action */ + cursor_mask = state->cursor < 0 ? -1 : 1ul << state->cursor; + state->cursor = -1; + + move_mask = do_move(state, x, y); + if ((cursor_mask & move_mask) == 0) { + curs_redraw_game(state, cursor_mask); + doupdate(); + } + + return 1; +} + static void do_mouse(struct app_state *state) { unsigned long bstate; @@ -371,56 +531,155 @@ static void do_mouse(struct app_state *state) y = MOUSE_Y_POS; bstate = 0; - if (BUTTON_CHANGED(1)) { - switch (BUTTON_STATUS(1)) { - case BUTTON_RELEASED: bstate |= BUTTON1_RELEASED; - case BUTTON_PRESSED: bstate |= BUTTON1_PRESSED; - } - } + +#define set_bstate_helper(button) do { if (BUTTON_CHANGED(button)) \ + switch (BUTTON_STATUS(button)) { \ + case BUTTON_RELEASED: bstate |= BUTTON ## button ## _RELEASED; break; \ + case BUTTON_PRESSED: bstate |= BUTTON ## button ## _PRESSED; break; \ + } \ +} while (0); + + set_bstate_helper(1); + set_bstate_helper(3); #endif - if (bstate == BUTTON1_PRESSED) { - int w, h; + if (bstate & BUTTON3_PRESSED) { + state->view_goal_on_game = 1; + curs_redraw_game(state, game_check_goal(&state->board)); + doupdate(); + } - /* Determine size of the game area */ - getmaxyx(state->gamewin[WINDOW_TILEBORDER], h, w); - w = 2*(w+1)/2; + if (bstate & BUTTON3_RELEASED) { + state->view_goal_on_game = 0; + curs_redraw_game(state, game_check_goal(&state->board)); + doupdate(); + } - if (x < 4 || (x -= 4)/5 >= w) return; - if (y < 2 || (y -= 2)/5 >= h) return; + /* Ignore button1 if holding button3 to view goal */ + if (state->view_goal_on_game & 1) + return; - do_move(state, x/w, y/h); + if (bstate & BUTTON1_PRESSED) { + if (press_toolbar(state, x, y)); + else if (press_tile(state, x, y)); + } + + if (bstate & BUTTON1_RELEASED) { + release_toolbar(state, x, y); } } -#endif +#endif /* HAVE_CURSES_MOUSE_SUPPORT */ -int main(int argc, char **argv) +static void do_move_cursor(struct app_state *state, int c) { - setlocale(LC_ALL, ""); - app_initialize(argc, argv); + uint_fast32_t mask = 0; - curs_redraw_game(&state, -1); - curs_redraw_goal(&state, -1); - refresh(); + if (state->cursor < 0) { + /* Cursor was hidden; reset it */ + do_reset_cursor(state); + } else { + mask = 1ul << state->cursor; + } + + if (state->view_goal_on_game) { + state->view_goal_on_game = 0; + mask |= game_check_goal(&state->board); + } + + switch (c) { + case KEY_UP: + if ((state->cursor -= 5) < 0) + state->cursor += 25; + break; + case KEY_DOWN: + if ((state->cursor += 5) >= 25) + state->cursor -= 25; + break; + case KEY_LEFT: + if ((state->cursor -= 1) % 5 == 4 || state->cursor < 0) + state->cursor += 5; + break; + case KEY_RIGHT: + if ((state->cursor += 1) % 5 == 0) + state->cursor -= 5; + break; + } - while (1) { - int c = getch(); + curs_redraw_game(state, mask | 1ul << state->cursor); + doupdate(); +} - switch (c) { +static void do_keystroke(struct app_state *state, int c) +{ + int last_input = state->last_input; + state->last_input = c; + + switch (c) { + case KEY_DOWN: case KEY_UP: case KEY_LEFT: case KEY_RIGHT: + do_move_cursor(state, c); + break; + case '\t': + state->view_goal_on_game ^= 2u; + curs_redraw_game(state, game_check_goal(&state->board)); + doupdate(); + break; + case ' ': + if (!(state->view_goal_on_game & 1) && state->cursor >= 0) + do_move(state, state->cursor%5, state->cursor/5); + break; + } + + /* ESC+# keys */ + if (last_input == '\33' && c >= '0' && c <= '9') { + curs_execute_function(state, (c -= '0') == 0 ? 10 : c); + } + + /* F# keys */ + if (c >= KEY_F(1) && c <= KEY_F(10)) { + curs_execute_function(state, c - KEY_F0); + } +} + +/* One iteration of main input loop */ +void do_mainloop(struct app_state *state) +{ + int c = getch(); + + switch (c) { #ifdef KEY_RESIZE - case KEY_RESIZE: - setup_mainwin(&state); - clear(); - refresh(); - curs_redraw_game(&state, -1); - curs_redraw_goal(&state, -1); - refresh(); - break; + case KEY_RESIZE: + setup_mainwin(state); + clear(); + refresh(); + curs_redraw_game(state, -1); + curs_redraw_goal(state, -1); + curs_draw_toolbar(state); + update_timer(state, state->timer_ms); + doupdate(); + break; #endif #if HAVE_CURSES_MOUSE_SUPPORT - case KEY_MOUSE: - do_mouse(&state); - break; + case KEY_MOUSE: + do_mouse(state); + break; #endif - } + default: + do_keystroke(state, c); + case ERR:; + } + + if (state->board.x <= 4) { + update_timer(state, game_elapsed(&state->board)); + doupdate(); } } + +int main(int argc, char **argv) +{ + setlocale(LC_ALL, ""); + app_initialize(argc, argv); + + curs_new_game(&state); + while (1) + do_mainloop(&state); + abort(); +}