]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_draw.c
extension to writeconfig command: also take a file name
[xonotic/darkplaces.git] / gl_draw.c
index a6b98684d2c3f2bfc2a342a957e2355d8c5e821e..f3241e561a2ebe373795293d2912cdcabc262bf3 100644 (file)
--- a/gl_draw.c
+++ b/gl_draw.c
@@ -42,7 +42,7 @@ static int numcachepics;
 
 static rtexturepool_t *drawtexturepool;
 
-static unsigned char concharimage[FONT_FILESIZE] =
+static const unsigned char concharimage[FONT_FILESIZE] =
 {
 #include "lhfont.h"
 };
@@ -106,7 +106,7 @@ static rtexture_t *draw_generateditherpattern(void)
        for (y = 0;y < 8;y++)
                for (x = 0;x < 8;x++)
                        pixels[y][x] = ((x^y) & 4) ? 254 : 0;
-       return R_LoadTexture2D(drawtexturepool, "ditherpattern", 8, 8, pixels[0], TEXTYPE_PALETTE, TEXF_FORCENEAREST | TEXF_PRECACHE, NULL);
+       return R_LoadTexture2D(drawtexturepool, "ditherpattern", 8, 8, pixels[0], TEXTYPE_PALETTE, TEXF_FORCENEAREST | TEXF_PRECACHE, palette_bgra_transparent);
 }
 
 typedef struct embeddedpic_s
@@ -118,7 +118,7 @@ typedef struct embeddedpic_s
 }
 embeddedpic_t;
 
