1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-12-04 14:46:45 -05:00

Add convenience functions left(), center() and right()

This commit is contained in:
John Zaitseff 2011-08-15 21:04:07 +10:00
parent a6c362d2e8
commit 665605d144
5 changed files with 231 additions and 215 deletions

View File

@ -111,11 +111,10 @@ void init_game (void)
// Try to load an old game, if possible // Try to load an old game, if possible
if (game_num != 0) { if (game_num != 0) {
chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype));
int lines, width; int width;
lines = mkchstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1, mkchstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1, WIN_COLS - 7,
WIN_COLS - 7, &width, 1, &width, 1, "Loading game %d... ", game_num);
"Loading game %d... ", game_num);
newtxwin(5, width + 5, 6, WCENTER, true, attr_status_window); newtxwin(5, width + 5, 6, WCENTER, true, attr_status_window);
centerch(curwin, 2, 0, chbuf, 1, &width); centerch(curwin, 2, 0, chbuf, 1, &width);
wrefresh(curwin); wrefresh(curwin);
@ -144,13 +143,13 @@ void init_game (void)
// Try to load the game, if possible // Try to load the game, if possible
chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype));
int lines, width; int width;
game_num = choice; game_num = choice;
lines = mkchstr(chbuf, BUFSIZE, attr_status_window, mkchstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1,
0, 0, 1, WIN_COLS - 7, &width, 1, WIN_COLS - 7, &width, 1,
"Loading game %d... ", game_num); "Loading game %d... ", game_num);
newtxwin(5, width + 5, 9, WCENTER, true, attr_status_window); newtxwin(5, width + 5, 9, WCENTER, true, attr_status_window);
centerch(curwin, 2, 0, chbuf, 1, &width); centerch(curwin, 2, 0, chbuf, 1, &width);
wrefresh(curwin); wrefresh(curwin);
@ -249,10 +248,8 @@ static int ask_number_players (void)
chbuf = xmalloc(BUFSIZE * sizeof(chtype)); chbuf = xmalloc(BUFSIZE * sizeof(chtype));
lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, 2, WIN_COLS
sizeof(widthbuf) / sizeof(widthbuf[0]), WIN_COLS - 7, - 7, widthbuf, 2, "Enter number of players [^{1^}-^{%d^}] "
widthbuf, sizeof(widthbuf) / sizeof(widthbuf[0]),
"Enter number of players [^{1^}-^{%d^}] "
"or ^{<C>^} to continue a game: ", MAX_PLAYERS); "or ^{<C>^} to continue a game: ", MAX_PLAYERS);
assert(lines == 1 || lines == 2); assert(lines == 1 || lines == 2);
maxwidth = ((lines == 1) ? widthbuf[0] : MAX(widthbuf[0], widthbuf[1])) + 5; 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)); chbuf = xmalloc(BUFSIZE * sizeof(chtype));
lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, 2, WIN_COLS
sizeof(widthbuf) / sizeof(widthbuf[0]), WIN_COLS - 7, - 7, widthbuf, 2, "Enter game number [^{1^}-^{9^}] "
widthbuf, sizeof(widthbuf) / sizeof(widthbuf[0]),
"Enter game number [^{1^}-^{9^}] "
"or ^{<CTRL><C>^} to cancel: "); "or ^{<CTRL><C>^} to cancel: ");
assert(lines == 1 || lines == 2); assert(lines == 1 || lines == 2);
maxwidth = ((lines == 1) ? widthbuf[0] : MAX(widthbuf[0], widthbuf[1])) + 5; 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) void ask_player_names (void)
{ {
chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype));
int width;
if (number_players == 1) { if (number_players == 1) {
// Ask for the player's name // Ask for the player's name
newtxwin(5, WIN_COLS - 4, 9, WCENTER, true, attr_normal_window); 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 x = getcurx(curwin);
int w = getmaxx(curwin) - x - 2; int w = getmaxx(curwin) - x - 2;
@ -385,16 +384,13 @@ void ask_player_names (void)
} }
} }
chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype)); mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, 1,
int lines, width; WIN_COLS - YESNO_COLS - 6, &width, 1,
"Do you need any instructions? [^{Y^}/^{N^}] ");
lines = 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, newtxwin(5, width + YESNO_COLS + 4, 6, WCENTER, true,
attr_normal_window); attr_normal_window);
leftch(curwin, 2, 2, chbuf, lines, &width); leftch(curwin, 2, 2, chbuf, 1, &width);
free(chbuf);
if (answer_yesno(curwin)) { if (answer_yesno(curwin)) {
show_help(); show_help();
} }
@ -402,23 +398,18 @@ void ask_player_names (void)
} else { } else {
// Ask for all of the player names // Ask for all of the player names
chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype));
int lines, width;
bool entered[MAX_PLAYERS]; bool entered[MAX_PLAYERS];
bool done, modified; bool done, modified;
int cur, len, i; int cur, len, i;
newtxwin(number_players + 5, WIN_COLS - 4, 9, WCENTER, newtxwin(number_players + 5, WIN_COLS - 4, 9, WCENTER,
true, attr_normal_window); true, attr_normal_window);
lines = mkchstr(chbuf, BUFSIZE, attr_title, 0, 0, 1, WIN_COLS - 8, center(curwin, 1, 0, attr_title, 0, 0, " Enter Player Names ");
&width, 1, " Enter Player Names ");
centerch(curwin, 1, 0, chbuf, lines, &width);
for (i = 0; i < number_players; i++) { for (i = 0; i < number_players; i++) {
player[i].name = NULL; player[i].name = NULL;
entered[i] = false; 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; int x = getcurx(curwin) + 1;
@ -494,21 +485,21 @@ void ask_player_names (void)
} }
} }
lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, 1,
1, WIN_COLS - YESNO_COLS - 6, &width, 1, WIN_COLS - YESNO_COLS - 6, &width, 1,
"Does any player need instructions? [^{Y^}/^{N^}] "); "Does any player need instructions? [^{Y^}/^{N^}] ");
newtxwin(5, width + YESNO_COLS + 4, 6, WCENTER, true, newtxwin(5, width + YESNO_COLS + 4, 6, WCENTER, true,
attr_normal_window); attr_normal_window);
leftch(curwin, 2, 2, chbuf, lines, &width); leftch(curwin, 2, 2, chbuf, 1, &width);
if (answer_yesno(curwin)) { if (answer_yesno(curwin)) {
show_help(); show_help();
} }
free(chbuf);
} }
deltxwin(); // "Need instructions?" window deltxwin(); // "Need instructions?" window
deltxwin(); // "Enter player names" window deltxwin(); // "Enter player names" window
free(chbuf);
} }
@ -589,8 +580,6 @@ void end_game (void)
void show_map (bool closewin) void show_map (bool closewin)
{ {
chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype));
int lines, width;
int x, y; int x, y;
@ -603,16 +592,11 @@ void show_map (bool closewin)
mvwhline(curwin, 1, 2, ' ' | attr_mapwin_title, getmaxx(curwin) - 4); mvwhline(curwin, 1, 2, ' ' | attr_mapwin_title, getmaxx(curwin) - 4);
// Display current player and turn number // Display current player and turn number
lines = mkchstr(chbuf, BUFSIZE, attr_mapwin_title, attr_mapwin_highlight, left(curwin, 1, 2, attr_mapwin_title, attr_mapwin_highlight, 0,
0, 1, WIN_COLS - 4, &width, 1, " Player: ^{%s^} ", " Player: ^{%s^} ", player[current_player].name);
player[current_player].name); right(curwin, 1, getmaxx(curwin) - 2, attr_mapwin_title,
leftch(curwin, 1, 2, chbuf, lines, &width); attr_mapwin_highlight, attr_mapwin_blink, (turn_number != max_turn) ?
" Turn: ^{%d^} " : " ^[*** Last Turn ***^] ", turn_number);
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);
wattrset(curwin, attr_map_window); wattrset(curwin, attr_map_window);
@ -657,8 +641,6 @@ void show_map (bool closewin)
deltxwin(); // Galaxy map window deltxwin(); // Galaxy map window
txrefresh(); txrefresh();
} }
free(chbuf);
} }
@ -667,8 +649,6 @@ void show_map (bool closewin)
void show_status (int num) void show_status (int num)
{ {
chtype *chbuf;
int lines, width;
double val; double val;
int i, line; int i, line;
@ -677,24 +657,14 @@ void show_status (int num)
newtxwin(MAX_COMPANIES + 15, WIN_COLS, 1, WCENTER, true, newtxwin(MAX_COMPANIES + 15, WIN_COLS, 1, WCENTER, true,
attr_normal_window); attr_normal_window);
center(curwin, 1, 0, attr_title, 0, 0, " Stock Portfolio ");
chbuf = xmalloc(BUFSIZE * sizeof(chtype)); center(curwin, 2, 0, attr_normal, attr_highlight, 0, "Player: ^{%s^}",
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^}",
player[num].name); player[num].name);
centerch(curwin, 2, 0, chbuf, lines, &width);
val = total_value(num); val = total_value(num);
if (val == 0.0) { if (val == 0.0) {
lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_highlight, center(curwin, 11, 0, attr_normal, attr_highlight, attr_blink,
attr_blink, 1, WIN_COLS - 4, &width, 1,
"^[* * * B A N K R U P T * * *^]"); "^[* * * B A N K R U P T * * *^]");
centerch(curwin, 11, 0, chbuf, lines, &width);
} else { } else {
char *buf = xmalloc(BUFSIZE); char *buf = xmalloc(BUFSIZE);
@ -708,10 +678,8 @@ void show_status (int num)
} }
if (none) { if (none) {
lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_highlight, 0, center(curwin, 8, 0, attr_normal, attr_highlight, 0,
1, WIN_COLS - 4, &width, 1,
"No companies on the map"); "No companies on the map");
centerch(curwin, 8, 0, chbuf, lines, &width);
} else { } else {
// Handle the locale's currency symbol // Handle the locale's currency symbol
snprintf(buf, BUFSIZE, "share (%s)", lconvinfo.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); wait_for_key(curwin, getmaxy(curwin) - 2, attr_waitforkey);
deltxwin(); deltxwin();
txrefresh(); txrefresh();
free(chbuf);
} }

