From d5aab98ac9ef0d5387cecc2e5d94383843b49c53 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 24 Nov 2019 14:08:27 +0100 Subject: [PATCH] Review PR #1015 Just simplified code a bit --- src/text.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/text.c b/src/text.c index 2fff08d8..d8756b9e 100644 --- a/src/text.c +++ b/src/text.c @@ -1102,24 +1102,26 @@ unsigned int TextLength(const char *text) } // Formatting of text with variables to 'embed' -// WARNING: the string returned will expire after this function is called MAX_TEXT_BUFFERS times +// WARNING: String returned will expire after this function is called MAX_TEXTFORMAT_BUFFERS times const char *TextFormat(const char *text, ...) { - #define MAX_TEXT_BUFFERS 8 - // We create an array of buffers so strings don't expire until MAX_TEXT_BUFFERS invocations - static char cache[MAX_TEXT_BUFFERS][MAX_TEXT_BUFFER_LENGTH] = { 0 }; + #define MAX_TEXTFORMAT_BUFFERS 4 + + // We create an array of buffers so strings don't expire until MAX_TEXTFORMAT_BUFFERS invocations + static char buffers[MAX_TEXTFORMAT_BUFFERS][MAX_TEXT_BUFFER_LENGTH] = { 0 }; static int index = 0; - char *buffer = cache[index]; - index += 1; - index %= MAX_TEXT_BUFFERS; + + char *currentBuffer = buffers[index]; va_list args; va_start(args, text); - vsprintf(buffer, text, args); + vsprintf(currentBuffer, text, args); va_end(args); + + index += 1; // Move to next buffer for next function call + if (index >= MAX_TEXTFORMAT_BUFFERS) index = 0; - return buffer; - #undef MAX_TEXT_BUFFERS + return currentBuffer; } // Get a piece of a text string