From 665605d1448bd25f298b2f6ce6555b85da0fa96e Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Mon, 15 Aug 2011 21:04:07 +1000 Subject: [PATCH] Add convenience functions left(), center() and right() --- src/game.c | 109 +++++++++++++++++------------------------------- src/help.c | 55 ++++--------------------- src/intf.c | 119 +++++++++++++++++++++++++++++++++++++---------------- src/intf.h | 65 ++++++++++++++++++++++++++++- src/move.c | 98 +++++++++++++++++-------------------------- 5 files changed, 231 insertions(+), 215 deletions(-) diff --git a/src/game.c b/src/game.c index f76b093..c3dde3d 100644 --- a/src/game.c +++ b/src/game.c @@ -111,11 +111,10 @@ void init_game (void) // Try to load an old game, if possible if (game_num != 0) { chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); - int lines, width; + int width; - lines = mkchstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1, - WIN_COLS - 7, &width, 1, - "Loading game %d... ", game_num); + mkchstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1, WIN_COLS - 7, + &width, 1, "Loading game %d... ", game_num); newtxwin(5, width + 5, 6, WCENTER, true, attr_status_window); centerch(curwin, 2, 0, chbuf, 1, &width); wrefresh(curwin); @@ -144,13 +143,13 @@ void init_game (void) // Try to load the game, if possible chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); - int lines, width; + int width; game_num = choice; - lines = mkchstr(chbuf, BUFSIZE, attr_status_window, - 0, 0, 1, WIN_COLS - 7, &width, 1, - "Loading game %d... ", game_num); + mkchstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1, + WIN_COLS - 7, &width, 1, + "Loading game %d... ", game_num); newtxwin(5, width + 5, 9, WCENTER, true, attr_status_window); centerch(curwin, 2, 0, chbuf, 1, &width); wrefresh(curwin); @@ -249,10 +248,8 @@ static int ask_number_players (void) chbuf = xmalloc(BUFSIZE * sizeof(chtype)); - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, - sizeof(widthbuf) / sizeof(widthbuf[0]), WIN_COLS - 7, - widthbuf, sizeof(widthbuf) / sizeof(widthbuf[0]), - "Enter number of players [^{1^}-^{%d^}] " + lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, 2, WIN_COLS + - 7, widthbuf, 2, "Enter number of players [^{1^}-^{%d^}] " "or ^{^} to continue a game: ", MAX_PLAYERS); assert(lines == 1 || lines == 2); maxwidth = ((lines == 1) ? widthbuf[0] : MAX(widthbuf[0], widthbuf[1])) + 5; @@ -314,10 +311,8 @@ int ask_game_number (void) chbuf = xmalloc(BUFSIZE * sizeof(chtype)); - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, - sizeof(widthbuf) / sizeof(widthbuf[0]), WIN_COLS - 7, - widthbuf, sizeof(widthbuf) / sizeof(widthbuf[0]), - "Enter game number [^{1^}-^{9^}] " + lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, 2, WIN_COLS + - 7, widthbuf, 2, "Enter game number [^{1^}-^{9^}] " "or ^{^} to cancel: "); assert(lines == 1 || lines == 2); maxwidth = ((lines == 1) ? widthbuf[0] : MAX(widthbuf[0], widthbuf[1])) + 5; @@ -365,11 +360,15 @@ int ask_game_number (void) void ask_player_names (void) { + chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + int width; + + if (number_players == 1) { // Ask for the player's name newtxwin(5, WIN_COLS - 4, 9, WCENTER, true, attr_normal_window); - mvwaddstr(curwin, 2, 2, "Please enter your name: "); + left(curwin, 2, 2, attr_normal, 0, 0, "Please enter your name: "); int x = getcurx(curwin); int w = getmaxx(curwin) - x - 2; @@ -385,16 +384,13 @@ void ask_player_names (void) } } - chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); - int lines, width; - - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, - 1, WIN_COLS - YESNO_COLS - 6, &width, 1, - "Do you need any instructions? [^{Y^}/^{N^}] "); + mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, 1, + WIN_COLS - YESNO_COLS - 6, &width, 1, + "Do you need any instructions? [^{Y^}/^{N^}] "); newtxwin(5, width + YESNO_COLS + 4, 6, WCENTER, true, attr_normal_window); - leftch(curwin, 2, 2, chbuf, lines, &width); - free(chbuf); + leftch(curwin, 2, 2, chbuf, 1, &width); + if (answer_yesno(curwin)) { show_help(); } @@ -402,23 +398,18 @@ void ask_player_names (void) } else { // Ask for all of the player names - chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); - int lines, width; - bool entered[MAX_PLAYERS]; bool done, modified; int cur, len, i; newtxwin(number_players + 5, WIN_COLS - 4, 9, WCENTER, true, attr_normal_window); - lines = mkchstr(chbuf, BUFSIZE, attr_title, 0, 0, 1, WIN_COLS - 8, - &width, 1, " Enter Player Names "); - centerch(curwin, 1, 0, chbuf, lines, &width); + center(curwin, 1, 0, attr_title, 0, 0, " Enter Player Names "); for (i = 0; i < number_players; i++) { player[i].name = NULL; entered[i] = false; - mvwprintw(curwin, i + 3, 2, "Player %d:", i + 1); + left(curwin, i + 3, 2, attr_normal, 0, 0, "Player %d:", i + 1); } int x = getcurx(curwin) + 1; @@ -494,21 +485,21 @@ void ask_player_names (void) } } - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, - 1, WIN_COLS - YESNO_COLS - 6, &width, 1, - "Does any player need instructions? [^{Y^}/^{N^}] "); + mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, 1, + WIN_COLS - YESNO_COLS - 6, &width, 1, + "Does any player need instructions? [^{Y^}/^{N^}] "); newtxwin(5, width + YESNO_COLS + 4, 6, WCENTER, true, attr_normal_window); - leftch(curwin, 2, 2, chbuf, lines, &width); + leftch(curwin, 2, 2, chbuf, 1, &width); + if (answer_yesno(curwin)) { show_help(); } - - free(chbuf); } deltxwin(); // "Need instructions?" window deltxwin(); // "Enter player names" window + free(chbuf); } @@ -589,8 +580,6 @@ void end_game (void) void show_map (bool closewin) { - chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); - int lines, width; int x, y; @@ -603,16 +592,11 @@ void show_map (bool closewin) mvwhline(curwin, 1, 2, ' ' | attr_mapwin_title, getmaxx(curwin) - 4); // Display current player and turn number - lines = mkchstr(chbuf, BUFSIZE, attr_mapwin_title, attr_mapwin_highlight, - 0, 1, WIN_COLS - 4, &width, 1, " Player: ^{%s^} ", - player[current_player].name); - leftch(curwin, 1, 2, chbuf, lines, &width); - - lines = mkchstr(chbuf, BUFSIZE, attr_mapwin_title, attr_mapwin_highlight, - attr_mapwin_blink, 1, WIN_COLS / 2, &width, 1, - (turn_number != max_turn) ? " Turn: ^{%d^} " : - " ^[*** Last Turn ***^] ", turn_number); - rightch(curwin, 1, WIN_COLS - 2, chbuf, lines, &width); + left(curwin, 1, 2, attr_mapwin_title, attr_mapwin_highlight, 0, + " Player: ^{%s^} ", player[current_player].name); + right(curwin, 1, getmaxx(curwin) - 2, attr_mapwin_title, + attr_mapwin_highlight, attr_mapwin_blink, (turn_number != max_turn) ? + " Turn: ^{%d^} " : " ^[*** Last Turn ***^] ", turn_number); wattrset(curwin, attr_map_window); @@ -657,8 +641,6 @@ void show_map (bool closewin) deltxwin(); // Galaxy map window txrefresh(); } - - free(chbuf); } @@ -667,8 +649,6 @@ void show_map (bool closewin) void show_status (int num) { - chtype *chbuf; - int lines, width; double val; int i, line; @@ -677,24 +657,14 @@ void show_status (int num) newtxwin(MAX_COMPANIES + 15, WIN_COLS, 1, WCENTER, true, attr_normal_window); - - chbuf = xmalloc(BUFSIZE * sizeof(chtype)); - lines = mkchstr(chbuf, BUFSIZE, attr_title, 0, 0, 1, WIN_COLS - 4, - &width, 1, " Stock Portfolio "); - centerch(curwin, 1, 0, chbuf, lines, &width); - - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_highlight, 0, 1, - WIN_COLS - 4, &width, 1, "Player: ^{%s^}", + center(curwin, 1, 0, attr_title, 0, 0, " Stock Portfolio "); + center(curwin, 2, 0, attr_normal, attr_highlight, 0, "Player: ^{%s^}", player[num].name); - centerch(curwin, 2, 0, chbuf, lines, &width); val = total_value(num); if (val == 0.0) { - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_highlight, - attr_blink, 1, WIN_COLS - 4, &width, 1, + center(curwin, 11, 0, attr_normal, attr_highlight, attr_blink, "^[* * * B A N K R U P T * * *^]"); - centerch(curwin, 11, 0, chbuf, lines, &width); - } else { char *buf = xmalloc(BUFSIZE); @@ -708,10 +678,8 @@ void show_status (int num) } if (none) { - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_highlight, 0, - 1, WIN_COLS - 4, &width, 1, + center(curwin, 8, 0, attr_normal, attr_highlight, 0, "No companies on the map"); - centerch(curwin, 8, 0, chbuf, lines, &width); } else { // Handle the locale's currency symbol snprintf(buf, BUFSIZE, "share (%s)", lconvinfo.currency_symbol); @@ -761,7 +729,6 @@ void show_status (int num) wait_for_key(curwin, getmaxy(curwin) - 2, attr_waitforkey); deltxwin(); txrefresh(); - free(chbuf); } diff --git a/src/help.c b/src/help.c index 4f9184f..ac02394 100644 --- a/src/help.c +++ b/src/help.c @@ -157,45 +157,17 @@ static const char *help_text[] = { void show_help (void) { - chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); - int lines, width; - int curpage = 0; int numpages; bool done = false; - chtype *ch_title; // Title string - chtype *ch_contfirst; // "Continue", first page - chtype *ch_contnext; // "Continue", following pages - int w_title, ln_title; - int w_contfirst, ln_contfirst; - int w_contnext, ln_contnext; - // Count how many pages appear in the help text for (numpages = 0; help_text[numpages] != NULL; numpages++) ; - if (numpages == 0) { - free(chbuf); + if (numpages == 0) return; - } - - // Prepare fixed-text strings - ln_title = mkchstr(chbuf, BUFSIZE, attr_title, 0, 0, 1, WIN_COLS - 4, - &w_title, 1, " How to Play "); - ch_title = chstrdup(chbuf, BUFSIZE); - - ln_contfirst = mkchstr(chbuf, BUFSIZE, attr_waitforkey, 0, 0, 1, - WIN_COLS - 4, &w_contfirst, 1, - "[ Press to continue ] "); - ch_contfirst = chstrdup(chbuf, BUFSIZE); - - ln_contnext = mkchstr(chbuf, BUFSIZE, attr_waitforkey, 0, 0, 1, - WIN_COLS - 4, &w_contnext, 1, - "[ Press to continue or " - " for the previous page ] "); - ch_contnext = chstrdup(chbuf, BUFSIZE); newtxwin(WIN_LINES - 1, WIN_COLS, 1, WCENTER, false, 0); @@ -205,12 +177,10 @@ void show_help (void) werase(curwin); wbkgd(curwin, attr_normal_window); box(curwin, 0, 0); - centerch(curwin, 1, 0, ch_title, ln_title, &w_title); - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_highlight, 0, 1, - WIN_COLS - 4, &width, 1, "Page %d of %d", - curpage + 1, numpages); - centerch(curwin, 2, 0, chbuf, lines, &width); + center(curwin, 1, 0, attr_title, 0, 0, " How to Play "); + center(curwin, 2, 0, attr_normal, attr_highlight, 0, + "Page %d of %d", curpage + 1, numpages); wmove(curwin, 4, 2); // Process the help text string @@ -355,14 +325,10 @@ void show_help (void) s++; } - if (curpage == 0) { - centerch(curwin, getmaxy(curwin) - 2, 0, ch_contfirst, - ln_contfirst, &w_contfirst); - } else { - centerch(curwin, getmaxy(curwin) - 2, 0, ch_contnext, - ln_contnext, &w_contnext); - } - + center(curwin, getmaxy(curwin) - 2, 0, attr_waitforkey, 0, 0, + (curpage == 0) ? "[ Press to continue ] " : + "[ Press to continue or " + "for the previous page ] "); wrefresh(curwin); int key = gettxchar(curwin); @@ -399,9 +365,4 @@ void show_help (void) deltxwin(); txrefresh(); - - free(ch_title); - free(ch_contfirst); - free(ch_contnext); - free(chbuf); } diff --git a/src/intf.c b/src/intf.c index e416456..4f3e1e1 100644 --- a/src/intf.c +++ b/src/intf.c @@ -402,25 +402,12 @@ void end_screen (void) void init_title (void) { - chtype *chbuf; - int width; - int lines; - - bkgd(attr_root_window); + attrset(attr_root_window); clear(); - move(0, 0); - for (int i = 0; i < COLS; i++) { - addch(attr_game_title | ' '); - } - - chbuf = xmalloc(BUFSIZE * sizeof(chtype)); - lines = mkchstr(chbuf, BUFSIZE, attr_game_title, 0, 0, 1, COLS, - &width, 1, _("Star Traders")); - centerch(stdscr, 0, 0, chbuf, lines, &width); - attrset(attr_root_window); - free(chbuf); + mvwhline(stdscr, 0, 0, ' ' | attr_game_title, COLS); + center(stdscr, 0, 0, attr_game_title, 0, 0, _("Star Traders")); } @@ -620,14 +607,7 @@ int txdlgbox (int maxlines, int ncols, int begin_y, int begin_x, true, bkgd_attr); if (usetitle) { - chtype *titlebuf = xmalloc(BUFSIZE * sizeof(chtype)); - int titlewidth; - int titlelines; - - titlelines = mkchstr(titlebuf, BUFSIZE, title_attr, 0, 0, 1, - ncols - 4, &titlewidth, 1, boxtitle); - centerch(curwin, 1, 0, titlebuf, titlelines, &titlewidth); - free(titlebuf); + center(curwin, 1, 0, title_attr, 0, 0, boxtitle); } centerch(curwin, usetitle ? 3 : 2, 0, chbuf, lines, widthbuf); @@ -654,8 +634,7 @@ int mkchstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm, va_start(args, format); lines = vmkchstr(chbuf, chbufsize, attr_norm, attr_alt1, attr_alt2, - maxlines, maxwidth, widthbuf, widthbufsize, format, - args); + maxlines, maxwidth, widthbuf, widthbufsize, format, args); va_end(args); return lines; } @@ -1308,6 +1287,84 @@ int rightch (WINDOW *win, int y, int x, const chtype *restrict chstr, } +/***********************************************************************/ +// left: Print strings left-aligned + +int left (WINDOW *win, int y, int x, chtype attr_norm, chtype attr_alt1, + chtype attr_alt2, const char *restrict format, ...) +{ + va_list args; + chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + int widthbuf[MAX_DLG_LINES]; + int lines; + int ret; + + + va_start(args, format); + lines = vmkchstr(chbuf, BUFSIZE, attr_norm, attr_alt1, attr_alt2, + MAX_DLG_LINES, getmaxx(win) - x - 2, widthbuf, + MAX_DLG_LINES, format, args); + ret = leftch(win, y, x, chbuf, lines, widthbuf); + assert(ret == OK); + va_end(args); + + free(chbuf); + return ret; +} + + +/***********************************************************************/ +// center: Print strings centred in window + +int center (WINDOW *win, int y, int offset, chtype attr_norm, chtype attr_alt1, + chtype attr_alt2, const char *restrict format, ...) +{ + va_list args; + chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + int widthbuf[MAX_DLG_LINES]; + int lines; + int ret; + + + va_start(args, format); + lines = vmkchstr(chbuf, BUFSIZE, attr_norm, attr_alt1, attr_alt2, + MAX_DLG_LINES, getmaxx(win) - 4, widthbuf, + MAX_DLG_LINES, format, args); + ret = centerch(win, y, offset, chbuf, lines, widthbuf); + assert(ret == OK); + va_end(args); + + free(chbuf); + return ret; +} + + +/***********************************************************************/ +// right: Print strings right-aligned + +int right (WINDOW *win, int y, int x, chtype attr_norm, chtype attr_alt1, + chtype attr_alt2, const char *restrict format, ...) +{ + va_list args; + chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + int widthbuf[MAX_DLG_LINES]; + int lines; + int ret; + + + va_start(args, format); + lines = vmkchstr(chbuf, BUFSIZE, attr_norm, attr_alt1, attr_alt2, + MAX_DLG_LINES, x - 2, widthbuf, MAX_DLG_LINES, + format, args); + ret = rightch(win, y, x, chbuf, lines, widthbuf); + assert(ret == OK); + va_end(args); + + free(chbuf); + return ret; +} + + /***********************************************************************/ // old_attrpr: Print a string with a particular character rendition @@ -2435,9 +2492,6 @@ bool answer_yesno (WINDOW *win) void wait_for_key (WINDOW *win, int y, chtype attr) { - chtype *chbuf; - int width; - int lines; int key; bool done; @@ -2446,10 +2500,7 @@ void wait_for_key (WINDOW *win, int y, chtype attr) meta(win, true); wtimeout(win, -1); - chbuf = xmalloc(BUFSIZE * sizeof(chtype)); - lines = mkchstr(chbuf, BUFSIZE, attr, 0, 0, 1, getmaxx(win) - 4, - &width, 1, _("[ Press to continue ] ")); - centerch(win, y, 0, chbuf, lines, &width); + center(curwin, y, 0, attr, 0, 0, _("[ Press to continue ] ")); wrefresh(win); done = false; @@ -2470,8 +2521,6 @@ void wait_for_key (WINDOW *win, int y, chtype attr) done = true; } } - - free(chbuf); } diff --git a/src/intf.h b/src/intf.h index 2e74e24..4541bc4 100644 --- a/src/intf.h +++ b/src/intf.h @@ -411,7 +411,7 @@ extern chtype *chstrdup (const chtype *restrict chstr, int chstrsize); chstr - chtype string as returned from mkchstr() lines - Number of lines in chstr as returned from mkchstr() widthbuf - Widths of each line as returned from mkchstr() - Returns: int - Error code OK + Returns: int - Always returns OK This function takes the strings in the chtype array chstr and prints them left-aligned in the window win. Note that wrefresh() is NOT @@ -460,6 +460,69 @@ extern int rightch (WINDOW *win, int y, int x, const chtype *restrict chstr, int lines, const int *restrict widthbuf); +/* + Function: left - Print strings left-aligned + Parameters: win - Window to use (should be curwin) + y - Line on which to print first string + x - Starting column number for each line + attr_norm - Normal character rendition to use + attr_alt1 - First alternate character rendition to use + attr_alt2 - Second alternate character rendition to use + format - Format string as described for mkchstr() + ... - Arguments for the format string + Returns: int - Always returns OK + + This shortcut function prepares a chtype string using mkchstr(), then + prints the string using leftch(). At most MAX_DLG_LINES are printed, + with the maximum width being that of the window win - x - 2 (the "2" is + for the right-hand border). +*/ +extern int left (WINDOW *win, int y, int x, chtype attr_norm, chtype attr_alt1, + chtype attr_alt2, const char *restrict format, ...); + + +/* + Function: center - Print strings centred in window + Parameters: win - Window to use (should be curwin) + y - Line on which to print first string + offset - Column offset to add to position for each line + attr_norm - Normal character rendition to use + attr_alt1 - First alternate character rendition to use + attr_alt2 - Second alternate character rendition to use + format - Format string as described for mkchstr() + ... - Arguments for the format string + Returns: int - Always returns OK + + This shortcut function prepares a chtype string using mkchstr(), then + prints the string using centerch(). At most MAX_DLG_LINES are printed, + with the maximum width being that of the window win - 4 (for borders). +*/ +extern int center (WINDOW *win, int y, int offset, chtype attr_norm, + chtype attr_alt1, chtype attr_alt2, + const char *restrict format, ...); + + +/* + Function: right - Print strings right-aligned + Parameters: win - Window to use (should be curwin) + y - Line on which to print first string + x - Ending column number for each line + attr_norm - Normal character rendition to use + attr_alt1 - First alternate character rendition to use + attr_alt2 - Second alternate character rendition to use + format - Format string as described for mkchstr() + ... - Arguments for the format string + Returns: int - Always returns OK + + This shortcut function prepares a chtype string using mkchstr(), then + prints the string using rightch(). At most MAX_DLG_LINES are printed, + with the maximum width being that of x - 2 (the "2" is for the + left-hand border). +*/ +extern int right (WINDOW *win, int y, int x, chtype attr_norm, chtype attr_alt1, + chtype attr_alt2, const char *restrict format, ...); + + /* Function: old_attrpr - Print a string with a particular character rendition Parameters: win - Window to use (should be curwin) diff --git a/src/move.c b/src/move.c index 17c86f9..a727593 100644 --- a/src/move.c +++ b/src/move.c @@ -208,8 +208,6 @@ void select_moves (void) selection_t get_move (void) { selection_t selection = SEL_NONE; - chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); - int lines, width; if (quit_selected || abort_game) { @@ -233,31 +231,19 @@ selection_t get_move (void) werase(curwin); box(curwin, 0, 0); - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, - 1, getmaxx(curwin) / 2 - 4, &width, 1, - "^{<1>^} Display stock portfolio"); - leftch(curwin, 2, 2, chbuf, lines, &width); + left(curwin, 2, 2, attr_normal, attr_keycode, 0, + "^{<1>^} Display stock portfolio"); + left(curwin, 3, 2, attr_normal, attr_keycode, 0, + "^{<2>^} Declare bankruptcy"); + left(curwin, 2, getmaxx(curwin) / 2, attr_normal, attr_keycode, 0, + "^{<3>^} Save and end the game"); + left(curwin, 3, getmaxx(curwin) / 2, attr_normal, attr_keycode, 0, + "^{^} Quit the game"); - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, - 1, getmaxx(curwin) / 2 - 4, &width, 1, - "^{<2>^} Declare bankruptcy"); - leftch(curwin, 3, 2, chbuf, lines, &width); - - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, - 1, getmaxx(curwin) / 2 - 4, &width, 1, - "^{<3>^} Save and end the game"); - leftch(curwin, 2, getmaxx(curwin) / 2, chbuf, lines, &width); - - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, - 1, getmaxx(curwin) / 2 - 4, &width, 1, - "^{^} Quit the game"); - leftch(curwin, 3, getmaxx(curwin) / 2, chbuf, lines, &width); - - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, attr_choice, - 1, getmaxx(curwin) / 2 - 4, &width, 1, - "Select move [^[%c^]-^[%c^]/^{1^}-^{3^}/^{^}]: ", - MOVE_TO_KEY(0), MOVE_TO_KEY(NUMBER_MOVES - 1)); - rightch(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width); + right(curwin, 1, getmaxx(curwin) / 2, attr_normal, attr_keycode, + attr_choice, "Select move " + "[^[%c^]-^[%c^]/^{1^}-^{3^}/^{^}]: ", + MOVE_TO_KEY(0), MOVE_TO_KEY(NUMBER_MOVES - 1)); curs_set(CURS_ON); wrefresh(curwin); @@ -270,10 +256,8 @@ selection_t get_move (void) selection = KEY_TO_MOVE(key); curs_set(CURS_OFF); - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_choice, 0, - 1, getmaxx(curwin) / 2 - 4, &width, 1, - "Move ^{%c^}", key); - leftch(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width); + left(curwin, 1, getmaxx(curwin) / 2, attr_normal, attr_choice, + 0, "Move ^{%c^}", key); } else { switch (key) { case '1': @@ -286,22 +270,18 @@ selection_t get_move (void) selection = SEL_BANKRUPT; curs_set(CURS_OFF); - lines = mkchstr(chbuf, BUFSIZE, attr_normal, - attr_normal | A_BOLD, 0, 1, - getmaxx(curwin) / 2 - 4, &width, 1, - "^{<2>^} (Declare bankruptcy)"); - leftch(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width); + left(curwin, 1, getmaxx(curwin) / 2, attr_normal, + attr_normal | A_BOLD, 0, + "^{<2>^} (Declare bankruptcy)"); break; case '3': selection = SEL_SAVE; curs_set(CURS_OFF); - lines = mkchstr(chbuf, BUFSIZE, attr_normal, - attr_normal | A_BOLD, 0, 1, - getmaxx(curwin) / 2 - 4, &width, 1, - "^{<3>^} (Save and end the game)"); - leftch(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width); + left(curwin, 1, getmaxx(curwin) / 2, attr_normal, + attr_normal | A_BOLD, 0, + "^{<3>^} (Save and end the game)"); break; case KEY_ESC: @@ -313,11 +293,9 @@ selection_t get_move (void) selection = SEL_QUIT; curs_set(CURS_OFF); - lines = mkchstr(chbuf, BUFSIZE, attr_normal, - attr_normal | A_BOLD, 0, 1, - getmaxx(curwin) / 2 - 4, &width, 1, - "^{^} (Quit the game)"); - leftch(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width); + left(curwin, 1, getmaxx(curwin) / 2, attr_normal, + attr_normal | A_BOLD, 0, + "^{^} (Quit the game)"); break; default: @@ -331,10 +309,8 @@ selection_t get_move (void) mvwhline(curwin, 3, 2, ' ' | attr_normal, getmaxx(curwin) - 4); // Ask the player to confirm their choice - lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, - 1, getmaxx(curwin) / 2 - 4, &width, 1, - "Are you sure? [^{Y^}/^{N^}] "); - rightch(curwin, 2, getmaxx(curwin) / 2, chbuf, lines, &width); + right(curwin, 2, getmaxx(curwin) / 2, attr_normal, attr_keycode, 0, + "Are you sure? [^{Y^}/^{N^}] "); wrefresh(curwin); if (! answer_yesno(curwin)) { @@ -343,13 +319,15 @@ selection_t get_move (void) // Save the game if required if (selection == SEL_SAVE) { + chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + int width; + bool saved = false; if (game_loaded) { // Save the game to the same game number - lines = mkchstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1, - WIN_COLS - 7, &width, 1, - "Saving game %d... ", game_num); + mkchstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1, WIN_COLS + - 7, &width, 1, "Saving game %d... ", game_num); newtxwin(5, width + 5, 7, WCENTER, true, attr_status_window); centerch(curwin, 2, 0, chbuf, 1, &width); wrefresh(curwin); @@ -366,13 +344,10 @@ selection_t get_move (void) int key; bool done; int widthbuf[2]; - int maxwidth; - + int lines, maxwidth; lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, - sizeof(widthbuf) / sizeof(widthbuf[0]), - WIN_COLS - 7, widthbuf, - sizeof(widthbuf) / sizeof(widthbuf[0]), + 2, WIN_COLS - 7, widthbuf, 2, "Enter game number [^{1^}-^{9^}] " "or ^{^} to cancel: "); assert(lines == 1 || lines == 2); @@ -417,9 +392,9 @@ selection_t get_move (void) // Try to save the game, if possible game_num = key - '0'; - lines = mkchstr(chbuf, BUFSIZE, attr_status_window, - 0, 0, 1, WIN_COLS - 7, &width, 1, - "Saving game %d... ", game_num); + mkchstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1, + WIN_COLS - 7, &width, 1, + "Saving game %d... ", game_num); newtxwin(5, width + 5, 7, WCENTER, true, attr_status_window); centerch(curwin, 2, 0, chbuf, 1, &width); wrefresh(curwin); @@ -443,10 +418,11 @@ selection_t get_move (void) selection = SEL_NONE; } + + free(chbuf); } } - free(chbuf); return selection; }