View File

@ -157,45 +157,17 @@ static const char *help_text[] = {
void show_help (void) void show_help (void)
{ {
chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype));
int lines, width;
int curpage = 0; int curpage = 0;
int numpages; int numpages;
bool done = false; 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 // Count how many pages appear in the help text
for (numpages = 0; help_text[numpages] != NULL; numpages++) for (numpages = 0; help_text[numpages] != NULL; numpages++)
; ;
if (numpages == 0) { if (numpages == 0)
free(chbuf);
return; 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 <SPACE> to continue ] ");
ch_contfirst = chstrdup(chbuf, BUFSIZE);
ln_contnext = mkchstr(chbuf, BUFSIZE, attr_waitforkey, 0, 0, 1,
WIN_COLS - 4, &w_contnext, 1,
"[ Press <SPACE> to continue or "
"<BACKSPACE> for the previous page ] ");
ch_contnext = chstrdup(chbuf, BUFSIZE);
newtxwin(WIN_LINES - 1, WIN_COLS, 1, WCENTER, false, 0); newtxwin(WIN_LINES - 1, WIN_COLS, 1, WCENTER, false, 0);
@ -205,12 +177,10 @@ void show_help (void)
werase(curwin); werase(curwin);
wbkgd(curwin, attr_normal_window); wbkgd(curwin, attr_normal_window);
box(curwin, 0, 0); box(curwin, 0, 0);
centerch(curwin, 1, 0, ch_title, ln_title, &w_title);
lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_highlight, 0, 1, center(curwin, 1, 0, attr_title, 0, 0, " How to Play ");
WIN_COLS - 4, &width, 1, "Page %d of %d", center(curwin, 2, 0, attr_normal, attr_highlight, 0,
curpage + 1, numpages); "Page %d of %d", curpage + 1, numpages);
centerch(curwin, 2, 0, chbuf, lines, &width);
wmove(curwin, 4, 2); wmove(curwin, 4, 2);
// Process the help text string // Process the help text string
@ -355,14 +325,10 @@ void show_help (void)
s++; s++;
} }
if (curpage == 0) { center(curwin, getmaxy(curwin) - 2, 0, attr_waitforkey, 0, 0,
centerch(curwin, getmaxy(curwin) - 2, 0, ch_contfirst, (curpage == 0) ? "[ Press <SPACE> to continue ] " :
ln_contfirst, &w_contfirst); "[ Press <SPACE> to continue or <BACKSPACE> "
} else { "for the previous page ] ");
centerch(curwin, getmaxy(curwin) - 2, 0, ch_contnext,
ln_contnext, &w_contnext);
}
wrefresh(curwin); wrefresh(curwin);
int key = gettxchar(curwin); int key = gettxchar(curwin);
@ -399,9 +365,4 @@ void show_help (void)
deltxwin(); deltxwin();
txrefresh(); txrefresh();
free(ch_title);
free(ch_contfirst);
free(ch_contnext);
free(chbuf);
} }

