From ef46a97a671490b02552c64ca88f3f64bf42f3aa Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 1 May 2021 16:00:35 +0100 Subject: [PATCH] Minor text rendering code tweaks. --- src/structs.h | 1 - src/system/text.c | 32 +++++++++++++------------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/structs.h b/src/structs.h index f950b1c..43810d9 100644 --- a/src/structs.h +++ b/src/structs.h @@ -43,7 +43,6 @@ typedef struct Tuple Tuple; typedef struct Credit Credit; typedef struct AtlasImage AtlasImage; typedef struct Font Font; -typedef struct Glyph Glyph; typedef struct { int debug; diff --git a/src/system/text.c b/src/system/text.c index 1bee8ac..bd12f5c 100644 --- a/src/system/text.c +++ b/src/system/text.c @@ -78,9 +78,7 @@ static void initFont(char *name, char *filename, char *characters) i = 0; - n = nextGlyph(characters, &i, glyphBuffer); - - while (n) + while ((n = nextGlyph(characters, &i, glyphBuffer)) != 0) { largest = MAX(largest, n); @@ -108,8 +106,6 @@ static void initFont(char *name, char *filename, char *characters) SDL_FreeSurface(text); dest.x += dest.w; - - n = nextGlyph(characters, &i, glyphBuffer); } TTF_CloseFont(font); @@ -242,9 +238,7 @@ static void drawWord(char *word, int *x, int *y, int startX) i = 0; - n = nextGlyph(word, &i, NULL); - - while (n) + while ((n = nextGlyph(word, &i, NULL)) != 0) { dest.x = *x; dest.y = *y; @@ -254,8 +248,6 @@ static void drawWord(char *word, int *x, int *y, int startX) SDL_RenderCopy(app.renderer, activeFont->texture, &activeFont->glyphs[n], &dest); *x += activeFont->glyphs[n].w * scale; - - n = nextGlyph(word, &i, NULL); } } @@ -285,14 +277,10 @@ void calcTextDimensions(const char *text, int size, int *w, int *h) i = 0; - n = nextGlyph(text, &i, NULL); - - while (n) + while ((n = nextGlyph(text, &i, NULL)) != 0) { *w += activeFont->glyphs[n].w * scale; *h = MAX(activeFont->glyphs[n].h * scale, *h); - - n = nextGlyph(text, &i, NULL); } } @@ -337,10 +325,11 @@ int getWrappedTextHeight(char *text, int size) static int nextGlyph(const char *str, int *i, char *glyphBuffer) { - int n, len; + int len; unsigned bit; + const char *p; - bit = (unsigned char)str[*i]; + bit = (unsigned char) str[*i]; if (bit < ' ') { @@ -377,11 +366,16 @@ static int nextGlyph(const char *str, int *i, char *glyphBuffer) /* only fill the buffer if it's been supplied */ if (glyphBuffer != NULL) { + p = str + *i; + memset(glyphBuffer, 0, MAX_GLYPH_SIZE); - for (n = 0 ; n < len ; n++) + memcpy(glyphBuffer, p, len); + + if (bit >= MAX_GLYPHS) { - glyphBuffer[n] = str[*i + n]; + printf("Glyph '%s' index exceeds array size (%d >= %d)\n", glyphBuffer, bit, MAX_GLYPHS); + exit(1); } }