]> git.draconx.ca Git - rrace.git/blobdiff - src/curses.c
curses: Parameterize vertical position of the game.
[rrace.git] / src / curses.c
index eecf3a19e736a6f9e13552eea465fe130a9fb5dc..d5b1cc4be149681d9a383183adb3493c97f4bd79 100644 (file)
 #define MIN(a, b) ((a) < (b) ? (a) : (b))
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 
+enum {
+       GAME_YPOS = 1 // top row of game and goal areas.
+};
+
 static const char *progname = "rrace";
 static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
 
@@ -45,6 +49,7 @@ static struct app_state {
        struct board board;
 
        WINDOW *gamewin[WINDOW_MAX], *goalwin[WINDOW_MAX];
+       int cursor;
 } state;
 
 static void print_version(void)
@@ -81,12 +86,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;
+       int w, h, attr, ch, bc = selected ? '#' : 0;
 
        assert(colour < TILE_MAX);
        attr = COLOR_PAIR(colour);
@@ -97,18 +101,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(TILE_MAX);
        }
 
        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 +119,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,7 +140,7 @@ 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;
 }
 
@@ -142,9 +151,9 @@ static void redraw_area_border(WINDOW **win, unsigned x, unsigned sz)
 
        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 = ' ';
        }
 
@@ -170,7 +179,8 @@ static void curs_redraw_game(struct app_state *state, uint_fast32_t mask)
        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;
        }
@@ -191,7 +201,7 @@ static void curs_redraw_goal(struct app_state *state, uint_fast32_t mask)
        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;
        }
@@ -271,13 +281,13 @@ 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[WINDOW_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[WINDOW_AREA], h, w, GAME_YPOS, split);
 }
 
 static void app_initialize(int argc, char **argv)
@@ -309,11 +319,11 @@ static void app_initialize(int argc, char **argv)
        }
 
        game_reset(&state.board);
+       state.cursor = 5*state.board.y + state.board.x;
 
        initscr();
        start_color();
-       if (curs_set(0) != ERR)
-               leaveok(stdscr, TRUE);
+       curs_set(0);
 
        cbreak();
        keypad(stdscr, TRUE);
@@ -336,12 +346,13 @@ 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(TILE_MAX, COLOR_BLACK, COLOR_BLACK);
 
        setup_mainwin(&state);
        refresh();
 }
 
-static void do_move(struct app_state *state, int x, int y)
+static uint_fast32_t do_move(struct app_state *state, int x, int y)
 {
        uint_fast32_t mask;
 
@@ -349,6 +360,8 @@ static void do_move(struct app_state *state, int x, int y)
                curs_redraw_game(state, mask);
                refresh();
        }
+
+       return mask;
 }
 
 #if HAVE_CURSES_MOUSE_SUPPORT
@@ -379,6 +392,7 @@ static void do_mouse(struct app_state *state)
        }
 #endif
        if (bstate == BUTTON1_PRESSED) {
+               uint_fast32_t cursor_mask, move_mask;
                int w, h;
 
                /* Determine size of the game area */
@@ -386,13 +400,66 @@ static void do_mouse(struct app_state *state)
                w = 2*(w+1)/2;
 
                if (x < 4 || (x -= 4)/5 >= w) return;
-               if (y < 2 || (y -= 2)/5 >= h) return;
+               if (y <= GAME_YPOS || (y -= GAME_YPOS+1)/5 >= h) return;
+
+               /* Turn off the keyboard cursor when using the mouse */
+               cursor_mask = state->cursor < 0 ? -1 : 1ul << state->cursor;
+               state->cursor = -1;
 
-               do_move(state, x/w, y/h);
+               move_mask = do_move(state, x/w, y/h);
+               if ((cursor_mask & move_mask) == 0) {
+                       curs_redraw_game(state, cursor_mask);
+                       refresh();
+               }
        }
 }
 #endif
 
+static void do_move_cursor(struct app_state *state, int c)
+{
+       uint_fast32_t mask = 1ul << state->cursor;
+
+       if (state->cursor < 0) {
+               /* Reset keyboard cursor to the empty position */
+               state->cursor = 5*state->board.y + state->board.x;
+       }
+
+       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;
+       }
+
+       curs_redraw_game(state, mask | 1ul << state->cursor);
+       refresh();
+}
+
+static void do_keystroke(struct app_state *state, int c)
+{
+       switch (c) {
+       case KEY_DOWN: case KEY_UP: case KEY_LEFT: case KEY_RIGHT:
+               do_move_cursor(state, c);
+               break;
+       case ' ':
+               if (state->cursor >= 0)
+                       do_move(state, state->cursor%5, state->cursor/5);
+               break;
+       }
+}
+
 int main(int argc, char **argv)
 {
        setlocale(LC_ALL, "");
@@ -421,6 +488,8 @@ int main(int argc, char **argv)
                        do_mouse(&state);
                        break;
 #endif
+               default:
+                       do_keystroke(&state, c);
                }
        }
 }