DEADSOFTWARE

gl: fix invalid color cache
[d2df-sdl.git] / src / game / renders / opengl / r_draw.pas
index 9db91836645d5460279debd7a4937a1ab3471bb9..c839936087a0754aa00405d235b32c67087422a3 100644 (file)
@@ -41,6 +41,9 @@ interface
   procedure r_Draw_SetRect (l, t, r, b: Integer);
   procedure r_Draw_GetRect (out l, t, r, b: Integer);
 
+  procedure r_Draw_EnableTexture2D (enable: Boolean);
+  procedure r_Draw_SetColor (r, g, b, a: Byte);
+
 implementation
 
   uses
@@ -63,6 +66,30 @@ implementation
     sl, st, sr, sb: Integer;
     ScreenWidth, ScreenHeight: Integer;
 
+    enableTexture2D: Boolean;
+    curR, curG, curB, curA: Byte;
+
+  procedure r_Draw_EnableTexture2D (enable: Boolean);
+  begin
+    if enable <> enableTexture2D then
+    begin
+      if enable then glEnable(GL_TEXTURE_2D) else glDisable(GL_TEXTURE_2D);
+      enableTexture2D := enable;
+    end;
+  end;
+
+  procedure r_Draw_SetColor (r, g, b, a: Byte);
+  begin
+    if (r <> curR) or (g <> curG) or (b <> curB) or (curA <> a) then
+    begin
+      glColor4ub(r, g, b, a);
+      curR := r;
+      curG := g;
+      curB := b;
+      curA := a;
+    end;
+  end;
+
   procedure r_Draw_Setup (w, h: Integer);
   begin
     ASSERT(w >= 0);
@@ -95,9 +122,9 @@ implementation
   begin
     if tile = nil then
     begin
-      glColor4ub(rr, gg, bb, aa);
+      r_Draw_SetColor(rr, gg, bb, aa);
       if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-      glDisable(GL_TEXTURE_2D);
+      r_Draw_EnableTexture2D(false);
       glEnable(GL_BLEND);
       DrawQuad(x, y, w, h);
     end
@@ -110,10 +137,10 @@ implementation
       ay := (tile.t) / nw;
       by := (tile.b + 1) / nh;
       l := x; t := y; r := x + w; b := y + h;
-      glBindTexture(GL_TEXTURE_2D, tile.id);
-      glColor4ub(rr, gg, bb, aa);
+      r_Textures_GL_Bind(tile.id);
+      r_Draw_SetColor(rr, gg, bb, aa);
+      r_Draw_EnableTexture2D(true);
       if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-      glEnable(GL_TEXTURE_2D);
       glEnable(GL_BLEND);
       glBegin(GL_QUADS);
         glTexCoord2f(ax, ay); glVertex2i(r, t);
@@ -121,11 +148,30 @@ implementation
         glTexCoord2f(bx, by); glVertex2i(l, b);
         glTexCoord2f(ax, by); glVertex2i(r, b);
       glEnd();
-      glDisable(GL_TEXTURE_2D);
-      glBindTexture(GL_TEXTURE_2D, 0);
     end
   end;
 
+  procedure DrawHWTexture (gltex: GLint; nw, nh, x, y, w, h: Integer; flip: Boolean; rr, gg, bb, aa: Byte; blend: Boolean);
+    var ax, bx, ay, by: GLfloat; l, t, r, b: Integer;
+  begin
+    ax := IfThen(flip, 0, w) / nw;
+    bx := IfThen(flip, w, 0) / nh;
+    ay := 0 / nw;
+    by := h / nh;
+    l := x; t := y; r := x + w; b := y + h;
+    r_Textures_GL_Bind(gltex);
+    r_Draw_SetColor(rr, gg, bb, aa);
+    r_Draw_EnableTexture2D(true);
+    if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+    glEnable(GL_BLEND);
+    glBegin(GL_QUADS);
+      glTexCoord2f(ax, ay); glVertex2i(r, t);
+      glTexCoord2f(bx, ay); glVertex2i(l, t);
+      glTexCoord2f(bx, by); glVertex2i(l, b);
+      glTexCoord2f(ax, by); glVertex2i(r, b);
+    glEnd();
+  end;
+
   procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
     var i, j, first, last, step: Integer; n: TGLAtlasNode;
   begin
@@ -157,6 +203,22 @@ implementation
     end
   end;
 
+  function r_Draw_IsHWRepeatable (img: TGLTexture): Boolean;
+    var n: TGLAtlasNode; a: TGLAtlas;
+  begin
+    ASSERT(img <> nil);
+    result := false;
+    if (img.cols = 1) and (img.lines = 1) then
+    begin
+      n := img.GetTile(0, 0);
+      if (n.width = img.width) and (n.height = img.height) then
+      begin
+        a := n.base;
+        result := (a.GetWidth() = img.width) and (a.GetHeight() = img.height)
+      end;
+    end;
+  end;
+
   procedure r_Draw_TextureRepeat (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
     var i, j: Integer;
   begin
@@ -164,6 +226,8 @@ implementation
     ASSERT(h >= 0);
     if img = nil then
       r_Draw_Texture(nil, x, y, w, h, flip, NTR, NTG, NTB, NTB, blend)
+    else if r_Draw_IsHWRepeatable(img) then
+      DrawHWTexture(img.GetTile(0, 0).base.id, img.width, img.height, x, y, w, h, flip, r, g, b, a, blend)
     else
       for j := 0 to (h - 1) div img.height do
         for i := 0 to (w - 1) div img.width do
@@ -226,8 +290,8 @@ implementation
     ASSERT(b >= t);
     glEnable(GL_BLEND);
     glBlendFunc(GL_ZERO, GL_SRC_COLOR);
-    glDisable(GL_TEXTURE_2D);
-    glColor4ub(rr, gg, bb, aa);
+    r_Draw_EnableTexture2D(false);
+    r_Draw_SetColor(rr, gg, bb, aa);
     glBegin(GL_QUADS);
       glVertex2i(l, t);
       glVertex2i(r, t);
@@ -242,8 +306,8 @@ implementation
     ASSERT(b >= t);
     glEnable(GL_BLEND);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-    glDisable(GL_TEXTURE_2D);
-    glColor4ub(rr, gg, bb, aa);
+    r_Draw_EnableTexture2D(false);
+    r_Draw_SetColor(rr, gg, bb, aa);
     glBegin(GL_LINE_LOOP);
 {
       glVertex2i(l, t);
@@ -264,8 +328,8 @@ implementation
     ASSERT(b >= t);
     glEnable(GL_BLEND);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-    glDisable(GL_TEXTURE_2D);
-    glColor4ub(rr, gg, bb, aa);
+    r_Draw_EnableTexture2D(false);
+    r_Draw_SetColor(rr, gg, bb, aa);
     glBegin(GL_QUADS);
 {
       glVertex2i(l, t);
@@ -292,8 +356,8 @@ implementation
     ASSERT(b >= t);
     glEnable(GL_BLEND);
     glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
-    glDisable(GL_TEXTURE_2D);
-    glColor4ub(rr, gg, bb, aa);
+    r_Draw_EnableTexture2D(false);
+    r_Draw_SetColor(rr, gg, bb, aa);
     glBegin(GL_QUADS);
       glVertex2i(l, t);
       glVertex2i(r, t);