diff --git a/src/game.c b/src/game.c index 41d06f7..8c8b085 100644 --- a/src/game.c +++ b/src/game.c @@ -110,14 +110,21 @@ void init_game (void) { // Try to load an old game, if possible if (game_num != 0) { - newtxwin(5, 30, 6, WCENTER, true, attr_status_window); - center(curwin, 2, attr_status_window, "Loading game %d... ", game_num); + chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + int lines, width; + + lines = prepstr(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); + pr_center(curwin, 2, 0, chbuf, 1, &width); wrefresh(curwin); game_loaded = load_game(game_num); deltxwin(); txrefresh(); + free(chbuf); } // Initialise game data, if not already loaded @@ -134,18 +141,25 @@ void init_game (void) choice = ask_game_number(); if (choice != ERR) { + // Try to load the game, if possible + + chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + int lines, width; + game_num = choice; - // Try to load the game, if possible - newtxwin(5, 30, 9, WCENTER, true, attr_status_window); - center(curwin, 2, attr_status_window, - "Loading game %d... ", game_num); + lines = prepstr(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); + pr_center(curwin, 2, 0, chbuf, 1, &width); wrefresh(curwin); game_loaded = load_game(game_num); deltxwin(); txrefresh(); + free(chbuf); } deltxwin(); // "Enter game number" window @@ -227,22 +241,24 @@ void init_game (void) static int ask_number_players (void) { + chtype *chbuf; + int lines, maxwidth; + int widthbuf[2]; int key, ret; bool done; - // Ask for the number of players - newtxwin(5, 62, 3, WCENTER, true, attr_normal_window); + chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + lines = prepstr(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^}] " + "or ^{^} to continue a game: ", MAX_PLAYERS); + maxwidth = MAX(widthbuf[0], widthbuf[1]) + 5; - mvwaddstr(curwin, 2, 2, "Enter number of players "); - waddstr(curwin, "["); - attrpr(curwin, attr_keycode, "1"); - waddstr(curwin, "-"); - attrpr(curwin, attr_keycode, "%d", MAX_PLAYERS); - waddstr(curwin, "]"); - waddstr(curwin, " or "); - attrpr(curwin, attr_keycode, ""); - waddstr(curwin, " to continue a game: "); + newtxwin(lines + 4, maxwidth, 3, WCENTER, true, attr_normal_window); + pr_left(curwin, 2, 2, chbuf, lines, widthbuf); + free(chbuf); curs_set(CURS_ON); wrefresh(curwin); @@ -289,22 +305,24 @@ static int ask_number_players (void) int ask_game_number (void) { + chtype *chbuf; + int lines, maxwidth; + int widthbuf[2]; int key, ret; bool done; - // Ask which game to load - newtxwin(5, 54, 6, WCENTER, true, attr_normal_window); + chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + lines = prepstr(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^}] " + "or ^{^} to cancel: "); + maxwidth = MAX(widthbuf[0], widthbuf[1]) + 5; - mvwaddstr(curwin, 2, 2, "Enter game number "); - waddstr(curwin, "["); - attrpr(curwin, attr_keycode, "1"); - waddstr(curwin, "-"); - attrpr(curwin, attr_keycode, "9"); - waddstr(curwin, "]"); - waddstr(curwin, " or "); - attrpr(curwin, attr_keycode, ""); - waddstr(curwin, " to cancel: "); + newtxwin(lines + 4, maxwidth, 6, WCENTER, true, attr_normal_window); + pr_left(curwin, 2, 2, chbuf, lines, widthbuf); + free(chbuf); curs_set(CURS_ON); wrefresh(curwin); @@ -349,7 +367,6 @@ void ask_player_names (void) // 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: "); int x = getcurx(curwin); @@ -366,23 +383,35 @@ void ask_player_names (void) } } - newtxwin(5, 44, 6, WCENTER, true, attr_normal_window); - mvwaddstr(curwin, 2, 2, "Do you need any instructions?"); - if (answer_yesno(curwin, attr_keycode)) { + chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + int lines, width; + + lines = prepstr(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); + pr_left(curwin, 2, 2, chbuf, lines, &width); + free(chbuf); + if (answer_yesno(curwin)) { show_help(); } } 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); - - center(curwin, 1, attr_title, " Enter Player Names "); + lines = prepstr(chbuf, BUFSIZE, attr_title, 0, 0, 1, WIN_COLS - 8, + &width, 1, " Enter Player Names "); + pr_center(curwin, 1, 0, chbuf, lines, &width); for (i = 0; i < number_players; i++) { player[i].name = NULL; @@ -463,11 +492,17 @@ void ask_player_names (void) } } - newtxwin(5, 50, 6, WCENTER, true, attr_normal_window); - mvwaddstr(curwin, 2, 2, "Does any player need instructions?"); - if (answer_yesno(curwin, attr_keycode)) { + lines = prepstr(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); + pr_left(curwin, 2, 2, chbuf, lines, &width); + if (answer_yesno(curwin)) { show_help(); } + + free(chbuf); } deltxwin(); // "Need instructions?" window @@ -552,48 +587,30 @@ void end_game (void) void show_map (bool closewin) { - int n, x, y; + chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + int lines, width; + int x, y; newtxwin(MAX_Y + 4, WIN_COLS, 1, WCENTER, true, attr_map_window); - // Draw various borders + // Draw various borders and highlights mvwaddch(curwin, 2, 0, ACS_LTEE); whline(curwin, ACS_HLINE, getmaxx(curwin) - 2); mvwaddch(curwin, 2, getmaxx(curwin) - 1, ACS_RTEE); + mvwhline(curwin, 1, 2, ' ' | attr_mapwin_title, getmaxx(curwin) - 4); // Display current player and turn number - wattrset(curwin, attr_mapwin_title); - mvwaddstr(curwin, 1, 2, " "); - waddstr(curwin, "Player: "); - n = getmaxx(curwin) - getcurx(curwin) - 4; - wattrset(curwin, attr_mapwin_highlight); - wprintw(curwin, "%-*.*s", n, n, player[current_player].name); - wattrset(curwin, attr_mapwin_title); - waddstr(curwin, " "); + lines = prepstr(chbuf, BUFSIZE, attr_mapwin_title, attr_mapwin_highlight, + 0, 1, WIN_COLS - 4, &width, 1, " Player: ^{%s^} ", + player[current_player].name); + pr_left(curwin, 1, 2, chbuf, lines, &width); - if (turn_number != max_turn) { - const char *initial = "Turn: "; - char *buf = xmalloc(BUFSIZE); - - int len1 = strlen(initial); - int len2 = snprintf(buf, BUFSIZE, "%d", turn_number); - - mvwaddstr(curwin, 1, getmaxx(curwin) - (len1 + len2) - 6, " "); - waddstr(curwin, initial); - attrpr(curwin, attr_mapwin_highlight, "%s", buf); - waddstr(curwin, " "); - - free(buf); - - } else { - const char *buf = "*** Last Turn ***"; - int len = strlen(buf); - - mvwaddstr(curwin, 1, getmaxx(curwin) - len - 6, " "); - attrpr(curwin, attr_mapwin_blink, "%s", buf); - waddstr(curwin, " "); - } + lines = prepstr(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); + pr_right(curwin, 1, WIN_COLS - 2, chbuf, lines, &width); wattrset(curwin, attr_map_window); @@ -638,6 +655,8 @@ void show_map (bool closewin) deltxwin(); // Galaxy map window txrefresh(); } + + free(chbuf); } @@ -646,6 +665,8 @@ void show_map (bool closewin) void show_status (int num) { + chtype *chbuf; + int lines, width; double val; int i, line; @@ -655,13 +676,22 @@ void show_status (int num) newtxwin(MAX_COMPANIES + 15, WIN_COLS, 1, WCENTER, true, attr_normal_window); - center(curwin, 1, attr_title, " Stock Portfolio "); - center2(curwin, 2, attr_normal, attr_highlight, "Player: ", "%s", - player[num].name); + chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + lines = prepstr(chbuf, BUFSIZE, attr_title, 0, 0, 1, WIN_COLS - 4, + &width, 1, " Stock Portfolio "); + pr_center(curwin, 1, 0, chbuf, lines, &width); + + lines = prepstr(chbuf, BUFSIZE, attr_normal, attr_highlight, 0, 1, + WIN_COLS - 4, &width, 1, "Player: ^{%s^}", + player[num].name); + pr_center(curwin, 2, 0, chbuf, lines, &width); val = total_value(num); if (val == 0.0) { - center(curwin, 11, attr_blink, "* * * B A N K R U P T * * *"); + lines = prepstr(chbuf, BUFSIZE, attr_normal, attr_highlight, + attr_blink, 1, WIN_COLS - 4, &width, 1, + "^[* * * B A N K R U P T * * *^]"); + pr_center(curwin, 11, 0, chbuf, lines, &width); } else { char *buf = xmalloc(BUFSIZE); @@ -676,7 +706,10 @@ void show_status (int num) } if (none) { - center(curwin, 8, attr_normal, "No companies on the map"); + lines = prepstr(chbuf, BUFSIZE, attr_normal, attr_highlight, 0, + 1, WIN_COLS - 4, &width, 1, + "No companies on the map"); + pr_center(curwin, 8, 0, chbuf, lines, &width); } else { // Handle the locale's currency symbol snprintf(buf, BUFSIZE, "share (%s)", lconvinfo.currency_symbol); @@ -726,6 +759,7 @@ 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 4f9a520..dd78cf5 100644 --- a/src/help.c +++ b/src/help.c @@ -157,17 +157,45 @@ 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) + if (numpages == 0) { + free(chbuf); return; + } + + // Prepare fixed-text strings + ln_title = prepstr(chbuf, BUFSIZE, attr_title, 0, 0, 1, WIN_COLS - 4, + &w_title, 1, " How to Play "); + ch_title = chbufdup(chbuf, BUFSIZE); + + ln_contfirst = prepstr(chbuf, BUFSIZE, attr_waitforkey, 0, 0, 1, + WIN_COLS - 4, &w_contfirst, 1, + "[ Press to continue ] "); + ch_contfirst = chbufdup(chbuf, BUFSIZE); + + ln_contnext = prepstr(chbuf, BUFSIZE, attr_waitforkey, 0, 0, 1, + WIN_COLS - 4, &w_contnext, 1, + "[ Press to continue or " + " for the previous page ] "); + ch_contnext = chbufdup(chbuf, BUFSIZE); newtxwin(WIN_LINES - 1, WIN_COLS, 1, WCENTER, false, 0); @@ -177,8 +205,12 @@ void show_help (void) werase(curwin); wbkgd(curwin, attr_normal_window); box(curwin, 0, 0); - center(curwin, 1, attr_title, " How to Play "); - center(curwin, 2, attr_normal, "Page %d of %d", curpage + 1, numpages); + pr_center(curwin, 1, 0, ch_title, ln_title, &w_title); + + lines = prepstr(chbuf, BUFSIZE, attr_normal, attr_highlight, 0, 1, + WIN_COLS - 4, &width, 1, "Page %d of %d", + curpage + 1, numpages); + pr_center(curwin, 2, 0, chbuf, lines, &width); wmove(curwin, 4, 2); // Process the help text string @@ -323,9 +355,13 @@ void show_help (void) s++; } - center(curwin, 21, attr_waitforkey, (curpage == 0) ? - "[ Press to continue ] " : - "[ Press to continue or for the previous page ] "); + if (curpage == 0) { + pr_center(curwin, getmaxy(curwin) - 2, 0, ch_contfirst, + ln_contfirst, &w_contfirst); + } else { + pr_center(curwin, getmaxy(curwin) - 2, 0, ch_contnext, + ln_contnext, &w_contnext); + } wrefresh(curwin); @@ -363,4 +399,9 @@ 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 b3e4493..3d08ab0 100644 --- a/src/intf.c +++ b/src/intf.c @@ -2352,7 +2352,7 @@ int gettxlong (WINDOW *win, long int *restrict result, long int min, /***********************************************************************/ // answer_yesno: Wait for a Yes/No answer -bool answer_yesno (WINDOW *win, chtype attr_keys) +bool answer_yesno (WINDOW *win) { int key; bool done; @@ -2365,12 +2365,6 @@ bool answer_yesno (WINDOW *win, chtype attr_keys) meta(win, true); wtimeout(win, -1); - waddstr(curwin, " ["); - attrpr(curwin, attr_keys, "Y"); - waddstr(curwin, "/"); - attrpr(curwin, attr_keys, "N"); - waddstr(curwin, "] "); - curs_set(CURS_ON); done = false; diff --git a/src/intf.h b/src/intf.h index d5ef379..a06c394 100644 --- a/src/intf.h +++ b/src/intf.h @@ -53,9 +53,10 @@ #define WIN_LINES MIN_LINES // Number of lines used in main window #define WIN_COLS MIN_COLS // Number of columns used in main window -#define WCENTER -1 // Centre the new window +#define WCENTER -1 // Centre the new window -#define MAX_DLG_LINES 10 // Default maximum lines in dialog box +#define MAX_DLG_LINES 10 // Default maximum lines of text in dialog box +#define YESNO_COLS 4 // Space to allow for "Yes" or "No" response // Check if resizing events are supported @@ -728,17 +729,14 @@ extern int gettxlong (WINDOW *win, long int *restrict result, long int min, /* Function: answer_yesno - Wait for a Yes/No answer Parameters: win - Window to use (should be curwin) - attr_keys - Window rendition to use for key choices Returns: bool - True if Yes was selected, false if No - This function prompts the user by printing " [Y/N] " using appropriate - character renditions ("Y" and "N" in attr_keys, the rest in the current - rendition), then waits for the user to press either "Y" (for Yes) or - "N" (for No) on the keyboard, then prints the answer using A_BOLD. - True is returned if "Y" was selected, false if "N". Note that the - cursor becomes invisible after calling this function. + This function waits for the user to press either "Y" (for Yes) or "N" + (for No) on the keyboard, then prints the answer using A_BOLD. True is + returned if "Y" was selected, false if "N". Note that the cursor + becomes invisible after calling this function. */ -extern bool answer_yesno (WINDOW *win, chtype attr_keys); +extern bool answer_yesno (WINDOW *win); /* diff --git a/src/move.c b/src/move.c index 98fc17e..fcba6ef 100644 --- a/src/move.c +++ b/src/move.c @@ -207,7 +207,6 @@ void select_moves (void) selection_t get_move (void) { - int i, x, y; selection_t selection = SEL_NONE; @@ -219,7 +218,7 @@ selection_t get_move (void) show_map(false); // Display current move choices on the galaxy map - for (i = 0; i < NUMBER_MOVES; i++) { + for (int i = 0; i < NUMBER_MOVES; i++) { mvwaddch(curwin, game_move[i].y + 3, game_move[i].x * 2 + 2, MOVE_TO_KEY(i) | attr_map_choice); } @@ -324,18 +323,20 @@ selection_t get_move (void) } // Clear the menu choices (but not the prompt!) - wattrset(curwin, attr_normal); - for (y = 2; y < 4; y++) { - wmove(curwin, y, 2); - for (x = 2; x < getmaxx(curwin) - 2; x++) { - waddch(curwin, ' ' | attr_normal); - } - } - wrefresh(curwin); + mvwhline(curwin, 2, 2, ' ' | attr_normal, getmaxx(curwin) - 4); + mvwhline(curwin, 3, 2, ' ' | attr_normal, getmaxx(curwin) - 4); // Ask the player to confirm their choice - mvwaddstr(curwin, 2, 22, "Are you sure?"); - if (! answer_yesno(curwin, attr_keycode)) { + wattrset(curwin, attr_normal); + mvwaddstr(curwin, 2, 22, "Are you sure? "); + waddstr(curwin, "["); + attrpr(curwin, attr_keycode, "Y"); + waddstr(curwin, "/"); + attrpr(curwin, attr_keycode, "N"); + waddstr(curwin, "] "); + wrefresh(curwin); + + if (! answer_yesno(curwin)) { selection = SEL_NONE; } @@ -345,15 +346,21 @@ selection_t get_move (void) if (game_loaded) { // Save the game to the same game number - newtxwin(5, 30, 7, WCENTER, true, attr_status_window); - center(curwin, 2, attr_status_window, - "Saving game %d... ", game_num); + chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + int lines, width; + + lines = prepstr(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); + pr_center(curwin, 2, 0, chbuf, 1, &width); wrefresh(curwin); saved = save_game(game_num); deltxwin(); txrefresh(); + free(chbuf); } if (! saved) { @@ -405,18 +412,24 @@ selection_t get_move (void) curs_set(CURS_OFF); if (key != KEY_CANCEL) { + // Try to save the game, if possible + chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); + int lines, width; + game_num = key - '0'; - // Try to save the game, if possible - newtxwin(5, 30, 7, WCENTER, true, attr_status_window); - center(curwin, 2, attr_status_window, - "Saving game %d... ", game_num); + lines = prepstr(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); + pr_center(curwin, 2, 0, chbuf, 1, &width); wrefresh(curwin); saved = save_game(game_num); deltxwin(); txrefresh(); + free(chbuf); } deltxwin(); // "Enter game number" window