X-Git-Url: http://git.shiar.net/netris.git/blobdiff_plain/e3d58186949bfcdb149cc2a545ce6c14a8689268..4f561019fc85c2817e3a72341397d1df32bc0868:/curses.c diff --git a/curses.c b/curses.c index 4f8ddbd..030ed25 100644 --- a/curses.c +++ b/curses.c @@ -30,32 +30,15 @@ # define HAVE_NCURSES #endif -#ifdef HAVE_NCURSES -static struct -{ - BlockType type; - short color; -} myColorTable[] = -{ - { BT_white, COLOR_WHITE }, - { BT_blue, COLOR_BLUE }, - { BT_magenta, COLOR_MAGENTA }, - { BT_cyan, COLOR_CYAN }, - { BT_yellow, COLOR_YELLOW }, - { BT_green, COLOR_GREEN }, - { BT_red, COLOR_RED }, - { BT_none, 0 } -}; -#endif - ExtFunc void PlotBlock1(int scr, int y, int x, BlockType type); -static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event); +static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event); static EventGenRec keyGen = { NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key }; static int boardYPos[MAX_SCREENS], boardXPos[MAX_SCREENS]; static int statusYPos, statusXPos; +static int messageYPos, messageXPos, messageHeight, messageWidth; static int haveColor; static char *term_vi; /* String to make cursor invisible */ @@ -72,7 +55,7 @@ ExtFunc void InitScreens(void) * Ctrl-C during initialization might leave the terminal in a bad state. */ BlockSignals(&oldMask, SIGINT, 0); - initscr(); + initscr(); //start curses #ifdef CURSES_HACK { @@ -85,46 +68,50 @@ ExtFunc void InitScreens(void) #ifdef HAVE_NCURSES haveColor = Game.color && has_colors(); if (haveColor) { + static struct { + BlockType type; + short color; + } myColorTable[] = { + { BT_white, COLOR_WHITE }, + { BT_blue, COLOR_BLUE }, + { BT_magenta, COLOR_MAGENTA }, + { BT_cyan, COLOR_CYAN }, + { BT_yellow, COLOR_YELLOW }, + { BT_green, COLOR_GREEN }, + { BT_red, COLOR_RED }, + { BT_none, 0 } + }; //myColorTable int i = 0; start_color(); + if (can_change_color()) { + init_color (COLOR_YELLOW, 1000, 1000, 0); + } //I've never worked on a color-changable terminal, so no idea.. for (i = 0; myColorTable[i].type != BT_none; ++i) init_pair(myColorTable[i].type, COLOR_BLACK, myColorTable[i].color); - } + } //haveColor #else haveColor = 0; #endif - AtExit(CleanupScreens); + AtExit(CleanupScreens); //restore everything when done RestoreSignals(NULL, &oldMask); - cbreak(); + cbreak(); //no line buffering noecho(); +// keypad(stdscr, TRUE); //get arrow/functionkeys 'n stuff OutputTermStr(term_vi, 0); - AddEventGen(&keyGen); - - move(0, 1); - addstr("NETRIS "); - addstr(version_string); - { - int rows, cols; - - getmaxyx(stdscr, rows, cols); - move(0, cols - 48); - addstr("(C)1994-1996,1999 Mark H. Weaver, (C)2002 Shiar"); -// addstr(" \"netris -h\" for more info"); - } - statusYPos = 22; - statusXPos = 0; -} + AddEventGen(&keyGen); //key handler + standend(); //normal text +} //InitScreens ExtFunc void CleanupScreens(void) { RemoveEventGen(&keyGen); - endwin(); + endwin(); //end curses OutputTermStr(term_ve, 1); -} +} //CleanupScreens ExtFunc void GetTermcapInfo(void) { @@ -180,217 +167,301 @@ ExtFunc void GetTermcapInfo(void) } if (!term_vi || !term_ve) term_vi = term_ve = NULL; -} +} //GetTermcapInfo ExtFunc void OutputTermStr(char *str, int flush) { if (str) { fputs(str, stdout); - if (flush) - fflush(stdout); + if (flush) fflush(stdout); } -} +} //OutputTermStr -ExtFunc void DrawField(int scr) +ExtFunc void DrawTitle(void) { - { - int y, x; - - for (y = Players[scr].boardVisible - 1; y >= 0; --y) { - mvaddch(boardYPos[scr] - y, boardXPos[scr] - 1, - Game.ascii ? '|' : ACS_VLINE); //left - mvaddch(boardYPos[scr] - y, - boardXPos[scr] + 2 * Players[scr].boardWidth, - Game.ascii ? '|' : ACS_VLINE); //right - } - move(2, boardXPos[scr] - 1); //top - addch(Game.ascii ? '+' : ACS_ULCORNER); - for (x = Players[scr].boardWidth * 2 - 1; x >= 0; --x) - addch(Game.ascii ? '-' : ACS_HLINE); - addch(Game.ascii ? '+' : ACS_URCORNER); - move(boardYPos[scr] + 1, boardXPos[scr] - 1); //bottom - addch(Game.ascii ? '+' : ACS_LLCORNER); - for (x = Players[scr].boardWidth * 2 - 1; x >= 0; --x) - addch(Game.ascii ? '-' : ACS_HLINE); - addch(Game.ascii ? '+' : ACS_LRCORNER); - } //draw field grid + int rows, cols; + char s[255]; - { - char userstr[300]; +#ifdef HAVE_NCURSES + attrset(A_REVERSE); +#else + standout(); +#endif + getmaxyx(stdscr, rows, cols); + sprintf(s, " NETRIS %s", version_string); + memset(&s[strlen(s)], ' ', 254 - strlen(s)); + if (cols > 56 + strlen(version_string)) + memcpy(&s[cols - 48], + "(C)1994-1996,1999 Mark H. Weaver, (C)2002 Shiar \0", 49); + else memcpy(&s[cols], "\0", 1); + mvaddstr(0, 0, s); + standend(); //normal text +} //DrawTitle + +ExtFunc void DrawBox(int x1, int y1, int x2, int y2) +{ //draw grid + int y, x; + + for (y = y1 + 1; y < y2; y++) { + mvaddch(y, x1, Game.ascii ? '|' : ACS_VLINE); //left + mvaddch(y, x2, Game.ascii ? '|' : ACS_VLINE); //right + } + move(y1, x1); //top + addch(Game.ascii ? '+' : ACS_ULCORNER); + for (x = x1 + 1; x < x2; x++) + addch(Game.ascii ? '-' : ACS_HLINE); + addch(Game.ascii ? '+' : ACS_URCORNER); + move(y2, x1); //bottom + addch(Game.ascii ? '+' : ACS_LLCORNER); + for (x = x1 + 1; x < x2; x++) + addch(Game.ascii ? '-' : ACS_HLINE); + addch(Game.ascii ? '+' : ACS_LRCORNER); +} //DrawBox - sprintf(userstr, "%s <%s>", Players[scr].name, Players[scr].host); - userstr[20 - 7*((Players[scr].flags & SCF_usingRobot) != 0) +ExtFunc void DrawField(int scr) +{ //draw field for player scr + if (!Players[scr].spy) return; + DrawBox(boardXPos[scr] - 1, boardYPos[scr] - Players[scr].boardVisible, + boardXPos[scr] + 2 * Players[scr].boardWidth, boardYPos[scr] + 1); + { + char s[2*Players[scr].boardWidth]; + + memset(s, ' ', sizeof(s)); + if (Players[scr].host && Players[scr].host[0]) + snprintf(s, sizeof(s), "%s <%s>", + Players[scr].name, Players[scr].host); + else snprintf(s, sizeof(s), "%s", Players[scr].name); + s[strlen(s)] = ' '; + s[sizeof(s) - 7*((Players[scr].flags & SCF_usingRobot) != 0) - 5*((Players[scr].flags & SCF_fairRobot) != 0)] = 0; - mvaddstr(1, boardXPos[scr], userstr); + mvaddstr(1, boardXPos[scr], s); if (Players[scr].flags & SCF_usingRobot) { addstr((Players[scr].flags & SCF_fairRobot) ? "(fair robot)" : "(robot)"); - } + } //add robot indicator } //display playername/host -} //DrawScreen -ExtFunc void InitFields() -{ + // draw blocks (which is usually just clear field) + + ShowPause(scr); +} //DrawField + +ExtFunc void InitFields(void) +{ //calculate positions of all fields int scr, prevscr; + int y, x; + getmaxyx(stdscr, y, x); boardXPos[me] = 1; boardYPos[me] = 22; + statusXPos = 2 * Players[me].boardWidth + 3; + statusYPos = 22; + messageXPos = 2; + messageYPos = 25; + messageWidth = x - messageXPos - 2; + if ((messageHeight = y - messageYPos - 1) < 0) messageHeight = 0; + else DrawBox(messageXPos - 2, messageYPos - 1, + messageXPos + messageWidth + 1, messageYPos + messageHeight); prevscr = me; - for (scr = 1; scr <= totalPlayers + 1; scr++) if (scr != me) { + for (scr = 1; scr < MAX_SCREENS; scr++) if (scr != me) { boardXPos[scr] = boardXPos[prevscr] + 2 * Players[prevscr].boardWidth + 3; if (prevscr == me) - boardXPos[scr] += 24; //scorebar + boardXPos[scr] += 14; //scorebar boardYPos[scr] = 22; + if (x < boardXPos[scr] + 2 * Players[scr].boardWidth + 1) + Players[scr].spy = 0; //field doesn't fit on screen prevscr = scr; } - statusXPos = 2 * Players[me].boardWidth + 3; -} //InitScreen + for (scr = 1; scr <= Game.maxplayers; scr++) + DrawField(scr); +} //InitFields ExtFunc void CleanupScreen(int scr) { } +ExtFunc void Messagef(char *fmt, ...) +{ //print game/bot message + static int line = 0; + va_list args; + char s[255]; + char *p, *psearch; + int i; + + if (!messageHeight) return; + va_start(args, fmt); + move(messageYPos + line, messageXPos); +// vwprintw(stdscr, fmt, args); //doesn't seem to be a vprintw + vsprintf(s, fmt, args); + p = s; + while (psearch = strchr(s, '\\')) { + *psearch = '\0'; + addstr(p); + if (haveColor) + attrset(A_REVERSE | COLOR_PAIR(atoi(psearch + 1) + 1)); + p = psearch + 2; + } //search for color escapes (\) + addstr(p); + if (messageHeight > 1) { + char s[messageWidth + 1]; + line = (line + 1) % messageHeight; + memset(s, ' ', messageWidth); + s[messageWidth] = 0; + mvaddstr(messageYPos + line, messageXPos, s); + } //multiple lines + if (haveColor) standend(); + va_end(args); +} //Message + ExtFunc void PlotBlock1(int scr, int y, int x, BlockType type) { int colorIndex = abs(type); move(y, x); - - if (type == BT_none) - addstr(" "); - else - { - if (Game.standout) - { + if (type == BT_none) addstr(" "); + else if (type == BT_shadow) addstr("::"); + else { + if (Game.standout) { #ifdef HAVE_NCURSES - if (haveColor) - attrset(COLOR_PAIR(colorIndex)); - else + if (haveColor) attrset(COLOR_PAIR(colorIndex)); + else attrset(A_REVERSE); #endif - standout(); } - addstr(type ? "[]" : "$$"); - standend(); - } -} - +#ifdef HAVE_NCURSES + if (Game.standout) standend(); +#endif + } //display one brick +} //PlotBlock1 ExtFunc void PlotBlock(int scr, int y, int x, BlockType type) { if (y >= 0 && y < Players[scr].boardVisible && x >= 0 && x < Players[scr].boardWidth) PlotBlock1(scr, boardYPos[scr] - y, boardXPos[scr] + 2 * x, type); -} +} //PlotBlock -ExtFunc void PlotUnderline(int scr, int x, int flag) +ExtFunc void PlotShadowBlock1(int scr, int y, int x, BlockType type) +{ + move(y, x); + if (type == BT_none) addstr(" "); + else addstr("::"); +} //PlotShadowBlock1 +ExtFunc void PlotShadowBlock(int scr, int y, int x, BlockType type) +{ + if (y >= 0 && y < Players[scr].boardVisible && + x >= 0 && x < Players[scr].boardWidth) + PlotShadowBlock1(scr, boardYPos[scr] - y, boardXPos[scr] + 2 * x, type); +} //PlotShadowBlock + +ExtFunc void PlotBlockS1(int scr, int y, int x, BlockType type) +{ //DOESN"T WORK YET... + move(y, x); + if (type == BT_none) addstr(" "); + else { + addstr(type ? "O" : "$"); + standend(); + } //display one brick +} //PlotBlock1 +ExtFunc void PlotBlockS(int scr, int y, int x, BlockType type) { + if (y >= 0 && y < Players[scr].boardVisible && + x >= 0 && x < Players[scr].boardWidth) + PlotBlockS1(scr, boardYPos[scr] - y, boardXPos[scr] + x, type); +} //PlotBlock + +ExtFunc void PlotUnderline(int scr, int x, int flag) +{ //display piece of bottom fieldgrid move(boardYPos[scr] + 1, boardXPos[scr] + 2 * x); if (Game.ascii) addstr(flag ? "==" : "--"); else { addch(flag ? ACS_BTEE : ACS_HLINE); addch(flag ? ACS_BTEE : ACS_HLINE); - } -} - -ExtFunc void ShowDisplayInfo(void) -{ - move(statusYPos - 9, statusXPos); - printw("Seed: %010d", Game.seed); - // move(statusYPos - 8, statusXPos); - // printw("Speed: %dms ", speed / 1000); - if (robotEnable) { - move(statusYPos - 6, statusXPos); - if (fairRobot) - addstr("Controlled by a fair robot"); - else - addstr("Controlled by a robot"); - // clrtoeol(); - } - if (Players[1].flags & SCF_usingRobot) { - move(statusYPos - 5, statusXPos); - if (Players[1].flags & SCF_fairRobot) - addstr("The opponent is a fair robot"); - else - addstr("The opponent is a robot"); - // clrtoeol(); - } -} + } //ncurses graphics +} //PlotUnderline ExtFunc void ShowScore(int scr, struct _Score score) -{ +{ //show score stuff float timer; - move(6, statusXPos); addstr("Next: "); - move(7, statusXPos + 7); addstr(" "); + mvaddstr(13, statusXPos, "next "); + mvaddstr(14, statusXPos + 5, " "); ShapeIterate(Players[scr].nextShape, scr, - ShapeToNetNum(Players[scr].nextShape) == 15 ? 13 : 14, - statusXPos / 2 + 5, 1, GlanceFunc, NULL); - move(statusYPos - 21 + 1, statusXPos); - printw("Score:%6d level: %2d", score.score, score.level); - move(statusYPos - 20 + 1, statusXPos); + ShapeToNetNum(Players[scr].nextShape) == 15 ? 6 : 7, + statusXPos / 2 + 4, 1, GlanceFunc, NULL); + mvprintw(3, statusXPos, "level %5d", score.level); + mvprintw(5, statusXPos, "score%8d", score.score); + mvprintw(6, statusXPos, "lines%8d", score.lines); timer = CurTimeval() / 1e6; - printw("Lines:%6d", score.lines); if (timer > 4) { - printw(" ppm:%5.1f", score.drops * 60 / timer); - move(statusYPos - 18, statusXPos); - if (score.lines > 0) - printw("yield: %3d%%", 100 * score.adds / score.lines); - else addstr(" "); - printw(" apm:%5.1f", score.adds * 60 / timer); + mvprintw(9, statusXPos, "ppm %9.1f", score.drops * 60 / timer); + if (score.lines > 0) { + mvprintw(7, statusXPos, + "yield %3d%%", 100 * score.adds / score.lines); + mvprintw(10, statusXPos, "apm %9.1f", score.adds * 60 / timer); + } } -} - -ExtFunc void ShowPause(int pausedByMe, int pausedByThem) -{ - move(statusYPos - 3, statusXPos); - if (pausedByThem) - addstr("Game paused by opponent"); - else - addstr(" "); - move(statusYPos - 2, statusXPos); - if (pausedByMe) - addstr("Game paused by you"); - else - addstr(" "); -} - -ExtFunc void Message(char *s) -{ - static int line = 0; +} //ShowScore + +ExtFunc void FieldMessage(int playa, char *message) +{ //put a message over playa's field + if (!Players[playa].spy) return; + if (message) { + char s[MAX_BOARD_WIDTH+1]; + memset(s, ' ', MAX_BOARD_WIDTH); + memcpy(&s[Players[playa].boardWidth - strlen(message) / 2], + message, strlen(message)); + s[Players[playa].boardWidth * 2] = 0; +#ifdef HAVE_NCURSES + attrset(A_REVERSE); +#else + standout(); +#endif + mvprintw(boardYPos[playa] - Players[playa].boardVisible / 2, + boardXPos[playa], "%s", s); + standend(); + } //display + else { + int x, y; + y = Players[playa].boardVisible / 2; + for (x = 0; x <= Players[playa].boardWidth; x++) + PlotBlock(playa, y, x, GetBlock(playa, y, x)); + } //restore field +} //FieldMessage + +ExtFunc void ShowPause(int playa) +{ //put paused over player's field + if (Players[playa].alive) + if (Players[playa].flags & SCF_paused) + FieldMessage(playa, Game.started > 1 + ? "P A U S E D" : "N O T R E A D Y"); + else FieldMessage(playa, Game.started > 1 ? NULL : "R E A D Y"); + else FieldMessage(playa, playa > maxPlayer + ? "E M P T Y" : "G A M E O V E R"); +} //ShowPause -// move(statusYPos - 20 + line, statusXPos); - move(statusYPos + 2 + line, 1); - addstr(s); /* XXX Should truncate long lines */ - clrtoeol(); - line = (line + 1) % 10; -// move(statusYPos - 20 + line, statusXPos); - move(statusYPos + 2 + line, 1); - clrtoeol(); -} ExtFunc void ShowTime(void) -{ - move(statusYPos, statusXPos); - printw("Timer: %.0f ", CurTimeval() / 1e6); - move(boardYPos[0] + 1, boardXPos[0] + 2 * Players[0].boardWidth + 1); +{ //display timer + mvprintw(statusYPos, statusXPos, "timer %7.0f ", CurTimeval() / 1e6); +// move(boardYPos[0] + 1, boardXPos[0] + 2 * Players[0].boardWidth + 1); // refresh(); -} +} //ShowTime ExtFunc void ScheduleFullRedraw(void) { touchwin(stdscr); -} +} //ScheduleFullRedraw static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event) -{ +{ //read keypresses if (MyRead(gen->fd, &event->u.key, 1)) return E_key; else return E_none; -} +} //KeyGenFunc /* * vi: ts=4 ai