From: divverent Date: Fri, 25 Dec 2009 15:14:09 +0000 (+0000) Subject: add a menu/csqc field .drawfontscale to scale the font drawn by drawfont but not... X-Git-Tag: xonotic-v0.1.0preview~958 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=b580442d0be79e1c4c78b9cb568fe30cf3e03f76;p=xonotic%2Fdarkplaces.git add a menu/csqc field .drawfontscale to scale the font drawn by drawfont but not change the bitmap - useful for smoothly changing font size if snapping is active git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@9690 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/draw.h b/draw.h index 114f90cc..14c69c78 100644 --- a/draw.h +++ b/draw.h @@ -145,6 +145,7 @@ 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_String_Font_Scale(float x, float y, const char *text, size_t maxlen, float sizex, float sizey, float scalex, float scaley, float basered, float basegreen, float baseblue, float basealpha, int flags, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt); // you are STRONGLY DISCOURAGED to use a version without the _Size suffix!!! /* don't use: */float DrawQ_TextWidth_Font(const char *text, size_t maxlen, qboolean ignorecolorcodes, const dp_font_t *fnt); /* use this: */float DrawQ_TextWidth_Font_Size(const char *text, float w, float h, size_t maxlen, qboolean ignorecolorcodes, const dp_font_t *fnt); @@ -152,6 +153,7 @@ float DrawQ_String_Font(float x, float y, const char *text, size_t maxlen, float /* use this: */float DrawQ_TextWidth_Font_UntilWidth_Size(const char *text, float w, float h, size_t *maxlen, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxWidth); /* don't use: */float DrawQ_TextWidth_Font_UntilWidth_TrackColors(const char *text, size_t *maxlen, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxwidth); /* use this: */float DrawQ_TextWidth_Font_UntilWidth_TrackColors_Size(const char *text, float w, float h, size_t *maxlen, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxwidth); +/* use this: */float DrawQ_TextWidth_Font_UntilWidth_TrackColors_Size_Scale(const char *text, float w, float h, float sw, float sh, 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 1f9aa358..1f749284 100644 --- a/gl_draw.c +++ b/gl_draw.c @@ -1074,7 +1074,7 @@ static void DrawQ_GetTextColor(float color[4], int colorindex, float r, float g, } // NOTE: this function always draws exactly one character if maxwidth <= 0 -float DrawQ_TextWidth_Font_UntilWidth_TrackColors_Size(const char *text, float w, float h, size_t *maxlen, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxwidth) +float DrawQ_TextWidth_Font_UntilWidth_TrackColors_Size_Scale(const char *text, float w, float h, float sw, float sh, size_t *maxlen, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxwidth) { const char *text_start = text; int colorindex = STRING_COLOR_DEFAULT; @@ -1093,6 +1093,7 @@ float DrawQ_TextWidth_Font_UntilWidth_TrackColors_Size(const char *text, float w // float ftbase_x; qboolean snap = true; qboolean least_one = false; + float dw, dh; // display w/h if (!h) h = w; if (!h) { @@ -1112,6 +1113,16 @@ float DrawQ_TextWidth_Font_UntilWidth_TrackColors_Size(const char *text, float w map_index = Font_IndexForSize(ft2, h, NULL, NULL); fontmap = Font_MapForIndex(ft2, map_index); } + if(snap) + { + if(fabs(sw - 1) > 0.001 || fabs(sh - 1) > 0.001) + snap = false; // turn off pixel snapping for better animation + else + sw = sh = 1; + } + + dw = w * sw; + dh = h * sh; if (*maxlen < 1) *maxlen = 1<<30; @@ -1142,12 +1153,12 @@ float DrawQ_TextWidth_Font_UntilWidth_TrackColors_Size(const char *text, float w if (ch == ' ' && !fontmap) { if(!least_one || i0) // never skip the first character - if(x + fnt->width_of[(int) ' '] * w > maxwidth) + if(x + fnt->width_of[(int) ' '] * dw > maxwidth) { i = i0; break; // oops, can't draw this } - x += fnt->width_of[(int) ' '] * w; + x += fnt->width_of[(int) ' '] * dw; continue; } // i points to the char after ^ @@ -1214,12 +1225,12 @@ float DrawQ_TextWidth_Font_UntilWidth_TrackColors_Size(const char *text, float w map = ft2_oldstyle_map; prevch = 0; if(!least_one || i0) // never skip the first character - if(x + fnt->width_of[ch] * w > maxwidth) + if(x + fnt->width_of[ch] * dw > maxwidth) { i = i0; break; // oops, can't draw this } - x += fnt->width_of[ch] * w; + x += fnt->width_of[ch] * dw; } else { if (!map || map == ft2_oldstyle_map || map->start < ch || map->start + FONT_CHARS_PER_MAP >= ch) { @@ -1234,8 +1245,8 @@ float DrawQ_TextWidth_Font_UntilWidth_TrackColors_Size(const char *text, float w } mapch = ch - map->start; if (prevch && Font_GetKerningForMap(ft2, map_index, w, h, prevch, ch, &kx, NULL)) - x += kx * w; - x += map->glyphs[mapch].advance_x * w; + x += kx * dw; + x += map->glyphs[mapch].advance_x * dw; prevmap = map; prevch = ch; } @@ -1249,7 +1260,7 @@ float DrawQ_TextWidth_Font_UntilWidth_TrackColors_Size(const char *text, float w 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) +float DrawQ_String_Font_Scale(float startx, float starty, const char *text, size_t maxlen, float w, float h, float sw, float sh, float basered, float basegreen, float baseblue, float basealpha, int flags, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt) { int shadow, colorindex = STRING_COLOR_DEFAULT; size_t i; @@ -1274,6 +1285,7 @@ float DrawQ_String_Font(float startx, float starty, const char *text, size_t max qboolean snap = true; float pix_x, pix_y; size_t bytes_left; + float dw, dh; int tw, th; tw = R_TextureWidth(fnt->tex); @@ -1297,10 +1309,20 @@ float DrawQ_String_Font(float startx, float starty, const char *text, size_t max map_index = Font_IndexForSize(ft2, h, NULL, NULL); fontmap = Font_MapForIndex(ft2, map_index); } + if(snap) + { + if(fabs(sw - 1) > 0.001 || fabs(sh - 1) > 0.001) + snap = false; // turn off pixel snapping for better animation + else + sw = sh = 1; + } + + dw = w * sw; + dh = h * sh; // draw the font at its baseline when using freetype //ftbase_x = 0; - ftbase_y = h * (4.5/6.0); + ftbase_y = dh * (4.5/6.0); if (maxlen < 1) maxlen = 1<<30; @@ -1321,7 +1343,8 @@ float DrawQ_String_Font(float startx, float starty, const char *text, size_t max batchcount = 0; //ftbase_x = snap_to_pixel_x(ftbase_x); - ftbase_y = snap_to_pixel_y(ftbase_y, 0.3); + if(snap) + ftbase_y = snap_to_pixel_y(ftbase_y, 0.3); pix_x = vid.width / vid_conwidth.value; pix_y = vid.height / vid_conheight.value; @@ -1358,7 +1381,7 @@ float DrawQ_String_Font(float startx, float starty, const char *text, size_t max } if (ch == ' ' && !fontmap) { - x += fnt->width_of[(int) ' '] * w; + x += fnt->width_of[(int) ' '] * dw; continue; } if (ch == STRING_COLOR_TAG && !ignorecolorcodes && i < maxlen) @@ -1465,9 +1488,9 @@ float DrawQ_String_Font(float startx, float starty, const char *text, size_t max at[ 4] = s+u ; at[ 5] = t+v ; at[ 6] = s ; at[ 7] = t+v ; av[ 0] = x ; av[ 1] = y ; av[ 2] = 10; - av[ 3] = x+w*thisw ; av[ 4] = y ; av[ 5] = 10; - av[ 6] = x+w*thisw ; av[ 7] = y+h ; av[ 8] = 10; - av[ 9] = x ; av[10] = y+h ; av[11] = 10; + av[ 3] = x+dw*thisw ; av[ 4] = y ; av[ 5] = 10; + av[ 6] = x+dw*thisw ; av[ 7] = y+dh ; av[ 8] = 10; + av[ 9] = x ; av[10] = y+dh ; av[11] = 10; ac += 16; at += 8; av += 12; @@ -1482,7 +1505,7 @@ float DrawQ_String_Font(float startx, float starty, const char *text, size_t max at = texcoord2f; av = vertex3f; } - x += thisw * w; + x += thisw * dw; } else { if (!map || map == ft2_oldstyle_map || map->start < ch || map->start + FONT_CHARS_PER_MAP >= ch) { @@ -1524,8 +1547,8 @@ float DrawQ_String_Font(float startx, float starty, const char *text, size_t max y += ftbase_y; if (prevch && Font_GetKerningForMap(ft2, map_index, w, h, prevch, ch, &kx, &ky)) { - x += kx * w; - y += ky * h; + x += kx * dw; + y += ky * dh; } else kx = ky = 0; @@ -1537,14 +1560,14 @@ float DrawQ_String_Font(float startx, float starty, const char *text, size_t max at[2] = map->glyphs[mapch].txmax; at[3] = map->glyphs[mapch].tymin; at[4] = map->glyphs[mapch].txmax; at[5] = map->glyphs[mapch].tymax; at[6] = map->glyphs[mapch].txmin; at[7] = map->glyphs[mapch].tymax; - av[ 0] = x + w * map->glyphs[mapch].vxmin; av[ 1] = y + h * map->glyphs[mapch].vymin; av[ 2] = 10; - av[ 3] = x + w * map->glyphs[mapch].vxmax; av[ 4] = y + h * map->glyphs[mapch].vymin; av[ 5] = 10; - av[ 6] = x + w * map->glyphs[mapch].vxmax; av[ 7] = y + h * map->glyphs[mapch].vymax; av[ 8] = 10; - av[ 9] = x + w * map->glyphs[mapch].vxmin; av[10] = y + h * map->glyphs[mapch].vymax; av[11] = 10; + av[ 0] = x + dw * map->glyphs[mapch].vxmin; av[ 1] = y + dh * map->glyphs[mapch].vymin; av[ 2] = 10; + av[ 3] = x + dw * map->glyphs[mapch].vxmax; av[ 4] = y + dh * map->glyphs[mapch].vymin; av[ 5] = 10; + av[ 6] = x + dw * map->glyphs[mapch].vxmax; av[ 7] = y + dh * map->glyphs[mapch].vymax; av[ 8] = 10; + av[ 9] = x + dw * map->glyphs[mapch].vxmin; av[10] = y + dh * map->glyphs[mapch].vymax; av[11] = 10; //x -= ftbase_x; y -= ftbase_y; - x += thisw * w; + x += thisw * dw; ac += 16; at += 8; av += 12; @@ -1584,11 +1607,21 @@ float DrawQ_String_Font(float startx, float starty, const char *text, size_t max 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_Scale(startx, starty, text, maxlen, w, h, 1, 1, basered, basegreen, baseblue, basealpha, flags, outcolor, ignorecolorcodes, fnt); +} + 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_TrackColors_Size(const char *text, float w, float h, size_t *maxlen, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxwidth) +{ + return DrawQ_TextWidth_Font_UntilWidth_TrackColors_Size_Scale(text, w, h, 1, 1, maxlen, outcolor, ignorecolorcodes, fnt, maxwidth); +} + float DrawQ_TextWidth_Font(const char *text, size_t maxlen, qboolean ignorecolorcodes, const dp_font_t *fnt) { return DrawQ_TextWidth_Font_UntilWidth(text, &maxlen, ignorecolorcodes, fnt, 1000000000); diff --git a/progsvm.h b/progsvm.h index 64a54890..cab3b749 100644 --- a/progsvm.h +++ b/progsvm.h @@ -289,6 +289,7 @@ typedef struct prvm_prog_globaloffsets_s int dmg_origin; // csqc int sb_showscores; // csqc int drawfont; // csqc / menu + int drawfontscale; // csqc / menu int require_spawnfunc_prefix; // ssqc int worldstatus; // ssqc int servertime; // csqc diff --git a/prvm_cmds.c b/prvm_cmds.c index 702d8a82..3d6cec40 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -3216,6 +3216,21 @@ void VM_freepic(void) Draw_FreePic(s); } +void getdrawfontscale(float *sx, float *sy) +{ + vec3_t v; + *sx = *sy = 1; + if(prog->globaloffsets.drawfontscale >= 0) + { + VectorCopy(PRVM_G_VECTOR(prog->globaloffsets.drawfontscale), v); + if(VectorLength2(v) > 0) + { + *sx = v[0]; + *sy = v[1]; + } + } +} + dp_font_t *getdrawfont(void) { if(prog->globaloffsets.drawfont >= 0) @@ -3241,6 +3256,7 @@ void VM_drawcharacter(void) float *pos,*scale,*rgb; char character; int flag; + float sx, sy; VM_SAFEPARMCOUNT(6,VM_drawcharacter); character = (char) PRVM_G_FLOAT(OFS_PARM1); @@ -3273,7 +3289,8 @@ void VM_drawcharacter(void) return; } - DrawQ_String_Font(pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont()); + getdrawfontscale(&sx, &sy); + DrawQ_String_Font_Scale(pos[0], pos[1], &character, 1, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont()); PRVM_G_FLOAT(OFS_RETURN) = 1; } @@ -3289,6 +3306,7 @@ void VM_drawstring(void) float *pos,*scale,*rgb; const char *string; int flag; + float sx, sy; VM_SAFEPARMCOUNT(6,VM_drawstring); string = PRVM_G_STRING(OFS_PARM1); @@ -3314,7 +3332,8 @@ void VM_drawstring(void) if(pos[2] || scale[2]) Con_Printf("VM_drawstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale"))); - DrawQ_String_Font(pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont()); + getdrawfontscale(&sx, &sy); + DrawQ_String_Font_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont()); //Font_DrawString(pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true); PRVM_G_FLOAT(OFS_RETURN) = 1; } @@ -3331,6 +3350,7 @@ void VM_drawcolorcodedstring(void) float *pos,*scale; const char *string; int flag,color; + float sx, sy; VM_SAFEPARMCOUNT(5,VM_drawstring); string = PRVM_G_STRING(OFS_PARM1); @@ -3356,7 +3376,8 @@ void VM_drawcolorcodedstring(void) Con_Printf("VM_drawcolorcodedstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale"))); color = -1; - DrawQ_String_Font(pos[0], pos[1], string, 0, scale[0], scale[1], 1, 1, 1, PRVM_G_FLOAT(OFS_PARM3), flag, NULL, false, getdrawfont()); + getdrawfontscale(&sx, &sy); + DrawQ_String_Font_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, 1, 1, 1, PRVM_G_FLOAT(OFS_PARM3), flag, NULL, false, getdrawfont()); PRVM_G_FLOAT(OFS_RETURN) = 1; } /* @@ -3372,7 +3393,8 @@ void VM_stringwidth(void) float *szv; float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell int colors; - float x[200]; + float sx, sy; + size_t maxlen = 0; VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring); if(prog->argc == 3) @@ -3386,12 +3408,12 @@ void VM_stringwidth(void) szv = defsize; mult = 1; } - x[180] = 3; + getdrawfontscale(&sx, &sy); string = PRVM_G_STRING(OFS_PARM0); colors = (int)PRVM_G_FLOAT(OFS_PARM1); - PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font_Size(string, szv[0], szv[1], 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw + PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font_UntilWidth_TrackColors_Size_Scale(string, szv[0], szv[1], sx, sy, &maxlen, NULL, !colors, getdrawfont(), 1000000000) * mult; // 1x1 characters, don't actually draw /* if(prog->argc == 3) { diff --git a/prvm_edict.c b/prvm_edict.c index dbbff6a8..c6a1a859 100644 --- a/prvm_edict.c +++ b/prvm_edict.c @@ -1686,6 +1686,7 @@ void PRVM_FindOffsets(void) prog->globaloffsets.dmg_save = PRVM_ED_FindGlobalOffset("dmg_save"); prog->globaloffsets.dmg_take = PRVM_ED_FindGlobalOffset("dmg_take"); prog->globaloffsets.drawfont = PRVM_ED_FindGlobalOffset("drawfont"); + prog->globaloffsets.drawfontscale = PRVM_ED_FindGlobalOffset("drawfontscale"); prog->globaloffsets.gettaginfo_forward = PRVM_ED_FindGlobalOffset("gettaginfo_forward"); prog->globaloffsets.gettaginfo_name = PRVM_ED_FindGlobalOffset("gettaginfo_name"); prog->globaloffsets.gettaginfo_offset = PRVM_ED_FindGlobalOffset("gettaginfo_offset");