# HG changeset patch # User Mike Becker # Date 1537275728 -7200 # Node ID c8f2c280cff757b1c10eab2193e60f253264748b # Parent b34de5ce7d0e5d3bc984ac11738492115f8df515 adds unicode support diff -r b34de5ce7d0e -r c8f2c280cff7 conf.mk --- a/conf.mk Wed Aug 29 17:31:36 2018 +0200 +++ b/conf.mk Tue Sep 18 15:02:08 2018 +0200 @@ -32,10 +32,10 @@ BIN = terminal-chess CC = gcc -CFLAGS = -O2 -std=gnu99 `pkg-config --cflags ncurses` -CFLAGS_D = -g -std=gnu99 -Wall -pedantic `pkg-config --cflags ncurses` +CFLAGS = -O2 -std=gnu99 `pkg-config --cflags ncursesw` +CFLAGS_D = -g -std=gnu99 -Wall -pedantic `pkg-config --cflags ncursesw` LD = gcc -LDFLAGS = `pkg-config --libs ncurses` +LDFLAGS = `pkg-config --libs ncursesw` ARFLAGS = -r MKDIRFLAGS = -p RMFLAGS = -f -R diff -r b34de5ce7d0e -r c8f2c280cff7 src/chess/rules.c --- a/src/chess/rules.c Wed Aug 29 17:31:36 2018 +0200 +++ b/src/chess/rules.c Tue Sep 18 15:02:08 2018 +0200 @@ -204,6 +204,18 @@ } } +unsigned char* getpieceunicode(uint8_t piece) { + switch (piece & PIECE_MASK) { + case PAWN: return "\u265f"; + case ROOK: return "\u265c"; + case KNIGHT: return "\u265e"; + case BISHOP: return "\u265d"; + case QUEEN: return "\u265b"; + case KING: return "\u265a"; + default: return ""; + } +} + uint8_t getpiece(char c) { switch (c) { case 'R': return ROOK; diff -r b34de5ce7d0e -r c8f2c280cff7 src/chess/rules.h --- a/src/chess/rules.h Wed Aug 29 17:31:36 2018 +0200 +++ b/src/chess/rules.h Tue Sep 18 15:02:08 2018 +0200 @@ -174,12 +174,23 @@ * * Does not work for pawns, scince they don't have a character. * - * @param piece one of ROOK, KNIGHT, BISHOP, QUEEN, KING + * @param piece may have color or additional flags * @return character value for the specified piece */ char getpiecechr(uint8_t piece); /** + * Maps a piece to a unicode character sequence. + * + * The returned unicode is for black pieces. + * You may colorize the output by setting the terminal foreground color. + * + * @param piece the piece to dispaly + * @return unicode character sequence for the specified piece + */ +unsigned char* getpieceunicode(uint8_t piece); + +/** * Checks, if a specified field is covered by a piece of a certain color. * * The out-parameters may both be NULL, but if any of them is set, the other diff -r b34de5ce7d0e -r c8f2c280cff7 src/game.c --- a/src/game.c Wed Aug 29 17:31:36 2018 +0200 +++ b/src/game.c Tue Sep 18 15:02:08 2018 +0200 @@ -71,7 +71,9 @@ return 0; } -static void draw_board(GameState *gamestate, uint8_t perspective) { +static void draw_board(GameState *gamestate, + uint8_t perspective, + _Bool unicode) { char fen[90]; compute_fen(fen, gamestate); mvaddstr(0, 0, fen); @@ -80,11 +82,18 @@ for (uint8_t x = 0 ; x < 8 ; x++) { uint8_t col = gamestate->board[y][x] & COLOR_MASK; uint8_t piece = gamestate->board[y][x] & PIECE_MASK; - char piecec; + unsigned char piecestr[5]; if (piece) { - piecec = piece == PAWN ? 'P' : getpiecechr(piece); + if (unicode) { + unsigned char* uc = getpieceunicode(piece); + strncpy(piecestr, uc, 5); + } else { + piecestr[0] = piece == PAWN ? 'P' : getpiecechr(piece); + piecestr[1] = '\0'; + } } else { - piecec = ' '; + piecestr[0] = ' '; + piecestr[1] = '\0'; } _Bool boardblack = (y&1)==(x&1); @@ -97,9 +106,7 @@ int cy = perspective == WHITE ? boardy-y : boardy-7+y; int cx = perspective == WHITE ? boardx+x*3 : boardx+21-x*3; - mvaddch(cy, cx, ' '); - mvaddch(cy, cx+1, piecec); - mvaddch(cy, cx+2, ' '); + mvprintw(cy, cx, " %s ", piecestr); } } @@ -460,9 +467,11 @@ } } -static void post_game(GameState *gamestate, GameInfo *gameinfo) { +static void post_game(Settings* settings, GameState *gamestate) { + GameInfo *gameinfo = &(settings->gameinfo); + move(0,0); - draw_board(gamestate, WHITE); + draw_board(gamestate, WHITE, settings->unicode); // TODO: network connection is still open here - think about it! @@ -520,13 +529,13 @@ _Bool running; do { clear(); - draw_board(&gamestate, curcol); + draw_board(&gamestate, curcol, settings->unicode); running = !domove_singlemachine(&gamestate, &(settings->gameinfo), curcol); curcol = opponent_color(curcol); } while (running); - post_game(&gamestate, &(settings->gameinfo)); + post_game(settings, &gamestate); } void game_continue(Settings *settings, int opponent, GameState *gamestate) { @@ -541,7 +550,7 @@ _Bool running; do { clear(); - draw_board(gamestate, mycolor); + draw_board(gamestate, mycolor, settings->unicode); if (myturn) { running = !sendmove(gamestate, &(settings->gameinfo), opponent, mycolor); @@ -551,7 +560,7 @@ myturn ^= TRUE; } while (running); - post_game(gamestate, &(settings->gameinfo)); + post_game(settings, gamestate); } void game_start(Settings *settings, int opponent) { diff -r b34de5ce7d0e -r c8f2c280cff7 src/main.c --- a/src/main.c Wed Aug 29 17:31:36 2018 +0200 +++ b/src/main.c Tue Sep 18 15:02:08 2018 +0200 @@ -34,6 +34,7 @@ #include #include #include +#include int get_settings(int argc, char **argv, Settings *settings) { char *valid; @@ -41,7 +42,7 @@ uint8_t timeunit = 60; size_t len; - for (int opt ; (opt = getopt(argc, argv, "a:bc:hp:rsS:t:v")) != -1 ;) { + for (int opt ; (opt = getopt(argc, argv, "a:bc:hp:rsS:t:Uv")) != -1 ;) { switch (opt) { case 'c': settings->continuepgn = optarg; @@ -58,6 +59,9 @@ case 'S': settings->analyzepgn = optarg; break; + case 'U': + settings->unicode = 0; + break; case 't': case 'a': len = strlen(optarg); @@ -114,6 +118,7 @@ " -r Distribute color randomly\n" " -s Single machine mode\n" " -t