View File

@ -402,25 +402,12 @@ void end_screen (void)
void init_title (void) void init_title (void)
{ {
chtype *chbuf;
int width;
int lines;
bkgd(attr_root_window); bkgd(attr_root_window);
attrset(attr_root_window);
clear(); clear();
move(0, 0); mvwhline(stdscr, 0, 0, ' ' | attr_game_title, COLS);
for (int i = 0; i < COLS; i++) { center(stdscr, 0, 0, attr_game_title, 0, 0, _("Star Traders"));
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);
} }
@ -620,14 +607,7 @@ int txdlgbox (int maxlines, int ncols, int begin_y, int begin_x,
true, bkgd_attr); true, bkgd_attr);
if (usetitle) { if (usetitle) {
chtype *titlebuf = xmalloc(BUFSIZE * sizeof(chtype)); center(curwin, 1, 0, title_attr, 0, 0, boxtitle);
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);
} }
centerch(curwin, usetitle ? 3 : 2, 0, chbuf, lines, widthbuf); 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); va_start(args, format);
lines = vmkchstr(chbuf, chbufsize, attr_norm, attr_alt1, attr_alt2, lines = vmkchstr(chbuf, chbufsize, attr_norm, attr_alt1, attr_alt2,
maxlines, maxwidth, widthbuf, widthbufsize, format, maxlines, maxwidth, widthbuf, widthbufsize, format, args);
args);
va_end(args); va_end(args);
return lines; 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 // 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) void wait_for_key (WINDOW *win, int y, chtype attr)
{ {
chtype *chbuf;
int width;
int lines;
int key; int key;
bool done; bool done;
@ -2446,10 +2500,7 @@ void wait_for_key (WINDOW *win, int y, chtype attr)
meta(win, true); meta(win, true);
wtimeout(win, -1); wtimeout(win, -1);
chbuf = xmalloc(BUFSIZE * sizeof(chtype)); center(curwin, y, 0, attr, 0, 0, _("[ Press <SPACE> to continue ] "));
lines = mkchstr(chbuf, BUFSIZE, attr, 0, 0, 1, getmaxx(win) - 4,
&width, 1, _("[ Press <SPACE> to continue ] "));
centerch(win, y, 0, chbuf, lines, &width);
wrefresh(win); wrefresh(win);
done = false; done = false;
@ -2470,8 +2521,6 @@ void wait_for_key (WINDOW *win, int y, chtype attr)
done = true; done = true;
} }
} }
free(chbuf);
} }

