From f992aebf332edc291b8324af54a4830a1be11c64 Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <coelckers@users.noreply.github.com>
Date: Fri, 4 Oct 2019 19:17:55 +0200
Subject: [PATCH] - two more

---
 source/build/src/engine.cpp    | 72 ++++------------------------------
 source/build/src/palette.cpp   | 42 ++++++++++----------
 source/build/src/polymost.cpp  | 18 ++++-----
 source/glbackend/glbackend.cpp | 24 ++++++++++++
 source/glbackend/glbackend.h   | 25 ++++++++++++
 5 files changed, 87 insertions(+), 94 deletions(-)

diff --git a/source/build/src/engine.cpp b/source/build/src/engine.cpp
index 48c910b8f..1991ea201 100644
--- a/source/build/src/engine.cpp
+++ b/source/build/src/engine.cpp
@@ -9001,7 +9001,7 @@ killsprite:
                 }
                 else
                 {
-                    glDepthMask(GL_FALSE);
+					GLInterface.SetDepthMask(false);
 
                     for (bssize_t k = j-1; k >= i; k--)
                     {
@@ -9009,9 +9009,9 @@ killsprite:
                         renderDrawSprite(k);
                     }
 
-                    glDepthMask(GL_TRUE);
+					GLInterface.SetDepthMask(true);
 
-                    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+					GLInterface.SetColorMask(false);
                     
                     for (bssize_t k = j-1; k >= i; k--)
                     {
@@ -9019,7 +9019,7 @@ killsprite:
                         tspriteptr[k] = NULL;
                     }
 
-                    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+					GLInterface.SetColorMask(true);
 
                 }
                 i = j;
@@ -9042,30 +9042,8 @@ killsprite:
 
         GLInterface.EnableBlend(true);
         GLInterface.EnableAlphaTest(true);
-        glDepthMask(GL_FALSE);
-    }
-#endif
-
-#if 0
-        for (i=spritesortcnt-1; i>=0; i--)
-        {
-            double xs = tspriteptr[i]->x-globalposx;
-            double ys = tspriteptr[i]->y-globalposy;
-            int32_t zs = tspriteptr[i]->z-globalposz;
-
-            int32_t xp = ys*cosglobalang-xs*singlobalang;
-            int32_t yp = (zs<<1);
-            int32_t zp = xs*cosglobalang+ys*singlobalang;
-
-            xs = ((double)xp*(halfxdimen<<12)/zp)+((halfxdimen+windowxy1.x)<<12);
-            ys = ((double)yp*(xdimenscale<<12)/zp)+((globalhoriz+windowxy1.y)<<12);
-
-            if (xs >= INT32_MIN && xs <= INT32_MAX && ys >= INT32_MIN && ys <= INT32_MAX)
-            {
-                drawline256(xs-65536,ys-65536,xs+65536,ys+65536,31);
-                drawline256(xs+65536,ys-65536,xs-65536,ys+65536,31);
-            }
-        }
+		GLInterface.SetDepthMask(false);
+	}
 #endif
 
     vec2f_t pos;
@@ -9204,45 +9182,11 @@ killsprite:
 #ifdef USE_OPENGL
     if (videoGetRenderMode() == REND_POLYMOST)
     {
-        glDepthMask(GL_TRUE);
-        polymost_setClamp(0);
+		GLInterface.SetDepthMask(true);
+		polymost_setClamp(0);
     }
 #endif
 
-#ifdef POLYMER
-    if (videoGetRenderMode() == REND_POLYMER)
-        polymer_drawmasks();
-#endif
-#ifdef DEBUG_MASK_DRAWING
-    if (g_maskDrawMode && videoGetRenderMode() == REND_CLASSIC)
-    {
-        for (i=0; i<dmasknum; i++)
-        {
-            EDUKE32_STATIC_ASSERT(MAXWALLS <= 32768 && MAXSPRITES <= 32768);
-            int32_t spritep = !!(debugmask[i].di & 32768);
-            int32_t di = debugmask[i].di & 32767;
-//            int32_t ii = debugmask[i].i;
-
-            char numstr[12];
-            Bsprintf(numstr, "%d", i+1);
-
-            if (spritep)
-            {
-                int32_t sx = spritesxyz[di].x>>8, sy = ydim/2 + 8;
-                // XXX: printext256 really ought to do bound checking on the
-                // x/y coords!
-                sx = clamp(sx, 0, xdim-8*Bstrlen(numstr)-1);
-                printext256(sx, sy, 241, 0, numstr, 0);
-            }
-            else
-            {
-                int32_t sx = xb1[di] + (xb2[di]-xb1[di])/2, sy = ydim/2;
-                sx = clamp(sx, 0, xdim-8*Bstrlen(numstr)-1);
-                printext256(sx, sy, 31, 0, numstr, 0);
-            }
-        }
-    }
-#endif
 
     videoEndDrawing();   //}}}
 }
