]> de.git.xonotic.org Git - voretournament/voretournament.git/blobdiff - misc/source/darkplaces-src/gl_draw.c
Update the engine
[voretournament/voretournament.git] / misc / source / darkplaces-src / gl_draw.c
index ef1fee21241ecccbc7803fc06dbe00beb29f6a8f..05d6c92527fefa1c570359d3a4e8e84f51f90981 100644 (file)
@@ -323,6 +323,7 @@ cachepic_t *Draw_CachePic_Flags(const char *path, unsigned int cachepicflags)
        qboolean ddshasalpha;
        float ddsavgcolor[4];
        qboolean loaded = false;
+       char vabuf[1024];
 
        texflags = TEXF_ALPHA;
        if (!(cachepicflags & CACHEPICFLAG_NOCLAMP))
@@ -361,30 +362,38 @@ cachepic_t *Draw_CachePic_Flags(const char *path, unsigned int cachepicflags)
                return cachepics; // return the first one
        }
        pic = cachepics + (numcachepics++);
+       memset(pic, 0, sizeof(*pic));
        strlcpy (pic->name, path, sizeof(pic->name));
        // link into list
        pic->chain = cachepichash[hashkey];
        cachepichash[hashkey] = pic;
 
 reload:
+       // TODO why does this crash?
+       if(pic->allow_free_tex && pic->tex)
+               R_PurgeTexture(pic->tex);
+
        // check whether it is an dynamic texture (if so, we can directly use its texture handler)
        pic->flags = cachepicflags;
        pic->tex = CL_GetDynTexture( path );
        // if so, set the width/height, too
        if( pic->tex ) {
+               pic->allow_free_tex = false;
                pic->width = R_TextureWidth(pic->tex);
                pic->height = R_TextureHeight(pic->tex);
                // we're done now (early-out)
                return pic;
        }
 
+       pic->allow_free_tex = true;
+
        pic->hasalpha = true; // assume alpha unless we know it has none
        pic->texflags = texflags;
        pic->autoload = (cachepicflags & CACHEPICFLAG_NOTPERSISTENT);
        pic->lastusedframe = draw_frame;
 
        // load a high quality image from disk if possible
-       if (!loaded && r_texture_dds_load.integer != 0 && (pic->tex = R_LoadTextureDDSFile(drawtexturepool, va("dds/%s.dds", pic->name), pic->texflags, &ddshasalpha, ddsavgcolor, 0)))
+       if (!loaded && r_texture_dds_load.integer != 0 && (pic->tex = R_LoadTextureDDSFile(drawtexturepool, va(vabuf, sizeof(vabuf), "dds/%s.dds", pic->name), vid.sRGB2D, pic->texflags, &ddshasalpha, ddsavgcolor, 0)))
        {
                // note this loads even if autoload is true, otherwise we can't get the width/height
                loaded = true;
@@ -413,8 +422,10 @@ reload:
                if (!pic->autoload)
                {
                        pic->tex = R_LoadTexture2D(drawtexturepool, pic->name, image_width, image_height, pixels, vid.sRGB2D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, pic->texflags & (pic->hasalpha ? ~0 : ~TEXF_ALPHA), -1, NULL);
+#ifndef USE_GLES2
                        if (r_texture_dds_save.integer && qglGetCompressedTexImageARB && pic->tex)
-                               R_SaveTextureDDSFile(pic->tex, va("dds/%s.dds", pic->name), r_texture_dds_save.integer < 2, pic->hasalpha);
+                               R_SaveTextureDDSFile(pic->tex, va(vabuf, sizeof(vabuf), "dds/%s.dds", pic->name), r_texture_dds_save.integer < 2, pic->hasalpha);
+#endif
                }
        }
        if (!loaded)
@@ -488,6 +499,7 @@ reload:
                pic->tex = draw_generatepic(pic->name, (cachepicflags & CACHEPICFLAG_QUIET) != 0);
                pic->width = R_TextureWidth(pic->tex);
                pic->height = R_TextureHeight(pic->tex);
+               pic->allow_free_tex = (pic->tex != r_texture_notexture);
        }
 
        return pic;
