From 31e58f1156f4af6c084f6f9f21b33a0724bf307a Mon Sep 17 00:00:00 2001 From: spherallic Date: Sat, 9 Mar 2024 18:08:02 +0100 Subject: [PATCH] Rewrite aligned string drawing, using a modified version of strtok --- src/doomtype.h | 1 + src/string.c | 24 ++++++++++++++++++++++++ src/v_video.c | 37 +++++-------------------------------- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/doomtype.h b/src/doomtype.h index 4070e346a..fa8616793 100644 --- a/src/doomtype.h +++ b/src/doomtype.h @@ -108,6 +108,7 @@ char *nongnu_strcasestr(const char *in, const char *what); int startswith (const char *base, const char *tag); int endswith (const char *base, const char *tag); +char *xstrtok(char *line, const char *delims); #if defined (_WIN32) || defined (__HAIKU__) #define HAVE_DOSSTR_FUNCS diff --git a/src/string.c b/src/string.c index 2f16fa4c6..b24e12e6e 100644 --- a/src/string.c +++ b/src/string.c @@ -68,3 +68,27 @@ int endswith(const char *base, const char *tag) return !memcmp(&base[base_length - tag_length], tag, tag_length); } + +// strtok version that only skips over one delimiter at a time +char *xstrtok(char *line, const char *delims) +{ + static char *saveline = NULL; + char *p; + + if(line != NULL) + saveline = line; + + // see if we have reached the end of the line + if(saveline == NULL || *saveline == '\0') + return NULL; + + p = saveline; // save start of this token + + saveline += strcspn(saveline, delims); // get the number of non-delims characters, go past delimiter + + if(*saveline != '\0') // trash the delim if necessary + *saveline++ = '\0'; + + return p; +} + diff --git a/src/v_video.c b/src/v_video.c index 6893ac30d..7662c06df 100644 --- a/src/v_video.c +++ b/src/v_video.c @@ -2112,41 +2112,12 @@ void V_DrawFontStringAtFixed(fixed_t x, fixed_t y, INT32 option, fixed_t pscale, void V_DrawAlignedFontStringAtFixed(fixed_t x, fixed_t y, INT32 option, fixed_t pscale, fixed_t vscale, const char *string, fontdef_t font, enum string_align align) { - char line[MAXLINELEN]; - size_t i, start = 0; + char *text = strdup(string); + char* line = xstrtok(text, "\n"); fixed_t lx = x, ly = y; - while (*(string+start)) + while (line) { - for (i = 0; i < strlen(string+start); i++) - { - if (*(string+start+i) == '\n') - { - memset(line, 0, MAXLINELEN); - if (i >= MAXLINELEN) - { - CONS_Printf("V_DrawAlignedFontStringAtFixed: a line exceeds max. length %d (string: %s)\n", MAXLINELEN, string); - return; - } - strncpy(line,string + start, i); - line[i] = '\0'; - start += i + 1; - i = (size_t)-1; //added : 07-02-98 : damned! - break; - } - } - - if (i == strlen(string + start)) - { - if (i >= MAXLINELEN) - { - CONS_Printf("V_DrawAlignedFontStringAtFixed: a line exceeds max. length %d (string: %s)\n", MAXLINELEN, string); - return; - } - strcpy(line, string + start); - start += i; - } - switch(align) { case alignleft: @@ -2163,6 +2134,8 @@ void V_DrawAlignedFontStringAtFixed(fixed_t x, fixed_t y, INT32 option, fixed_t V_DrawFontStringAtFixed(lx, ly, option, pscale, vscale, line, font); ly += FixedMul(((option & V_RETURN8) ? 8 : font.linespacing)<