-static embeddedpic_t embeddedpics[] =
+static const embeddedpic_t embeddedpics[] =
 {
        {
        "gfx/prydoncursor001", 16, 16,
@@ -388,7 +388,7 @@ static embeddedpic_t embeddedpics[] =
 
 static rtexture_t *draw_generatepic(const char *name)
 {
-       embeddedpic_t *p;
+       const embeddedpic_t *p;
        for (p = embeddedpics;p->name;p++)
                if (!strcmp(name, p->name))
                        return R_LoadTexture2D(drawtexturepool, p->name, p->width, p->height, (const unsigned char *)p->pixels, TEXTYPE_PALETTE, TEXF_ALPHA | TEXF_PRECACHE, palette_bgra_embeddedpic);
@@ -726,7 +726,7 @@ void GL_Draw_Init (void)
        strlcpy(FONT_CENTERPRINT->title, "centerprint", sizeof(FONT_CENTERPRINT->title));
        strlcpy(FONT_INFOBAR->title, "infobar", sizeof(FONT_INFOBAR->title));
        strlcpy(FONT_MENU->title, "menu", sizeof(FONT_MENU->title));
-       for(i = 0, j = 0; i < MAX_FONTS; ++i)
+       for(i = 0, j = 0; i < MAX_USERFONTS; ++i)
                if(!FONT_USER[i].title[0])
                        dpsnprintf(FONT_USER[i].title, sizeof(FONT_USER[i].title), "user%d", j++);
 }
@@ -792,6 +792,12 @@ void DrawQ_Pic(float x, float y, cachepic_t *pic, float width, float height, flo
                R_Mesh_TexBind(0, R_GetTexture(pic->tex));
                R_Mesh_TexCoordPointer(0, 2, floats + 12, 0, 0);
 
+#if 1
+               floats[12] = 0.0f;floats[13] = 0.0f;
+               floats[14] = 1.0f;floats[15] = 0.0f;
+               floats[16] = 1.0f;floats[17] = 1.0f;
+               floats[18] = 0.0f;floats[19] = 1.0f;
+#else
       // AK07: lets be texel correct on the corners
       {
          float horz_offset = 0.5f / pic->width;
@@ -802,6 +808,7 @@ void DrawQ_Pic(float x, float y, cachepic_t *pic, float width, float height, flo
                   floats[16] = 1.0f - horz_offset;floats[17] = 1.0f - vert_offset;
                   floats[18] = 0.0f + horz_offset;floats[19] = 1.0f - vert_offset;
       }
+#endif
        }
 
        floats[2] = floats[5] = floats[8] = floats[11] = 0;
@@ -834,7 +841,7 @@ void DrawQ_Fill(float x, float y, float width, float height, float red, float gr
 }
 
 // color tag printing
-static vec4_t string_colors[] =
+static const vec4_t string_colors[] =
 {
        // Quake3 colors
        // LordHavoc: why on earth is cyan before magenta in Quake3?
@@ -875,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;
@@ -886,28 +943,22 @@ 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)
-       {
-               _DrawQ_ProcessDrawFlag(flags);
+       _DrawQ_ProcessDrawFlag(flags);
 
-               R_Mesh_ColorPointer(color4f, 0, 0);
-               R_Mesh_ResetTextureState();
-               R_Mesh_TexBind(0, R_GetTexture(fnt->tex));
-               R_Mesh_TexCoordPointer(0, 2, texcoord2f, 0, 0);
-               R_Mesh_VertexPointer(vertex3f, 0, 0);
-       }
+       R_Mesh_ColorPointer(color4f, 0, 0);
+       R_Mesh_ResetTextureState();
+       R_Mesh_TexBind(0, R_GetTexture(fnt->tex));
+       R_Mesh_TexCoordPointer(0, 2, texcoord2f, 0, 0);
+       R_Mesh_VertexPointer(vertex3f, 0, 0);
 
        ac = color4f;
        at = texcoord2f;
        av = vertex3f;
        batchcount = 0;
-       checkwidth = (maxx >= startx);
 
        for (shadow = r_textshadow.value != 0 && basealpha > 0;shadow >= 0;shadow--)
        {
@@ -915,8 +966,7 @@ static float DrawQ_String_Font_UntilX(float startx, float starty, const char *te
                        colorindex = STRING_COLOR_DEFAULT;
                else
                        colorindex = *outcolor;
-               if(basealpha > 0)
-                       DrawQ_GetTextColor(color, colorindex, basered, basegreen, baseblue, basealpha, shadow);
+               DrawQ_GetTextColor(color, colorindex, basered, basegreen, baseblue, basealpha, shadow);
 
                x = startx;
                y = starty;
@@ -925,17 +975,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)
                                {
@@ -950,60 +997,45 @@ 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
+                       s = (num & 15)*0.0625f + (0.5f / 256.0f);
+                       t = (num >> 4)*0.0625f + (0.5f / 256.0f);
+                       u = 0.0625f - (1.0f / 256.0f);
+                       v = 0.0625f - (1.0f / 256.0f);
+                       ac[ 0] = color[0];ac[ 1] = color[1];ac[ 2] = color[2];ac[ 3] = color[3];
+                       ac[ 4] = color[0];ac[ 5] = color[1];ac[ 6] = color[2];ac[ 7] = color[3];
+                       ac[ 8] = color[0];ac[ 9] = color[1];ac[10] = color[2];ac[11] = color[3];
+                       ac[12] = color[0];ac[13] = color[1];ac[14] = color[2];ac[15] = color[3];
+                       at[ 0] = s  ;at[ 1] = t  ;
+                       at[ 2] = s+u;at[ 3] = t  ;
+                       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;av[ 4] = y  ;av[ 5] = 10;
+                       av[ 6] = x+w;av[ 7] = y+h;av[ 8] = 10;
+                       av[ 9] = x  ;av[10] = y+h;av[11] = 10;
+                       ac += 16;
+                       at += 8;
+                       av += 12;
+                       batchcount++;
+                       if (batchcount >= QUADELEMENTS_MAXQUADS)
                        {
-                               // FIXME make these smaller to just include the occupied part of the character for slightly faster rendering
-                               s = (num & 15)*0.0625f + (0.5f / 256.0f);
-                               t = (num >> 4)*0.0625f + (0.5f / 256.0f);
-                               u = 0.0625f - (1.0f / 256.0f);
-                               v = 0.0625f - (1.0f / 256.0f);
-                               ac[ 0] = color[0];ac[ 1] = color[1];ac[ 2] = color[2];ac[ 3] = color[3];
-                               ac[ 4] = color[0];ac[ 5] = color[1];ac[ 6] = color[2];ac[ 7] = color[3];
-                               ac[ 8] = color[0];ac[ 9] = color[1];ac[10] = color[2];ac[11] = color[3];
-                               ac[12] = color[0];ac[13] = color[1];ac[14] = color[2];ac[15] = color[3];
-                               at[ 0] = s  ;at[ 1] = t  ;
-                               at[ 2] = s+u;at[ 3] = t  ;
-                               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;av[ 4] = y  ;av[ 5] = 10;
-                               av[ 6] = x+w;av[ 7] = y+h;av[ 8] = 10;
-                               av[ 9] = x  ;av[10] = y+h;av[11] = 10;
-                               ac += 16;
-                               at += 8;
-                               av += 12;
-                               batchcount++;
-                               if (batchcount >= QUADELEMENTS_MAXQUADS)
-                               {
-                                       if (basealpha >= (1.0f / 255.0f))
-                                       {
-                                               GL_LockArrays(0, batchcount * 4);
-                                               R_Mesh_Draw(0, batchcount * 4, batchcount * 2, quadelements, 0, 0);
-                                               GL_LockArrays(0, 0);
-                                       }
-                                       batchcount = 0;
-                                       ac = color4f;
-                                       at = texcoord2f;
-                                       av = vertex3f;
-                               }
+                               GL_LockArrays(0, batchcount * 4);
+                               R_Mesh_Draw(0, batchcount * 4, batchcount * 2, quadelements, 0, 0);
+                               GL_LockArrays(0, 0);
+                               batchcount = 0;
+                               ac = color4f;
+                               at = texcoord2f;
+                               av = vertex3f;
                        }
                        x += fnt->width_of[num] * w;
                }
-               if(checkwidth)
-                       *maxlen = i;
-               checkwidth = 0; // we've done all we had to
        }
-       if (basealpha > 0)
+       if (batchcount > 0)
        {
-               if (batchcount > 0)
-               {
-                       GL_LockArrays(0, batchcount * 4);
-                       R_Mesh_Draw(0, batchcount * 4, batchcount * 2, quadelements, 0, 0);
-                       GL_LockArrays(0, 0);
-               }
+               GL_LockArrays(0, batchcount * 4);
+               R_Mesh_Draw(0, batchcount * 4, batchcount * 2, quadelements, 0, 0);
+               GL_LockArrays(0, 0);
        }
 
        if (outcolor)
@@ -1013,24 +1045,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