diff --git a/source/build/src/palette.cpp b/source/build/src/palette.cpp
index fd03b2a8d..3801bd040 100644
--- a/source/build/src/palette.cpp
+++ b/source/build/src/palette.cpp
@@ -60,7 +60,7 @@ void fullscreen_tint_gl(uint8_t r, uint8_t g, uint8_t b, uint8_t f)
     GLInterface.EnableAlphaTest(false);
     polymost_setFogEnabled(false);
 
-    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+    GLInterface.SetBlendFunc(STYLEALPHA_Src, STYLEALPHA_InvSrc);
     GLInterface.EnableBlend(true);
     GLInterface.SetColorub(r, g, b, f);
 
@@ -95,7 +95,7 @@ void fullscreen_tint_gl_blood(void)
     GLInterface.EnableAlphaTest(false);
     polymost_setFogEnabled(false);
 
-    glBlendFunc(GL_ONE, GL_ONE);
+	GLInterface.SetBlendFunc(STYLEALPHA_One, STYLEALPHA_One);
     GLInterface.EnableBlend(true);
 
     polymost_useColorOnly(true);
@@ -106,7 +106,7 @@ void fullscreen_tint_gl_blood(void)
 	vt[1].Set(2.5f, 1.f);
 	vt[2].Set(.0f, -2.5f);
 	GLInterface.Draw(DT_TRIANGLES, data.first, 3);
-	glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
+	GLInterface.SetBlendOp(STYLEOP_RevSub);
 	GLInterface.SetColorub(max(-tint_blood_r, 0), max(-tint_blood_g, 0), max(-tint_blood_b, 0), 255);
 	data = GLInterface.AllocVertices(3);
 	vt = data.second;
@@ -114,10 +114,10 @@ void fullscreen_tint_gl_blood(void)
 	vt[1].Set(2.5f, 1.f);
 	vt[2].Set(.0f, -2.5f);
 	GLInterface.Draw(DT_TRIANGLES, data.first, 3);
-	glBlendEquation(GL_FUNC_ADD);
-    GLInterface.SetColorub(0,0,0,0);
-    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-    polymost_useColorOnly(false);
+	GLInterface.SetBlendOp(STYLEOP_Add);
+	GLInterface.SetColorub(0,0,0,0);
+	GLInterface.SetBlendFunc(STYLEALPHA_Src, STYLEALPHA_InvSrc);
+	polymost_useColorOnly(false);
 
 	GLInterface.SetMatrix(Matrix_Projection, &oldproj);
 	GLInterface.SetMatrix(Matrix_ModelView, &oldmv);
@@ -579,26 +579,26 @@ void handle_blend(uint8_t enable, uint8_t blend, uint8_t def)
 {
     static GLenum const blendFuncTokens[NUMBLENDFACTORS] =
     {
-        GL_ZERO,
-        GL_ONE,
-        GL_SRC_COLOR,
-        GL_ONE_MINUS_SRC_COLOR,
-        GL_SRC_ALPHA,
-        GL_ONE_MINUS_SRC_ALPHA,
-        GL_DST_ALPHA,
-        GL_ONE_MINUS_DST_ALPHA,
-        GL_DST_COLOR,
-        GL_ONE_MINUS_DST_COLOR,
-    };
+        STYLEALPHA_Zero,
+        STYLEALPHA_One,
+        STYLEALPHA_SrcCol,
+        STYLEALPHA_InvSrcCol,
+        STYLEALPHA_Src,
+        STYLEALPHA_InvSrc,
+        STYLEALPHA_Dst,
+		STYLEALPHA_InvDst,
+		STYLEALPHA_DstCol,
+		STYLEALPHA_InvDstCol,
+	};
 
     if (!enable)
     {
-        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-        return;
+		GLInterface.SetBlendFunc(STYLEALPHA_Src, STYLEALPHA_InvSrc);
+		return;
     }
 
     glblenddef_t const * const glbdef = glblend[blend].def + def;
-    glBlendFunc(blendFuncTokens[glbdef->src], blendFuncTokens[glbdef->dst]);
+	GLInterface.SetBlendFunc(blendFuncTokens[glbdef->src], blendFuncTokens[glbdef->dst]);
 }
 #endif
 
diff --git a/source/build/src/polymost.cpp b/source/build/src/polymost.cpp
index b41e5969b..f5697ddac 100644
--- a/source/build/src/polymost.cpp
+++ b/source/build/src/polymost.cpp
@@ -2022,7 +2022,7 @@ static void polymost_drawpoly(vec2f_t const * const dpxy, int32_t const n, int32
     if (!waloff[globalpicnum])
     {
         tsiz.x = tsiz.y = 1;
-        glColorMask(false, false, false, false); //Hack to update Z-buffer for invalid mirror textures
+		GLInterface.SetColorMask(false); //Hack to update Z-buffer for invalid mirror textures
     }
 
     static int32_t fullbright_pass = 0;
@@ -2371,7 +2371,7 @@ do
     if (videoGetRenderMode() != REND_POLYMOST)
     {
         if (!waloff[globalpicnum])
-            glColorMask(true, true, true, true);
+			GLInterface.SetColorMask(true);
 
         return;
     }
@@ -2408,15 +2408,15 @@ do
         vec3d_t const bxtex = xtex, bytex = ytex, botex = otex;
         xtex = xtex2, ytex = ytex2, otex = otex2;
         skyzbufferhack_pass++;
-        glColorMask(false, false, false, false);
-        polymost_drawpoly(dpxy, n, DAMETH_MASK);
-        glColorMask(true, true, true, true);
-        xtex = bxtex, ytex = bytex, otex = botex;
+		GLInterface.SetColorMask(false);
+		polymost_drawpoly(dpxy, n, DAMETH_MASK);
+		GLInterface.SetColorMask(true);
+		xtex = bxtex, ytex = bytex, otex = botex;
         skyzbufferhack_pass--;
     }
 
     if (!waloff[globalpicnum])
