From 773e0a178599b53bf0737366ebcb626ae71a412d Mon Sep 17 00:00:00 2001 From: divverent Date: Mon, 17 Dec 2007 08:21:08 +0000 Subject: [PATCH] split up DrawQ_String_Font_UntilX in DrawQ_TextWidth_Font_TrackColors and DrawQ_String_Font; the latter no longer needs to check if the text is too long, and the former no longer needs alpha checks (to be removed later perhaps) git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7813 d7cf8633-e32d-0410-b094-e92efae38249 --- cl_screen.c | 8 ++--- console.c | 8 ++--- draw.h | 5 ++-- gl_draw.c | 84 +++++++++++++++++++++++++++++++++++++---------------- prvm_cmds.c | 2 +- sbar.c | 6 ++-- 6 files changed, 74 insertions(+), 39 deletions(-) diff --git a/cl_screen.c b/cl_screen.c index bf23f29f..34cf2d3a 100644 --- a/cl_screen.c +++ b/cl_screen.c @@ -141,7 +141,7 @@ void SCR_DrawCenterString (void) // scan the number of characters on the line, not counting color codes char *newline = strchr(start, '\n'); int l = newline ? (newline - start) : (int)strlen(start); - float width = DrawQ_TextWidth_Font(start, l, 8, 8, false, FONT_CENTERPRINT); + float width = DrawQ_TextWidth_Font(start, l, false, FONT_CENTERPRINT) * 8; x = (vid_conwidth.integer - width)/2; if (l > 0) @@ -463,7 +463,7 @@ static int SCR_DrawQWDownload(int offset) else dpsnprintf(temp, sizeof(temp), "Downloading %s %3i%% (%i/%i) at %i bytes/s\n", cls.qw_downloadname, cls.qw_downloadpercent, cls.qw_downloadmemorycursize, cls.qw_downloadmemorymaxsize, cls.qw_downloadspeedrate); len = (int)strlen(temp); - x = (vid_conwidth.integer - DrawQ_TextWidth_Font(temp, len, size, size, 0, FONT_INFOBAR)) / 2; + x = (vid_conwidth.integer - DrawQ_TextWidth_Font(temp, len, 0, FONT_INFOBAR) * size) / 2; y = vid_conheight.integer - size - offset; DrawQ_Fill(0, y, vid_conwidth.integer, size, 0, 0, 0, cls.signon == SIGNONS ? 0.5 : 1, 0); DrawQ_String_Font(x, y, temp, len, size, size, 1, 1, 1, 1, 0, NULL, true, FONT_INFOBAR); @@ -496,7 +496,7 @@ static int SCR_DrawCurlDownload(int offset) if(addinfo) { len = (int)strlen(addinfo); - x = (vid_conwidth.integer - DrawQ_TextWidth_Font(addinfo, len, size, size, false, FONT_INFOBAR)) / 2; + x = (vid_conwidth.integer - DrawQ_TextWidth_Font(addinfo, len, false, FONT_INFOBAR) * size) / 2; DrawQ_Fill(0, y - size, vid_conwidth.integer, size, 1, 1, 1, cls.signon == SIGNONS ? 0.8 : 1, 0); DrawQ_String_Font(x, y - size, addinfo, len, size, size, 0, 0, 0, 1, 0, NULL, true, FONT_INFOBAR); } @@ -510,7 +510,7 @@ static int SCR_DrawCurlDownload(int offset) else dpsnprintf(temp, sizeof(temp), "Downloading %s ... %5.1f%% @ %.1f KiB/s\n", downinfo[i].filename, 100.0 * downinfo[i].progress, downinfo[i].speed / 1024.0); len = (int)strlen(temp); - x = (vid_conwidth.integer - DrawQ_TextWidth_Font(temp, len, size, size, false, FONT_INFOBAR)) / 2; + x = (vid_conwidth.integer - DrawQ_TextWidth_Font(temp, len, false, FONT_INFOBAR) * size) / 2; DrawQ_Fill(0, y + i * size, vid_conwidth.integer, size, 0, 0, 0, cls.signon == SIGNONS ? 0.5 : 1, 0); DrawQ_String_Font(x, y + i * size, temp, len, size, size, 1, 1, 1, 1, 0, NULL, true, FONT_INFOBAR); } diff --git a/console.c b/console.c index 3a26df8f..dba6db30 100644 --- a/console.c +++ b/console.c @@ -1145,7 +1145,7 @@ void Con_DrawInput (void) // text[key_linepos + 1] = 0; - x = vid_conwidth.value * 0.95 - DrawQ_TextWidth_Font(text, key_linepos, con_textsize.value, con_textsize.value, false, FONT_CONSOLE); + x = vid_conwidth.value * 0.95 - DrawQ_TextWidth_Font(text, key_linepos, false, FONT_CONSOLE) * con_textsize.value; if(x >= 0) x = 0; @@ -1180,7 +1180,7 @@ float Con_WordWidthFunc(void *passthrough, const char *w, size_t *length, float ti->colorindex = -1; return ti->fontsize * ti->font->width_of[0]; } - return DrawQ_TextWidth_Font_UntilWidth(w, length, ti->fontsize, ti->fontsize, false, ti->font, maxWidth); + return DrawQ_TextWidth_Font_UntilWidth(w, length, false, ti->font, maxWidth) * ti->fontsize; } int Con_CountLineFunc(void *passthrough, const char *line, size_t length, float width, qboolean isContinuation) @@ -1375,7 +1375,7 @@ void Con_DrawNotify (void) // FIXME word wrap inputsize = (numChatlines ? con_chatsize : con_notifysize).value; - x = vid_conwidth.value - DrawQ_TextWidth_Font(temptext, 0, inputsize, inputsize, false, FONT_CHAT); + x = vid_conwidth.value - DrawQ_TextWidth_Font(temptext, 0, false, FONT_CHAT) * inputsize; if(x > 0) x = 0; DrawQ_String_Font(x, v, temptext, 0, inputsize, inputsize, 1.0, 1.0, 1.0, 1.0, 0, &colorindex, false, FONT_CHAT); @@ -1501,7 +1501,7 @@ void Con_DrawConsole (int lines) // draw the background DrawQ_Pic(0, lines - vid_conheight.integer, scr_conbrightness.value >= 0.01f ? Draw_CachePic("gfx/conback", true) : NULL, vid_conwidth.integer, vid_conheight.integer, scr_conbrightness.value, scr_conbrightness.value, scr_conbrightness.value, cls.signon == SIGNONS ? scr_conalpha.value : 1.0, 0); // always full alpha when not in game - DrawQ_String_Font(vid_conwidth.integer - DrawQ_TextWidth_Font(engineversion, 0, con_textsize.value, con_textsize.value, false, FONT_CONSOLE), lines - con_textsize.value, engineversion, 0, con_textsize.value, con_textsize.value, 1, 0, 0, 1, 0, NULL, true, FONT_CONSOLE); + DrawQ_String_Font(vid_conwidth.integer - DrawQ_TextWidth_Font(engineversion, 0, false, FONT_CONSOLE) * con_textsize.value, lines - con_textsize.value, engineversion, 0, con_textsize.value, con_textsize.value, 1, 0, 0, 1, 0, NULL, true, FONT_CONSOLE); // draw the text if(con_lines_count > 0) diff --git a/draw.h b/draw.h index a94d79ca..1e6be908 100644 --- a/draw.h +++ b/draw.h @@ -110,8 +110,9 @@ void DrawQ_Fill(float x, float y, float width, float height, float red, float gr // if r_textshadow is not zero, an additional instance of the text is drawn first at an offset with an inverted shade of gray (black text produces a white shadow, brightly colored text produces a black shadow) float DrawQ_String(float x, float y, const char *text, size_t maxlen, float scalex, float scaley, float basered, float basegreen, float baseblue, float basealpha, int flags, int *outcolor, qboolean ignorecolorcodes); float DrawQ_String_Font(float x, float y, const char *text, size_t maxlen, float scalex, float scaley, float basered, float basegreen, float baseblue, float basealpha, int flags, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt); -float DrawQ_TextWidth_Font(const char *text, size_t maxlen, float scalex, float scaley, qboolean ignorecolorcodes, const dp_font_t *fnt); -float DrawQ_TextWidth_Font_UntilWidth(const char *text, size_t *maxlen, float scalex, float scaley, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxWidth); +float DrawQ_TextWidth_Font(const char *text, size_t maxlen, qboolean ignorecolorcodes, const dp_font_t *fnt); +float DrawQ_TextWidth_Font_UntilWidth(const char *text, size_t *maxlen, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxWidth); +float DrawQ_TextWidth_Font_UntilWidth_TrackColors(const char *text, size_t *maxlen, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxwidth); // draw a very fancy pic (per corner texcoord/color control), the order is tl, tr, bl, br void DrawQ_SuperPic(float x, float y, cachepic_t *pic, float width, float height, float s1, float t1, float r1, float g1, float b1, float a1, float s2, float t2, float r2, float g2, float b2, float a2, float s3, float t3, float r3, float g3, float b3, float a3, float s4, float t4, float r4, float g4, float b4, float a4, int flags); // draw a triangle mesh diff --git a/gl_draw.c b/gl_draw.c index de47e546..b5fcfa85 100644 --- a/gl_draw.c +++ b/gl_draw.c @@ -882,7 +882,57 @@ static void DrawQ_GetTextColor(float color[4], int colorindex, float r, float g, } } -static float DrawQ_String_Font_UntilX(float startx, float starty, const char *text, size_t *maxlen, float w, float h, float basered, float basegreen, float baseblue, float basealpha, int flags, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxx) +float DrawQ_TextWidth_Font_UntilWidth_TrackColors(const char *text, size_t *maxlen, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxwidth) +{ + int num, colorindex = STRING_COLOR_DEFAULT; + size_t i; + float x = 0; + + if (*maxlen < 1) + *maxlen = 1<<30; + + if (!outcolor || *outcolor == -1) + colorindex = STRING_COLOR_DEFAULT; + else + colorindex = *outcolor; + + for (i = 0;i < *maxlen && text[i];i++) + { + if (text[i] == ' ') + { + if(x + fnt->width_of[' '] > maxwidth) + break; // oops, can't draw this + x += fnt->width_of[' ']; + continue; + } + if (text[i] == STRING_COLOR_TAG && !ignorecolorcodes && i + 1 < *maxlen) + { + if (text[i+1] == STRING_COLOR_TAG) + { + i++; + } + else if (text[i+1] >= '0' && text[i+1] <= '9') + { + colorindex = text[i+1] - '0'; + i++; + continue; + } + } + num = (unsigned char) text[i]; + if(x + fnt->width_of[num] > maxwidth) + break; // oops, can't draw this + x += fnt->width_of[num]; + } + + *maxlen = i; + + if (outcolor) + *outcolor = colorindex; + + return x; +} + +float DrawQ_String_Font(float startx, float starty, const char *text, size_t maxlen, float w, float h, float basered, float basegreen, float baseblue, float basealpha, int flags, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt) { int num, shadow, colorindex = STRING_COLOR_DEFAULT; size_t i; @@ -893,10 +943,9 @@ static float DrawQ_String_Font_UntilX(float startx, float starty, const char *te float vertex3f[QUADELEMENTS_MAXQUADS*4*3]; float texcoord2f[QUADELEMENTS_MAXQUADS*4*2]; float color4f[QUADELEMENTS_MAXQUADS*4*4]; - qboolean checkwidth; - if (*maxlen < 1) - *maxlen = 1<<30; + if (maxlen < 1) + maxlen = 1<<30; // when basealpha == 0, skip as much as possible (just return width) if(basealpha > 0) @@ -914,7 +963,6 @@ static float DrawQ_String_Font_UntilX(float startx, float starty, const char *te at = texcoord2f; av = vertex3f; batchcount = 0; - checkwidth = (maxx >= startx); for (shadow = r_textshadow.value != 0 && basealpha > 0;shadow >= 0;shadow--) { @@ -932,17 +980,14 @@ static float DrawQ_String_Font_UntilX(float startx, float starty, const char *te x += r_textshadow.value; y += r_textshadow.value; } - for (i = 0;i < *maxlen && text[i];i++) + for (i = 0;i < maxlen && text[i];i++) { if (text[i] == ' ') { - if(checkwidth) - if(x + fnt->width_of[' '] * w > maxx) - break; // oops, can't draw this x += fnt->width_of[' '] * w; continue; } - if (text[i] == STRING_COLOR_TAG && !ignorecolorcodes && i + 1 < *maxlen) + if (text[i] == STRING_COLOR_TAG && !ignorecolorcodes && i + 1 < maxlen) { if (text[i+1] == STRING_COLOR_TAG) { @@ -957,9 +1002,6 @@ static float DrawQ_String_Font_UntilX(float startx, float starty, const char *te } } num = (unsigned char) text[i]; - if(checkwidth) - if(x + fnt->width_of[num] * w > maxx) - break; // oops, can't draw this if(basealpha > 0) { // FIXME make these smaller to just include the occupied part of the character for slightly faster rendering @@ -999,9 +1041,6 @@ static float DrawQ_String_Font_UntilX(float startx, float starty, const char *te } x += fnt->width_of[num] * w; } - if(checkwidth) - *maxlen = i; - checkwidth = 0; // we've done all we had to } if (basealpha > 0) { @@ -1020,24 +1059,19 @@ static float DrawQ_String_Font_UntilX(float startx, float starty, const char *te return x; } -float DrawQ_String_Font(float startx, float starty, const char *text, size_t maxlen, float w, float h, float basered, float basegreen, float baseblue, float basealpha, int flags, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt) -{ - return DrawQ_String_Font_UntilX(startx, starty, text, &maxlen, w, h, basered, basegreen, baseblue, basealpha, flags, outcolor, ignorecolorcodes, fnt, startx-1); -} - float DrawQ_String(float startx, float starty, const char *text, size_t maxlen, float w, float h, float basered, float basegreen, float baseblue, float basealpha, int flags, int *outcolor, qboolean ignorecolorcodes) { return DrawQ_String_Font(startx, starty, text, maxlen, w, h, basered, basegreen, baseblue, basealpha, flags, outcolor, ignorecolorcodes, &dp_fonts[0]); } -float DrawQ_TextWidth_Font_UntilWidth(const char *text, size_t *maxlen, float scalex, float scaley, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxWidth) +float DrawQ_TextWidth_Font_UntilWidth(const char *text, size_t *maxlen, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxWidth) { - return DrawQ_String_Font_UntilX(0, 0, text, maxlen, scalex, scaley, 1, 1, 1, 0, 0, NULL, ignorecolorcodes, fnt, maxWidth); + return DrawQ_TextWidth_Font_UntilWidth_TrackColors(text, maxlen, NULL, ignorecolorcodes, fnt, maxWidth); } -float DrawQ_TextWidth_Font(const char *text, size_t maxlen, float scalex, float scaley, qboolean ignorecolorcodes, const dp_font_t *fnt) +float DrawQ_TextWidth_Font(const char *text, size_t maxlen, qboolean ignorecolorcodes, const dp_font_t *fnt) { - return DrawQ_TextWidth_Font_UntilWidth(text, &maxlen, scalex, scaley, ignorecolorcodes, fnt, -1); + return DrawQ_TextWidth_Font_UntilWidth(text, &maxlen, ignorecolorcodes, fnt, 1000000000); } #if 0 diff --git a/prvm_cmds.c b/prvm_cmds.c index cc01c830..7f099c46 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -2726,7 +2726,7 @@ void VM_stringwidth(void) string = PRVM_G_STRING(OFS_PARM0); colors = (int)PRVM_G_FLOAT(OFS_PARM1); - PRVM_G_FLOAT(OFS_RETURN) = DrawQ_String_Font(0, 0, string, 0, 1, 1, 0, 0, 0, 0, 0, NULL, !colors, getdrawfont()); // 1x1 characters, don't actually draw + PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()); // 1x1 characters, don't actually draw } /* ========= diff --git a/sbar.c b/sbar.c index ec29774e..deee45df 100644 --- a/sbar.c +++ b/sbar.c @@ -1080,7 +1080,7 @@ void Sbar_ShowFPS(void) fps_y = vid_conheight.integer - fps_height; if (fpsstring[0]) { - fps_x = vid_conwidth.integer - DrawQ_TextWidth_Font(fpsstring, 0, fps_scalex, fps_scaley, true, FONT_INFOBAR); + fps_x = vid_conwidth.integer - DrawQ_TextWidth_Font(fpsstring, 0, true, FONT_INFOBAR) * fps_scalex; DrawQ_Fill(fps_x, fps_y, vid_conwidth.integer - fps_x, fps_scaley, 0, 0, 0, 0.5, 0); if (red) DrawQ_String_Font(fps_x, fps_y, fpsstring, 0, fps_scalex, fps_scaley, 1, 0, 0, 1, 0, NULL, true, FONT_INFOBAR); @@ -1090,14 +1090,14 @@ void Sbar_ShowFPS(void) } if (timestring[0]) { - fps_x = vid_conwidth.integer - DrawQ_TextWidth_Font(timestring, 0, fps_scalex, fps_scaley, true, FONT_INFOBAR); + fps_x = vid_conwidth.integer - DrawQ_TextWidth_Font(timestring, 0, true, FONT_INFOBAR) * fps_scalex; DrawQ_Fill(fps_x, fps_y, vid_conwidth.integer - fps_x, fps_scaley, 0, 0, 0, 0.5, 0); DrawQ_String_Font(fps_x, fps_y, timestring, 0, fps_scalex, fps_scaley, 1, 1, 1, 1, 0, NULL, true, FONT_INFOBAR); fps_y += fps_scaley; } if (datestring[0]) { - fps_x = vid_conwidth.integer - DrawQ_TextWidth_Font(datestring, 0, fps_scalex, fps_scaley, true, FONT_INFOBAR); + fps_x = vid_conwidth.integer - DrawQ_TextWidth_Font(datestring, 0, true, FONT_INFOBAR) * fps_scalex; DrawQ_Fill(fps_x, fps_y, vid_conwidth.integer - fps_x, fps_scaley, 0, 0, 0, 0.5, 0); DrawQ_String_Font(fps_x, fps_y, datestring, 0, fps_scalex, fps_scaley, 1, 1, 1, 1, 0, NULL, true, FONT_INFOBAR); fps_y += fps_scaley; -- 2.39.2