From b787cdc7885d8a75f5aea5aa8e25124b72325f99 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Fri, 24 May 2024 23:42:14 +0200 Subject: [PATCH] move cursor --- battleships.c | 69 +++++++++++++++++++++++++++++++++++++++++++++------ board.c | 7 ++++-- board.h | 2 +- 3 files changed, 68 insertions(+), 10 deletions(-) diff --git a/battleships.c b/battleships.c index a7ff248..7e1f010 100644 --- a/battleships.c +++ b/battleships.c @@ -10,6 +10,8 @@ #include "board.h" #include "debug.h" +#define KEY_ESC 0x1b + /* static variables */ char *progname = NULL; char *version = "0.1"; @@ -65,23 +67,76 @@ int main (int argc, char *argv[]) VERBOSE (DEBUG, fprintf (stdout, "debug mode\n")); initscr (); - cbreak (); - noecho (); - curs_set (0); + + cbreak (); /* direct input (no newline required)... */ + noecho (); /* ... without echoing */ + nodelay (stdscr, TRUE); /* don't wait for input... */ + halfdelay (10); /* ...well, no more than a second, anyway */ + keypad (stdscr, TRUE); /* enable cursor keys */ + scrollok (stdscr, TRUE); /* enable scrolling in main window */ + + curs_set (2); start_color (); - board_t *board = initboard (20, 10); + int xsize = 20; + int ysize = 10; + int xoffset = 3; + int yoffset = 3; + + board_t *board = initboard (xsize, ysize); board->tab[3] = 'X'; board->tab[4] = 'O'; board->tab[5] = 'S'; + + int mode = 0; + displayboard (board, xoffset, yoffset, mode); + + int x = (xsize + 1) / 2 - 1; + int y = (ysize + 1) / 2 - 1; + int stop = 0; while (!stop) { - displayboard (board, 4, 4); - int c = getch (); - switch (c) { + move (y + yoffset, x + xoffset); + switch (getch ()) { + case KEY_ESC: +#ifndef __PDCURSES__ + if (getch () != KEY_ESC) { + break; + } +#endif /* __PDCURSES__ */ + case ':': + break; + case KEY_UP: + case 'i': + if (y > 0) { + y--; + } + break; + case KEY_LEFT: + case 'j': + if (x > 0) { + x--; + } + break; + case KEY_DOWN: + case 'k': + if (y < ysize - 1) { + y++; + } + break; + case KEY_RIGHT: + case 'l': + if (x < xsize - 1) { + x++; + } + break; case 'q': stop = 1; break; + case 't': + mode ^= 1; + displayboard (board, xoffset, yoffset, mode); + break; case ERR: break; } diff --git a/board.c b/board.c index e0002f3..6d91d0d 100644 --- a/board.c +++ b/board.c @@ -39,7 +39,7 @@ void setcolor (color_t color) attrset (COLOR_PAIR (color)); } -void displayboard (board_t *board, int xoffset, int yoffset) +void displayboard (board_t *board, int xoffset, int yoffset, int mode) { int x, y; @@ -77,9 +77,12 @@ void displayboard (board_t *board, int xoffset, int yoffset) setcolor (yellow); break; } + if (!mode) { + c = ' '; + } } - mvaddch (yoffset + 1 + y, xoffset + 1 + x, c); + mvaddch (yoffset + y, xoffset + x, c); setcolor (white); } } diff --git a/board.h b/board.h index 18a1352..7c83702 100644 --- a/board.h +++ b/board.h @@ -9,6 +9,6 @@ typedef struct { board_t *initboard (int xsize, int ysize); -void displayboard (board_t *board, int xoffset, int yoffset); +void displayboard (board_t *board, int xoffset, int yoffset, int mode); #endif /* __BOARD_H__ */ -- 2.30.2