-        glColorMask(true, true, true, true);
+		GLInterface.SetColorMask(true);
 }
 
 
@@ -7290,7 +7290,7 @@ int32_t polymost_printext256(int32_t xpos, int32_t ypos, int16_t col, int16_t ba
     polymostSet2dView();	// disables blending, texturing, and depth testing
 
     GLInterface.EnableAlphaTest(false);
-    glDepthMask(GL_FALSE);	// disable writing to the z-buffer
+	GLInterface.SetDepthMask(false);
 
     // XXX: Don't fogify the OSD text in Mapster32 with r_usenewshading >= 2.
     polymost_setFogEnabled(false);
@@ -7380,7 +7380,7 @@ int32_t polymost_printext256(int32_t xpos, int32_t ypos, int16_t col, int16_t ba
     }
 
 
-    glDepthMask(GL_TRUE);	// re-enable writing to the z-buffer
+	GLInterface.SetDepthMask(true);
 
     if (!nofog) polymost_setFogEnabled(true);
 
diff --git a/source/glbackend/glbackend.cpp b/source/glbackend/glbackend.cpp
index 17968b94d..4924e2e4d 100644
--- a/source/glbackend/glbackend.cpp
+++ b/source/glbackend/glbackend.cpp
@@ -221,3 +221,27 @@ void GLInstance::SetFogExp2(float* color, float coefficient)
 	glFogf(GL_FOG_DENSITY, coefficient);
 	glFogfv(GL_FOG_COLOR, color);
 }
+
+void GLInstance::SetColorMask(bool on)
+{
+	glColorMask(on, on, on, on);
+}
+
+void GLInstance::SetDepthMask(bool on)
+{
+	glDepthMask(on);
+}
+
+static int blendstyles[] = { GL_ZERO, GL_ONE, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA };
+
+void GLInstance::SetBlendFunc(int src, int dst)
+{
+	glBlendFunc(blendstyles[src], blendstyles[dst]);
+}
+
+static int renderops[] = { GL_FUNC_ADD, GL_FUNC_SUBTRACT, GL_FUNC_REVERSE_SUBTRACT };
+
+void GLInstance::SetBlendOp(int op)
+{
+	glBlendEquation(renderops[op]);
+}
diff --git a/source/glbackend/glbackend.h b/source/glbackend/glbackend.h
index a304072b2..46acf3e5c 100644
--- a/source/glbackend/glbackend.h
+++ b/source/glbackend/glbackend.h
@@ -76,6 +76,27 @@ enum EDepthFunc
 	Depth_LessEqual
 };
 
+enum ERenderAlpha
+{
+	STYLEALPHA_Zero,		// Blend factor is 0.0
+	STYLEALPHA_One,			// Blend factor is 1.0
+	STYLEALPHA_Src,			// Blend factor is alpha
+	STYLEALPHA_InvSrc,		// Blend factor is 1.0 - alpha
+	STYLEALPHA_SrcCol,		// Blend factor is color (HWR only)
+	STYLEALPHA_InvSrcCol,	// Blend factor is 1.0 - color (HWR only)
+	STYLEALPHA_DstCol,		// Blend factor is dest. color (HWR only)
+	STYLEALPHA_InvDstCol,	// Blend factor is 1.0 - dest. color (HWR only)
+	STYLEALPHA_Dst,			// Blend factor is dest. alpha
+	STYLEALPHA_InvDst,		// Blend factor is 1.0 - dest. alpha
+	STYLEALPHA_MAX
+};
+
+enum ERenderOp
+{
+	STYLEOP_Add,			// Add source to destination
+	STYLEOP_Sub,			// Subtract source from destination
+	STYLEOP_RevSub,			// Subtract destination from source
+};
 class GLInstance
 {
 	enum
@@ -140,6 +161,10 @@ public:
 	void SetDepthFunc(int func);
 	void SetFogLinear(float* color, float start, float end);
 	void SetFogExp2(float* color, float coefficient);
+	void SetColorMask(bool on);
+	void SetDepthMask(bool on);
+	void SetBlendFunc(int src, int dst);
+	void SetBlendOp(int op);
 };
 
 extern GLInstance GLInterface;