From: uis Date: Tue, 25 Jun 2024 20:44:53 +0000 (+0300) Subject: Reduce stack usage by Con_CenterPrintf X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=b2d0815fef1d28cf3956f4f7d92670d7cbf8f3a3;p=xonotic%2Fdarkplaces.git Reduce stack usage by Con_CenterPrintf Signed-off-by: bones_was_here --- diff --git a/console.c b/console.c index 96ea7af7..c1c3f2f1 100644 --- a/console.c +++ b/console.c @@ -1547,7 +1547,6 @@ void Con_CenterPrintf(int maxLineLength, const char *fmt, ...) { va_list argptr; char msg[MAX_INPUTLINE]; // the original message - char line[MAX_INPUTLINE]; // one line from the message char spaces[21]; // buffer for spaces char *msgCursor, *lineEnding; int lineLength, msgLength; @@ -1570,13 +1569,13 @@ void Con_CenterPrintf(int maxLineLength, const char *fmt, ...) lineEnding = strchr(msgCursor, '\n'); if (lineEnding) { - lineLength = dp_ustr2stp(line, sizeof(line), msgCursor, lineEnding - msgCursor) - line; - msgCursor = lineEnding + 1; + lineLength = lineEnding - msgCursor; // print just the line + lineEnding++; // set cursor to next character after new line } else // last line { - lineLength = dp_strlcpy(line, msgCursor, sizeof(line)); - msgCursor = msg + msgLength; + lineLength = msgLength; // print entire message + lineEnding = msgCursor + lineLength; // set next line cursor to terminator } if (lineLength < maxLineLength) @@ -1584,10 +1583,12 @@ void Con_CenterPrintf(int maxLineLength, const char *fmt, ...) indentSize = min(sizeof(spaces) - 1, (size_t)(maxLineLength - lineLength) / 2); memset(spaces, ' ', indentSize); spaces[indentSize] = 0; - Con_MaskPrintf(CON_MASK_HIDENOTIFY, "%s%s\n", spaces, line); + Con_MaskPrintf(CON_MASK_HIDENOTIFY, "%s%.*s\n", spaces, lineLength, msgCursor); } else - Con_MaskPrintf(CON_MASK_HIDENOTIFY, "%s\n", line); + Con_MaskPrintf(CON_MASK_HIDENOTIFY, "%.*s\n", lineLength, msgCursor); + msgLength -= lineEnding - msgCursor; // Consume all characters until end of line + msgCursor = lineEnding; // Update message cursor } }