View File

@ -411,7 +411,7 @@ extern chtype *chstrdup (const chtype *restrict chstr, int chstrsize);
chstr - chtype string as returned from mkchstr() chstr - chtype string as returned from mkchstr()
lines - Number of lines in chstr as returned from mkchstr() lines - Number of lines in chstr as returned from mkchstr()
widthbuf - Widths of each line 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 This function takes the strings in the chtype array chstr and prints
them left-aligned in the window win. Note that wrefresh() is NOT 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); 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 Function: old_attrpr - Print a string with a particular character rendition
Parameters: win - Window to use (should be curwin) Parameters: win - Window to use (should be curwin)

View File

@ -208,8 +208,6 @@ void select_moves (void)
selection_t get_move (void) selection_t get_move (void)
{ {
selection_t selection = SEL_NONE; selection_t selection = SEL_NONE;
chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype));
int lines, width;
if (quit_selected || abort_game) { if (quit_selected || abort_game) {
@ -233,31 +231,19 @@ selection_t get_move (void)
werase(curwin); werase(curwin);
box(curwin, 0, 0); box(curwin, 0, 0);
lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, left(curwin, 2, 2, attr_normal, attr_keycode, 0,
1, getmaxx(curwin) / 2 - 4, &width, 1, "^{<1>^} Display stock portfolio");
"^{<1>^} Display stock portfolio"); left(curwin, 3, 2, attr_normal, attr_keycode, 0,
leftch(curwin, 2, 2, chbuf, lines, &width); "^{<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,
"^{<CTRL><C>^} Quit the game");
lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, right(curwin, 1, getmaxx(curwin) / 2, attr_normal, attr_keycode,
1, getmaxx(curwin) / 2 - 4, &width, 1, attr_choice, "Select move "
"^{<2>^} Declare bankruptcy"); "[^[%c^]-^[%c^]/^{1^}-^{3^}/^{<CTRL><C>^}]: ",
leftch(curwin, 3, 2, chbuf, lines, &width); MOVE_TO_KEY(0), MOVE_TO_KEY(NUMBER_MOVES - 1));
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,
"^{<CTRL><C>^} 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^}/^{<CTRL><C>^}]: ",
MOVE_TO_KEY(0), MOVE_TO_KEY(NUMBER_MOVES - 1));
rightch(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width);
curs_set(CURS_ON); curs_set(CURS_ON);
wrefresh(curwin); wrefresh(curwin);
@ -270,10 +256,8 @@ selection_t get_move (void)
selection = KEY_TO_MOVE(key); selection = KEY_TO_MOVE(key);
curs_set(CURS_OFF); curs_set(CURS_OFF);
lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_choice, 0, left(curwin, 1, getmaxx(curwin) / 2, attr_normal, attr_choice,
1, getmaxx(curwin) / 2 - 4, &width, 1, 0, "Move ^{%c^}", key);
"Move ^{%c^}", key);
leftch(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width);
} else { } else {
switch (key) { switch (key) {
case '1': case '1':
@ -286,22 +270,18 @@ selection_t get_move (void)
selection = SEL_BANKRUPT; selection = SEL_BANKRUPT;
curs_set(CURS_OFF); curs_set(CURS_OFF);
lines = mkchstr(chbuf, BUFSIZE, attr_normal, left(curwin, 1, getmaxx(curwin) / 2, attr_normal,
attr_normal | A_BOLD, 0, 1, attr_normal | A_BOLD, 0,
getmaxx(curwin) / 2 - 4, &width, 1, "^{<2>^} (Declare bankruptcy)");
"^{<2>^} (Declare bankruptcy)");
leftch(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width);
break; break;
case '3': case '3':
selection = SEL_SAVE; selection = SEL_SAVE;
curs_set(CURS_OFF); curs_set(CURS_OFF);
lines = mkchstr(chbuf, BUFSIZE, attr_normal, left(curwin, 1, getmaxx(curwin) / 2, attr_normal,
attr_normal | A_BOLD, 0, 1, attr_normal | A_BOLD, 0,
getmaxx(curwin) / 2 - 4, &width, 1, "^{<3>^} (Save and end the game)");
"^{<3>^} (Save and end the game)");
leftch(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width);
break; break;
case KEY_ESC: case KEY_ESC:
@ -313,11 +293,9 @@ selection_t get_move (void)
selection = SEL_QUIT; selection = SEL_QUIT;
curs_set(CURS_OFF); curs_set(CURS_OFF);
lines = mkchstr(chbuf, BUFSIZE, attr_normal, left(curwin, 1, getmaxx(curwin) / 2, attr_normal,
attr_normal | A_BOLD, 0, 1, attr_normal | A_BOLD, 0,
getmaxx(curwin) / 2 - 4, &width, 1, "^{<CTRL><C>^} (Quit the game)");
"^{<CTRL><C>^} (Quit the game)");
leftch(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width);
break; break;
default: default:
@ -331,10 +309,8 @@ selection_t get_move (void)
mvwhline(curwin, 3, 2, ' ' | attr_normal, getmaxx(curwin) - 4); mvwhline(curwin, 3, 2, ' ' | attr_normal, getmaxx(curwin) - 4);
// Ask the player to confirm their choice // Ask the player to confirm their choice
lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, right(curwin, 2, getmaxx(curwin) / 2, attr_normal, attr_keycode, 0,
1, getmaxx(curwin) / 2 - 4, &width, 1, "Are you sure? [^{Y^}/^{N^}] ");
"Are you sure? [^{Y^}/^{N^}] ");
rightch(curwin, 2, getmaxx(curwin) / 2, chbuf, lines, &width);
wrefresh(curwin); wrefresh(curwin);
if (! answer_yesno(curwin)) { if (! answer_yesno(curwin)) {
@ -343,13 +319,15 @@ selection_t get_move (void)
// Save the game if required // Save the game if required
if (selection == SEL_SAVE) { if (selection == SEL_SAVE) {
chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype));
int width;
bool saved = false; bool saved = false;
if (game_loaded) { if (game_loaded) {
// Save the game to the same game number // Save the game to the same game number
lines = mkchstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1, mkchstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1, WIN_COLS
WIN_COLS - 7, &width, 1, - 7, &width, 1, "Saving game %d... ", game_num);
"Saving game %d... ", game_num);
newtxwin(5, width + 5, 7, WCENTER, true, attr_status_window); newtxwin(5, width + 5, 7, WCENTER, true, attr_status_window);
centerch(curwin, 2, 0, chbuf, 1, &width); centerch(curwin, 2, 0, chbuf, 1, &width);
wrefresh(curwin); wrefresh(curwin);
@ -366,13 +344,10 @@ selection_t get_move (void)
int key; int key;
bool done; bool done;
int widthbuf[2]; int widthbuf[2];
int maxwidth; int lines, maxwidth;
lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0, lines = mkchstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0,
sizeof(widthbuf) / sizeof(widthbuf[0]), 2, WIN_COLS - 7, widthbuf, 2,
WIN_COLS - 7, widthbuf,
sizeof(widthbuf) / sizeof(widthbuf[0]),
"Enter game number [^{1^}-^{9^}] " "Enter game number [^{1^}-^{9^}] "
"or ^{<CTRL><C>^} to cancel: "); "or ^{<CTRL><C>^} to cancel: ");
assert(lines == 1 || lines == 2); assert(lines == 1 || lines == 2);
@ -417,9 +392,9 @@ selection_t get_move (void)
// Try to save the game, if possible // Try to save the game, if possible
game_num = key - '0'; game_num = key - '0';
lines = mkchstr(chbuf, BUFSIZE, attr_status_window, mkchstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1,
0, 0, 1, WIN_COLS - 7, &width, 1, WIN_COLS - 7, &width, 1,
"Saving game %d... ", game_num); "Saving game %d... ", game_num);
newtxwin(5, width + 5, 7, WCENTER, true, attr_status_window); newtxwin(5, width + 5, 7, WCENTER, true, attr_status_window);
centerch(curwin, 2, 0, chbuf, 1, &width); centerch(curwin, 2, 0, chbuf, 1, &width);
wrefresh(curwin); wrefresh(curwin);
@ -443,10 +418,11 @@ selection_t get_move (void)
selection = SEL_NONE; selection = SEL_NONE;
} }
free(chbuf);
} }
} }
free(chbuf);
return selection; return selection;
} }