@@ -500,25 +512,30 @@ cachepic_t *Draw_CachePic (const char *path)
 
 rtexture_t *Draw_GetPicTexture(cachepic_t *pic)
 {
+       char vabuf[1024];
        if (pic->autoload && !pic->tex)
        {
                if (pic->tex == NULL && r_texture_dds_load.integer != 0)
                {
                        qboolean ddshasalpha;
                        float ddsavgcolor[4];
-                       pic->tex = R_LoadTextureDDSFile(drawtexturepool, va("dds/%s.dds", pic->name), pic->texflags, &ddshasalpha, ddsavgcolor, 0);
+                       pic->tex = R_LoadTextureDDSFile(drawtexturepool, va(vabuf, sizeof(vabuf), "dds/%s.dds", pic->name), vid.sRGB2D, pic->texflags, &ddshasalpha, ddsavgcolor, 0);
                }
                if (pic->tex == NULL)
                {
                        pic->tex = loadtextureimage(drawtexturepool, pic->name, false, pic->texflags, true, vid.sRGB2D);
+#ifndef USE_GLES2
                        if (r_texture_dds_save.integer && qglGetCompressedTexImageARB && pic->tex)
-                               R_SaveTextureDDSFile(pic->tex, va("dds/%s.dds", pic->name), r_texture_dds_save.integer < 2, pic->hasalpha);
+                               R_SaveTextureDDSFile(pic->tex, va(vabuf, sizeof(vabuf), "dds/%s.dds", pic->name), r_texture_dds_save.integer < 2, pic->hasalpha);
+#endif
                }
                if (pic->tex == NULL && !strncmp(pic->name, "gfx/", 4))
                {
                        pic->tex = loadtextureimage(drawtexturepool, pic->name+4, false, pic->texflags, true, vid.sRGB2D);
+#ifndef USE_GLES2
                        if (r_texture_dds_save.integer && qglGetCompressedTexImageARB && pic->tex)
-                               R_SaveTextureDDSFile(pic->tex, va("dds/%s.dds", pic->name), r_texture_dds_save.integer < 2, pic->hasalpha);
+                               R_SaveTextureDDSFile(pic->tex, va(vabuf, sizeof(vabuf), "dds/%s.dds", pic->name), r_texture_dds_save.integer < 2, pic->hasalpha);
+#endif
                }
                if (pic->tex == NULL)
                        pic->tex = draw_generatepic(pic->name, true);
@@ -567,26 +584,24 @@ cachepic_t *Draw_NewPic(const char *picname, int width, int height, int alpha, u
        }
        else
        {
-               if (pic == NULL)
+               if (numcachepics == MAX_CACHED_PICS)
                {
-                       if (numcachepics == MAX_CACHED_PICS)
-                       {
-                               Con_Printf ("Draw_NewPic: numcachepics == MAX_CACHED_PICS\n");
-                               // FIXME: support NULL in callers?
-                               return cachepics; // return the first one
-                       }
-                       pic = cachepics + (numcachepics++);
-                       strlcpy (pic->name, picname, sizeof(pic->name));
-                       // link into list
-                       pic->chain = cachepichash[hashkey];
-                       cachepichash[hashkey] = pic;
+                       Con_Printf ("Draw_NewPic: numcachepics == MAX_CACHED_PICS\n");
+                       // FIXME: support NULL in callers?
+                       return cachepics; // return the first one
                }
+               pic = cachepics + (numcachepics++);
+               memset(pic, 0, sizeof(*pic));
+               strlcpy (pic->name, picname, sizeof(pic->name));
+               // link into list
+               pic->chain = cachepichash[hashkey];
+               cachepichash[hashkey] = pic;
        }
 
        pic->flags = CACHEPICFLAG_NEWPIC; // disable texflags checks in Draw_CachePic
        pic->width = width;
        pic->height = height;
-       if (pic->tex)
+       if (pic->allow_free_tex && pic->tex)
                R_FreeTexture(pic->tex);
        pic->tex = R_LoadTexture2D(drawtexturepool, picname, width, height, pixels_bgra, TEXTYPE_BGRA, (alpha ? TEXF_ALPHA : 0), -1, NULL);
        return pic;
@@ -693,7 +708,7 @@ void LoadFont(qboolean override, const char *name, dp_font_t *fnt, float scale,
                ch = 0;
                while(ch < 256)
                {
-                       if(!COM_ParseToken_Simple(&p, false, false))
+                       if(!COM_ParseToken_Simple(&p, false, false, true))
                                return;
 
                        switch(*com_token)
@@ -717,20 +732,20 @@ void LoadFont(qboolean override, const char *name, dp_font_t *fnt, float scale,
                                default:
                                        if(!strcmp(com_token, "extraspacing"))
                                        {
-                                               if(!COM_ParseToken_Simple(&p, false, false))
+                                               if(!COM_ParseToken_Simple(&p, false, false, true))
                                                        return;
                                                extraspacing = atof(com_token);
                                        }
                                        else if(!strcmp(com_token, "scale"))
                                        {
-                                               if(!COM_ParseToken_Simple(&p, false, false))
+                                               if(!COM_ParseToken_Simple(&p, false, false, true))
                                                        return;
                                                fnt->settings.scale = atof(com_token);
                                        }
                                        else
                                        {
                                                Con_Printf("Warning: skipped unknown font property %s\n", com_token);
-                                               if(!COM_ParseToken_Simple(&p, false, false))
+                                               if(!COM_ParseToken_Simple(&p, false, false, true))
                                                        return;
                                        }
                                        break;
@@ -983,6 +998,7 @@ Draw_Init
 static void gl_draw_start(void)
 {
        int i;
+       char vabuf[1024];
        drawtexturepool = R_AllocTexturePool();
 
        numcachepics = 0;
@@ -993,7 +1009,7 @@ static void gl_draw_start(void)
        // load default font textures
        for(i = 0; i < dp_fonts.maxsize; ++i)
                if (dp_fonts.f[i].title[0])
-                       LoadFont(false, va("gfx/font_%s", dp_fonts.f[i].title), &dp_fonts.f[i], 1, 0);
+                       LoadFont(false, va(vabuf, sizeof(vabuf), "gfx/font_%s", dp_fonts.f[i].title), &dp_fonts.f[i], 1, 0);
 
        // draw the loading screen so people have something to see in the newly opened window
        SCR_UpdateLoadingScreen(true);
@@ -1053,33 +1069,19 @@ void GL_Draw_Init (void)
        R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown, gl_draw_newmap, NULL, NULL);
 }
 
-static void _DrawQ_Setup(void)
+static void _DrawQ_Setup(void) // see R_ResetViewRendering2D
 {
-       r_viewport_t viewport;
        if (r_refdef.draw2dstage == 1)
                return;
        r_refdef.draw2dstage = 1;
-       CHECKGLERROR
-       R_Viewport_InitOrtho(&viewport, &identitymatrix, r_refdef.view.x, vid.height - r_refdef.view.y - r_refdef.view.height, r_refdef.view.width, r_refdef.view.height, 0, 0, vid_conwidth.integer, vid_conheight.integer, -10, 100, NULL);
-       R_Mesh_ResetRenderTargets();
-       R_SetViewport(&viewport);
-       GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
-       GL_DepthFunc(GL_LEQUAL);
-       GL_PolygonOffset(0,0);
-       GL_CullFace(GL_NONE);
-       R_EntityMatrix(&identitymatrix);
 
-       GL_DepthRange(0, 1);
-       GL_PolygonOffset(0, 0);
-       GL_DepthTest(false);
-       GL_Color(1,1,1,1);
+       R_ResetViewRendering2D_Common(0, NULL, NULL, vid_conwidth.integer, vid_conheight.integer);
 }
 
 qboolean r_draw2d_force = false;
-void _DrawQ_SetupAndProcessDrawFlag(int flags, cachepic_t *pic, float alpha)
+static void _DrawQ_SetupAndProcessDrawFlag(int flags, cachepic_t *pic, float alpha)
 {
        _DrawQ_Setup();
-       CHECKGLERROR
        if(!r_draw2d.integer && !r_draw2d_force)
                return;
        DrawQ_ProcessDrawFlag(flags, (alpha < 1) || (pic && pic->hasalpha));
@@ -1141,7 +1143,7 @@ void DrawQ_Pic(float x, float y, cachepic_t *pic, float width, float height, flo
                        width = pic->width;
                if (height == 0)
                        height = pic->height;
-               R_SetupShader_Generic(Draw_GetPicTexture(pic), NULL, GL_MODULATE, 1, true);
+               R_SetupShader_Generic(Draw_GetPicTexture(pic), NULL, GL_MODULATE, 1, (flags & DRAWFLAGS_BLEND) ? false : true, true, false);
 
 #if 0
       // AK07: lets be texel correct on the corners
@@ -1157,7 +1159,7 @@ void DrawQ_Pic(float x, float y, cachepic_t *pic, float width, float height, flo
 #endif
        }
        else
-               R_SetupShader_Generic(NULL, NULL, GL_MODULATE, 1, true);
+               R_SetupShader_Generic_NoTexture((flags & DRAWFLAGS_BLEND) ? false : true, true);
 
        floats[2] = floats[5] = floats[8] = floats[11] = 0;
        floats[0] = floats[9] = x;
@@ -1190,10 +1192,10 @@ void DrawQ_RotPic(float x, float y, cachepic_t *pic, float width, float height,
                        width = pic->width;
                if (height == 0)
                        height = pic->height;
-               R_SetupShader_Generic(Draw_GetPicTexture(pic), NULL, GL_MODULATE, 1, true);
+               R_SetupShader_Generic(Draw_GetPicTexture(pic), NULL, GL_MODULATE, 1, (flags & DRAWFLAGS_BLEND) ? false : true, true, false);
        }
        else
-               R_SetupShader_Generic(NULL, NULL, GL_MODULATE, 1, true);
+               R_SetupShader_Generic_NoTexture((flags & DRAWFLAGS_BLEND) ? false : true, true);
 
        floats[2] = floats[5] = floats[8] = floats[11] = 0;
 
@@ -1235,7 +1237,7 @@ void DrawQ_Fill(float x, float y, float width, float height, float red, float gr
                return;
 
 //     R_Mesh_ResetTextureState();
-       R_SetupShader_Generic(NULL, NULL, GL_MODULATE, 1, true);
+       R_SetupShader_Generic_NoTexture((flags & DRAWFLAGS_BLEND) ? false : true, true);
 
        floats[2] = floats[5] = floats[8] = floats[11] = 0;
        floats[0] = floats[9] = x;
@@ -1562,7 +1564,7 @@ float DrawQ_String_Scale(float startx, float starty, const char *text, size_t ma
 //     R_Mesh_ResetTextureState();
        if (!fontmap)
                R_Mesh_TexBind(0, fnt->tex);
-       R_SetupShader_Generic(fnt->tex, NULL, GL_MODULATE, 1, true);
+       R_SetupShader_Generic(fnt->tex, NULL, GL_MODULATE, 1, (flags & DRAWFLAGS_BLEND) ? false : true, true, false);
 
        ac = color4f;
        at = texcoord2f;
@@ -1698,7 +1700,7 @@ float DrawQ_String_Scale(float startx, float starty, const char *text, size_t ma
                                                        at = texcoord2f;
                                                        av = vertex3f;
                                                }
-                                               R_SetupShader_Generic(fnt->tex, NULL, GL_MODULATE, 1, true);
+                                               R_SetupShader_Generic(fnt->tex, NULL, GL_MODULATE, 1, (flags & DRAWFLAGS_BLEND) ? false : true, true, false);
                                                map = ft2_oldstyle_map;
                                        }
                                }
@@ -1767,7 +1769,7 @@ float DrawQ_String_Scale(float startx, float starty, const char *text, size_t ma
                                                        break;
                                                }
                                        }
-                                       R_SetupShader_Generic(map->pic->tex, NULL, GL_MODULATE, 1, true);
+                                       R_SetupShader_Generic(map->pic->tex, NULL, GL_MODULATE, 1, (flags & DRAWFLAGS_BLEND) ? false : true, true, false);
                                }
 
                                mapch = ch - map->start;
@@ -1911,10 +1913,10 @@ void DrawQ_SuperPic(float x, float y, cachepic_t *pic, float width, float height
                        width = pic->width;
                if (height == 0)
                        height = pic->height;
-               R_SetupShader_Generic(Draw_GetPicTexture(pic), NULL, GL_MODULATE, 1, true);
+               R_SetupShader_Generic(Draw_GetPicTexture(pic), NULL, GL_MODULATE, 1, (flags & DRAWFLAGS_BLEND) ? false : true, true, false);
        }
        else
-               R_SetupShader_Generic(NULL, NULL, GL_MODULATE, 1, true);
+               R_SetupShader_Generic_NoTexture((flags & DRAWFLAGS_BLEND) ? false : true, true);
 
        floats[2] = floats[5] = floats[8] = floats[11] = 0;
        floats[0] = floats[9] = x;
@@ -1937,13 +1939,12 @@ void DrawQ_SuperPic(float x, float y, cachepic_t *pic, float width, float height
 void DrawQ_Mesh (drawqueuemesh_t *mesh, int flags, qboolean hasalpha)
 {
        _DrawQ_Setup();
-       CHECKGLERROR
        if(!r_draw2d.integer && !r_draw2d_force)
                return;
        DrawQ_ProcessDrawFlag(flags, hasalpha);
 
 //     R_Mesh_ResetTextureState();
-       R_SetupShader_Generic(mesh->texture, NULL, GL_MODULATE, 1, true);
+       R_SetupShader_Generic(mesh->texture, NULL, GL_MODULATE, 1, (flags & DRAWFLAGS_BLEND) ? false : true, true, false);
 
        R_Mesh_PrepareVertices_Generic_Arrays(mesh->num_vertices, mesh->data_vertex3f, mesh->data_color4f, mesh->data_texcoord2f);
        R_Mesh_Draw(0, mesh->num_vertices, 0, mesh->num_triangles, mesh->data_element3i, NULL, 0, mesh->data_element3s, NULL, 0);
@@ -1963,6 +1964,7 @@ void DrawQ_LineLoop (drawqueuemesh_t *mesh, int flags)
        case RENDERPATH_GL11:
        case RENDERPATH_GL13:
        case RENDERPATH_GL20:
+#ifndef USE_GLES2
                CHECKGLERROR
                qglBegin(GL_LINE_LOOP);
                for (num = 0;num < mesh->num_vertices;num++)
@@ -1973,6 +1975,7 @@ void DrawQ_LineLoop (drawqueuemesh_t *mesh, int flags)
                }
                qglEnd();
                CHECKGLERROR
+#endif
                break;
        case RENDERPATH_D3D9:
                //Con_DPrintf("FIXME D3D9 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
@@ -2000,13 +2003,14 @@ void DrawQ_Line (float width, float x1, float y1, float x2, float y2, float r, f
        if(!r_draw2d.integer && !r_draw2d_force)
                return;
 
-       R_SetupShader_Generic(NULL, NULL, GL_MODULATE, 1, true);
+       R_SetupShader_Generic_NoTexture((flags & DRAWFLAGS_BLEND) ? false : true, true);
 
        switch(vid.renderpath)
        {
        case RENDERPATH_GL11:
        case RENDERPATH_GL13:
        case RENDERPATH_GL20:
+#ifndef USE_GLES2
                CHECKGLERROR
 
                //qglLineWidth(width);CHECKGLERROR
@@ -2018,6 +2022,7 @@ void DrawQ_Line (float width, float x1, float y1, float x2, float y2, float r, f
                qglVertex2f(x2, y2);
                qglEnd();
                CHECKGLERROR
+#endif
                break;
        case RENDERPATH_D3D9:
                //Con_DPrintf("FIXME D3D9 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
@@ -2058,7 +2063,7 @@ void DrawQ_Lines (float width, int numlines, const float *vertex3f, const float
        case RENDERPATH_GL20:
                CHECKGLERROR
 
-               R_SetupShader_Generic(NULL, NULL, GL_MODULATE, 1, true);
+               R_SetupShader_Generic_NoTexture((flags & DRAWFLAGS_BLEND) ? false : true, true);
 
                //qglLineWidth(width);CHECKGLERROR
 
@@ -2163,7 +2168,7 @@ void R_DrawGamma(void)
        }
        // all the blends ignore depth
 //     R_Mesh_ResetTextureState();
-       R_SetupShader_Generic(NULL, NULL, GL_MODULATE, 1, true);
+       R_SetupShader_Generic_NoTexture(true, true);
        GL_DepthMask(true);
        GL_DepthRange(0, 1);
        GL_PolygonOffset(0, 0);