DEADSOFTWARE

gl: optimize repeatable textures
[d2df-sdl.git] / src / game / renders / opengl / r_draw.pas
index 1a6b573db136ff35341a7046f1d73d4471fcd74d..67bd23041275b237de894398f5960198e4691c30 100644 (file)
@@ -76,7 +76,8 @@ implementation
     glOrtho(0, w, h, 0, 0, 1);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity;
-//    glTranslatef(0.5, 0.5, 0);
+    glEnable(GL_SCISSOR_TEST);
+    r_Draw_SetRect(0, 0, w - 1, h - 1);
   end;
 
   procedure DrawQuad (x, y, w, h: Integer);
@@ -125,8 +126,31 @@ implementation
     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;
+    glBindTexture(GL_TEXTURE_2D, gltex);
+    glColor4ub(rr, gg, bb, aa);
+    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);
+      glTexCoord2f(bx, ay); glVertex2i(l, t);
+      glTexCoord2f(bx, by); glVertex2i(l, b);
+      glTexCoord2f(ax, by); glVertex2i(r, b);
+    glEnd();
+    glDisable(GL_TEXTURE_2D);
+    glBindTexture(GL_TEXTURE_2D, 0);
+  end;
+
   procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
-    var i, j, offx, offy: Integer; n: TGLAtlasNode;
+    var i, j, first, last, step: Integer; n: TGLAtlasNode;
   begin
     ASSERT(w >= 0);
     ASSERT(h >= 0);
@@ -134,27 +158,44 @@ implementation
       DrawTile(nil, x, y, w, h, flip, NTR, NTB, NTG, NTA, blend)
     else
     begin
-      offx := 0;
-      offy := 0;
+      if flip then first := img.cols - 1 else first := 0;
+      if flip then last  := -1           else last  := img.cols;
+      if flip then step  := -1           else step  := +1;
+      glPushMatrix;
+      glTranslatef(x, y, 0);
+      glScalef(w / img.width, h / img.height, 1);
       for j := 0 to img.lines - 1 do
       begin
-        for i := 0 to img.cols - 1 do
-        begin
+        i := first;
+        repeat
           n := img.GetTile(i, j);
           ASSERT(n <> nil);
-          glPushMatrix;
-          glTranslatef(x + offx, y + offy, 0);
-          glScalef(w / img.width, h / img.height, 1);
           DrawTile(n, 0, 0, n.width, n.height, flip, r, g, b, a, blend);
-          glPopMatrix;
-          offx := offx + n.width;
-        end;
-        offx := 0;
-        offy := offy + n.height;
+          glTranslatef(n.width, 0, 0);
+          i := i + step;
+        until i = last;
+        glTranslatef(-img.width, n.height, 0);
       end;
+      glPopMatrix;
     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
@@ -162,6 +203,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