Skip to content

Commit

Permalink
Reduce stack usage by Con_CenterPrintf
Browse files Browse the repository at this point in the history
Signed-off-by: bones_was_here <bones_was_here@xonotic.au>
  • Loading branch information
uis246 authored and bones-was-here committed Jul 22, 2024
1 parent fd4fe9b commit b2d0815
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -1570,24 +1569,26 @@ 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)
{
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
}
}

Expand Down

0 comments on commit b2d0815

Please sign in to comment.