code cosmetics
[netris.git] / curses.c
index 0e841ef2066bf2e9e4079cdce1adde389aae8df3..b1604b0b51384a272a99fae1c64867d92f14a1ec 100644 (file)
--- a/curses.c
+++ b/curses.c
@@ -403,8 +403,7 @@ static void block_draw_2(int y, int x, unsigned char type)
        else {
 #ifdef HAVE_NCURSES
                if (Sets.standout) {
-                       if (haveColor) attrset(COLOR_PAIR(type & 15));
-                       else attrset(A_REVERSE);
+                       attrset(haveColor ? COLOR_PAIR(type & 15) : A_REVERSE);
                }
 #endif
                switch (Sets.drawstyle) {
@@ -471,13 +470,11 @@ static void block_draw_1(int y, int x, unsigned char type)
        if (type == BT_none) addch(' ');
        else if (type == BT_shadow) addch(':');
        else {
-               if (Sets.standout) {
 #ifdef HAVE_NCURSES
-                       if (haveColor)
-                               attrset(COLOR_PAIR(type & 15));
-                       else attrset(A_REVERSE);
-#endif
+               if (Sets.standout) {
+                       attrset(haveColor ? COLOR_PAIR(type & 15) : A_REVERSE);
                }
+#endif
                if ((type & 192) == 64)
                        addch('[');
                else if ((type & 192) == 128)
@@ -536,11 +533,13 @@ void window_msg(int player, char *message)
 { //put a message over player's field
        if (!window[player].shown) return;
        if (message) {
-               char s[MAX_BOARD_WIDTH+1];
-               memset(s, ' ', MAX_BOARD_WIDTH);
-               memcpy(&s[(window[player].size * Players[player].boardWidth / 2) - (strlen(message) / 2)],
-                       message, strlen(message));
-               s[window[player].size * Players[player].boardWidth] = 0;
+               const int fieldsize = Players[player].boardWidth * window[player].size;
+               const int centered = (fieldsize - strlen(message)) / 2;
+               char s[fieldsize + 1];
+
+               memset(s, ' ', fieldsize);
+               memcpy(&s[centered], message, strlen(message));
+               s[fieldsize] = 0;
 #ifdef HAVE_NCURSES
                attrset(A_REVERSE);
 #else
@@ -558,31 +557,63 @@ void window_msg(int player, char *message)
        } //restore field
 }
 
+void window_msg_wide(int player, char *message)
+{
+       int i;
+       char *messagewide = malloc(strlen(message) * 2); // max += strlen - 1
+       const int fieldsize = Players[player].boardWidth * window[player].size;
+
+       const bool sep = strchr(message, ' ') != NULL;
+               // whitespace to pad at convenience
+       const bool pad = strlen(message) * 2 - sep <= fieldsize;
+               // (space to) put whitespace between all characters
+       bool odd = fieldsize & 1;
+               // odd number of characters (center off; try to change padding at sep)
+       if (!pad) odd ^= strlen(message) & 1;
+               // for odd strings, check for even fieldsize instead
+
+       if (pad || (sep && odd && strlen(message) < fieldsize)) {
+               // generate padded message in messagewide
+               for (i = 0; ; message++) {
+                       messagewide[i++] = *message;
+                       if (message[1] == 0) {
+                               messagewide[i] = 0;
+                               break;
+                       }
+                       if (pad ? (*message != ' ' || odd) : (*message == ' ' && odd)) {
+                               // add padding if wide; different padding at space if odd
+                               messagewide[i++] = ' ';
+                               odd = 0;
+                       }
+               }
+               message = messagewide;
+       }
+       window_msg(player, message);
+}
+
 void window_msg_status(int player)
 { //put status (pause, readiness, game over) over player's field
        if (Players[player].alive > 0)
                if (Players[player].flags & SCF_paused)
                        if (Game.started > 1)
-                               window_msg(player, window[player].size > 1 ? "P A U S E D" : "PAUSED");
+                               window_msg_wide(player, MSG_PLAYER_PAUSE);
                        else
-                               window_msg(player,
-                                       window[player].size > 1 ? "N O T  R E A D Y" : "NOT  READY");
+                               window_msg_wide(player, MSG_PLAYER_JOIN);
                else
                        if (Game.started > 1)
                                window_msg(player, NULL);
                        else
-                               window_msg(player, window[player].size > 1 ? "R E A D Y" : "READY");
+                               window_msg_wide(player, MSG_PLAYER_START);
        else if (!Players[player].alive)
-               window_msg(player,
-                       window[player].size > 1 ? "G A M E  O V E R" : "GAME  OVER");
+               window_msg_wide(player, MSG_PLAYER_STOP);
        else
-               window_msg(player, window[player].size > 1 ? "E M P T Y" : "EMPTY");
+               window_msg_wide(player, MSG_PLAYER_PART);
 }
 
 
 void status_tick(void)
 { //display timer
-       mvprintw(statusYPos, statusXPos, "timer %7.0f ", CurTimeval() / 1e6);
+       mvprintw(statusYPos, statusXPos, MSG_TIME, CurTimeval() / 1e6);
 }
 
 void ScheduleFullRedraw(void)